rubygame 2.4.1 → 2.5.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.
@@ -1,6 +1,7 @@
1
1
  #--
2
+ #
2
3
  # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
- # Copyright (C) 2004-2008 John Croisant
4
+ # Copyright (C) 2004-2009 John Croisant
4
5
  #
5
6
  # This library is free software; you can redistribute it and/or
6
7
  # modify it under the terms of the GNU Lesser General Public
@@ -15,6 +16,7 @@
15
16
  # You should have received a copy of the GNU Lesser General Public
16
17
  # License along with this library; if not, write to the Free Software
17
18
  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
18
20
  #++
19
21
 
20
22
  require 'rubygame/event_hook'
@@ -248,6 +250,7 @@ module Rubygame::EventHandler::HasEventHandler
248
250
  # By default, triggers are converted according to these rules:
249
251
  #
250
252
  # * Symbols starting with "mouse" become a MouseClickTrigger.
253
+ # * The symbol :tick becomes a TickTrigger. (Rubygame 2.5+)
251
254
  # * Keyboard symbols become a KeyPressTrigger.
252
255
  # * Classes become an InstanceOfTrigger.
253
256
  # * Objects with a #match? method are duplicated and used
@@ -286,13 +289,27 @@ module Rubygame::EventHandler::HasEventHandler
286
289
  # :mouse_left => :shoot,
287
290
  # DiedEvent => died_action )
288
291
  #
289
- def make_magic_hooks( hash )
290
- hash.collect do |trigger, action|
292
+ def make_magic_hooks( hooks_hash )
293
+ hooks_hash.collect do |trigger, action|
291
294
  append_hook( :trigger => _make_magic_trigger( trigger ),
292
295
  :action => _make_magic_action( action ))
293
296
  end
294
297
  end
295
298
 
299
+
300
+ # Exactly like #make_magic_hooks, but the hooks' owner will be the
301
+ # given object, instead of self. See EventHook for more information
302
+ # about hook owners.
303
+ #
304
+ def make_magic_hooks_for( owner, hooks_hash )
305
+ hooks_hash.collect do |trigger, action|
306
+ append_hook( :owner => owner,
307
+ :trigger => _make_magic_trigger( trigger ),
308
+ :action => _make_magic_action( action ) )
309
+ end
310
+ end
311
+
312
+
296
313
  # Exactly like #append_hook, except that the hook is put at the
297
314
  # top of the stack (it will be handled first).
298
315
  #
@@ -419,6 +436,8 @@ module Rubygame::EventHandler::HasEventHandler
419
436
  case(trigger.to_s)
420
437
  when /mouse/
421
438
  Rubygame::EventTriggers::MousePressTrigger.new(trigger)
439
+ when "tick"
440
+ Rubygame::EventTriggers::TickTrigger.new
422
441
  else
423
442
  Rubygame::EventTriggers::KeyPressTrigger.new(trigger)
424
443
  end
@@ -57,16 +57,16 @@ class EventHook
57
57
  # the following keys. See the class documentation for EventHook for
58
58
  # more information about what these mean.
59
59
  #
60
- # :owner:: the hook's owner. (any object, required)
61
- # :trigger:: an event trigger which matches certain events.
62
- # (Object with +#match?(event)+, required)
63
- # :action:: an event action to do when an event matches.
64
- # (Object with +#perform(owner,event)+, required)
65
- # :consumes:: if true, the hook will "eat" matching so
66
- # later hooks won't see them. Default: false.
67
- # (true or false, optional)
68
- # :active:: if false, the hook will ignore all events.
69
- # Default: true. (true or false, optional)
60
+ # :owner :: the hook's owner. (any object, required)
61
+ # :trigger :: an event trigger which matches certain events.
62
+ # (Object with +#match?(event)+, required)
63
+ # :action :: an event action to do when an event matches.
64
+ # (Object with +#perform(owner,event)+, required)
65
+ # :consumes :: if true, the hook will "eat" matching so
66
+ # later hooks won't see them. Default: false.
67
+ # (true or false, optional)
68
+ # :active :: if false, the hook will ignore all events.
69
+ # Default: true. (true or false, optional)
70
70
  #
71
71
  # NOTE: None of the attributes are truly required to create a hook.
72
72
  # But, the hook will do nothing unless both @trigger and @action are
@@ -42,7 +42,7 @@ require 'rubygame'
42
42
  # impact on your game's framerate.
43
43
  #
44
44
  # Here is an overview of the event trigger classes that
45
- # come with Rubygame as of version 2.4:
45
+ # come with Rubygame as of version 2.5:
46
46
  #
47
47
  #
48
48
  # AndTrigger:: Holds multiple other triggers, and
@@ -75,6 +75,8 @@ require 'rubygame'
75
75
  #
76
76
  # MouseReleaseTrigger:: Matches certain MouseReleased events.
77
77
  #
78
+ # TickTrigger:: Matches ClockTicked events.
79
+ #
78
80
  # YesTrigger:: Matches every event, no matter what.
79
81
  #
80
82
  module Rubygame::EventTriggers
@@ -668,11 +670,17 @@ end
668
670
 
669
671
 
670
672
 
671
- # class TickTrigger
672
- # def match?( event )
673
- # event.kind_of?( Rubygame::Events::ClockTicked )
674
- # end
675
- # end
673
+ #
674
+ # TickTrigger is an event trigger which will fire
675
+ # when the Clock ticks (ClockTicked).
676
+ #
677
+ class TickTrigger
678
+
679
+ # Returns true if the event is a ClockTicked event.
680
+ def match?( event )
681
+ event.kind_of?( Rubygame::Events::ClockTicked )
682
+ end
683
+ end
676
684
 
677
685
 
678
686
 
@@ -0,0 +1,58 @@
1
+ #--
2
+ # This file is one part of:
3
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
4
+ #
5
+ # Copyright (C) 2009 John Croisant
6
+ #
7
+ # This library is free software; you can redistribute it and/or
8
+ # modify it under the terms of the GNU Lesser General Public
9
+ # License as published by the Free Software Foundation; either
10
+ # version 2.1 of the License, or (at your option) any later version.
11
+ #
12
+ # This library is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ # Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public
18
+ # License along with this library; if not, write to the Free Software
19
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
+ # ++
21
+
22
+
23
+ module Rubygame
24
+
25
+ module Events
26
+
27
+ # ClockTicked is an event returned by Clock#tick, if the Clock
28
+ # has been configured with Clock#enable_tick_events.
29
+ #
30
+ # ClockTicked stores the time that has passed since the previous
31
+ # tick. You can access that information with #seconds or
32
+ # #milliseconds. This is useful to calculate how far a character
33
+ # should move during the current frame, for example.
34
+ #
35
+ class ClockTicked
36
+
37
+ # Create a new ClockTicked event.
38
+ #
39
+ # milliseconds:: The time since the last tick,
40
+ # in milliseconds. (Numeric, required)
41
+ #
42
+ def initialize( milliseconds )
43
+ @milliseconds = milliseconds
44
+ end
45
+
46
+ # Return the time since the last tick, in milliseconds.
47
+ def milliseconds
48
+ @milliseconds
49
+ end
50
+
51
+ # Return the time since the last tick, in seconds.
52
+ def seconds
53
+ @seconds or (@seconds = @milliseconds * 0.001)
54
+ end
55
+ end
56
+
57
+ end
58
+ end
data/lib/rubygame/ftor.rb CHANGED
@@ -18,8 +18,21 @@
18
18
  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
19
  #++
20
20
 
21
+ require "rubygame"
22
+
23
+ #--
24
+ # Ftor is DEPRECATED and will be removed in Rubygame 3.0!
25
+ # Warn the user when this file is loaded.
26
+ #++
27
+ Rubygame.deprecated("Rubygame::Ftor", "3.0")
28
+
29
+
21
30
  module Rubygame
22
31
 
32
+ # *NOTE*: Ftor is DEPRECATED and will be removed in Rubygame 3.0!
33
+ # A mostly-compatible vector class will be provided at or before
34
+ # that time.
35
+ #
23
36
  # *NOTE*: you must require 'rubygame/ftor' manually to gain access to
24
37
  # Rubygame::Ftor. It is not imported with Rubygame by default!
25
38
  #
@@ -19,8 +19,18 @@
19
19
 
20
20
  require "rubygame"
21
21
 
22
+ #--
23
+ # MediaBag is DEPRECATED and will be removed in Rubygame 3.0!
24
+ # Warn the user when this file is loaded.
25
+ #++
26
+ Rubygame.deprecated("Rubygame::MediaBag", "3.0")
27
+
28
+
22
29
  module Rubygame
23
30
 
31
+ # *NOTE*: MediaBag is DEPRECATED and will be removed in Rubygame 3.0!
32
+ # Use the NamedResource functionality of Music, Sound, and Surface instead.
33
+ #
24
34
  # *NOTE*: you must require 'rubygame/mediabag' manually to gain access to
25
35
  # Rubygame::MediaBag. It is not imported with Rubygame by default!
26
36
  #
@@ -0,0 +1,36 @@
1
+ #
2
+ # Common / utility methods.
3
+ #
4
+ #--
5
+ #
6
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
7
+ # Copyright (C) 2009 John Croisant
8
+ #
9
+ # This library is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU Lesser General Public
11
+ # License as published by the Free Software Foundation; either
12
+ # version 2.1 of the License, or (at your option) any later version.
13
+ #
14
+ # This library is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ # Lesser General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU Lesser General Public
20
+ # License along with this library; if not, write to the Free Software
21
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
+ #
23
+ #++
24
+
25
+
26
+ module Rubygame
27
+
28
+ # Warn of a deprecated Rubygame feature.
29
+ def self.deprecated( feature, version ) # :nodoc:
30
+ if $VERBOSE
31
+ warn( "warning: #{feature} is DEPRECATED and will be removed in " + \
32
+ "Rubygame #{version}! Please see the docs for more information." )
33
+ end
34
+ end
35
+
36
+ end
data/samples/chimp.rb CHANGED
@@ -229,8 +229,9 @@ def main
229
229
 
230
230
  # This also differs from pygame. Rather than pass the desired framerate
231
231
  # when you call clock.tick, you set the framerate for the clock, either
232
- # when you create it, or afterwards with the desired_fps accessors.
233
- clock = Clock.new { |clock| clock.target_framerate = 30 }
232
+ # when you create it, or afterwards with the target_framerate accessor.
233
+ clock = Clock.new
234
+ clock.target_framerate = 30
234
235
 
235
236
  # Autoload the sound effects
236
237
  whiff_sound = Sound['whiff.wav']
data/samples/demo_gl.rb CHANGED
@@ -106,8 +106,8 @@ GL::NewList(cube_list,GL::COMPILE_AND_EXECUTE)
106
106
  GL::Vertex(cube[1]);
107
107
  GL::Vertex(cube[2]);
108
108
  GL::Vertex(cube[7]);
109
- GL::PopMatrix()
110
- GL::End()
109
+ GL::End()
110
+ GL::PopMatrix()
111
111
  GL::EndList()
112
112
 
113
113
  angle = 0
@@ -151,8 +151,10 @@ GL::NewList(cube_list,GL::COMPILE_AND_EXECUTE)
151
151
  GL::Vertex(cube[2]);
152
152
  GL::TexCoord(cube_st[5][3]);
153
153
  GL::Vertex(cube[7]);
154
+
155
+ GL::End()
156
+
154
157
  GL::PopMatrix()
