Skip to content

Instantly share code, notes, and snippets.

@mirichi
Created September 14, 2015 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirichi/4ed3239f6ac4d8ea274b to your computer and use it in GitHub Desktop.
Save mirichi/4ed3239f6ac4d8ea274b to your computer and use it in GitHub Desktop.
sdl2rでDXRuby互換ぽいライブラリを作る
require 'sdl2r'
require_relative 'fpstimer'
SDL.init(SDL::INIT_EVERYTHING)
module DXRuby
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.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
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
end
include DXRuby
END{
SDL.quit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment