Skip to content

Instantly share code, notes, and snippets.

@mirichi
Last active September 17, 2015 12:05
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/ec4c0f5c63a0da06acb7 to your computer and use it in GitHub Desktop.
Save mirichi/ec4c0f5c63a0da06acb7 to your computer and use it in GitHub Desktop.
sdl2rでDXRuby互換ぽいライブラリを作る(4)
require 'sdl2r'
require_relative 'fpstimer'
SDL.init(SDL::INIT_EVERYTHING)
module DXRuby
C_WHITE = [255, 255, 255]
C_RED = [255, 0, 0]
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 = ->{
image._create_texture unless image._texture
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
# SDL2のイベントによりマウスとキーボードの処理をする
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_accessor :_surface, :_texture, :_pixels
def initialize(w, h, color=[0, 0, 0, 0])
# wとhの両方が0の場合はSurfaceを生成しない
return if w == 0 and h == 0
# IntelCPUはリトルエンディアンだがビッグエンディアンにも一応対応しておく
# 画像フォーマットは32bit固定
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))
# Pixelsオブジェクト取得
@_pixels = @_surface.pixels
end
def width
@_surface.w
end
def height
@_surface.h
end
# テクスチャを破棄する
# 次に描画で使われる際に再生成される
def _modify
if @_texture
SDL.destroy_texture(@_texture)
@_texture = nil
end
end
# テクスチャ生成
def _create_texture
# テクスチャ生成
@_texture = SDL.create_texture_from_surface(Window._renderer, @_surface)
end
# ピクセルに色を置く
def []=(x, y, color)
@_pixels[x, y] = DXRuby._convert_color_dxruby_to_sdl(color)
self._modify
end
# ピクセルの色を取得する
def [](x, y)
@p_ixels[x, y]
self._modify
end
end
# DXRuby色配列からSDL::Color色配列へ変換
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