155
- GL::End()
156
158
  GL::EndList()
157
159
 
158
160
  angle = 0
@@ -66,16 +66,6 @@ Joystick.activate_all
66
66
  ########################
67
67
 
68
68
 
69
- # Holds information about the clock, created each frame.
70
- class ClockTicked
71
- attr_reader :time, :framerate
72
-
73
- def initialize( ms, framerate )
74
- @time = ms / 1000.0
75
- @framerate = framerate
76
- end
77
- end
78
-
79
69
  # Signals sprites to draw themselves on the screen
80
70
  class DrawSprites
81
71
  attr_accessor :screen
@@ -138,12 +128,12 @@ class Panda
138
128
  # do nothing in base class, rotate/zoom image in subs
139
129
  end
140
130
 
141
- def update( event )
131
+ def update( tick_event )
142
132
  x,y = @rect.center
143
- self.update_image( event.time * 1000.0 )
133
+ self.update_image( tick_event.seconds * 1000.0 )
144
134
  @rect.size = @image.size
145
135
 
146
- base = @speed * event.time
136
+ base = @speed * tick_event.seconds
147
137
  @rect.centerx = x + @vx * base
148
138
  @rect.centery = y + @vy * base
149
139
  end
@@ -252,7 +242,7 @@ class << pandas
252
242
  end
253
243
  end
254
244
 
255
- pandas.make_magic_hooks( ClockTicked => :update,
245
+ pandas.make_magic_hooks( :tick => :update,
256
246
  DrawSprites => :do_draw,
257
247
  UndrawSprites => :do_undraw )
258
248
 
@@ -297,13 +287,6 @@ ttfont.render( "Press escape or q to quit.",
297
287
  true, [250,250,250] ).blit( background, [20,180] )
298
288
 
299
289
 
300
- # Now blit the background onto the screen and update the screen once.
301
- # During the loop, we'll use 'dirty rect' updating to refresh only the
302
- # parts of the screen that have changed.
303
- background.blit(screen,[0,0])
304
- screen.update()
305
-
306
-
307
290
 
308
291
 
309
292
  ############################
@@ -417,6 +400,12 @@ class Game
417
400
  setup_queue()
418
401
  setup_event_hooks()
419
402
 
403
+ # Now blit the background onto the screen and update the screen
404
+ # once. During the loop, we'll use 'dirty rect' updating to
405
+ # refresh only the parts of the screen that have changed.
406
+ @background.blit(screen,[0,0])
407
+ @screen.update()
408
+
420
409
  end
421
410
 
422
411
 
@@ -457,6 +446,14 @@ class Game
457
446
  def setup_clock
458
447
  @clock = Clock.new()
459
448
  @clock.target_framerate = 50
449
+
450
+ # Adjust the assumed granularity to match the system.
451
+ # This helps minimize CPU usage on systems with clocks
452
+ # that are more accurate than the default granularity.
453
+ @clock.calibrate
454
+
455
+ # Make Clock#tick return a ClockTicked event.
456
+ @clock.enable_tick_events
460
457
  end
461
458
 
462
459
 
@@ -482,7 +479,7 @@ class Game
482
479
  WindowExposed => :update_screen,
483
480
 
484
481
  # Refresh the window title.
485
- ClockTicked => :update_framerate
482
+ :tick => :update_framerate
486
483
  }
487
484
 
488
485
  make_magic_hooks( hooks )
@@ -507,8 +504,7 @@ class Game
507
504
  @queue << UndrawSprites.new( @screen, @background )
508
505
  @queue.fetch_sdl_events
509
506
  @queue << DrawSprites.new( @screen )
510
- @queue << ClockTicked.new( $game.clock.tick,
511
- $game.clock.framerate )
507
+ @queue << $game.clock.tick
512
508
  @queue.each do |event|
513
509
  handle( event )
514
510
  end
@@ -524,9 +520,10 @@ class Game
524
520
 
