-
-
Save mirichi/4c4054681648d02078c8 to your computer and use it in GitHub Desktop.
sdl2rでDXRuby互換ぽいライブラリを作る(3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sdl2r' | |
require_relative 'fpstimer' | |
SDL.init(SDL::INIT_EVERYTHING) | |
module DXRuby | |
C_WHITE = [255, 255, 255] | |
module Window | |
@width = 640 | |
@height = 480 | |
@window = SDL.create_window("dxsdl2r Sample Application", | |
SDL::WINDOWPOS_UNDEFINED, | |
SDL::WINDOWPOS_UNDEFINED, | |
@width, | |
@height, | |
SDL::WINDOW_HIDDEN) | |
@renderer = SDL.create_renderer(@window, -1, 0) | |
def self.width;@width;end | |
def self.height;@height;end | |
def self._window;@window;end | |
def self._renderer;@renderer;end | |
def self.width=(v);@width=v;end | |
def self.height=(v);@height=v;end | |
@reservation = [] | |
def self.draw_box_fill(x1, y1, x2, y2, color, z=0) | |
tmp = DXRuby._convert_color_dxruby_to_sdl(color) | |
prc = ->{ | |
SDL.set_render_draw_color(@renderer, tmp[0], tmp[1], tmp[2], tmp[3]) | |
SDL.render_fill_rect(@renderer, SDL::Rect.new(x1, y1, x2 - x1 + 1, y2 - y1 + 1)) | |
} | |
@reservation << [z, prc] | |
end | |
def self.draw(x, y, image, z=0) | |
prc = ->{ | |
SDL.render_copy(@renderer, image._texture, nil, SDL::Rect.new(x, y, image.width, image.height)) | |
} | |
@reservation << [z, prc] | |
end | |
def self.loop | |
timer = FPSTimer.instance | |
timer.reset | |
SDL.set_window_size(@window, @width, @height) | |
SDL.show_window(@window) | |
Kernel.loop do | |
timer.wait_frame do | |
return if Input.update | |
SDL.set_render_draw_color(@renderer, 0, 0, 0, 255) | |
SDL.render_clear(@renderer) | |
yield | |
@reservation.sort_by!{|v|v[0]}.each{|v|v[1].call} | |
@reservation.clear | |
SDL.render_present(@renderer) | |
end | |
end | |
end | |
def self.caption | |
SDL.get_window_title(@window) | |
end | |
def self.caption=(str) | |
SDL.set_window_title(@window, str) | |
str | |
end | |
end | |
module Input | |
@mouse_x = @mouse_y = 0 | |
def self.update | |
while event = SDL.poll_event do | |
case event.type | |
when SDL::QUIT | |
return true | |
when SDL::MOUSEMOTION | |
@mouse_x, @mouse_y = event.x, event.y | |
end | |
end | |
false | |
end | |
def self.mouse_x | |
@mouse_x | |
end | |
def self.mouse_y | |
@mouse_y | |
end | |
end | |
class Image | |
attr_reader :_surface, :_texture, :width, :height | |
def initialize(w, h, color=[0, 0, 0, 0]) | |
if SDL::BYTEORDER == SDL::BIG_ENDIAN | |
@_surface = SDL.create_rgb_surface(0, w, h, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff) | |
else | |
@_surface = SDL.create_rgb_surface(0, w, h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000) | |
end | |
SDL.fill_rect(@_surface, nil, DXRuby._convert_color_dxruby_to_sdl(color)) | |
@_texture = SDL.create_texture_from_surface(Window._renderer, @_surface) | |
@width, @height = w, h | |
end | |
end | |
def self._convert_color_dxruby_to_sdl(color) | |
if color.size == 3 | |
color + [255] | |
else | |
color[1..3] << color[0] | |
end | |
end | |
end | |
include DXRuby | |
END{ | |
SDL.quit | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment