rubygame 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,144 @@
1
+ #--
2
+ # This file is one part of:
3
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
4
+ #
5
+ # Copyright (C) 2008 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
+ # InputFocusGained is an event that occurs when
28
+ # the Rubygame application gains input focus.
29
+ #
30
+ # Input focus means that the app will receive events from
31
+ # input devices, such as the keyboard and joysticks.
32
+ #
33
+ # Usually, an application has input focus when it is the "active"
34
+ # application (the one the user has clicked on or switched to most
35
+ # recently).
36
+ #
37
+ class InputFocusGained; end
38
+
39
+ # InputFocusLost is an event that occurs when
40
+ # the Rubygame application loses input focus.
41
+ #
42
+ # See InputFocusGained for a description of "input focus".
43
+ #
44
+ class InputFocusLost; end
45
+
46
+
47
+
48
+ # MouseFocusGained is an event that occurs when
49
+ # the Rubygame application gains mouse focus.
50
+ #
51
+ # Mouse focus means that the mouse cursor is inside the
52
+ # app window. When the app has mouse focus, it will receive
53
+ # mouse events, particularly MouseMoved.
54
+ #
55
+ class MouseFocusGained; end
56
+
57
+ # MouseFocusLost is an event that occurs when
58
+ # the Rubygame application loses mouse focus.
59
+ #
60
+ # See MouseFocusGained for a description of "mouse focus".
61
+ #
62
+ class MouseFocusLost; end
63
+
64
+
65
+
66
+ # WindowMinimized is an event that occurs when
67
+ # the Rubygame application window becomes minimized (also
68
+ # called 'iconified').
69
+ #
70
+ class WindowMinimized; end
71
+
72
+ # WindowRestored is an event that occurs when the
73
+ # Rubygame application window is restored after it had been
74
+ # minimized.
75
+ #
76
+ class WindowUnminimized; end
77
+
78
+
79
+
80
+ # WindowExposed is an event that occurs in
81
+ # certain situations when the Rubygame application
82
+ # window is exposed after being covered by another
83
+ # application.
84
+ #
85
+ # This event may not occur on all platforms, but
86
+ # when it does occur, your app should refresh the
87
+ # entire window via Screen#flip (or
88
+ # Rubygame::GL.swap_buffers, if using OpenGL).
89
+ #
90
+ class WindowExposed; end
91
+
92
+
93
+
94
+ # QuitRequested is an event that occurs when the
95
+ # application receives a quit request, usually due to the
96
+ # user clicking the "Close" button on the app window.
97
+ #
98
+ # Almost always, your application should respond to this
99
+ # event by quitting or by displaying a "Quit/Cancel"
100
+ # dialog. If you ignore this event, the user may become
101
+ # frustrated that your app won't close properly!
102
+ #
103
+ class QuitRequested; end
104
+
105
+
106
+
107
+ # WindowResized is an event that occurs when the
108
+ # Rubygame application window is resized by the user.
109
+ # This can only happen if the Screen mode was set with
110
+ # the "resizable" flag.
111
+ #
112
+ # Your application should respond to this event by
113
+ # setting the Screen mode again with the new #size and
114
+ # redrawing.
115
+ #
116
+ # If you ignore this event, the "active" area of the
117
+ # Screen will stay the same size, and the rest (if the
118
+ # window was enlarged) will be black and won't receive
119
+ # any changes (blits, drawing, etc.).
120
+ #
121
+ class WindowResized
122
+ attr_reader :size
123
+
124
+ def initialize( size )
125
+
126
+ @size = size.to_ary.dup
127
+ @size.freeze
128
+
129
+ unless @size.length == 2
130
+ raise ArgumentError, "size must have exactly 2 parts (got %s)"%@size.length
131
+ end
132
+
133
+ @size.each do |part|
134
+ if part <= 0
135
+ raise ArgumentError, "size must be positive (got %s)"%part
136
+ end
137
+ end
138
+
139
+ end
140
+ end
141
+
142
+
143
+ end
144
+ end
@@ -0,0 +1,155 @@
1
+ #--
2
+ # This file is one part of:
3
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
4
+ #
5
+ # Copyright (C) 2008 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
+ #
28
+ # MouseButtonEvent is a mixin module included in the MousePressed
29
+ # and MouseReleased classes. It defines the #button and #pos
30
+ # attribute readers.
31
+ #
32
+ module MouseButtonEvent
33
+ attr_reader :button, :pos
34
+
35
+ #
36
+ # Initialize the MouseButtonEvent.
37
+ #
38
+ # button:: a symbol for the button that was pressed or
39
+ # released. (Symbol, required)
40
+ #
41
+ # pos:: an Array for the position of the mouse cursor
42
+ # when the event occured. [0,0] is the top-left
43
+ # corner of the window (or the screen if running
44
+ # full-screen). (Array, required)
45
+ #
46
+ def initialize( pos, button )
47
+
48
+ unless button.kind_of? Symbol
49
+ raise ArgumentError, "button must be a :symbol"
50
+ end
51
+
52
+ @button = button
53
+
54
+ @pos = pos.to_ary.dup
55
+ @pos.freeze
56
+
57
+ end
58
+ end
59
+
60
+
61
+ #
62
+ # MousePressed is an event class which occurs when a button
63
+ # on the mouse is pressed down.
64
+ #
65
+ # This class gains #button and #pos attribute readers from
66
+ # the MouseButtonEvent mixin module.
67
+ #
68
+ class MousePressed
69
+ include MouseButtonEvent
70
+
71
+ #
72
+ # Create a new MousePressed instance.
73
+ #
74
+ # button:: a symbol for the button that was pressed or
75
+ # released. (Symbol, required)
76
+ #
77
+ # pos:: an Array for the position of the mouse cursor
78
+ # when the event occured. [0,0] is the top-left
79
+ # corner of the window (or the screen if running
80
+ # full-screen). (Array, required)
81
+ #
82
+ def initialize( pos, button )
83
+ super
84
+ end
85
+ end
86
+
87
+
88
+ #
89
+ # MouseReleased is an event class which occurs when a button
90
+ # on the mouse is released (no longer being pressed).
91
+ #
92
+ # This class gains #button and #pos attribute readers from
93
+ # the MouseButtonEvent mixin module.
94
+ #
95
+ class MouseReleased
96
+ include MouseButtonEvent
97
+
98
+ #
99
+ # Create a new MouseReleased instance.
100
+ #
101
+ # button:: a symbol for the button that was pressed or
102
+ # released. (Symbol, required)
103
+ #
104
+ # pos:: an Array for the position of the mouse cursor
105
+ # when the event occured. [0,0] is the top-left
106
+ # corner of the window (or the screen if running
107
+ # full-screen). (Array, required)
108
+ #
109
+ def initialize( pos, button )
110
+ super
111
+ end
112
+ end
113
+
114
+
115
+ #
116
+ # MouseMoved is an event class which occurs when the mouse
117
+ # cursor moves. It has attribute readers for #pos (new position),
118
+ # #rel (change since the last MouseMoved event), and #buttons
119
+ # (an Array of the mouse buttons that were held down while moving).
120
+ #
121
+ class MouseMoved
122
+
123
+ attr_reader :pos, :rel, :buttons
124
+
125
+ #
126
+ # Create a new MouseReleased instance.
127
+ #
128
+ # pos:: an Array for the new position of the mouse cursor.
129
+ # The point [0,0] is the top-left corner of the window
130
+ # (or the screen if running full-screen).
131
+ # (Array, required)
132
+ #
133
+ # rel:: an Array for the position change since the last
134
+ # MouseMoved event, in pixels. (Array, required)
135
+ #
136
+ # buttons:: an Array of symbols for the mouse buttons that were
137
+ # being held down while the mouse was moving. [] if
138
+ # no buttons were being held. (Array, optional)
139
+ #
140
+ def initialize( pos, rel, buttons=[] )
141
+
142
+ @pos = pos.to_ary.dup
143
+ @pos.freeze
144
+
145
+ @rel = rel.to_ary.dup
146
+ @rel.freeze
147
+
148
+ @buttons = buttons.to_ary.dup
149
+ @buttons.freeze
150
+
151
+ end
152
+ end
153
+
154
+ end
155
+ end
data/lib/rubygame/ftor.rb CHANGED
@@ -80,7 +80,7 @@ class Ftor
80
80
  # Create a new Ftor by specifying its #angle (in radians) and #magnitude.
81
81
  # See also #new.
82
82
  def self.new_am(a,m)
83
- v = Ftor.new(1,0)
83
+ v = self.new(1,0)
84
84
  v.a, v.m = a, m
85
85
  return v
86
86
  end
@@ -92,7 +92,7 @@ class Ftor
92
92
  # In other words, assuming +v+ is the Ftor returned by this function:
93
93
  # p1 + v = p2
94
94
  def self.new_from_to(p1,p2)
95
- return self.class.new(p2[0]-p1[0],p2[1]-p1[1])
95
+ return self.new(p2[0]-p1[0],p2[1]-p1[1])
96
96
  end
97
97
 
98
98
  attr_reader :x # The x component of the Ftor.
@@ -1,6 +1,9 @@
1
1
  #--
2
- # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
- # Copyright (C) 2004-2007 John Croisant
2
+ #
3
+ # This file is one part of:
4
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
5
+ #
6
+ # Copyright (C) 2004-2008 John Croisant
4
7
  #
5
8
  # This library is free software; you can redistribute it and/or
6
9
  # modify it under the terms of the GNU Lesser General Public
@@ -15,6 +18,7 @@
15
18
  # You should have received a copy of the GNU Lesser General Public
16
19
  # License along with this library; if not, write to the Free Software
17
20
  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
+ #
18
22
  #++
19
23
 
20
24
  require "rubygame/event"
@@ -26,16 +30,17 @@ module Rubygame
26
30
  # joystick movement, etc. You can also post custom events to the