525
521
  # Update the window title to display the current framerate.
526
522
  def update_framerate( event )
527
- unless @old_framerate == event.framerate
528
- @screen.title = "Rubygame test [%d fps]"%event.framerate
529
- @old_framerate = event.framerate
523
+ new_framerate = @clock.framerate.to_i
524
+ unless @old_framerate == new_framerate
525
+ @screen.title = "Rubygame test [%d fps]"%new_framerate
526
+ @old_framerate = new_framerate
530
527
  end
531
528
  end
532
529
 
@@ -0,0 +1,120 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #--
4
+ # This program is released to the PUBLIC DOMAIN.
5
+ # It is distributed in the hope that it will be useful,
6
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
7
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8
+ #++
9
+ #
10
+ # This is a simple demonstration of "framerate independence".
11
+ # The speed of gameplay is defined independently of the framerate
12
+ # of the game, i.e. as pixels per second instead of pixels per frame.
13
+ #
14
+ # You can read more about framerate independence and how to use Clock
15
+ # in doc/managing_framerate.rdoc.
16
+ #
17
+
18
+
19
+ require "rubygame"
20
+
21
+ # How fast the Mover moves. (pixels per second)
22
+ $mover_speed = 30
23
+
24
+ # How fast to run the app. (frames per second)
25
+ $framerate = 20
26
+
27
+ # How long to run the app. (seconds)
28
+ $run_time = 1.0
29
+
30
+
31
+
32
+ class Mover
33
+
34
+ def initialize()
35
+
36
+ # Define the Mover's speed.
37
+ @speed = $mover_speed
38
+
39
+ # Define the initial position of the Mover.
40
+ @position = 0
41
+
42
+ end
43
+
44
+
45
+ # This method will be called once per frame to update the
46
+ # Mover's position. tick_event will be a ClockTicked instance.
47
+ #
48
+ def update( tick_event )
49
+
50
+ # Calculate how far it moved this frame.
51
+ # @speed is defined in pixels per second, so we use tick.seconds.
52
+ change = @speed * tick_event.seconds
53
+
54
+ # Apply the movement.
55
+ move_by( change, tick_event.seconds )
56
+
57
+ end
58
+
59
+
60
+ # This method updates the Mover's position and outputs a
61
+ # message to the user, for demonstration purposes.
62
+ #
63
+ def move_by( change, seconds )
64
+
65
+ # Temporarily store the old position, for the message.
66
+ old_pos = @position
67
+
68
+ # Update position.
69
+ @position += change
70
+
71
+ # Calculate the actual speed, for the message.
72
+ actual_speed = change / seconds
73
+
74
+ # Calculate the framerate (frames per second) for the message.
75
+ framerate = 1.0 / seconds
76
+
77
+ puts( "Moved from #{old_pos} to #{@position} " +
78
+ "(diff: #{change}) in #{seconds} sec (#{framerate} FPS). " +
79
+ "Speed: #{actual_speed}" )
80
+
81
+ end
82
+
83
+ end # class Mover
84
+
85
+
86
+
87
+ def main_loop
88
+
89
+ # Create and configure the Clock
90
+ clock = Rubygame::Clock.new
91
+ clock.target_framerate = $framerate
92
+ clock.enable_tick_events
93
+
94
+ puts "Calibrating..."
95
+ clock.calibrate
96
+
97
+ # Create the Mover
98
+ mover = Mover.new
99
+
100
+
101
+ puts "Go!"
102
+
103
+ # Find out when the app should stop.
104
+ stop_time = Time.now + $run_time
105
+
106
+ until( Time.now >= stop_time )
107
+
108
+ # Tick the clock. Returns a ClockTicked instance
109
+ # telling us how long this frame was.
110
+ tick_event = clock.tick
111
+
112
+ # Update the Mover.
113
+ mover.update( tick_event )
114
+
115
+ end
116
+
117
+ end
118
+
119
+
120
+ main_loop()