rubysdl 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +505 -0
- data/MANIFEST +81 -0
- data/NEWS.en +144 -0
- data/NEWS.ja +151 -0
- data/README.en +117 -0
- data/README.ja +166 -0
- data/SDL_kanji.c +403 -0
- data/SDL_kanji.h +48 -0
- data/depend +18 -0
- data/doc/Makefile +18 -0
- data/doc/cdrom.rsd +431 -0
- data/doc/collision.rsd +162 -0
- data/doc/event.rsd +1487 -0
- data/doc/font.rsd +839 -0
- data/doc/general.rsd +49 -0
- data/doc/init.rsd +175 -0
- data/doc/joystick.rsd +387 -0
- data/doc/mixer.rsd +837 -0
- data/doc/mpeg.rsd +595 -0
- data/doc/rsd.rb +125 -0
- data/doc/sdlskk.rsd +496 -0
- data/doc/time.rsd +45 -0
- data/doc/video.rsd +2499 -0
- data/doc/wm.rsd +113 -0
- data/extconf.rb +92 -0
- data/lib/rubysdl_aliases.rb +431 -0
- data/lib/sdl.rb +271 -0
- data/rubysdl.h +109 -0
- data/rubysdl_cdrom.c +176 -0
- data/rubysdl_const_list.txt +280 -0
- data/rubysdl_doc.en.rd +2180 -0
- data/rubysdl_doc_old.rd +2402 -0
- data/rubysdl_event.c +346 -0
- data/rubysdl_event2.c +417 -0
- data/rubysdl_event_key.c +356 -0
- data/rubysdl_image.c +53 -0
- data/rubysdl_joystick.c +156 -0
- data/rubysdl_kanji.c +135 -0
- data/rubysdl_main.c +202 -0
- data/rubysdl_mixer.c +422 -0
- data/rubysdl_mouse.c +96 -0
- data/rubysdl_opengl.c +63 -0
- data/rubysdl_pixel.c +133 -0
- data/rubysdl_ref.html +5550 -0
- data/rubysdl_ref.rd +6163 -0
- data/rubysdl_rwops.c +90 -0
- data/rubysdl_sdlskk.c +312 -0
- data/rubysdl_sge_video.c +622 -0
- data/rubysdl_smpeg.c +341 -0
- data/rubysdl_time.c +36 -0
- data/rubysdl_ttf.c +324 -0
- data/rubysdl_video.c +749 -0
- data/rubysdl_wm.c +71 -0
- data/sample/aadraw.rb +24 -0
- data/sample/alpha.rb +27 -0
- data/sample/alphadraw.rb +25 -0
- data/sample/bfont.rb +24 -0
- data/sample/cdrom.rb +17 -0
- data/sample/collision.rb +97 -0
- data/sample/cursor.bmp +0 -0
- data/sample/cursor.rb +22 -0
- data/sample/ellipses.rb +35 -0
- data/sample/event2.rb +32 -0
- data/sample/font.bmp +0 -0
- data/sample/font.rb +25 -0
- data/sample/fpstimer.rb +175 -0
- data/sample/icon.bmp +0 -0
- data/sample/joy2.rb +81 -0
- data/sample/kanji.rb +36 -0
- data/sample/movesp.rb +93 -0
- data/sample/playmod.rb +14 -0
- data/sample/plaympeg.rb +48 -0
- data/sample/playwave.rb +16 -0
- data/sample/randrect.rb +40 -0
- data/sample/sample.ttf +0 -0
- data/sample/sdlskk.rb +70 -0
- data/sample/sgetest.rb +31 -0
- data/sample/stetris.rb +275 -0
- data/sample/testgl.rb +166 -0
- data/sample/testsprite.rb +68 -0
- data/sample/transformblit.rb +41 -0
- metadata +121 -0
data/sample/icon.bmp
ADDED
Binary file
|
data/sample/joy2.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
#
|
2
|
+
# Usage:
|
3
|
+
# ruby joy2.rb [n]
|
4
|
+
# n: if given, use n'th joystick
|
5
|
+
# if not given, show all connected joystick
|
6
|
+
#
|
7
|
+
require 'sdl'
|
8
|
+
|
9
|
+
White = [255,255,255]
|
10
|
+
|
11
|
+
def print_joystick_info
|
12
|
+
for i in 0..SDL::Joystick.num-1
|
13
|
+
print i,":",SDL::Joystick.indexName(0),"\n"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def display_button_state ( screen, joy )
|
18
|
+
for i in 0..joy.numButtons-1
|
19
|
+
if joy.button(i) then
|
20
|
+
screen.fillRect( i*30+30, 50, 10, 10, [0,0,128] )
|
21
|
+
else
|
22
|
+
screen.fillRect( i*30+30, 50, 10, 10, White )
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def display_hat_state ( screen, joy )
|
28
|
+
# display the state of only first hat
|
29
|
+
if joy.numHats > 0 then
|
30
|
+
x = y = 0
|
31
|
+
x = 1 if ( joy.hat(0) & SDL::Joystick::HAT_RIGHT ) != 0
|
32
|
+
x = -1 if ( joy.hat(0) & SDL::Joystick::HAT_LEFT ) != 0
|
33
|
+
y = 1 if ( joy.hat(0) & SDL::Joystick::HAT_DOWN ) != 0
|
34
|
+
y = -1 if ( joy.hat(0) & SDL::Joystick::HAT_UP ) != 0
|
35
|
+
screen.fillRect( 450 + x*40, 200 + y*40, 10, 10, White )
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def display_axis_state ( screen, joy )
|
40
|
+
for i in 0..joy.numAxes-1
|
41
|
+
screen.fillRect( i*30+130, joy.axis(i)*100/32768+200, 20, 20, White )
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
SDL.init( SDL::INIT_VIDEO|SDL::INIT_JOYSTICK )
|
46
|
+
screen = SDL::setVideoMode(640, 480, 16, SDL::SWSURFACE)
|
47
|
+
|
48
|
+
if SDL::Joystick.num == 0 then
|
49
|
+
print "No joystick available\n"
|
50
|
+
exit
|
51
|
+
end
|
52
|
+
|
53
|
+
if ARGV.size == 0 then
|
54
|
+
print_joystick_info
|
55
|
+
exit
|
56
|
+
end
|
57
|
+
|
58
|
+
joynum = ARGV[0].to_i
|
59
|
+
|
60
|
+
if SDL::Joystick.num < joynum then
|
61
|
+
print "Joystick No.#{joynum} is not available\n"
|
62
|
+
exit
|
63
|
+
end
|
64
|
+
|
65
|
+
joy=SDL::Joystick.open(joynum)
|
66
|
+
|
67
|
+
while true
|
68
|
+
|
69
|
+
while event = SDL::Event2.poll
|
70
|
+
case event
|
71
|
+
when SDL::Event2::KeyDown, SDL::Event2::Quit
|
72
|
+
exit
|
73
|
+
end
|
74
|
+
end
|
75
|
+
SDL::Joystick.updateAll
|
76
|
+
screen.fillRect(0,0,640,480,0)
|
77
|
+
display_button_state screen, joy
|
78
|
+
display_hat_state screen, joy
|
79
|
+
display_axis_state screen, joy
|
80
|
+
screen.updateRect(0, 0, 0, 0)
|
81
|
+
end
|
data/sample/kanji.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
#!/usr/local/bin/ruby -Ke
|
2
|
+
#
|
3
|
+
# This sample needs following two bdf files
|
4
|
+
# 8x16.bdf : alphabets
|
5
|
+
# jiskan16.bdf : chinese characters and kana
|
6
|
+
#
|
7
|
+
require 'sdl'
|
8
|
+
|
9
|
+
SDL.init( SDL::INIT_VIDEO )
|
10
|
+
screen = SDL::setVideoMode(640,480,16,SDL::SWSURFACE)
|
11
|
+
|
12
|
+
font = SDL::Kanji.open("8x16.bdf",16)
|
13
|
+
font.add("jiskan16.bdf")
|
14
|
+
font.setCodingSystem(SDL::Kanji::EUC)
|
15
|
+
|
16
|
+
y = 0
|
17
|
+
x = 0
|
18
|
+
|
19
|
+
while true
|
20
|
+
while event = SDL::Event2.poll
|
21
|
+
case event
|
22
|
+
when SDL::Event2::KeyDown, SDL::Event2::Quit
|
23
|
+
exit
|
24
|
+
end
|
25
|
+
end
|
26
|
+
screen.fillRect(0,0,640,480,0)
|
27
|
+
|
28
|
+
y = (y + 1) % 480
|
29
|
+
x = (x + 1) % 640
|
30
|
+
|
31
|
+
font.put(screen,"SDL Kanji�Υƥ�����",40,y,128,128,0)
|
32
|
+
font.putTate(screen,"�Ľ�Ǥ��ޤ���",x,60,128,128,0)
|
33
|
+
|
34
|
+
screen.updateRect(0,0,0,0)
|
35
|
+
sleep 0.005
|
36
|
+
end
|
data/sample/movesp.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'sdl'
|
2
|
+
|
3
|
+
SDL.init( SDL::INIT_VIDEO )
|
4
|
+
|
5
|
+
screen = SDL::setVideoMode(640,480,16,SDL::SWSURFACE)
|
6
|
+
|
7
|
+
image = SDL::Surface.loadBMP("icon.bmp")
|
8
|
+
image.setColorKey( SDL::SRCCOLORKEY || SDL::RLEACCEL ,0)
|
9
|
+
$image = image.displayFormat
|
10
|
+
|
11
|
+
|
12
|
+
class Sprite
|
13
|
+
def initialize
|
14
|
+
@x=rand(640)
|
15
|
+
@y=rand(480)
|
16
|
+
@dx=rand(11)-5
|
17
|
+
@dy=rand(11)-5
|
18
|
+
end
|
19
|
+
|
20
|
+
def move
|
21
|
+
@x += @dx
|
22
|
+
if @x >= 640 then
|
23
|
+
@dx *= -1
|
24
|
+
@x = 639
|
25
|
+
end
|
26
|
+
if @x < 0 then
|
27
|
+
@dx *= -1
|
28
|
+
@x = 0
|
29
|
+
end
|
30
|
+
@y += @dy
|
31
|
+
if @y >= 480 then
|
32
|
+
@dy *= -1
|
33
|
+
@y = 479
|
34
|
+
end
|
35
|
+
@y += @dy
|
36
|
+
if @y < 0 then
|
37
|
+
@dy *= -1
|
38
|
+
@y = 0
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def draw(screen)
|
43
|
+
SDL.blitSurface($image,0,0,32,32,screen,@x,@y)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
sprites = []
|
49
|
+
for i in 1..5
|
50
|
+
sprites << Sprite.new
|
51
|
+
end
|
52
|
+
|
53
|
+
class MovableSp
|
54
|
+
def initialize()
|
55
|
+
@ud=@lr=0;
|
56
|
+
end
|
57
|
+
|
58
|
+
def move()
|
59
|
+
@ud=@lr=0;
|
60
|
+
@lr=-1 if SDL::Key.press?(SDL::Key::H) or SDL::Key.press?(SDL::Key::LEFT)
|
61
|
+
@lr=1 if SDL::Key.press?(SDL::Key::L) or SDL::Key.press?(SDL::Key::RIGHT)
|
62
|
+
@ud=1 if SDL::Key.press?(SDL::Key::J) or SDL::Key.press?(SDL::Key::DOWN)
|
63
|
+
@ud=-1 if SDL::Key.press?(SDL::Key::K) or SDL::Key.press?(SDL::Key::UP)
|
64
|
+
end
|
65
|
+
|
66
|
+
def draw(screen)
|
67
|
+
SDL.blitSurface($image,0,0,32,32,screen,300+@lr*50,200+@ud*50)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
sprites << MovableSp.new
|
72
|
+
|
73
|
+
while true
|
74
|
+
while event = SDL::Event2.poll
|
75
|
+
case event
|
76
|
+
when SDL::Event2::Quit
|
77
|
+
exit
|
78
|
+
when SDL::Event2::KeyDown
|
79
|
+
exit if event.sym == SDL::Key::ESCAPE
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
screen.fillRect(0,0,640,480,0)
|
84
|
+
SDL::Key.scan
|
85
|
+
|
86
|
+
sprites.each {|i|
|
87
|
+
i.move
|
88
|
+
i.draw(screen)
|
89
|
+
}
|
90
|
+
screen.updateRect(0,0,0,0)
|
91
|
+
end
|
92
|
+
|
93
|
+
|
data/sample/playmod.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# This sample needs a mod file `sample.it'.
|
2
|
+
#
|
3
|
+
require 'sdl'
|
4
|
+
SDL::init(SDL::INIT_AUDIO)
|
5
|
+
|
6
|
+
SDL::Mixer.open(22050)
|
7
|
+
SDL::Mixer::playMusic?
|
8
|
+
music = SDL::Mixer::Music.load("sample.it")
|
9
|
+
|
10
|
+
SDL::Mixer.playMusic(music,0)
|
11
|
+
|
12
|
+
while SDL::Mixer::playMusic?
|
13
|
+
sleep 1
|
14
|
+
end
|
data/sample/plaympeg.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# This sample needs a MPEG file `sample.mpg'.
|
2
|
+
require 'sdl'
|
3
|
+
|
4
|
+
SDL.init( SDL::INIT_VIDEO|SDL::INIT_AUDIO )
|
5
|
+
SDL::Mixer.open
|
6
|
+
|
7
|
+
screen = SDL.setVideoMode( 320, 240, 16, SDL::SWSURFACE )
|
8
|
+
|
9
|
+
mpeg = SDL::MPEG.load( 'sample.mpg' )
|
10
|
+
|
11
|
+
info = mpeg.info
|
12
|
+
|
13
|
+
p(info)
|
14
|
+
|
15
|
+
mpeg.enableAudio true
|
16
|
+
mpeg.enableVideo true
|
17
|
+
|
18
|
+
mpeg.setDisplay(screen)
|
19
|
+
mpeg.setDisplayRegion( 0, 0, screen.w, screen.h )
|
20
|
+
mpeg.play
|
21
|
+
|
22
|
+
loop do
|
23
|
+
|
24
|
+
case event = SDL::Event2.poll
|
25
|
+
when SDL::Event2::Quit
|
26
|
+
mpeg.stop
|
27
|
+
exit
|
28
|
+
when SDL::Event2::KeyDown
|
29
|
+
case event.sym
|
30
|
+
when SDL::Key::S
|
31
|
+
mpeg.stop
|
32
|
+
when SDL::Key::P
|
33
|
+
mpeg.play
|
34
|
+
when SDL::Key::R
|
35
|
+
mpeg.rewind
|
36
|
+
when SDL::Key::ESCAPE
|
37
|
+
exit
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
if mpeg.status != SDL::MPEG::PLAYING then
|
42
|
+
mpeg.stop
|
43
|
+
exit
|
44
|
+
end
|
45
|
+
|
46
|
+
sleep 0.1
|
47
|
+
|
48
|
+
end
|
data/sample/playwave.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# This sample needs a wave file `sample.wav', please prepare.
|
2
|
+
require 'sdl'
|
3
|
+
|
4
|
+
SDL::init(SDL::INIT_AUDIO|SDL::INIT_VIDEO)
|
5
|
+
SDL.setVideoMode(640,480,16,SDL::SWSURFACE)
|
6
|
+
|
7
|
+
SDL::Mixer.open
|
8
|
+
wave=SDL::Mixer::Wave.load("sample.wav")
|
9
|
+
|
10
|
+
SDL::Mixer.playChannel(0,wave,0)
|
11
|
+
|
12
|
+
while SDL::Mixer::play?(0)
|
13
|
+
sleep 1
|
14
|
+
end
|
15
|
+
|
16
|
+
|
data/sample/randrect.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# SDL random rect sample
|
2
|
+
# original code by adas@geocities.co.jp written by C
|
3
|
+
# auther TAMURA Kenichi <sgs02516@nifty.com>
|
4
|
+
#
|
5
|
+
require 'sdl'
|
6
|
+
|
7
|
+
src_w = 640
|
8
|
+
src_h = 480
|
9
|
+
src_d = 16
|
10
|
+
|
11
|
+
dst_w = 0
|
12
|
+
dst_h = 0
|
13
|
+
|
14
|
+
MAX = 1000
|
15
|
+
RECTSIZE = 400
|
16
|
+
|
17
|
+
SDL.init SDL::INIT_VIDEO
|
18
|
+
screen = SDL::setVideoMode src_w,src_h,src_d, SDL::SWSURFACE|SDL::ANYFORMAT
|
19
|
+
|
20
|
+
srand
|
21
|
+
|
22
|
+
(1 .. MAX).each do |i|
|
23
|
+
color = screen.mapRGB rand(256),rand(256),rand(256)
|
24
|
+
dst_w = rand(RECTSIZE) + 1
|
25
|
+
dst_h = rand(RECTSIZE) + 1
|
26
|
+
screen.fillRect rand(src_w-dst_w), rand(src_h-dst_h), dst_w, dst_h, color
|
27
|
+
screen.updateRect 0,0,0,0
|
28
|
+
|
29
|
+
while event = SDL::Event2.poll
|
30
|
+
case event
|
31
|
+
when SDL::Event2::Quit
|
32
|
+
exit
|
33
|
+
when SDL::Event2::KeyDown
|
34
|
+
exit if event.sym == SDL::Key::ESCAPE
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
SDL::Key.scan
|
39
|
+
end
|
40
|
+
|
data/sample/sample.ttf
ADDED
Binary file
|
data/sample/sdlskk.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
#
|
2
|
+
# SDLSKK from Ruby/SDL
|
3
|
+
#
|
4
|
+
# This sample needs three files.
|
5
|
+
# jisyo : SKK's dictionary
|
6
|
+
# rule_table : SDLSKK's
|
7
|
+
# nihongo.ttf : True Type Font file that has Japanese characters
|
8
|
+
#
|
9
|
+
# Usage:
|
10
|
+
# ruby sdlskk.rb [-m]
|
11
|
+
#
|
12
|
+
# -m Use minibuffer
|
13
|
+
#
|
14
|
+
require 'sdl'
|
15
|
+
|
16
|
+
use_minibuffer = (ARGV[0]=='-m')
|
17
|
+
|
18
|
+
SDL.init( SDL::INIT_VIDEO )
|
19
|
+
SDL::TTF.init
|
20
|
+
SDL::Event2.enableUNICODE
|
21
|
+
|
22
|
+
font = SDL::TTF.open( 'nihongo.ttf', 14 )
|
23
|
+
|
24
|
+
dict = SDL::SKK::Dictionary.new
|
25
|
+
dict.load( 'jisyo', false )
|
26
|
+
table = SDL::SKK::RomKanaRuleTable.new( 'rule_table' )
|
27
|
+
bind = SDL::SKK::Keybind.new
|
28
|
+
bind.set_default_key
|
29
|
+
|
30
|
+
context = SDL::SKK::Context.new( dict, table, bind, use_minibuffer )
|
31
|
+
|
32
|
+
screen = SDL::setVideoMode( 640, 480, 16, SDL::SWSURFACE )
|
33
|
+
SDL::WM.setCaption( $0, $0 )
|
34
|
+
|
35
|
+
BLACK = screen.mapRGB( 0, 0, 0 )
|
36
|
+
loop do
|
37
|
+
|
38
|
+
while event = SDL::Event2.poll do
|
39
|
+
case event
|
40
|
+
when SDL::Event2::Quit
|
41
|
+
exit
|
42
|
+
when SDL::Event2::KeyDown
|
43
|
+
if event.sym == SDL::Key::ESCAPE then
|
44
|
+
exit
|
45
|
+
end
|
46
|
+
if event.sym == SDL::Key::F1
|
47
|
+
dict.save("test_user_dict")
|
48
|
+
end
|
49
|
+
|
50
|
+
context.input( event )
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
text_surface = context.render_str( font, 255, 0, 255 )
|
55
|
+
|
56
|
+
screen.fillRect( 0, 0, 640, 480, BLACK )
|
57
|
+
SDL.blitSurface( text_surface, 0, 0, 0, 0, screen, 0, 0 )
|
58
|
+
|
59
|
+
if use_minibuffer then
|
60
|
+
minibuffer_surface = context.render_minibuffer_str( font, 255, 0, 255 )
|
61
|
+
if minibuffer_surface then
|
62
|
+
SDL.blitSurface( minibuffer_surface, 0, 0, 0, 0, screen, 0, 40 )
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
screen.updateRect( 0, 0, 0, 0 )
|
67
|
+
|
68
|
+
sleep 0.05
|
69
|
+
|
70
|
+
end
|
data/sample/sgetest.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'sdl'
|
2
|
+
|
3
|
+
SDL.init( SDL::INIT_VIDEO )
|
4
|
+
screen = SDL::setVideoMode(640,480,16,SDL::SWSURFACE)
|
5
|
+
SDL::WM::setCaption $0, $0
|
6
|
+
|
7
|
+
# draw red pixel at (200,200)
|
8
|
+
screen[200,200]= screen.format.mapRGB(255,0,0)
|
9
|
+
# draw green pixel at (250,200)
|
10
|
+
screen[250,200]= screen.format.mapRGB(0,255,0)
|
11
|
+
# draw blue pixel at (200,200)
|
12
|
+
screen[300,200]= screen.format.mapRGB(0,0,255)
|
13
|
+
|
14
|
+
Red=screen.format.mapRGB(255,0,0)
|
15
|
+
screen.drawLine(20,20,300,200,Red)
|
16
|
+
screen.drawRect(49,59,80,80,Red)
|
17
|
+
screen.drawCircle(100,100,50,[87,87,87])
|
18
|
+
screen.drawFilledCircle(300,300,30,Red)
|
19
|
+
|
20
|
+
screen.flip
|
21
|
+
|
22
|
+
while true
|
23
|
+
while event = SDL::Event2.poll
|
24
|
+
case event
|
25
|
+
when SDL::Event2::KeyDown, SDL::Event2::Quit
|
26
|
+
exit
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
sleep 0.2
|
31
|
+
end
|
data/sample/stetris.rb
ADDED
@@ -0,0 +1,275 @@
|
|
1
|
+
# This sample need ruby 1.8 or 1.6 with shim
|
2
|
+
# Thanks to Simon Strandgaard
|
3
|
+
require 'sdl'
|
4
|
+
if RUBY_VERSION < "1.7" then
|
5
|
+
require 'features/ruby18'
|
6
|
+
end
|
7
|
+
|
8
|
+
class Object
|
9
|
+
def deep_clone
|
10
|
+
Marshal::load(Marshal.dump(self))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Array
|
15
|
+
def random
|
16
|
+
at(Kernel.rand(size))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Pattern
|
21
|
+
def initialize(x, y, *data)
|
22
|
+
@x = x
|
23
|
+
@y = y
|
24
|
+
@data = data
|
25
|
+
end
|
26
|
+
attr_reader :data, :x, :y
|
27
|
+
alias width x
|
28
|
+
def rotate
|
29
|
+
@data = @data.reverse.transpose
|
30
|
+
@x, @y = @y, @x
|
31
|
+
end
|
32
|
+
PAT1 = Pattern.new(4, 1, [1, 1, 1, 1])
|
33
|
+
PAT2 = Pattern.new(3, 2, [1, 1, 1], [0, 1, 0])
|
34
|
+
PAT3 = Pattern.new(3, 2, [1, 1, 1], [1, 0, 0])
|
35
|
+
PAT4 = Pattern.new(3, 2, [1, 1, 1], [0, 0, 1])
|
36
|
+
PAT5 = Pattern.new(2, 2, [1, 1], [1, 1])
|
37
|
+
PAT6 = Pattern.new(3, 2, [1, 1, 0], [0, 1, 1])
|
38
|
+
PAT7 = Pattern.new(3, 2, [0, 1, 1], [1, 1, 0])
|
39
|
+
def Pattern::patterns
|
40
|
+
[PAT1, PAT2, PAT3, PAT4, PAT5, PAT6, PAT7]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class Level
|
45
|
+
def initialize(width=10, height=15)
|
46
|
+
@height = height
|
47
|
+
@width = width
|
48
|
+
@data = Array.new(height) { Array.new(width, 0) }
|
49
|
+
backup_background
|
50
|
+
end
|
51
|
+
attr_reader :width, :height
|
52
|
+
def test
|
53
|
+
@data[0][0] = 1
|
54
|
+
@data[0][@width-1] = 1
|
55
|
+
@data[@height-1][0] = 1
|
56
|
+
@data[@height-1][@width-1] = 1
|
57
|
+
end
|
58
|
+
def bg_is_collision(offset_x, offset_y, pattern)
|
59
|
+
return true if pattern.x + offset_x > @width
|
60
|
+
return true if pattern.y + offset_y > @height
|
61
|
+
y = offset_y
|
62
|
+
pattern.data.each do |row|
|
63
|
+
x = offset_x
|
64
|
+
row.each do |cell|
|
65
|
+
if (cell != 0) and (@bg[y][x] != 0)
|
66
|
+
return true
|
67
|
+
end
|
68
|
+
x += 1
|
69
|
+
end
|
70
|
+
y += 1
|
71
|
+
end
|
72
|
+
return false
|
73
|
+
end
|
74
|
+
def restore_background
|
75
|
+
@data = @bg
|
76
|
+
end
|
77
|
+
def backup_background
|
78
|
+
@bg = @data.deep_clone
|
79
|
+
end
|
80
|
+
def or_pattern(offset_x, offset_y, pattern)
|
81
|
+
backup_background
|
82
|
+
y = offset_y
|
83
|
+
pattern.data.each do |row|
|
84
|
+
x = offset_x
|
85
|
+
row.each do |cell|
|
86
|
+
@data[y][x] = cell if cell != 0
|
87
|
+
x += 1
|
88
|
+
end
|
89
|
+
y += 1
|
90
|
+
end
|
91
|
+
end
|
92
|
+
def find_filled_rows
|
93
|
+
res = []
|
94
|
+
@data.each_index do |y|
|
95
|
+
ok = true
|
96
|
+
@data[y].each do |cell|
|
97
|
+
if cell == 0
|
98
|
+
ok = false
|
99
|
+
break
|
100
|
+
end
|
101
|
+
end
|
102
|
+
res << y if ok
|
103
|
+
end
|
104
|
+
res
|
105
|
+
end
|
106
|
+
def remove_rows(*rows)
|
107
|
+
rows.uniq!
|
108
|
+
rows.each do |y|
|
109
|
+
@bg[y] = nil
|
110
|
+
end
|
111
|
+
@bg.compact!
|
112
|
+
extra = Array.new(rows.size) { Array.new(@width, 0) }
|
113
|
+
@bg = extra + @bg
|
114
|
+
raise "integrity error" if @bg.size != @height
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class Level
|
119
|
+
def load_render_data
|
120
|
+
@image = SDL::Surface.loadBMP("icon.bmp")
|
121
|
+
@image.setColorKey( SDL::SRCCOLORKEY ,0)
|
122
|
+
@image = @image.displayFormat
|
123
|
+
@step_x = 32 # todo: image width
|
124
|
+
@step_y = 32 # todo: image height
|
125
|
+
# todo: raise exception is level does not fit to screen!
|
126
|
+
@offset_x = 100
|
127
|
+
@offset_y = 20
|
128
|
+
|
129
|
+
@fill_removal_dir = false
|
130
|
+
end
|
131
|
+
def render
|
132
|
+
y = @offset_y
|
133
|
+
@data.each do |row|
|
134
|
+
render_line(y, row)
|
135
|
+
y += @step_y
|
136
|
+
end
|
137
|
+
end
|
138
|
+
def render_line(y, cells)
|
139
|
+
x = @offset_x
|
140
|
+
cells.each do |cell|
|
141
|
+
i = (cell == 0) ? 63 : 255
|
142
|
+
@image.setAlpha(SDL::SRCALPHA,i)
|
143
|
+
$screen.put(@image,x,y)
|
144
|
+
x += @step_x
|
145
|
+
end
|
146
|
+
end
|
147
|
+
FILL = (20 * 256*256) + (0 * 256) + 10
|
148
|
+
def render_removal(rows)
|
149
|
+
$screen.fillRect(0,0,640,512,0)
|
150
|
+
render
|
151
|
+
rows.reverse_each do |row|
|
152
|
+
y = (row * @step_y) + @offset_y
|
153
|
+
|
154
|
+
if @fill_removal_dir
|
155
|
+
i = 0
|
156
|
+
x = @offset_x
|
157
|
+
while i < @width
|
158
|
+
$screen.fillRect(x,y,@step_x,@step_y,FILL)
|
159
|
+
$screen.flip
|
160
|
+
i += 1
|
161
|
+
x += @step_x
|
162
|
+
end
|
163
|
+
@fill_removal_dir = false
|
164
|
+
else
|
165
|
+
i = 0
|
166
|
+
x = @offset_x + ((@width-1)*@step_x)
|
167
|
+
while i < @width
|
168
|
+
$screen.fillRect(x,y,@step_x,@step_y,FILL)
|
169
|
+
$screen.flip
|
170
|
+
i += 1
|
171
|
+
x -= @step_x
|
172
|
+
end
|
173
|
+
@fill_removal_dir = true
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
SDL.init( SDL::INIT_VIDEO )
|
180
|
+
$screen = SDL::setVideoMode(640,512,24,SDL::SWSURFACE)
|
181
|
+
level = Level.new
|
182
|
+
level.test
|
183
|
+
level.load_render_data
|
184
|
+
pat = Pattern::PAT3.clone
|
185
|
+
pat_x = 5
|
186
|
+
pat_y = 0
|
187
|
+
time_step = 0.5
|
188
|
+
time = Time.now
|
189
|
+
launch_new_pattern = true
|
190
|
+
loop do
|
191
|
+
if launch_new_pattern
|
192
|
+
launch_new_pattern = false
|
193
|
+
time_step = 0.5
|
194
|
+
time = Time.now
|
195
|
+
pat = Pattern::patterns.random.clone
|
196
|
+
pat_x = 5
|
197
|
+
pat_y = 0
|
198
|
+
level.backup_background
|
199
|
+
if level.bg_is_collision(pat_x, pat_y, pat)
|
200
|
+
puts "Sorry you are game over"
|
201
|
+
sleep 3
|
202
|
+
raise "game over"
|
203
|
+
end
|
204
|
+
level.or_pattern(pat_x, pat_y, pat)
|
205
|
+
end
|
206
|
+
|
207
|
+
# timer events
|
208
|
+
while Time.now > time + time_step
|
209
|
+
pat_y += 1
|
210
|
+
if level.bg_is_collision(pat_x, pat_y, pat)
|
211
|
+
launch_new_pattern = true
|
212
|
+
rows = level.find_filled_rows
|
213
|
+
if rows.size > 0
|
214
|
+
puts "rows filled"
|
215
|
+
p rows
|
216
|
+
level.render_removal(rows)
|
217
|
+
|
218
|
+
level.backup_background
|
219
|
+
level.remove_rows(*rows)
|
220
|
+
level.restore_background
|
221
|
+
end
|
222
|
+
else
|
223
|
+
level.restore_background
|
224
|
+
level.or_pattern(pat_x, pat_y, pat)
|
225
|
+
end
|
226
|
+
time += time_step
|
227
|
+
end
|
228
|
+
|
229
|
+
old_pat_x = pat_x
|
230
|
+
old_pat_y = pat_y
|
231
|
+
rotate = false
|
232
|
+
|
233
|
+
# handle keystrokes
|
234
|
+
while event = SDL::Event2.poll
|
235
|
+
case event
|
236
|
+
when SDL::Event2::Quit then exit
|
237
|
+
when SDL::Event2::KeyDown
|
238
|
+
case event.sym
|
239
|
+
when SDL::Key::ESCAPE then exit
|
240
|
+
when SDL::Key::UP then rotate = true
|
241
|
+
when SDL::Key::DOWN then time_step = 0.1
|
242
|
+
when SDL::Key::LEFT
|
243
|
+
pat_x -= 1 if pat_x > 0
|
244
|
+
when SDL::Key::RIGHT
|
245
|
+
pat_x += 1 if pat_x < (level.width - pat.width)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
SDL::Key.scan
|
250
|
+
|
251
|
+
# move pattern & do collition tests
|
252
|
+
if (pat_x <=> old_pat_x) or (pat_y <=> old_pat_y) or (rotate == true)
|
253
|
+
|
254
|
+
old_pat = pat.clone
|
255
|
+
pat.rotate if rotate
|
256
|
+
|
257
|
+
# if collision then restore last working-state
|
258
|
+
if level.bg_is_collision(pat_x, pat_y, pat)
|
259
|
+
pat_x = old_pat_x
|
260
|
+
pat_y = old_pat_y
|
261
|
+
pat = old_pat
|
262
|
+
else
|
263
|
+
# collision avoided.. therefore don't launch new pattern
|
264
|
+
launch_new_pattern = false
|
265
|
+
level.restore_background
|
266
|
+
level.or_pattern(pat_x, pat_y, pat)
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
# repaint screen
|
271
|
+
$screen.fillRect(0,0,640,512,0)
|
272
|
+
level.render
|
273
|
+
GC.start # release memory.. otherwise it would grow steady
|
274
|
+
$screen.flip
|
275
|
+
end
|