27
31
  # EventQueue to help manage the game state.
28
32
  #
29
- # This class replaces the old Rubygame::Queue class, which is no longer
30
- # available. While EventQueue class serves the same purpose as the old
31
- # class, they are somewhat different in behavior. Please note that while
32
- # the old class was a Singleton, this class is not; you may have as many
33
- # separate instances of EventQueue as you wish (although it is strongly
34
- # recommended that only one be used to #fetch_sdl_events).
35
- #
36
33
  # For basic usage, create a #new EventQueue with autofetch, then call the
37
34
  # #each method once per game loop, passing a block which handles events.
38
35
  # See the sample applications for examples of this.
36
+ #
37
+ # In Rubygame 2.4 and later, you can call #enable_new_style_events to make
38
+ # EventQueue fetch the new event classes (in the Rubygame::Events module).
39
+ # Otherwise, the old classes will be used, for backwards compatibility.
40
+ #
41
+ # It is **strongly recommended** that you use the new event classes.
42
+ # The old classes are deprecated as of Rubygame 2.4, and will be removed
43
+ # entirely in Rubygame 3.0.
39
44
  #
40
45
  # If you wish to ignore all events of a certain class, append those classes
41
46
  # the instance variable @ignore (accessors are provided). You can ignore as
@@ -45,22 +50,6 @@ module Rubygame
45
50
  # If the program has to pause and wait for an event (for example, if the
46
51
  # player must press a button to begin playing), you might find the #wait
47
52
  # method to be convenient.
48
- #
49
- # For reference, the full list of SDL events is:
50
- # - Event (base class, not used by itself)
51
- # - ActiveEvent
52
- # - JoyAxisEvent
53
- # - JoyBallEvent
54
- # - JoyDownEvent
55
- # - JoyHatEvent
56
- # - JoyUpEvent
57
- # - KeyDownEvent
58
- # - KeyUpEvent
59
- # - MouseDownEvent
60
- # - MouseMotionEvent
61
- # - MouseUpEvent
62
- # - QuitEvent
63
- # - ResizeEvent
64
53
  #
65
54
  class EventQueue < Array
66
55
  # Array of classes to be ignored by #push.
@@ -74,9 +63,27 @@ module Rubygame
74
63
  def initialize()
75
64
  @autofetch = true
76
65
  @ignore = []
66
+ @new_style_events = false
77
67
  yield self if block_given?
78
68
  end
79
69
 
70
+
71
+ # Enable new-style events. These are the event classes in the
72
+ # Rubygame::Events module, which were added in Rubygame 2.4.
73
+ #
74
+ # If you call this method, the new event classes will be used.
75
+ # Otherwise, the old classes will be used, for backwards
76
+ # compatibility.
77
+ #
78
+ # It is **strongly recommended** that you use the new event
79
+ # classes. The old classes are deprecated as of Rubygame 2.4,
80
+ # and will be removed entirely in Rubygame 3.0.
81
+ #
82
+ def enable_new_style_events
83
+ @new_style_events = true
84
+ end
85
+
86
+
80
87
  # Append events to the EventQueue.
81
88
  # Silently ignores events whose class is in @ignore.
82
89
  def push(*events)
@@ -88,7 +95,9 @@ module Rubygame
88
95
 
89
96
  alias post push
90
97
 
91
- alias peek_each each # Iterate through all events without removing.
98
+
99
+ alias :_old_each :each
100
+ private :_old_each
92
101
 
93
102
  # Iterate through all events in the EventQueue, yielding them one at a time
94
103
  # to the given block. The EventQueue is flushed after all events have been
@@ -96,19 +105,31 @@ module Rubygame
96
105
  #
97
106
  # If the internal variable @autofetch is true, this method will call
98
107
  # #fetch_sdl_events once before iterating.
99
- def each(&block)
108
+ def each( &block )
100
109
  fetch_sdl_events if @autofetch
101
- super
110
+ _old_each( &block )
102
111
  self.clear
103
112
  end
104
113
 
114
+ # Like #each, but doesn't remove the events from the queue after
115
+ # iterating.
116
+ def peek_each( &block )
117
+ fetch_sdl_events if @autofetch
118
+ _old_each( &block )
119
+ end
120
+
121
+
105
122
  # Posts pending SDL hardware events to the EventQueue. Only one EventQueue
106
123
  # should call this method per application, and only if you are not using
107
124
  # Rubygame#fetch_sdl_events to manually process events! Otherwise, some
108
125
  # events may be removed from SDL's event stack before they can be properly
109
126
  # processed!
110
127
  def fetch_sdl_events
111
- self.push(Rubygame.fetch_sdl_events())
128
+ if @new_style_events
129
+ self.push( Rubygame::Events.fetch_sdl_events() )
130
+ else
131
+ self.push( Rubygame.fetch_sdl_events() )
132
+ end
112
133
  end
113
134
 
114
135
  # Wait for an event to be posted, then return that event.