require 'sdl'
class Rect
attr_accessor :x, :y, :w, :h
def initialize( x, y, w, h )
@x = x
@y = y
@w = w
@h = h
end
def contain( x, y )
x > @x and x < @x + @w and y > @y and y < @y + @h
end
end
class Button
def initialize( x, y, w, h, image )
@box = Rect.new( x, y, w, h )
@clips = [ Rect.new( 0, 0, 320, 240 ),
Rect.new( 320, 0, 320, 240 ),
Rect.new( 0, 240, 320, 240 ),
Rect.new( 320, 240, 320, 240 ) ]
@image = image
@clip = @clips[1]
end
def handle_events( event )
case event
when SDL::Event::MouseMotion
x = event.x
y = event.y
if @box.contain( x, y )
@clip = @clips[0]
else
@clip = @clips[1]
end
when SDL::Event::MouseButtonDown
x = event.x
y = event.y
@clip = @clips[2] if @box.contain( x, y )
when SDL::Event::MouseButtonUp
x = event.x
y = event.y
@clip = @clips[3] if @box.contain( x, y )
end
end
def show( screen )
apply_surface( @box.x, @box.y, @image, screen, @clip )
end
end
def load_image( filename, color_key = nil )
loaded_image = SDL::Surface.load( filename )
optimized_image = loaded_image.display_format
if color_key
color_key = optimized_image.map_rgb( color_key[0], color_key[1], color_key[2] )
optimized_image.set_color_key( SDL::SRCCOLORKEY, color_key )
end
return optimized_image
end
def apply_surface( x, y, source, destination, rect = nil)
rect = Rect.new( 0, 0, source.w, source.h ) unless rect
SDL::Surface.blit( source, rect.x, rect.y, rect.w, rect.h, destination, x, y )
end
def init_video( screen_width, screen_height, screen_bpp, caption = "Hello World" )
SDL.init( SDL::INIT_VIDEO )
screen = SDL::set_video_mode( screen_width, screen_height, screen_bpp, SDL::HWSURFACE )
SDL::WM.set_caption( caption, "" )
return screen
end
def init_ttf( filename, size )
SDL::TTF.init
font = SDL::TTF.open( filename, size, index = 0 )
return font
end
def clean_up
# do nothing
end
def main
screen_width = 640
screen_height = 480
screen_bpp = 32
########################################################
screen = init_video( screen_width, screen_height, screen_bpp, "Button Test" )
########################################################
button_sheets = load_image( "button.png" )
button = Button.new( 170, 120, 320, 240, button_sheets )
########################################################
quit = false
while !quit
########################################################
screen.fill_rect( 0, 0, screen.w, screen.h, screen.map_rgb( 0xff, 0xff, 0xff ) )
button.show( screen )
########################################################
screen.flip
while event = SDL::Event.poll
########################################################
button.handle_events( event )
case event
when SDL::Event::Quit
quit = true
end
########################################################
end
end
clean_up
########################################################
end
main
댓글을 달아 주세요
코드가 좀 정신사나워 보이는듯...
개인적으로 Ruby/SDL보다는 RubyGame이 나아 보이던데. 그리고 RubyGame보다는 PyGame이... =3=3
ㅠ.ㅠ
잘 봤습니다. 저도 루비로 간단한게임 만들고 싶은데.,,. PyGame 이 더 좋다더군요. 루비 개발자 커뮤니티에서 그랬다능
루비도 좋습니다.
http://www.filepang.co.kr/entry/Ruby-Chipmunk-Gosu
이런것도 루비로 잘되요.