赞 | 1 |
VIP | 357 |
好人卡 | 24 |
积分 | 1 |
经验 | 14896 |
最后登录 | 2022-12-23 |
在线时间 | 1641 小时 |
Lv1.梦旅人
- 梦石
- 0
- 星屑
- 66
- 在线时间
- 1641 小时
- 注册时间
- 2011-9-26
- 帖子
- 313
|
- #------------------------------------------------------------------------------
- # SDL_RSGE
- #------------------------------------------------------------------------------
- # SDL_RSGE 库,是针对 RSGE 标准库的 Ruby/SDL 库二次封装。
- #------------------------------------------------------------------------------
- module SDL_RSGE
- #----------------------------------------------------------------------------
- # ● 引用库
- #----------------------------------------------------------------------------
- require 'sdl'
- require 'win32api'
- #----------------------------------------------------------------------------
- # ● 全局变量
- #----------------------------------------------------------------------------
- $SDL_title = "Untitled"
- $SDL_width = 640
- $SDL_height = 480
- $SDL_frame_rate = 60
- $SDL_frame_count = 0
- $SDL_brightness = 255
- $SDL_X_Adjust = 3
- $SDL_Y_Adjust = 14
- #----------------------------------------------------------------------------
- # SDL_RSGE::Input
- #----------------------------------------------------------------------------
- # 管理键盘输入的模块。
- #----------------------------------------------------------------------------
- module Input
- def update
- SDL::Key.scan
- end
- def press?(num)
- return SDL::Key.press?(num)
- end
- def trigger?(num)
- return SDL::Key.press?(num)
- end
- def repeat?(num)
- return SDL::Key.press?(num)
- end
- end
- #----------------------------------------------------------------------------
- # SDL_RSGE::FPS_Timer
- #----------------------------------------------------------------------------
- # 实现帧数控制及计时器的类。
- #----------------------------------------------------------------------------
- class FPS_Timer
- #----------------------------------------------------------------------------
- # ● 常量
- #----------------------------------------------------------------------------
- FPS_COUNT = 10
- #--------------------------------------------------------------------------
- # ● 实例变量
- #--------------------------------------------------------------------------
- attr_accessor :fps
- attr_reader :real_fps
- attr_reader :total_skip
- attr_reader :count_sleep
- #--------------------------------------------------------------------------
- # ● 初始化对像
- #--------------------------------------------------------------------------
- def initialize(fps = 60, accurary = 10, skip_limit = 15)
- @fps = fps
- @accurary = accurary / 1000.0
- @skip_limit = skip_limit
- end
- #--------------------------------------------------------------------------
- # ● 重置
- #--------------------------------------------------------------------------
- def reset
- @old = get_ticks
- @skip = 0
- @real_fps = @fps
- @frame_count = 0
- @fps_old = @old
- @count_sleep = 0
- @total_skip = 0
- end
- #--------------------------------------------------------------------------
- # ● 等待帧
- #--------------------------------------------------------------------------
- def wait_frame
- now = get_ticks
- nxt = @old + (1.0 / @fps)
- if nxt > now or @skip > @skip_limit
- yield
- @skip = 0
- delay(nxt)
- @old = nxt
- else
- @skip += 1
- @total_skip += 1
- @old = get_ticks
- end
- calc_real_fps
- end
- #--------------------------------------------------------------------------
- # ● 私有方法
- #--------------------------------------------------------------------------
- private
- #--------------------------------------------------------------------------
- # ● 等待 nxt 指定的毫秒数
- #--------------------------------------------------------------------------
- def delay(nxt)
- while nxt > get_ticks + @accurary
- sleep(@accurary - 0.005)
- @count_sleep += 1
- end
- while nxt > get_ticks
- # 循环超时,无操作
- end
- end
- #--------------------------------------------------------------------------
- # ● 获取当前毫秒数
- #--------------------------------------------------------------------------
- def get_ticks
- SDL.get_ticks / 1000.0
- end
- #--------------------------------------------------------------------------
- # ● 计算实际FPS
- #--------------------------------------------------------------------------
- def calc_real_fps
- @frame_count += 1
- if @frame_count >= FPS_COUNT
- @frame_count = 0
- now = get_ticks
- @real_fps = FPS_COUNT / (now - @fps_old)
- @fps_old = now
- end
- end
- end
- #----------------------------------------------------------------------------
- # SDL_RSGE::SDL_Frame
- #----------------------------------------------------------------------------
- # 生成游戏主窗口的类。
- #----------------------------------------------------------------------------
- class SDL_Frame
- #--------------------------------------------------------------------------
- # ● 类变量
- #--------------------------------------------------------------------------
- @@GetSMetrics = Win32API.new('user32', 'GetSystemMetrics', 'l', 'i')
- @@FindWindow = Win32API.new('user32', 'FindWindow', 'pp', 'i')
- @@SetWindowPos = Win32API.new('user32', 'SetWindowPos', 'lllllll', 'i')
- #--------------------------------------------------------------------------
- # ● 初始化对象
- #--------------------------------------------------------------------------
- def initialize
- @screen_width = @@GetSMetrics.call(0)
- @screen_height = @@GetSMetrics.call(1)
- SDL.init(SDL::INIT_VIDEO)
- SDL::WM::set_caption($SDL_title, $0)
- @hwnd = @@FindWindow.call(0, $SDL_title.encode($RSGE_System_Encode))
- x = (@screen_width - $SDL_width ) / 2 - $SDL_X_Adjust
- y = (@screen_height - $SDL_height) / 2 - $SDL_Y_Adjust
- @@SetWindowPos.call(@hwnd, 0, x, y, 0, 0, 0)
- $SDL_Screen = SDL_Screen.new
- SDL::Mouse.hide if $SDL_hide_mouse
- SDL::TTF.init
- end
- end
- #----------------------------------------------------------------------------
- # SDL_RSGE::SDL_Screen
- #----------------------------------------------------------------------------
- # 生成游戏主画面的类。
- #----------------------------------------------------------------------------
- class SDL_Screen
- #--------------------------------------------------------------------------
- # ● 类变量
- #--------------------------------------------------------------------------
- @@fps_timer = FPS_Timer.new($SDL_frame_rate)
- #--------------------------------------------------------------------------
- # ● 实例变量
- #--------------------------------------------------------------------------
- attr_accessor :screen
- #--------------------------------------------------------------------------
- # ● 初始化对象
- #--------------------------------------------------------------------------
- def initialize
- flag = SDL::SWSURFACE
- @screen = SDL::Screen.open($SDL_width, $SDL_height, 32, flag)
- @screen.flip
- @@fps_timer.reset
- SDL::TTF.init
- @font = SDL::TTF.open('simhei.ttf',18)
- @font.style = SDL::TTF::STYLE_NORMAL
- @sprites = []
- end
- #--------------------------------------------------------------------------
- # ● 刷新画面
- #--------------------------------------------------------------------------
- def update
- @@fps_timer.wait_frame do
- @sprites.each { |sprite_z|
- sprite_z.each { |sprite|
- sprite.update unless sprite.updated
- sprite.updated = true
- }
- }
- @font.draw_blended_utf8(@screen,@@fps_timer.real_fps.round.to_s,0,0,255,255,255)
- @screen.flip
- end
- @screen.fill_rect(0, 0, $SDL_width, $SDL_height, [0, 0, 0])
- end
- #--------------------------------------------------------------------------
- # ●
- #--------------------------------------------------------------------------
- def add_sprite(sprite)
- if @sprites[sprite.z] == nil
- @sprites[sprite.z] = Array.new
- end
- @sprites[sprite.z] << sprite
- end
- #--------------------------------------------------------------------------
- # ●
- #--------------------------------------------------------------------------
- def format
- return @screen.format
- end
- end
- #----------------------------------------------------------------------------
- # RSGE::Bitmap
- #----------------------------------------------------------------------------
- # 位图的类。位图就是所谓的“图像”。
- # 在画面上显示位图,必须使用精灵(Sprite 精灵类)之类的对象。
- #----------------------------------------------------------------------------
- class Bitmap
- #--------------------------------------------------------------------------
- # ● 定义实例变量
- #--------------------------------------------------------------------------
- attr_reader :layer_info
- #--------------------------------------------------------------------------
- # ● 初始化对像
- #--------------------------------------------------------------------------
- def initialize(*args)
- @layer_info = []
- @temp_bitmap = []
- @format = $SDL_Screen.format
- if args.size == 1
- @surface = SDL::Surface.load(args[0])
- elsif args.size == 2
- @surface = SDL::Surface.new(SDL::SRCALPHA, args[0], args[1], @format)
- else
- p "参数错误".encode($RSGE_System_Encode)
- end
- @surface.display_format_alpha
- @layer_info << [:nil, @surface]
- @width = @surface.w
- @height = @surface.h
- end
- #--------------------------------------------------------------------------
- # ● 释放位图。若是已经若显示端口已释放则什么都不做。
- #--------------------------------------------------------------------------
- def dispose
- return if self.disposed?
- @surface.destroy
- @layer_info.clear
- @temp_bitmap.each { |bmp| bmp.destroy }
- @temp_bitmap.clear
- end
- #--------------------------------------------------------------------------
- # ● 当位图已释放时返回 true。
- #--------------------------------------------------------------------------
- def disposed?
- return @surface.destroyed?
- end
- #--------------------------------------------------------------------------
- # ● 获取位图宽度。
- #--------------------------------------------------------------------------
- def width
- return @width
- end
- #--------------------------------------------------------------------------
- # ● 获取位图高度。
- #--------------------------------------------------------------------------
- def height
- return @height
- end
- #--------------------------------------------------------------------------
- # ● 获取位图矩形(Rect 矩形类)。
- #--------------------------------------------------------------------------
- def rect
- return Rect.new(0, 0, @width, @height)
- end
- #--------------------------------------------------------------------------
- # ● 执行从位图 src_bitmap 的矩形 src_rect(Rect 矩形类)
- # 到指定的位图坐标 (x, y) 之间的区块过渡。
- #--------------------------------------------------------------------------
- def blt(x, y, src_bitmap, src_rect, opacity = 255)
- temp_bitmap = src_bitmap.layer_info[0][1].\
- copy_rect(src_rect.x, src_rect.y, src_rect.width, src_rect.height)
- temp_bitmap.set_alpha(SDL::SRCALPHA, opacity)
- info = [:blt, temp_bitmap, x, y]
- @layer_info << info
- @temp_bitmap << temp_bitmap
- end
- #--------------------------------------------------------------------------
- # ● 执行从位图 src_bitmap 的矩形 src_rect(Rect 矩形类)
- # 带指定的位图目标矩形 dest_rect(Rect 矩形类)之间的区块过渡。
- #--------------------------------------------------------------------------
- def stretch_blt(dest_rect, src_bitmap, src_rect, opacity = 255)
- info = [:stretch_blt, dest_rect, src_bitmap, src_rect, opacity]
- @layer_info << info
- end
- #--------------------------------------------------------------------------
- # ● 将位图区域 (x, y, width, height) 或矩形(Rect 矩形类)
- # 填满指定的颜色 color (Color 色彩类)。
- #--------------------------------------------------------------------------
- def fill_rect(*args)
- if args.size == 5
- x, y, width, height = args[0], args[1], args[2], args[3]
- alpha = args[4].alpha
- color = $SDL_Screen.format.map_rgba(args[4].red, args[4].green,
- args[4].blue, args[4].alpha)
- elsif args.size == 2
- x, y, width, height = args[0].x, args[0].y, args[0].width, args[0].height
- alpha = args[1].alpha
- color = $SDL_Screen.format.map_rgba(args[1].red, args[1].green,
- args[1].blue, args[1].alpha)
- end
- temp_bitmap = SDL::Surface.new(SDL::SRCALPHA, width, height, @format)
- temp_bitmap.set_alpha(SDL::SRCALPHA, alpha)
- temp_bitmap.draw_rect(0, 0, width, height, color, true, alpha)
- info = [:fill_rect, temp_bitmap, x, y]
- @layer_info << info
- @temp_bitmap << temp_bitmap
- end
- #--------------------------------------------------------------------------
- # ● 在位图区域 (x, y, width, height) 或矩形 rect (Rect 矩形类)中
- # 描绘字符串 str 。
- #--------------------------------------------------------------------------
- def draw_text(*args)
- if args.size >= 5
- x, y, width, height = args[0], args[1], args[2], args[3]
- str = args[4]
- align = args[5] if args.size == 5
- else
- x, y, width, height = args[0].x, args[0].y, args[0].width, args[0].height
- str = args[2]
- align = args[3] if args.size == 3
- end
- temp_bitmap = SDL::Surface.new(SDL::SRCALPHA, width, height, @format)
- font = SDL::TTF.open('simhei.ttf', 20)
- font.style = SDL::TTF::STYLE_NORMAL
- font.draw_blended_utf8(temp_bitmap, str, 0, 0, 255, 255, 255)
- font.close
- info = [:fill_rect, temp_bitmap, x, y]
- @layer_info << info
- @temp_bitmap << temp_bitmap
- end
- end
- #----------------------------------------------------------------------------
- # RSGE::Sprite
- #----------------------------------------------------------------------------
- # 精灵的类。精灵是在游戏画面上显示角色等的基本概念。
- #----------------------------------------------------------------------------
- class Sprite
- attr_accessor :bitmap
- attr_accessor :src_rect
- attr_accessor :visible
- attr_accessor :x
- attr_accessor :y
- attr_accessor :z
- attr_accessor :updated
- #--------------------------------------------------------------------------
- # ● 初始化对像
- #--------------------------------------------------------------------------
- def initialize
- @bitmap = nil
- @src_rect = nil
- @visible = true
- @x = 0
- @y = 0
- @z = 0
- @ox = 0
- @oy = 0
- @updated = false
- $SDL_Screen.add_sprite(self)
- end
- #--------------------------------------------------------------------------
- # ● 释放精灵。
- #--------------------------------------------------------------------------
- def dispose
- @bitmap.dispose
- end
- #--------------------------------------------------------------------------
- # ● 当精灵已释放则返回 true。
- #--------------------------------------------------------------------------
- def disposed?
- return @bitmap.disposed?
- end
- #--------------------------------------------------------------------------
- # ● 刷新画面
- #--------------------------------------------------------------------------
- def update
- return if @bitmap == nil or not @visible
- @bitmap.layer_info.each { |info|
- case info[0]
- when :nil
- SDL::Surface.blit(info[1], @src_rect.x, @src_rect.y,
- @src_rect.width, @src_rect.height, $SDL_Screen.screen, @x+@ox, @y+@oy)
- when :blt
- SDL::Surface.blit(info[1], 0, 0, @src_rect.width-info[2],
- @src_rect.height-info[3], $SDL_Screen.screen, @x+@ox+info[2], @y+@oy+info[3])
- when :stretch_blt
- #待书写
- when :fill_rect
- SDL::Surface.blit(info[1], 0, 0, @src_rect.width-info[2],
- @src_rect.height-info[3], $SDL_Screen.screen, @x+@ox+info[2], @y+@oy+info[3])
- when :draw_text
- SDL::Surface.blit(info[1], 0, 0, @src_rect.width-info[2],
- @src_rect.height-info[3], $SDL_Screen.screen, @x+@ox+info[2], @y+@oy+info[3])
- end
- }
- #------------------------------------------------------------------------
- # ● 获取精灵的宽度,相当于 src_rect.width。
- #------------------------------------------------------------------------
- def width
- return @src_rect.width
- end
- #------------------------------------------------------------------------
- # ● 获取精灵的高度,相当于 src_rect.height。
- #------------------------------------------------------------------------
- def height
- return @src_rect.height
- end
- end
- #--------------------------------------------------------------------------
- # ● 引用作为精灵起始点的位图(Bitmap 点阵图类)。
- #--------------------------------------------------------------------------
- def bitmap=(bmp)
- @bitmap = bmp
- @src_rect = Rect.new(0, 0, bmp.width, bmp.height)
- end
- end
- #----------------------------------------------------------------------------
- # SDL_RSGE::Color
- #----------------------------------------------------------------------------
- # 颜色的类。
- #----------------------------------------------------------------------------
- class Color
- #--------------------------------------------------------------------------
- # ● 定义实例变量
- #--------------------------------------------------------------------------
- attr_reader :red
- attr_reader :green
- attr_reader :blue
- attr_reader :alpha
- #--------------------------------------------------------------------------
- # ● 初始化对像
- #--------------------------------------------------------------------------
- def initialize(red, green, blue, alpha = 255)
- set(red, green, blue, alpha)
- end
- #--------------------------------------------------------------------------
- # ● set
- # 一次设置所有属性。
- #--------------------------------------------------------------------------
- def set(red, green, blue, alpha = 255)
- @red = red
- @green = green
- @blue = blue
- @alpha = alpha
- end
- end
- #----------------------------------------------------------------------------
- # SDL_RSGE::Rect
- #----------------------------------------------------------------------------
- # 矩形的类。
- #----------------------------------------------------------------------------
- class Rect
- #--------------------------------------------------------------------------
- # ● 定义实例变量
- #--------------------------------------------------------------------------
- attr_reader :x
- attr_reader :y
- attr_reader :width
- attr_reader :height
- #--------------------------------------------------------------------------
- # ● 初始化对像
- #--------------------------------------------------------------------------
- def initialize(x, y, width, height)
- set(x, y, width, height)
- end
- #--------------------------------------------------------------------------
- # ● set
- # 一次设置所有属性。
- #--------------------------------------------------------------------------
- def set(x, y, width, height)
- @x = x
- @y = y
- @width = width
- @height = height
- end
- end
- #----------------------------------------------------------------------------
- # SDL_RSGE::Tone
- #----------------------------------------------------------------------------
- # 色调的类。
- #----------------------------------------------------------------------------
- class Tone
- #--------------------------------------------------------------------------
- # ● 定义实例变量
- #--------------------------------------------------------------------------
- attr_reader :red
- attr_reader :green
- attr_reader :blue
- attr_reader :gray
- #--------------------------------------------------------------------------
- # ● 初始化对像
- #--------------------------------------------------------------------------
- def initialize(red, green, blue, gray = 0)
- set(red, green, blue, gray)
- end
- #--------------------------------------------------------------------------
- # ● set
- # 一次设置所有属性。
- #--------------------------------------------------------------------------
- def set(red, green, blue, gray = 0)
- @red = red
- @green = green
- @blue = blue
- @alpha = gray
- end
- end
- end
复制代码 我的毕业论文其中就有一部分是用Ruby/SDL实现RGSS,可惜……那时我还年轻…… |
评分
-
查看全部评分
|