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/rubysdl_wm.c
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
/*
|
2
|
+
Ruby/SDL Ruby extension library for SDL
|
3
|
+
|
4
|
+
Copyright (C) 2001-2007 Ohbayashi Ippei
|
5
|
+
|
6
|
+
This library is free software; you can redistribute it and/or
|
7
|
+
modify it under the terms of the GNU Lesser General Public
|
8
|
+
License as published by the Free Software Foundation; either
|
9
|
+
version 2.1 of the License, or (at your option) any later version.
|
10
|
+
|
11
|
+
This library is distributed in the hope that it will be useful,
|
12
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
Lesser General Public License for more details.
|
15
|
+
|
16
|
+
You should have received a copy of the GNU Lesser General Public
|
17
|
+
License along with this library; if not, write to the Free Software
|
18
|
+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19
|
+
*/
|
20
|
+
#include "rubysdl.h"
|
21
|
+
|
22
|
+
static VALUE sdl_wm_getCaption(VALUE mod)
|
23
|
+
{
|
24
|
+
char *title,*icon;
|
25
|
+
SDL_WM_GetCaption( &title,&icon );
|
26
|
+
return rb_ary_new3( 2,rb_str_new2(title),rb_str_new2(icon) );
|
27
|
+
}
|
28
|
+
static VALUE sdl_wm_setCaption(VALUE mod,VALUE title,VALUE icon)
|
29
|
+
{
|
30
|
+
SDL_WM_SetCaption( GETCSTR(title),GETCSTR(icon) );
|
31
|
+
return Qnil;
|
32
|
+
}
|
33
|
+
static VALUE sdl_wm_setIcon(VALUE mod,VALUE icon)
|
34
|
+
{
|
35
|
+
SDL_Surface *surface;
|
36
|
+
if( ! rb_obj_is_kind_of(icon,cSurface) )
|
37
|
+
rb_raise(rb_eArgError,"type mismatch (expected Surface)");
|
38
|
+
Data_Get_Struct(icon,SDL_Surface,surface);
|
39
|
+
SDL_WM_SetIcon(surface,NULL);
|
40
|
+
return Qnil;
|
41
|
+
}
|
42
|
+
static VALUE sdl_wm_iconifyWindow(VALUE mod)
|
43
|
+
{
|
44
|
+
if( ! SDL_WM_IconifyWindow() )
|
45
|
+
rb_raise( eSDLError,"iconify failed: %s",SDL_GetError() );
|
46
|
+
return Qnil;
|
47
|
+
}
|
48
|
+
|
49
|
+
static void defineConstForWM()
|
50
|
+
{
|
51
|
+
rb_define_const(mWM,"GRAB_QUERY",INT2NUM(SDL_GRAB_QUERY));
|
52
|
+
rb_define_const(mWM,"GRAB_OFF",INT2NUM(SDL_GRAB_OFF));
|
53
|
+
rb_define_const(mWM,"GRAB_ON",INT2NUM(SDL_GRAB_ON));
|
54
|
+
}
|
55
|
+
|
56
|
+
static VALUE sdl_wm_grabInput(VALUE mod, VALUE flag)
|
57
|
+
{
|
58
|
+
return INT2FIX(SDL_WM_GrabInput(flag));
|
59
|
+
}
|
60
|
+
|
61
|
+
void init_wm()
|
62
|
+
{
|
63
|
+
mWM=rb_define_module_under(mSDL,"WM");
|
64
|
+
rb_define_module_function(mWM,"caption",sdl_wm_getCaption,0);
|
65
|
+
rb_define_module_function(mWM,"setCaption",sdl_wm_setCaption,2);
|
66
|
+
rb_define_module_function(mWM,"icon=",sdl_wm_setIcon,1);
|
67
|
+
rb_define_module_function(mWM,"iconify",sdl_wm_iconifyWindow,0);
|
68
|
+
rb_define_module_function(mWM,"grabInput",sdl_wm_grabInput,1);
|
69
|
+
|
70
|
+
defineConstForWM();
|
71
|
+
}
|
data/sample/aadraw.rb
ADDED
@@ -0,0 +1,24 @@
|
|
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
|
+
Red=screen.format.mapRGB(255,0,0)
|
8
|
+
screen.drawAALine(20,20,300,200,Red)
|
9
|
+
screen.drawAACircle(100,100,50,[87,87,87])
|
10
|
+
screen.drawAAFilledCircle(300,300,30,Red)
|
11
|
+
screen.drawAAEllipse(320,240,100,200,[200,255,0])
|
12
|
+
|
13
|
+
screen.flip
|
14
|
+
|
15
|
+
while true
|
16
|
+
while event = SDL::Event2.poll
|
17
|
+
case event
|
18
|
+
when SDL::Event2::KeyDown, SDL::Event2::Quit
|
19
|
+
exit
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
sleep 0.2
|
24
|
+
end
|
data/sample/alpha.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'sdl'
|
2
|
+
|
3
|
+
SDL.init( SDL::INIT_VIDEO )
|
4
|
+
screen = SDL::setVideoMode(640,480,16,SDL::SWSURFACE)
|
5
|
+
|
6
|
+
image = SDL::Surface.loadBMP("icon.bmp")
|
7
|
+
image.setColorKey( SDL::SRCCOLORKEY ,0)
|
8
|
+
image = image.displayFormat
|
9
|
+
|
10
|
+
event=SDL::Event.new
|
11
|
+
i=0;
|
12
|
+
while true
|
13
|
+
if event.poll != 0 then
|
14
|
+
if event.type==SDL::Event::QUIT then
|
15
|
+
break
|
16
|
+
end
|
17
|
+
if event.type==SDL::Event::KEYDOWN then
|
18
|
+
exit
|
19
|
+
end
|
20
|
+
end
|
21
|
+
screen.fillRect(0,0,640,480,0)
|
22
|
+
|
23
|
+
image.setAlpha(SDL::SRCALPHA,i%256)
|
24
|
+
screen.put(image,310,195)
|
25
|
+
i+=1
|
26
|
+
screen.flip
|
27
|
+
end
|
data/sample/alphadraw.rb
ADDED
@@ -0,0 +1,25 @@
|
|
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
|
+
Red=screen.format.mapRGB(255,0,0)
|
8
|
+
screen.drawAAFilledCircle(320,240,220,[140,180,0])
|
9
|
+
screen.drawAALineAlpha(20,20,300,200,Red,rand(64)+128)
|
10
|
+
screen.drawAACircleAlpha(100,100,50,[87,87,87],rand(64)+128)
|
11
|
+
screen.drawFilledCircleAlpha(300,170,50,[87,80,0],rand(64)+128)
|
12
|
+
screen.drawEllipseAlpha(320,240,100,200,[200,255,0],rand(64)+128)
|
13
|
+
|
14
|
+
screen.flip
|
15
|
+
|
16
|
+
while true
|
17
|
+
while event = SDL::Event2.poll
|
18
|
+
case event
|
19
|
+
when SDL::Event2::KeyDown, SDL::Event2::Quit
|
20
|
+
exit
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
sleep 0.2
|
25
|
+
end
|
data/sample/bfont.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'sdl'
|
2
|
+
|
3
|
+
SDL.init( SDL::INIT_VIDEO )
|
4
|
+
screen = SDL::setVideoMode(640,480,16,SDL::SWSURFACE)
|
5
|
+
|
6
|
+
font = SDL::BMFont.open("font.bmp",SDL::BMFont::TRANSPARENT)
|
7
|
+
|
8
|
+
y = 0
|
9
|
+
|
10
|
+
while true
|
11
|
+
while event = SDL::Event2.poll
|
12
|
+
case event
|
13
|
+
when SDL::Event2::KeyDown, SDL::Event2::Quit
|
14
|
+
exit
|
15
|
+
end
|
16
|
+
end
|
17
|
+
screen.fillRect(0,0,640,480,0)
|
18
|
+
|
19
|
+
y = (y + 1) % 480
|
20
|
+
font.textout(screen,"BitMapFont Testing..",40,y)
|
21
|
+
|
22
|
+
screen.updateRect(0,0,0,0)
|
23
|
+
sleep 0.005
|
24
|
+
end
|
data/sample/cdrom.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'sdl'
|
2
|
+
|
3
|
+
SDL.init( SDL::INIT_CDROM )
|
4
|
+
|
5
|
+
print SDL::CD.numDrive,"\n"
|
6
|
+
print SDL::CD.indexName(0),"\n"
|
7
|
+
cd=SDL::CD.open(0)
|
8
|
+
cd.status
|
9
|
+
print cd.numTracks," ",cd.status, "\n"
|
10
|
+
|
11
|
+
begin
|
12
|
+
cd.playTracks 1,0,1,0
|
13
|
+
sleep 100
|
14
|
+
ensure
|
15
|
+
cd.stop
|
16
|
+
end
|
17
|
+
|
data/sample/collision.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'sdl'
|
2
|
+
|
3
|
+
SDL.init( SDL::INIT_VIDEO )
|
4
|
+
screen = SDL::setVideoMode(640,480,16,SDL::SWSURFACE)
|
5
|
+
SDL::WM::setCaption('collision.rb','collision.rb icon')
|
6
|
+
$image = SDL::Surface.loadBMP("icon.bmp")
|
7
|
+
$image.setColorKey( SDL::SRCCOLORKEY ,0)
|
8
|
+
$image = $image.displayFormat
|
9
|
+
$cMap = $image.makeCollisionMap
|
10
|
+
|
11
|
+
class Sprite
|
12
|
+
|
13
|
+
attr_reader :x, :y
|
14
|
+
|
15
|
+
def initialize(screen)
|
16
|
+
@screen = screen
|
17
|
+
@x=rand(screen.w)
|
18
|
+
@y=rand(screen.h)
|
19
|
+
@dx=rand(11)-5
|
20
|
+
@dy=rand(11)-5
|
21
|
+
end
|
22
|
+
|
23
|
+
def move
|
24
|
+
@x, @dx = moveCoord(@x, @dx, xMax)
|
25
|
+
@y, @dy = moveCoord(@y, @dy, yMax)
|
26
|
+
end
|
27
|
+
|
28
|
+
def bounce
|
29
|
+
@dx *= -1
|
30
|
+
@dy *= -1
|
31
|
+
move
|
32
|
+
end
|
33
|
+
|
34
|
+
def draw
|
35
|
+
SDL.blitSurface($image,0,0,$image.w,$image.h,@screen,@x,@y)
|
36
|
+
end
|
37
|
+
|
38
|
+
def collidingWith(sprite)
|
39
|
+
$cMap.collisionCheck(@x, @y, $cMap, sprite.x, sprite.y) != nil
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def moveCoord(coord, delta, max)
|
45
|
+
coord += delta
|
46
|
+
if coord >= max then
|
47
|
+
delta *= -1
|
48
|
+
coord = max - 1
|
49
|
+
end
|
50
|
+
if coord < 0 then
|
51
|
+
delta *= -1
|
52
|
+
coord = 0
|
53
|
+
end
|
54
|
+
[coord, delta]
|
55
|
+
end
|
56
|
+
|
57
|
+
def xMax
|
58
|
+
@screen.w - $image.w
|
59
|
+
end
|
60
|
+
|
61
|
+
def yMax
|
62
|
+
@screen.h - $image.h
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
def detectCollisions(sprites)
|
68
|
+
collisions = []
|
69
|
+
for i in (0 ... sprites.size - 1) do
|
70
|
+
for j in (i + 1 ... sprites.size) do
|
71
|
+
if sprites[i].collidingWith(sprites[j])
|
72
|
+
collisions << sprites[i]
|
73
|
+
collisions << sprites[j]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
collisions.uniq
|
78
|
+
end
|
79
|
+
|
80
|
+
sprites = (1..8).collect {Sprite.new(screen)}
|
81
|
+
|
82
|
+
|
83
|
+
while true
|
84
|
+
while event = SDL::Event2.poll
|
85
|
+
case event
|
86
|
+
when SDL::Event2::KeyDown, SDL::Event2::Quit
|
87
|
+
exit
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
screen.fillRect(0,0,640,480,screen.mapRGB(64, 64, 64))
|
92
|
+
sprites.each {|i| i.move}
|
93
|
+
detectCollisions(sprites).each {|i| i.bounce}
|
94
|
+
sprites.each {|i| i.draw}
|
95
|
+
screen.updateRect(0,0,0,0)
|
96
|
+
end
|
97
|
+
|
data/sample/cursor.bmp
ADDED
Binary file
|
data/sample/cursor.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'sdl'
|
2
|
+
|
3
|
+
SDL.init( SDL::INIT_VIDEO )
|
4
|
+
screen = SDL::setVideoMode(640,480,16,SDL::SWSURFACE)
|
5
|
+
screen.fillRect 0,0,640,480,[32,240,100]
|
6
|
+
screen.flip
|
7
|
+
SDL::WM::setCaption $0,$0
|
8
|
+
|
9
|
+
image=SDL::Surface.loadBMP 'cursor.bmp'
|
10
|
+
SDL::Mouse.setCursor image,image[0,0],image[1,1],image[7,0],543
|
11
|
+
|
12
|
+
|
13
|
+
while true
|
14
|
+
|
15
|
+
while event = SDL::Event2.poll
|
16
|
+
case event
|
17
|
+
when SDL::Event2::KeyDown, SDL::Event2::Quit
|
18
|
+
exit
|
19
|
+
end
|
20
|
+
end
|
21
|
+
sleep 0.01
|
22
|
+
end
|
data/sample/ellipses.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# SDL random ellipses
|
2
|
+
# Author: Wayne Conrad <wconrad@yagni.com>
|
3
|
+
|
4
|
+
require 'sdl'
|
5
|
+
|
6
|
+
def show(function)
|
7
|
+
event = SDL::Event.new
|
8
|
+
screen = SDL::setVideoMode(640, 480, 16, SDL::SWSURFACE|SDL::ANYFORMAT)
|
9
|
+
loop do
|
10
|
+
color = screen.mapRGB(rand(256),rand(256),rand(256))
|
11
|
+
x = rand(screen.w)
|
12
|
+
y = rand(screen.h)
|
13
|
+
xr = rand(80)
|
14
|
+
yr = rand(80)
|
15
|
+
eval("screen.#{function}(x, y, xr, yr, color)")
|
16
|
+
if event.poll != 0 then
|
17
|
+
case event.type
|
18
|
+
when SDL::Event::QUIT
|
19
|
+
exit
|
20
|
+
when SDL::Event::MOUSEBUTTONDOWN
|
21
|
+
break
|
22
|
+
when SDL::Event::KEYDOWN
|
23
|
+
exit if event.keySym == SDL::Key::ESCAPE
|
24
|
+
break
|
25
|
+
end
|
26
|
+
end
|
27
|
+
SDL::Key.scan
|
28
|
+
screen.updateRect(0, 0, 0, 0)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
srand
|
33
|
+
SDL.init SDL::INIT_VIDEO
|
34
|
+
show("drawEllipse")
|
35
|
+
show("drawFilledEllipse")
|
data/sample/event2.rb
ADDED
@@ -0,0 +1,32 @@
|
|
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
|
+
loop do
|
8
|
+
if event=SDL::Event2.poll then
|
9
|
+
case event
|
10
|
+
when SDL::Event2::Active
|
11
|
+
print "Active ", (event.gain)?"gain":"lost","\n"
|
12
|
+
when SDL::Event2::KeyDown
|
13
|
+
print "KeyDown sym:#{event.sym} mod:#{event.mod}","\n"
|
14
|
+
if event.sym==SDL::Key::A then
|
15
|
+
e=SDL::Event2::KeyDown.new
|
16
|
+
e.sym=3333
|
17
|
+
e.mod=3333
|
18
|
+
e.press=true
|
19
|
+
e.unicode=200
|
20
|
+
SDL::Event2.push(e)
|
21
|
+
end
|
22
|
+
when SDL::Event2::KeyUp
|
23
|
+
print "KeyUp sym:#{event.sym} mod:#{event.mod}","\n"
|
24
|
+
|
25
|
+
when SDL::Event2::Quit
|
26
|
+
exit
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
|
data/sample/font.bmp
ADDED
Binary file
|
data/sample/font.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'sdl'
|
2
|
+
|
3
|
+
SDL.init( SDL::INIT_VIDEO )
|
4
|
+
|
5
|
+
screen = SDL::setVideoMode(640,480,16,SDL::SWSURFACE)
|
6
|
+
SDL::WM::setCaption($0,$0)
|
7
|
+
|
8
|
+
SDL::TTF.init
|
9
|
+
|
10
|
+
font = SDL::TTF.open('sample.ttf',24)
|
11
|
+
font.style = SDL::TTF::STYLE_NORMAL
|
12
|
+
|
13
|
+
font.drawSolidUTF8(screen,'test SDL_ttf',300,150,255,255,255)
|
14
|
+
|
15
|
+
screen.flip
|
16
|
+
|
17
|
+
while true
|
18
|
+
while event = SDL::Event2.poll
|
19
|
+
case event
|
20
|
+
when SDL::Event2::KeyDown, SDL::Event2::Quit
|
21
|
+
exit
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/sample/fpstimer.rb
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
require 'sdl'
|
2
|
+
|
3
|
+
class FPSTimerSample
|
4
|
+
FPS_COUNT = 10
|
5
|
+
|
6
|
+
attr_accessor :fps
|
7
|
+
attr_reader :real_fps, :total_skip
|
8
|
+
attr_reader :count_sleep
|
9
|
+
# +fps+ is the number of frames per second that you want to keep,
|
10
|
+
# +accurary+ is the accurary of sleep/SDL.delay in milisecond
|
11
|
+
def initialize(fps = 60, accurary = 10, skip_limit = 15)
|
12
|
+
@fps = fps
|
13
|
+
@accurary = accurary / 1000.0
|
14
|
+
@skip_limit = skip_limit
|
15
|
+
end
|
16
|
+
|
17
|
+
# reset timer, you should call just before starting loop
|
18
|
+
def reset
|
19
|
+
@old = get_ticks
|
20
|
+
@skip = 0
|
21
|
+
@real_fps = @fps
|
22
|
+
@frame_count = 0
|
23
|
+
@fps_old = @old
|
24
|
+
@count_sleep = 0
|
25
|
+
@total_skip = 0
|
26
|
+
end
|
27
|
+
|
28
|
+
# execute given block and wait
|
29
|
+
def wait_frame
|
30
|
+
now = get_ticks
|
31
|
+
nxt = @old + (1.0/@fps)
|
32
|
+
if nxt > now || @skip > @skip_limit
|
33
|
+
yield
|
34
|
+
@skip = 0
|
35
|
+
wait(nxt)
|
36
|
+
@old = nxt
|
37
|
+
else
|
38
|
+
@skip += 1
|
39
|
+
@total_skip += 1
|
40
|
+
@old = get_ticks
|
41
|
+
end
|
42
|
+
|
43
|
+
calc_real_fps
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def wait(nxt)
|
48
|
+
while nxt > get_ticks + @accurary
|
49
|
+
sleep(@accurary - 0.005)
|
50
|
+
@count_sleep += 1
|
51
|
+
end
|
52
|
+
|
53
|
+
while nxt > get_ticks
|
54
|
+
# busy loop, do nothing
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def get_ticks
|
59
|
+
SDL.getTicks / 1000.0
|
60
|
+
end
|
61
|
+
|
62
|
+
def calc_real_fps
|
63
|
+
@frame_count += 1
|
64
|
+
if @frame_count >= FPS_COUNT
|
65
|
+
@frame_count = 0
|
66
|
+
now = get_ticks
|
67
|
+
@real_fps = FPS_COUNT / (now - @fps_old)
|
68
|
+
@fps_old = now
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class FPSTimerLight
|
74
|
+
N = 12
|
75
|
+
DT = 2
|
76
|
+
FPS_COUNT = 10
|
77
|
+
|
78
|
+
attr_reader :fps
|
79
|
+
attr_reader :real_fps
|
80
|
+
# +fps+ is the number of frames per second that you want to keep,
|
81
|
+
# +accurary+ is the accurary of sleep/SDL.delay in milisecond
|
82
|
+
def initialize(fps = 60, accurary = 10, skip_limit = 15)
|
83
|
+
@fps = fps
|
84
|
+
@accurary = accurary * N
|
85
|
+
@skip_limit = 15
|
86
|
+
@one_frame = 1000*N / fps
|
87
|
+
@delay = accurary - 2
|
88
|
+
end
|
89
|
+
|
90
|
+
# reset timer, you should call just before starting loop
|
91
|
+
def reset
|
92
|
+
@old = get_ticks
|
93
|
+
@skip = 0
|
94
|
+
|
95
|
+
# for calculate real fps
|
96
|
+
@frame_count = 0
|
97
|
+
@fps_old = @old
|
98
|
+
@real_fps = @fps
|
99
|
+
end
|
100
|
+
|
101
|
+
def wait_frame
|
102
|
+
now = get_ticks
|
103
|
+
nxt = @old + @one_frame
|
104
|
+
if nxt > now || @skip > @skip_limit
|
105
|
+
yield
|
106
|
+
@skip = 0
|
107
|
+
wait(nxt)
|
108
|
+
@old = nxt
|
109
|
+
else
|
110
|
+
@skip += 1
|
111
|
+
@total_skip += 1
|
112
|
+
@old = get_ticks
|
113
|
+
end
|
114
|
+
|
115
|
+
calc_real_fps
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
def get_ticks
|
120
|
+
SDL.getTicks * N
|
121
|
+
end
|
122
|
+
|
123
|
+
def wait(nxt)
|
124
|
+
while nxt > get_ticks + @accurary
|
125
|
+
SDL.delay(@delay)
|
126
|
+
end
|
127
|
+
|
128
|
+
while nxt > get_ticks
|
129
|
+
# busy loop, do nothing
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def calc_real_fps
|
134
|
+
@frame_count += 1
|
135
|
+
if @frame_count >= FPS_COUNT
|
136
|
+
@frame_count = 0
|
137
|
+
now = get_ticks
|
138
|
+
@real_fps = (N*1000*FPS_COUNT)/(now - @fps_old)
|
139
|
+
@fps_old = now
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
if __FILE__ == $0
|
145
|
+
timer = FPSTimerSample.new
|
146
|
+
log = []
|
147
|
+
|
148
|
+
timer.reset
|
149
|
+
300.times do
|
150
|
+
sleep 0.005 if rand(5) == 0
|
151
|
+
timer.wait_frame do
|
152
|
+
log << timer.real_fps.to_s
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
puts log
|
157
|
+
printf "skip:%d\n", timer.total_skip
|
158
|
+
printf "sleep:%d\n", timer.count_sleep
|
159
|
+
|
160
|
+
puts
|
161
|
+
|
162
|
+
timer = FPSTimerLight.new
|
163
|
+
log = []
|
164
|
+
|
165
|
+
timer.reset
|
166
|
+
old = SDL.getTicks
|
167
|
+
300.times do
|
168
|
+
sleep 0.005 if rand(5) == 0
|
169
|
+
timer.wait_frame do
|
170
|
+
log << timer.real_fps
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
puts log
|
175
|
+
end
|