rubygame 2.5.3 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CREDITS +6 -4
- data/NEWS +79 -0
- data/README +55 -72
- data/ROADMAP +20 -13
- data/doc/custom_sdl_load_paths.rdoc +79 -0
- data/doc/getting_started.rdoc +65 -36
- data/doc/keyboard_symbols.rdoc +243 -0
- data/doc/macosx_install.rdoc +49 -35
- data/doc/windows_install.rdoc +36 -108
- data/lib/rubygame.rb +62 -24
- data/lib/rubygame/audio.rb +147 -0
- data/lib/rubygame/clock.rb +164 -1
- data/lib/rubygame/color.rb +40 -7
- data/lib/rubygame/color/models/hsl.rb +1 -1
- data/lib/rubygame/color/models/hsv.rb +1 -1
- data/lib/rubygame/color/models/rgb.rb +1 -1
- data/lib/rubygame/color/palettes/css.rb +1 -3
- data/lib/rubygame/color/palettes/x11.rb +1 -2
- data/lib/rubygame/constants.rb +297 -0
- data/lib/rubygame/deprecated_mixer.rb +555 -0
- data/lib/rubygame/event.rb +122 -6
- data/lib/rubygame/event_handler.rb +3 -1
- data/lib/rubygame/event_hook.rb +6 -2
- data/lib/rubygame/event_triggers.rb +1 -1
- data/lib/rubygame/events.rb +416 -1
- data/lib/rubygame/ftor.rb +1 -7
- data/lib/rubygame/gfx.rb +583 -0
- data/lib/rubygame/gl.rb +107 -0
- data/lib/rubygame/image.rb +140 -0
- data/lib/rubygame/joystick.rb +184 -0
- data/lib/rubygame/main.rb +82 -0
- data/lib/rubygame/mediabag.rb +1 -1
- data/lib/rubygame/mixer.rb +30 -0
- data/lib/rubygame/music.rb +493 -0
- data/lib/rubygame/queue.rb +3 -1
- data/lib/rubygame/rect.rb +9 -9
- data/lib/rubygame/screen.rb +357 -0
- data/lib/rubygame/shared.rb +40 -4
- data/lib/rubygame/sound.rb +428 -0
- data/lib/rubygame/surface.rb +626 -0
- data/lib/rubygame/ttf.rb +311 -0
- data/samples/FreeSans.ttf +0 -0
- data/samples/README +6 -5
- data/samples/demo_draw.rb +1 -1
- data/samples/demo_gl.rb +3 -1
- data/samples/demo_gl_tex.rb +4 -2
- data/samples/demo_rubygame.rb +114 -105
- data/samples/demo_sfont.rb +1 -1
- data/samples/demo_ttf.rb +3 -1
- data/samples/demo_utf8.rb +1 -1
- data/samples/image_viewer.rb +118 -0
- data/samples/load_and_blit.rb +1 -1
- data/samples/rubygame.png +0 -0
- metadata +34 -40
- data/Rakefile +0 -537
- data/doc/extended_readme.rdoc +0 -49
- data/ext/body/rubygame_body.so +0 -0
- data/ext/rubygame/rubygame_clock.c +0 -301
- data/ext/rubygame/rubygame_clock.h +0 -32
- data/ext/rubygame/rubygame_event.c +0 -760
- data/ext/rubygame/rubygame_event.h +0 -48
- data/ext/rubygame/rubygame_event2.c +0 -661
- data/ext/rubygame/rubygame_event2.h +0 -29
- data/ext/rubygame/rubygame_gfx.c +0 -942
- data/ext/rubygame/rubygame_gfx.h +0 -101
- data/ext/rubygame/rubygame_gl.c +0 -154
- data/ext/rubygame/rubygame_gl.h +0 -32
- data/ext/rubygame/rubygame_image.c +0 -252
- data/ext/rubygame/rubygame_image.h +0 -41
- data/ext/rubygame/rubygame_joystick.c +0 -336
- data/ext/rubygame/rubygame_joystick.h +0 -41
- data/ext/rubygame/rubygame_main.c +0 -158
- data/ext/rubygame/rubygame_main.h +0 -36
- data/ext/rubygame/rubygame_mixer.c +0 -1024
- data/ext/rubygame/rubygame_mixer.h +0 -36
- data/ext/rubygame/rubygame_music.c +0 -1017
- data/ext/rubygame/rubygame_music.h +0 -29
- data/ext/rubygame/rubygame_screen.c +0 -572
- data/ext/rubygame/rubygame_screen.h +0 -45
- data/ext/rubygame/rubygame_shared.c +0 -269
- data/ext/rubygame/rubygame_shared.h +0 -69
- data/ext/rubygame/rubygame_sound.c +0 -863
- data/ext/rubygame/rubygame_sound.h +0 -29
- data/ext/rubygame/rubygame_surface.c +0 -1153
- data/ext/rubygame/rubygame_surface.h +0 -62
- data/ext/rubygame/rubygame_ttf.c +0 -599
- data/ext/rubygame/rubygame_ttf.h +0 -69
- data/samples/keys.rb +0 -52
data/lib/rubygame/ttf.rb
ADDED
@@ -0,0 +1,311 @@
|
|
1
|
+
#--
|
2
|
+
# Rubygame -- Ruby code and bindings to SDL to facilitate game creation
|
3
|
+
# Copyright (C) 2004-2009 John Croisant
|
4
|
+
#
|
5
|
+
# This library is free software; you can redistribute it and/or
|
6
|
+
# modify it under the terms of the GNU Lesser General Public
|
7
|
+
# License as published by the Free Software Foundation; either
|
8
|
+
# version 2.1 of the License, or (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This library is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
+
# Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public
|
16
|
+
# License along with this library; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18
|
+
#++
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
require "ruby-sdl-ffi/ttf"
|
23
|
+
|
24
|
+
Rubygame::VERSIONS[:sdl_ttf] = SDL::TTF.Linked_Version().to_ary
|
25
|
+
|
26
|
+
|
27
|
+
# *IMPORTANT*: this class only exists if SDL_ttf is available!
|
28
|
+
# Your code should check "defined?(Rubygame::TTF) != nil" to see if
|
29
|
+
# you can use this class, or be prepared to rescue from NameError.
|
30
|
+
#
|
31
|
+
# TTF provides an interface to SDL_ttf, allowing TrueType Font files to be
|
32
|
+
# loaded and used to render text to Surfaces.
|
33
|
+
#
|
34
|
+
# The TTF class *must* be initialized with the #setup method before any
|
35
|
+
# TTF objects can be created or used.
|
36
|
+
#
|
37
|
+
class Rubygame::TTF
|
38
|
+
|
39
|
+
|
40
|
+
# Attempt to setup the TTF class for use by initializing SDL_ttf.
|
41
|
+
# This *must* be called before the TTF class can be used.
|
42
|
+
# Raises SDLError if there is a problem initializing SDL_ttf.
|
43
|
+
#
|
44
|
+
def self.setup
|
45
|
+
if( SDL::TTF.WasInit() == 0 and SDL::TTF.Init() != 0 )
|
46
|
+
raise( Rubygame::SDLError,
|
47
|
+
"Could not setup TTF class: #{SDL.GetError()}" )
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
# Clean up and quit SDL_ttf, making the TTF class unusable as a result
|
53
|
+
# (until it is setup again). This does not need to be called before Rubygame
|
54
|
+
# exits, as it will be done automatically.
|
55
|
+
#
|
56
|
+
def self.quit
|
57
|
+
if( SDL::TTF.WasInit() != 0 )
|
58
|
+
SDL::TTF.Quit()
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
# Create a new TTF object, which can render text to a Surface with a
|
65
|
+
# particular font style and size.
|
66
|
+
#
|
67
|
+
# file:: filename of the TrueType font to use. Should be a TTF or
|
68
|
+
# FON file.
|
69
|
+
# size:: point size (based on 72DPI). (That means the height in pixels from
|
70
|
+
# the bottom of the descent to the top of the ascent.)
|
71
|
+
#
|
72
|
+
def initialize( file, size )
|
73
|
+
if( SDL::TTF.WasInit() == 0 )
|
74
|
+
raise( Rubygame::SDLError,
|
75
|
+
"You must call TTF.setup before opening a font." )
|
76
|
+
end
|
77
|
+
|
78
|
+
@struct = SDL::TTF.OpenFont( file, size )
|
79
|
+
|
80
|
+
if( @struct.pointer.null? )
|
81
|
+
raise Rubygame::SDLError, "Could not open font: #{SDL.GetError()}"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
attr_reader :struct # :nodoc:
|
87
|
+
protected :struct
|
88
|
+
|
89
|
+
|
90
|
+
def _get_style( style ) # :nodoc:
|
91
|
+
return (SDL::TTF.GetFontStyle(@struct) & style == style)
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
# Sets the style and returns the old value.
|
96
|
+
#
|
97
|
+
def _set_style( enable, style ) # :nodoc:
|
98
|
+
old = SDL::TTF.GetFontStyle(@struct)
|
99
|
+
if( !enable and (old & style == style ) )
|
100
|
+
SDL::TTF.SetFontStyle( @struct, old ^ style )
|
101
|
+
return true
|
102
|
+
elsif( enable )
|
103
|
+
SDL::TTF.SetFontStyle( @struct, old | style )
|
104
|
+
return false
|
105
|
+
else
|
106
|
+
# No change
|
107
|
+
return enable;
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
# True if bold mode is enabled for this font.
|
114
|
+
#
|
115
|
+
def bold?
|
116
|
+
_get_style( SDL::TTF::STYLE_BOLD )
|
117
|
+
end
|
118
|
+
alias :bold :bold?
|
119
|
+
|
120
|
+
|
121
|
+
# Enable or disable bold mode for this font. Returns the old value.
|
122
|
+
#
|
123
|
+
def bold=( enabled )
|
124
|
+
_set_style( enabled, SDL::TTF::STYLE_BOLD )
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
# True if italic mode is enabled for this font.
|
130
|
+
#
|
131
|
+
def italic?
|
132
|
+
_get_style( SDL::TTF::STYLE_ITALIC )
|
133
|
+
end
|
134
|
+
alias :italic :italic?
|
135
|
+
|
136
|
+
|
137
|
+
# Enable or disable italic mode for this font. Returns the old
|
138
|
+
# value.
|
139
|
+
#
|
140
|
+
def italic=( enabled )
|
141
|
+
_set_style( enabled, SDL::TTF::STYLE_ITALIC )
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
# True if underline mode is enabled for this font.
|
147
|
+
#
|
148
|
+
def underline?
|
149
|
+
_get_style( SDL::TTF::STYLE_UNDERLINE )
|
150
|
+
end
|
151
|
+
alias :underline :underline?
|
152
|
+
|
153
|
+
|
154
|
+
# Enable or disable underline mode for this font. Returns the old
|
155
|
+
# value.
|
156
|
+
#
|
157
|
+
def underline=( enabled )
|
158
|
+
_set_style( enabled, SDL::TTF::STYLE_UNDERLINE )
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
# Return the biggest height (bottom to top; in pixels) of all glyphs
|
163
|
+
# in the font.
|
164
|
+
#
|
165
|
+
def height
|
166
|
+
SDL::TTF.FontHeight( @struct )
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
# Return the biggest ascent (baseline to top; in pixels) of all
|
171
|
+
# glyphs in the font.
|
172
|
+
#
|
173
|
+
def ascent
|
174
|
+
SDL::TTF.FontAnscent( @struct )
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
# Return the biggest descent (baseline to bottom; in pixels) of all
|
179
|
+
# glyphs in the font.
|
180
|
+
#
|
181
|
+
def descent
|
182
|
+
SDL::TTF.FontDescent( @struct )
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
# Return the recommended distance (in pixels) from a point on a line
|
187
|
+
# of text to the same point on the line of text below it.
|
188
|
+
#
|
189
|
+
def line_skip
|
190
|
+
SDL::TTF.FontLineSkip( @struct )
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
# The width and height the text would be if it were rendered,
|
196
|
+
# without the overhead of actually rendering it.
|
197
|
+
#
|
198
|
+
def size_text( text )
|
199
|
+
SDL::TTF.SizeText(@struct, text)
|
200
|
+
end
|
201
|
+
|
202
|
+
|
203
|
+
# The width and height the UTF-8 encoded text would be if it were
|
204
|
+
# rendered, without the overhead of actually rendering it.
|
205
|
+
#
|
206
|
+
def size_utf8( text )
|
207
|
+
SDL::TTF.SizeUTF8(@struct, text)
|
208
|
+
end
|
209
|
+
|
210
|
+
|
211
|
+
# The width and height the Unicode text would be if it were
|
212
|
+
# rendered, without the overhead of actually rendering it.
|
213
|
+
#
|
214
|
+
def size_unicode( text )
|
215
|
+
SDL::TTF.SizeUNICODE(@struct, text)
|
216
|
+
end
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
# Does the heavy lifting for the render methods.
|
221
|
+
#
|
222
|
+
def _render( text, smooth, color, back, shaded, blended, solid ) # :nodoc;
|
223
|
+
|
224
|
+
color = SDL::Color.new( Rubygame::Color.make_sdl_rgba(color) )
|
225
|
+
|
226
|
+
if back
|
227
|
+
back = SDL::Color.new( Rubygame::Color.make_sdl_rgba(back) )
|
228
|
+
end
|
229
|
+
|
230
|
+
surf =
|
231
|
+
if smooth
|
232
|
+
if back
|
233
|
+
shaded.call( @struct, text, color, back )
|
234
|
+
else
|
235
|
+
blended.call( @struct, text, color )
|
236
|
+
end
|
237
|
+
else
|
238
|
+
if back
|
239
|
+
s = solid.call( @struct, text, color )
|
240
|
+
SDL::SetColors( s, back.pointer, 0, 1 )
|
241
|
+
SDL::SetColorKey( s, 0, 0 );
|
242
|
+
s
|
243
|
+
else
|
244
|
+
solid.call( @struct, text, color )
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
if surf.pointer.null?
|
249
|
+
raise Rubygame::SDLError, "Could not render text: #{SDL.GetError()}"
|
250
|
+
end
|
251
|
+
|
252
|
+
return Rubygame::Surface.new( surf )
|
253
|
+
|
254
|
+
end
|
255
|
+
|
256
|
+
|
257
|
+
# Renders a string to a Surface with the font's style and the given
|
258
|
+
# color(s).
|
259
|
+
#
|
260
|
+
# text:: the text string to render
|
261
|
+
# smooth:: Use anti-aliasing if true. Enabling this makes the text
|
262
|
+
# look much nicer (smooth curves), but is much slower.
|
263
|
+
# color:: the color to render the text, in the form [r,g,b]
|
264
|
+
# back:: the color to use as a background for the text. This
|
265
|
+
# option can be omitted to have a transparent background.
|
266
|
+
#
|
267
|
+
def render( text, smooth, color, back=nil )
|
268
|
+
_render( text, smooth, color, back,
|
269
|
+
SDL::TTF.method(:RenderText_Shaded),
|
270
|
+
SDL::TTF.method(:RenderText_Blended),
|
271
|
+
SDL::TTF.method(:RenderText_Solid) )
|
272
|
+
end
|
273
|
+
|
274
|
+
|
275
|
+
# Renders a UTF-8 string to a Surface with the font's style and the
|
276
|
+
# given color(s).
|
277
|
+
#
|
278
|
+
# text:: the text string to render
|
279
|
+
# smooth:: Use anti-aliasing if true. Enabling this makes the text
|
280
|
+
# look much nicer (smooth curves), but is much slower.
|
281
|
+
# color:: the color to render the text, in the form [r,g,b]
|
282
|
+
# back:: the color to use as a background for the text. This
|
283
|
+
# option can be omitted to have a transparent background.
|
284
|
+
#
|
285
|
+
def render_utf8( text, smooth, color, back=nil )
|
286
|
+
_render( text, smooth, color, back,
|
287
|
+
SDL::TTF.method(:RenderUTF8_Shaded),
|
288
|
+
SDL::TTF.method(:RenderUTF8_Blended),
|
289
|
+
SDL::TTF.method(:RenderUTF8_Solid) )
|
290
|
+
end
|
291
|
+
|
292
|
+
|
293
|
+
# Renders a Unicode string to a Surface with the font's style and
|
294
|
+
# the given color(s).
|
295
|
+
#
|
296
|
+
# text:: the text string to render
|
297
|
+
# smooth:: Use anti-aliasing if true. Enabling this makes the text
|
298
|
+
# look much nicer (smooth curves), but is much slower.
|
299
|
+
# color:: the color to render the text, in the form [r,g,b]
|
300
|
+
# back:: the color to use as a background for the text. This
|
301
|
+
# option can be omitted to have a transparent background.
|
302
|
+
#
|
303
|
+
def render_unicode( text, smooth, color, back=nil )
|
304
|
+
_render( text, smooth, color, back,
|
305
|
+
SDL::TTF.method(:RenderUNICODE_Shaded),
|
306
|
+
SDL::TTF.method(:RenderUNICODE_Blended),
|
307
|
+
SDL::TTF.method(:RenderUNICODE_Solid) )
|
308
|
+
end
|
309
|
+
|
310
|
+
|
311
|
+
end
|
data/samples/FreeSans.ttf
CHANGED
Binary file
|
data/samples/README
CHANGED
@@ -6,14 +6,14 @@ Copyright and licensing information for those items is included herein
|
|
6
6
|
for your convenience.
|
7
7
|
|
8
8
|
|
9
|
-
|
10
|
-
"ruby.png" copyright (C) 2006 John Croisant
|
11
|
-
"term16.png" copyright (C) 2005 John Croisant
|
12
9
|
"panda.png" copyright (C) 2004 John Croisant
|
10
|
+
"ruby.png" copyright (C) 2006 John Croisant
|
11
|
+
"rubygame.png" copyright (C) 2009 John Croisant
|
13
12
|
"song.ogg" copyright (C) 1999 Bjorn De Meyer
|
13
|
+
"term16.png" copyright (C) 2005 John Croisant
|
14
14
|
|
15
15
|
|
16
|
-
"
|
16
|
+
"panda.png", "ruby.png", "rubygame.png", "song.ogg", and "term16.png"
|
17
17
|
are licensed under the Creative Commons Attribution-ShareAlike 2.5 License.
|
18
18
|
|
19
19
|
To view a copy of this license, visit
|
@@ -28,7 +28,8 @@ or send a letter to:
|
|
28
28
|
|
29
29
|
|
30
30
|
|
31
|
-
"FreeSans.ttf"
|
31
|
+
"FreeSans.ttf" Copyleft 2002, 2003, 2005, 2008, 2009
|
32
|
+
Free Software Foundation.
|
32
33
|
|
33
34
|
"FreeSans.ttf" is licensed under the GNU General Public License.
|
34
35
|
|
data/samples/demo_draw.rb
CHANGED
data/samples/demo_gl.rb
CHANGED
@@ -27,7 +27,7 @@ Rubygame::GL.set_attrib(Rubygame::GL::BLUE_SIZE, 5)
|
|
27
27
|
Rubygame::GL.set_attrib(Rubygame::GL::DEPTH_SIZE, 16)
|
28
28
|
Rubygame::GL.set_attrib(Rubygame::GL::DOUBLEBUFFER, 1)
|
29
29
|
|
30
|
-
Rubygame::Screen.
|
30
|
+
Rubygame::Screen.open([WIDE,HIGH], 16, [Rubygame::OPENGL])
|
31
31
|
queue = Rubygame::EventQueue.new()
|
32
32
|
clock = Rubygame::Clock.new { |c| c.target_framerate = 60 }
|
33
33
|
|
@@ -149,3 +149,5 @@ catch(:rubygame_quit) do
|
|
149
149
|
|
150
150
|
end
|
151
151
|
end
|
152
|
+
|
153
|
+
Rubygame.quit
|
data/samples/demo_gl_tex.rb
CHANGED
@@ -21,7 +21,7 @@ WIDE = 640
|
|
21
21
|
HIGH = 480
|
22
22
|
SCALE = 500.0
|
23
23
|
shadedCube=true
|
24
|
-
TEXTURE = "
|
24
|
+
TEXTURE = "rubygame.png"
|
25
25
|
|
26
26
|
Rubygame.init
|
27
27
|
|
@@ -31,7 +31,7 @@ Rubygame::GL.set_attrib(Rubygame::GL::BLUE_SIZE, 5)
|
|
31
31
|
Rubygame::GL.set_attrib(Rubygame::GL::DEPTH_SIZE, 16)
|
32
32
|
Rubygame::GL.set_attrib(Rubygame::GL::DOUBLEBUFFER, 1)
|
33
33
|
|
34
|
-
Rubygame::Screen.
|
34
|
+
Rubygame::Screen.open([WIDE,HIGH], 16, [Rubygame::OPENGL])
|
35
35
|
queue = Rubygame::EventQueue.new()
|
36
36
|
clock = Rubygame::Clock.new { |c| c.target_framerate = 60 }
|
37
37
|
|
@@ -197,3 +197,5 @@ catch(:rubygame_quit) do
|
|
197
197
|
|
198
198
|
end
|
199
199
|
end
|
200
|
+
|
201
|
+
Rubygame.quit
|
data/samples/demo_rubygame.rb
CHANGED
@@ -46,11 +46,13 @@ $smooth = false
|
|
46
46
|
Rubygame.init()
|
47
47
|
|
48
48
|
|
49
|
-
# SDL_gfx is required for
|
50
|
-
$gfx_ok =
|
49
|
+
# SDL_gfx is required for rotating/zooming Surfaces.
|
50
|
+
$gfx_ok = [:rotozoom, :zoom, :zoom_to].all? { |method|
|
51
|
+
Rubygame::Surface.new([1,1]).respond_to?( method )
|
52
|
+
}
|
51
53
|
|
52
54
|
unless ( $gfx_ok )
|
53
|
-
raise "
|
55
|
+
raise "This demo requires Surface rotozoom and zoom support (SDL_gfx)"
|
54
56
|
end
|
55
57
|
|
56
58
|
|
@@ -110,18 +112,23 @@ class Panda
|
|
110
112
|
include Sprites::Sprite
|
111
113
|
include EventHandler::HasEventHandler
|
112
114
|
|
113
|
-
|
114
|
-
|
115
|
-
|
115
|
+
def self.pandapic
|
116
|
+
return @pandapic if @pandapic
|
117
|
+
# Autoload the "panda.png" image and set its colorkey
|
118
|
+
@pandapic = Surface["panda.png"]
|
119
|
+
@pandapic.set_colorkey(@pandapic.get_at(0,0))
|
120
|
+
@pandapic.to_display_alpha
|
121
|
+
end
|
122
|
+
|
116
123
|
|
117
124
|
attr_accessor :vx, :vy, :speed
|
118
125
|
def initialize(x,y)
|
119
126
|
super()
|
120
127
|
@vx, @vy = 0,0
|
121
128
|
@speed = 40
|
122
|
-
|
123
|
-
@
|
124
|
-
|
129
|
+
@pandapic = self.class.pandapic
|
130
|
+
@image = @pandapic
|
131
|
+
@rect = Rect.new(x,y,*@pandapic.size)
|
125
132
|
end
|
126
133
|
|
127
134
|
def update_image(time)
|
@@ -154,7 +161,7 @@ class SpinnyPanda < Panda
|
|
154
161
|
|
155
162
|
def update_image(time)
|
156
163
|
@angle += (@rate * time) % 360
|
157
|
-
@image =
|
164
|
+
@image = @pandapic.rotozoom(@angle,1,$smooth)
|
158
165
|
end
|
159
166
|
end
|
160
167
|
|
@@ -172,7 +179,7 @@ class ExpandaPanda < Panda
|
|
172
179
|
def update_image(time)
|
173
180
|
@delta = (@delta + time*@rate/36) % (Math::PI*2)
|
174
181
|
zoom = 1 + Math.sin(@delta)/2
|
175
|
-
@image =
|
182
|
+
@image = @pandapic.zoom(zoom,$smooth)
|
176
183
|
end
|
177
184
|
end
|
178
185
|
|
@@ -189,63 +196,12 @@ class WobblyPanda < Panda
|
|
189
196
|
|
190
197
|
def update_image(time)
|
191
198
|
@delta = (@delta + time*@rate/36) % (Math::PI*2)
|
192
|
-
zoomx = (1.5 + Math.sin(@delta)/6) *
|
193
|
-
zoomy = (1.5 + Math.cos(@delta)/5) *
|
194
|
-
@image =
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
|
199
|
-
# Create the very cute panda objects!
|
200
|
-
panda1 = SpinnyPanda.new(100,50)
|
201
|
-
panda2 = ExpandaPanda.new(150,50)
|
202
|
-
panda3 = WobblyPanda.new(200,50,0.5)
|
203
|
-
|
204
|
-
# Set their depths. This affects which one appears in front
|
205
|
-
# of the other in case they overlap.
|
206
|
-
panda1.depth = 0 # in between the others
|
207
|
-
panda2.depth = 10 # behind both of the others
|
208
|
-
panda3.depth = -10 # in front of both of the others
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
###############
|
214
|
-
# PANDA GROUP #
|
215
|
-
###############
|
216
|
-
|
217
|
-
|
218
|
-
# Create a spritegroup to manage the pandas.
|
219
|
-
pandas = Sprites::Group.new
|
220
|
-
pandas.extend(Sprites::UpdateGroup)
|
221
|
-
pandas.extend(Sprites::DepthSortGroup)
|
222
|
-
|
223
|
-
# Add the pandas to the group.
|
224
|
-
pandas.push(panda1,panda2,panda3)
|
225
|
-
|
226
|
-
|
227
|
-
# Extend the pandas group with event hooks.
|
228
|
-
class << pandas
|
229
|
-
include EventHandler::HasEventHandler
|
230
|
-
|
231
|
-
# Draw all the sprites and refresh
|
232
|
-
# those parts of the screen
|
233
|
-
def do_draw( event )
|
234
|
-
dirty_rects = draw( event.screen )
|
235
|
-
event.screen.update_rects(dirty_rects)
|
236
|
-
end
|
237
|
-
|
238
|
-
# Erase the sprites from the screen by
|
239
|
-
# drawing over them with the background.
|
240
|
-
def do_undraw( event )
|
241
|
-
undraw( event.screen, event.background )
|
199
|
+
zoomx = (1.5 + Math.sin(@delta)/6) * @pandapic.width
|
200
|
+
zoomy = (1.5 + Math.cos(@delta)/5) * @pandapic.height
|
201
|
+
@image = @pandapic.zoom_to(zoomx,zoomy,$smooth)
|
242
202
|
end
|
243
203
|
end
|
244
204
|
|
245
|
-
pandas.make_magic_hooks( :tick => :update,
|
246
|
-
DrawSprites => :do_draw,
|
247
|
-
UndrawSprites => :do_undraw )
|
248
|
-
|
249
205
|
|
250
206
|
|
251
207
|
##########
|
@@ -254,7 +210,7 @@ pandas.make_magic_hooks( :tick => :update,
|
|
254
210
|
|
255
211
|
|
256
212
|
# Create the SDL window
|
257
|
-
screen = Screen.
|
213
|
+
screen = Screen.open([320,240])
|
258
214
|
screen.title = "Rubygame test"
|
259
215
|
screen.show_cursor = false;
|
260
216
|
|
@@ -289,9 +245,9 @@ ttfont.render( "Press escape or q to quit.",
|
|
289
245
|
|
290
246
|
|
291
247
|
|
292
|
-
|
293
|
-
# EVENT HOOKS
|
294
|
-
|
248
|
+
#########################
|
249
|
+
# EVENT HOOKS FACTORIES #
|
250
|
+
#########################
|
295
251
|
|
296
252
|
|
297
253
|
# Factory methods for creating event triggers
|
@@ -328,51 +284,105 @@ def joyreleased( button )
|
|
328
284
|
end
|
329
285
|
|
330
286
|
|
331
|
-
#######################
|
332
|
-
# PANDA 1 EVENT HOOKS #
|
333
|
-
#######################
|
334
287
|
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
:down => proc { |owner, event| owner.vy = 1 },
|
339
|
-
:left => proc { |owner, event| owner.vx = -1 },
|
340
|
-
:right => proc { |owner, event| owner.vx = 1 },
|
288
|
+
###################
|
289
|
+
# PANDA INSTANCES #
|
290
|
+
###################
|
341
291
|
|
342
|
-
# Stop moving when the arrow key is released
|
343
|
-
released( :up ) => proc { |owner, event| owner.vy = 0 },
|
344
|
-
released( :down ) => proc { |owner, event| owner.vy = 0 },
|
345
|
-
released( :left ) => proc { |owner, event| owner.vx = 0 },
|
346
|
-
released( :right ) => proc { |owner, event| owner.vx = 0 },
|
347
292
|
|
348
|
-
|
349
|
-
joyaxis( 0 ) => proc { |owner, event| owner.vx = event.value },
|
350
|
-
joyaxis( 1 ) => proc { |owner, event| owner.vy = event.value },
|
293
|
+
def make_pandas( game )
|
351
294
|
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
295
|
+
# Create the very cute panda objects!
|
296
|
+
panda1 = SpinnyPanda.new(100,50)
|
297
|
+
panda2 = ExpandaPanda.new(150,50)
|
298
|
+
panda3 = WobblyPanda.new(200,50,0.5)
|
356
299
|
|
357
|
-
|
300
|
+
# Set their depths. This affects which one appears in front
|
301
|
+
# of the other in case they overlap.
|
302
|
+
panda1.depth = 0 # in between the others
|
303
|
+
panda2.depth = 10 # behind both of the others
|
304
|
+
panda3.depth = -10 # in front of both of the others
|
358
305
|
|
359
306
|
|
360
|
-
|
361
|
-
# PANDA 2 EVENT HOOKS #
|
362
|
-
#######################
|
307
|
+
# PANDA GROUP #
|
363
308
|
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
309
|
+
# Create a spritegroup to manage the pandas.
|
310
|
+
pandas = Sprites::Group.new
|
311
|
+
pandas.extend(Sprites::UpdateGroup)
|
312
|
+
pandas.extend(Sprites::DepthSortGroup)
|
368
313
|
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
314
|
+
# Add the pandas to the group.
|
315
|
+
pandas.push(panda1,panda2,panda3)
|
316
|
+
|
317
|
+
|
318
|
+
# Extend the pandas group with event hooks.
|
319
|
+
class << pandas
|
320
|
+
include EventHandler::HasEventHandler
|
321
|
+
|
322
|
+
# Draw all the sprites and refresh
|
323
|
+
# those parts of the screen
|
324
|
+
def do_draw( event )
|
325
|
+
dirty_rects = draw( event.screen )
|
326
|
+
event.screen.update_rects(dirty_rects)
|
327
|
+
end
|
328
|
+
|
329
|
+
# Erase the sprites from the screen by
|
330
|
+
# drawing over them with the background.
|
331
|
+
def do_undraw( event )
|
332
|
+
undraw( event.screen, event.background )
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
pandas.make_magic_hooks( :tick => :update,
|
337
|
+
DrawSprites => :do_draw,
|
338
|
+
UndrawSprites => :do_undraw )
|
339
|
+
|
340
|
+
|
341
|
+
# PANDA 1 EVENT HOOKS #
|
342
|
+
|
343
|
+
hooks = {
|
344
|
+
# Start moving when an arrow key is pressed
|
345
|
+
:up => proc { |owner, event| owner.vy = -1 },
|
346
|
+
:down => proc { |owner, event| owner.vy = 1 },
|
347
|
+
:left => proc { |owner, event| owner.vx = -1 },
|
348
|
+
:right => proc { |owner, event| owner.vx = 1 },
|
373
349
|
|
374
|
-
|
350
|
+
# Stop moving when the arrow key is released
|
351
|
+
released( :up ) => proc { |owner, event| owner.vy = 0 },
|
352
|
+
released( :down ) => proc { |owner, event| owner.vy = 0 },
|
353
|
+
released( :left ) => proc { |owner, event| owner.vx = 0 },
|
354
|
+
released( :right ) => proc { |owner, event| owner.vx = 0 },
|
375
355
|
|
356
|
+
# Move according to how far the joystick axis is moved
|
357
|
+
joyaxis( 0 ) => proc { |owner, event| owner.vx = event.value },
|
358
|
+
joyaxis( 1 ) => proc { |owner, event| owner.vy = event.value },
|
359
|
+
|
360
|
+
# Fast speed when button is pressed, normal speed when released
|
361
|
+
joypressed( 4 ) => proc { |owner, event| owner.speed *= 2.0 },
|
362
|
+
joyreleased( 4 ) => proc { |owner, event| owner.speed *= 0.5 }
|
363
|
+
}
|
364
|
+
|
365
|
+
panda1.make_magic_hooks( hooks )
|
366
|
+
|
367
|
+
|
368
|
+
# PANDA 2 EVENT HOOKS #
|
369
|
+
|
370
|
+
hooks = {
|
371
|
+
# Move according to how far the joystick axis is moved
|
372
|
+
joyaxis( 2 ) => proc { |owner, event| owner.vx = event.value },
|
373
|
+
joyaxis( 3 ) => proc { |owner, event| owner.vy = event.value },
|
374
|
+
|
375
|
+
# Fast speed when button is pressed, normal speed when released
|
376
|
+
joypressed( 5 ) => proc { |owner, event| owner.speed *= 2.0 },
|
377
|
+
joyreleased( 5 ) => proc { |owner, event| owner.speed *= 0.5 }
|
378
|
+
}
|
379
|
+
|
380
|
+
panda2.make_magic_hooks( hooks )
|
381
|
+
|
382
|
+
|
383
|
+
game.register( pandas, panda1, panda2 )
|
384
|
+
|
385
|
+
end
|
376
386
|
|
377
387
|
|
378
388
|
|
@@ -538,8 +548,7 @@ end
|
|
538
548
|
|
539
549
|
$game = Game.new( screen, background )
|
540
550
|
|
541
|
-
|
542
|
-
$game.register( pandas, panda1, panda2 )
|
551
|
+
make_pandas( $game )
|
543
552
|
|
544
553
|
# Start the main game loop. It will repeat forever
|
545
554
|
# until the user quits the game!
|