ray 0.0.1 → 0.1.0.pre1

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.
Files changed (188) hide show
  1. data/.gemtest +0 -0
  2. data/.yardopts +4 -0
  3. data/README.md +17 -21
  4. data/Rakefile +18 -139
  5. data/VERSION +1 -1
  6. data/ext/audio.cpp +723 -0
  7. data/ext/{color.c → color.cpp} +25 -13
  8. data/ext/drawable.cpp +91 -0
  9. data/ext/event.cpp +460 -0
  10. data/ext/extconf.rb +5 -104
  11. data/ext/font.cpp +190 -0
  12. data/ext/image.cpp +733 -0
  13. data/ext/input.cpp +74 -0
  14. data/ext/ray.cpp +168 -0
  15. data/ext/ray.hpp +356 -0
  16. data/ext/{rect.c → rect.cpp} +51 -37
  17. data/ext/shader.cpp +169 -0
  18. data/ext/shape.cpp +409 -0
  19. data/ext/sprite.cpp +306 -0
  20. data/ext/text.cpp +181 -0
  21. data/ext/vector.cpp +215 -0
  22. data/guide.md +619 -0
  23. data/lib/ray/audio.rb +0 -41
  24. data/lib/ray/color.rb +32 -10
  25. data/lib/ray/drawable.rb +16 -0
  26. data/lib/ray/dsl/event_listener.rb +25 -2
  27. data/lib/ray/dsl/event_runner.rb +33 -5
  28. data/lib/ray/dsl/event_translator.rb +66 -30
  29. data/lib/ray/dsl/handler.rb +3 -2
  30. data/lib/ray/dsl/matcher.rb +58 -14
  31. data/lib/ray/font.rb +38 -96
  32. data/lib/ray/font_set.rb +8 -8
  33. data/lib/ray/game.rb +87 -66
  34. data/lib/ray/helper.rb +105 -10
  35. data/lib/ray/image.rb +150 -24
  36. data/lib/ray/image_set.rb +3 -1
  37. data/lib/ray/input.rb +10 -0
  38. data/lib/ray/music_set.rb +5 -3
  39. data/lib/ray/ray.rb +21 -9
  40. data/lib/ray/rect.rb +48 -7
  41. data/lib/ray/rmagick.rb +41 -0
  42. data/lib/ray/scene.rb +99 -43
  43. data/lib/ray/scene_list.rb +67 -0
  44. data/lib/ray/shape.rb +132 -0
  45. data/lib/ray/sound_set.rb +4 -2
  46. data/lib/ray/sprite.rb +49 -111
  47. data/lib/ray/text.rb +101 -0
  48. data/lib/ray/text_helper.rb +37 -0
  49. data/lib/ray/turtle.rb +215 -0
  50. data/lib/ray/vector.rb +226 -0
  51. data/samples/audio/spacial.rb +44 -0
  52. data/samples/hello_world/hello.rb +9 -13
  53. data/samples/hello_world/hello_dsl.rb +8 -12
  54. data/samples/hello_world/text.rb +15 -0
  55. data/samples/opengl/binding.rb +38 -0
  56. data/samples/opengl/image.rb +32 -0
  57. data/samples/opengl/opengl.rb +34 -0
  58. data/samples/opengl/shader.rb +42 -0
  59. data/samples/pong/pong.rb +14 -10
  60. data/samples/run_scene.rb +53 -0
  61. data/samples/shaders/scene.rb +40 -0
  62. data/samples/shaders/shaders.rb +42 -0
  63. data/samples/shaders/shape.rb +34 -0
  64. data/samples/sokoban/sokoban.rb +18 -18
  65. data/samples/test/actual_scene.rb +41 -0
  66. data/samples/test/scene_riot.rb +39 -0
  67. data/samples/test/scene_spec.rb +32 -0
  68. data/samples/test/scene_test_unit.rb +25 -0
  69. data/samples/turtle/byzantium.rb +45 -0
  70. data/samples/turtle/hilbert.rb +48 -0
  71. data/samples/turtle/koch.rb +55 -0
  72. data/samples/turtle/mandala.rb +61 -0
  73. data/samples/turtle/tree.rb +57 -0
  74. data/test/audio_test.rb +69 -0
  75. data/test/color_test.rb +77 -0
  76. data/test/drawable_test.rb +19 -0
  77. data/test/dsl_test.rb +93 -0
  78. data/test/font_test.rb +57 -0
  79. data/test/helpers.rb +94 -0
  80. data/test/image_test.rb +82 -0
  81. data/test/ray_test.rb +25 -0
  82. data/test/rect_test.rb +121 -0
  83. data/{spec → test}/res/VeraMono.ttf +0 -0
  84. data/{spec → test}/res/aqua.bmp +0 -0
  85. data/{spec → test}/res/aqua.png +0 -0
  86. data/{spec → test}/res/aqua2.bmp +0 -0
  87. data/{spec → test}/res/not_a_jpeg.jpeg +0 -0
  88. data/{spec → test}/res/pop.wav +0 -0
  89. data/test/resource_set_test.rb +99 -0
  90. data/test/run_all.rb +7 -0
  91. data/test/shape_test.rb +101 -0
  92. data/test/sprite_test.rb +89 -0
  93. data/test/text_test.rb +78 -0
  94. data/test/turtle_test.rb +176 -0
  95. data/test/vector_test.rb +111 -0
  96. data/yard_ext.rb +0 -28
  97. metadata +95 -139
  98. data/.gitignore +0 -23
  99. data/.gitmodules +0 -3
  100. data/.rspec +0 -3
  101. data/ext/audio.c +0 -473
  102. data/ext/event.c +0 -557
  103. data/ext/font.c +0 -287
  104. data/ext/image.c +0 -933
  105. data/ext/joystick.c +0 -145
  106. data/ext/ray.c +0 -489
  107. data/ext/ray.h +0 -245
  108. data/ext/ray_osx.m +0 -161
  109. data/lib/ray/joystick.rb +0 -30
  110. data/psp/SDL_psp_main.c +0 -84
  111. data/psp/bigdecimal/README +0 -60
  112. data/psp/bigdecimal/bigdecimal.c +0 -4697
  113. data/psp/bigdecimal/bigdecimal.h +0 -216
  114. data/psp/bigdecimal/lib/bigdecimal/jacobian.rb +0 -85
  115. data/psp/bigdecimal/lib/bigdecimal/ludcmp.rb +0 -84
  116. data/psp/bigdecimal/lib/bigdecimal/math.rb +0 -235
  117. data/psp/bigdecimal/lib/bigdecimal/newton.rb +0 -77
  118. data/psp/bigdecimal/lib/bigdecimal/util.rb +0 -65
  119. data/psp/digest/bubblebabble/bubblebabble.c +0 -142
  120. data/psp/digest/defs.h +0 -20
  121. data/psp/digest/digest.c +0 -643
  122. data/psp/digest/digest.h +0 -32
  123. data/psp/digest/lib/digest.rb +0 -50
  124. data/psp/digest/lib/md5.rb +0 -27
  125. data/psp/digest/lib/sha1.rb +0 -27
  126. data/psp/digest/md5/md5.c +0 -420
  127. data/psp/digest/md5/md5.h +0 -80
  128. data/psp/digest/md5/md5init.c +0 -40
  129. data/psp/digest/rmd160/rmd160.c +0 -457
  130. data/psp/digest/rmd160/rmd160.h +0 -56
  131. data/psp/digest/rmd160/rmd160init.c +0 -40
  132. data/psp/digest/sha1/sha1.c +0 -269
  133. data/psp/digest/sha1/sha1.h +0 -39
  134. data/psp/digest/sha1/sha1init.c +0 -40
  135. data/psp/digest/sha2/lib/sha2.rb +0 -73
  136. data/psp/digest/sha2/sha2.c +0 -919
  137. data/psp/digest/sha2/sha2.h +0 -109
  138. data/psp/digest/sha2/sha2init.c +0 -52
  139. data/psp/enumerator/enumerator.c +0 -298
  140. data/psp/etc/etc.c +0 -559
  141. data/psp/ext.c +0 -289
  142. data/psp/fcntl/fcntl.c +0 -187
  143. data/psp/lib/rbconfig.rb +0 -178
  144. data/psp/nkf/lib/kconv.rb +0 -367
  145. data/psp/nkf/nkf-utf8/config.h +0 -88
  146. data/psp/nkf/nkf-utf8/nkf.c +0 -6040
  147. data/psp/nkf/nkf-utf8/utf8tbl.c +0 -8500
  148. data/psp/nkf/nkf-utf8/utf8tbl.h +0 -34
  149. data/psp/nkf/nkf.c +0 -654
  150. data/psp/socket/addrinfo.h +0 -173
  151. data/psp/socket/getaddrinfo.c +0 -676
  152. data/psp/socket/getnameinfo.c +0 -270
  153. data/psp/socket/pspsocket.c +0 -71
  154. data/psp/socket/pspsocket.h +0 -28
  155. data/psp/socket/socket.c +0 -4662
  156. data/psp/socket/sockport.h +0 -76
  157. data/psp/stringio/stringio.c +0 -1306
  158. data/psp/strscan/strscan.c +0 -1320
  159. data/psp/syck/bytecode.c +0 -1166
  160. data/psp/syck/emitter.c +0 -1242
  161. data/psp/syck/gram.c +0 -1894
  162. data/psp/syck/gram.h +0 -79
  163. data/psp/syck/handler.c +0 -174
  164. data/psp/syck/implicit.c +0 -2990
  165. data/psp/syck/node.c +0 -408
  166. data/psp/syck/rubyext.c +0 -2367
  167. data/psp/syck/syck.c +0 -504
  168. data/psp/syck/syck.h +0 -456
  169. data/psp/syck/token.c +0 -2725
  170. data/psp/syck/yaml2byte.c +0 -257
  171. data/psp/syck/yamlbyte.h +0 -170
  172. data/psp/thread/thread.c +0 -1175
  173. data/psp/zlib/zlib.c +0 -3547
  174. data/script.rb +0 -10
  175. data/spec/ray/audio_spec.rb +0 -146
  176. data/spec/ray/color_spec.rb +0 -57
  177. data/spec/ray/event_spec.rb +0 -80
  178. data/spec/ray/font_spec.rb +0 -93
  179. data/spec/ray/image_set_spec.rb +0 -48
  180. data/spec/ray/image_spec.rb +0 -162
  181. data/spec/ray/joystick_spec.rb +0 -21
  182. data/spec/ray/matcher_spec.rb +0 -50
  183. data/spec/ray/ray_spec.rb +0 -88
  184. data/spec/ray/rect_spec.rb +0 -154
  185. data/spec/ray/resource_set_spec.rb +0 -105
  186. data/spec/ray/sprite_spec.rb +0 -163
  187. data/spec/spec.opts +0 -4
  188. data/spec/spec_helper.rb +0 -8
@@ -1,108 +1,50 @@
1
+ # -*- coding: utf-8 -*-
2
+
1
3
  module Ray
2
4
  class Font
3
- extend Ray::ResourceSet
4
- need_argument_count 1
5
- add_set(/^(.*)$/) { |filename, size| new(filename, size) }
5
+ include Ray::TextHelper
6
6
 
7
- # @return [true, false] True if the font has no style
8
- def normal?
9
- style == STYLE_NORMAL
10
- end
11
-
12
- # @return [true, false] True if the font is italic
13
- def italic?
14
- (style & STYLE_ITALIC) != 0
15
- end
16
-
17
- # @return [true, false] True if the font is bold
18
- def bold?
19
- (style & STYLE_BOLD) != 0
20
- end
21
-
22
- # @return [true, false] True if the font is underlined
23
- def underlined?
24
- (style & STYLE_UNDERLINE) != 0
7
+ extend Ray::ResourceSet
8
+ add_set(/^(.*)$/) { |filename| new(filename) }
9
+
10
+ # @param [String] string The string which should be drawn.
11
+ #
12
+ # @option opts [Integer] :size Size of the font
13
+ # @option opts [String, nil] :encoding Encoding of the text. Guessed in
14
+ # Ruby 1.9, assumes UTF-8 otherwise.
15
+ # @option opts [Integer, Array<Symbol>] :style Flags for the font style.
16
+ # Valid symbols are :normal, :italic, :bold, and :underline.
17
+ # @option opts [Ray::Color] :color Color to draw the text in. Defaults to
18
+ # white.
19
+ # @option opts [Ray::Image] :on The image to draw on. In this case,
20
+ # it will directly draw instead of returning an image containing nothing
21
+ # but the drawn text.
22
+ # @option opts [Ray::Vector2, #to_vector2] :to or :at where to draw on the image.
23
+ # @return The surface it drew the string on.
24
+ def draw(string, opts)
25
+ enc = opts[:encoding] || (string.respond_to?(:encoding) && string.encoding.to_s)
26
+ enc ||= "UTF-8"
27
+
28
+ opts[:style] = parse_style(opts[:style])
29
+
30
+ simple_draw internal_string(string, enc), opts
25
31
  end
26
- end
27
- end
28
-
29
- class String
30
- # Draws the receiver.
31
- #
32
- # @option opts [Ray::Font] :font The font used to render the string.
33
- # @option opts [Ray::Image] :on The image to draw on.
34
- # @option opts [Integer] :w Witdh of the image. Also called :width.
35
- # @option opts [Integer] ;h height of the image. Also called :height.
36
- # @option opts [Symbol] The encoding. Can be guessed in Ruby 1.9.
37
- # @option opts [Ray::Color] :color The color to draw the text in.
38
- # @option opts [Ray::Color] :background Background color in shaded mode.
39
- # @option opts [Symbol] :mode The drawing mode.
40
- # @option opts [Array<Symbol>] :style The different styles to apply.
41
- # :italic, :bold, and :underlined.
42
- #
43
- # @option opts [Array<Integer>] :at Where the image should be drawn.
44
- # Defaults to (0, 0)
45
- #
46
- # @see Ray::Font#draw
47
- def draw(opts = {})
48
- font = opts[:font]
49
-
50
- lines = split(/\r\n|\n|\r/)
51
- line_skip = font.line_skip
52
-
53
- target = opts[:on]
54
32
 
55
- string_encoding = opts[:encoding]
56
- string_encoding ||= if respond_to? :encoding # Ruby 1.9
57
- case encoding.to_s
58
- when /^utf-?8$/i
59
- :utf8
60
- when /^iso-8859-/i
61
- :latin1
62
- else
63
- nil
64
- end
65
- else
66
- nil
67
- end
33
+ # @param [Hash] opts (See #draw)
34
+ # @return [Ray::Vector2] Size of the text using this font.
35
+ def size_of(string, opts)
36
+ enc = opts[:encoding] || (string.respond_to?(:encoding) && string.encoding.to_s)
37
+ enc ||= "UTF-8"
68
38
 
69
- target ||= Ray::Image.new(:height => opts[:height] || opts[:h] ||
70
- line_skip * lines.size,
71
- :width => opts[:width] || opts[:w] ||
72
- lines.map { |i|
73
- font.size_of(self, string_encoding).width
74
- }.max)
39
+ opts[:style] = parse_style(opts[:style])
75
40
 
76
- color = opts[:color]
77
- background = opts[:background]
78
-
79
- mode = opts[:mode]
80
-
81
- if styles = opts[:style]
82
- font.style = styles.inject(0) do |flags, style|
83
- flags |= case style
84
- when :italic
85
- Ray::Font::STYLE_ITALIC
86
- when :bold
87
- Ray::Font::STYLE_BOLD
88
- when :underlined
89
- Ray::Font::STYLE_UNDERLINE
90
- else
91
- raise "Unknown flag #{style}"
92
- end
93
- end
41
+ simple_size_of internal_string(string, enc), opts
94
42
  end
95
43
 
96
- x, y = opts[:at]
97
- x ||= 0
98
- y ||= 0
99
-
100
- lines.each do |line|
101
- font.draw(line, :on => target, :at => [x, y], :encoding => string_encoding,
102
- :color => color, :background => background, :mode => mode)
103
- y += line_skip
44
+ # @return [Integer] Width required to draw the sring with this font.
45
+ # @see #size_of
46
+ def width_of(string, opts)
47
+ size_of(string, opts).width
104
48
  end
105
-
106
- target
107
49
  end
108
50
  end
@@ -1,11 +1,10 @@
1
1
  module Ray
2
2
  module FontSet
3
3
  extend Ray::ResourceSet
4
- need_argument_count 1
5
4
 
6
5
  class << self
7
- def missing_pattern(string, size)
8
- Ray::Font[string, size]
6
+ def missing_pattern(string)
7
+ Ray::Font[string]
9
8
  end
10
9
 
11
10
  def select!(&block)
@@ -18,20 +17,21 @@ module Ray
18
17
  # Creates a new font set.
19
18
  #
20
19
  # @param [Regexp] regex Regular expression used to match file
21
- # @yield [*args, size] Block returning the font
20
+ # @yield [*args] Block returning the font
22
21
  #
23
22
  # @yieldparam args Regex captures
24
- # @yieldparam size Size of the font
25
- def self.font_set(regex, &block)
23
+ def font_set(regex, &block)
26
24
  Ray::FontSet.add_set(regex, &block)
27
25
  end
26
+
27
+ module_function :font_set
28
28
  end
29
29
 
30
30
  begin
31
31
  require 'open-uri'
32
32
 
33
- Ray.font_set(/^(http|ftp):\/\/(\S+)$/) do |protocol, address, size|
34
- open("#{protocol}://#{address}") { |io| Ray::Font.new(io, size) }
33
+ Ray.font_set(/^(http|ftp):\/\/(\S+)$/) do |protocol, address|
34
+ open("#{protocol}://#{address}") { |io| Ray::Font.new(io) }
35
35
  end
36
36
  rescue LoadError
37
37
  end
@@ -1,5 +1,5 @@
1
1
  module Ray
2
- # Games are used to manage different scenes. They also init Ray and create a
2
+ # Games are used to manage different scenes. They also init Ray by creating a
3
3
  # window.
4
4
  #
5
5
  # == Creating a Game
@@ -68,65 +68,73 @@ module Ray
68
68
  # Creates a new game.
69
69
  #
70
70
  # You can pass all the arguments you would pass to create_window,
71
- # except width and height which should be given in :video_mode:
71
+ # except width and height which should be given in either :size or
72
+ # :video_modes:
72
73
  # Ray::Game.new('hello', :video_modes => %w(480x272 640x480))
73
74
  #
75
+ # :video_modes is an array of Vector2 or strings, :size is a single vector2.
76
+ # (vectors being interchangible with objects responding to #to_vector2)
77
+ #
74
78
  # It will try to get the biggest resolution available (so it will most
75
79
  # likely choose 640x480 in this case).
76
80
  #
77
81
  # If a block is passed, it is instance evaluated, then the game is
78
82
  # directly run.
79
83
  #
80
- # This methods creates a new window and inits Ray.
84
+ # This methods creates a new window.
85
+ #
86
+ # @param [Hash] hash (See Ray.create_window)
81
87
  def initialize(title, hash = {}, &block)
82
88
  @game_registred_scenes = {}
83
- @game_scenes = []
84
-
85
- defaults = {
86
- :double_buf => true,
87
- :bpp => 32,
88
- :hw_surface => true,
89
- :sw_surface => false,
90
- :video_modes => %w(480x272 640x480)
91
- }
89
+ @game_scenes = SceneList.new(self)
90
+ @game_register_block = nil
91
+ @game_running = true
92
92
 
93
+ create_event_runner
94
+
95
+ defaults = {:size => [640, 480]}
93
96
  options = defaults.merge(hash)
94
97
 
98
+ if !options[:video_modes]
99
+ size = options[:size].to_vector2
100
+ options[:video_modes] = [size]
101
+ end
102
+
95
103
  common_settings = {
96
- :sw_surface => options[:sw_surface],
97
- :hw_surface => options[:hw_surface],
98
- :bpp => options[:bpp] || options[:bits_per_pixel],
99
- :async_blit => options[:async_blit],
100
- :double_buf => options[:double_buf],
101
104
  :fullscreen => options[:fullscreen],
102
105
  :resizable => options[:resizable],
103
106
  :no_frame => options[:no_frame]
104
107
  }
105
108
 
106
- modes = options[:video_modes].map { |m| m.split('x').map { |i| i.to_i } }
109
+ modes = options[:video_modes].map do |size|
110
+ case size
111
+ when String
112
+ size.split('x').map { |i| i.to_i }
113
+ else
114
+ size.to_a
115
+ end
116
+ end
107
117
 
108
118
  # The biggest resolution is privileged
109
119
  modes = modes.sort_by { |(w, h)| w * h }.map do |(w, h)|
110
120
  common_settings.merge(:w => w, :h => h)
111
121
  end
112
122
 
113
- Ray.init
114
-
115
- last_mode = modes.select { |mode| Ray.can_use_mode? mode }.last
123
+ last_mode = modes.last
116
124
  raise ArgumentError, "No valid mode found" unless last_mode
117
125
 
126
+ @game_last_mode = last_mode
127
+ @game_window = Ray.create_window(last_mode)
128
+ @game_window.fill Ray::Color.none # Start by clearing the window
129
+
118
130
  if @game_title = title
119
131
  Ray.window_title = @game_title
120
- Ray.text_icon = @game_title
121
132
  end
122
133
 
123
134
  if icon = options[:icon]
124
- Ray.icon = icon.is_a?(Ray::Image) ? icon : icon.to_image
135
+ Ray.icon = icon.is_a?(String) ? Ray::ImageSet[icon] : icon
125
136
  end
126
137
 
127
- @game_last_mode = last_mode
128
- @game_window = Ray.create_window(last_mode)
129
-
130
138
  if block
131
139
  instance_eval(&block)
132
140
  run
@@ -143,16 +151,12 @@ module Ray
143
151
  # @param [Symbol] scene_name The name of the scene which should be pushed
144
152
  # @param *args Arguments passed to the scene
145
153
  def push_scene(scene_name, *args)
146
- scene = @game_registred_scenes[scene_name]
147
- raise ArgumentError, "Unknown scene #{scene_name}" unless scene
148
-
149
- @game_scenes << scene
150
- @game_scene_arguments = args
154
+ @game_scenes.push(scene_name, *args)
151
155
  end
152
156
 
153
157
  # Pops the last scene.
154
158
  def pop_scene
155
- @game_scenes.delete_at(-1)
159
+ @game_scenes.pop
156
160
  end
157
161
 
158
162
  # Registers a new scene with a given name. the block will be passed
@@ -161,32 +165,33 @@ module Ray
161
165
  # @param [Symobl] name the name of the new scene
162
166
  # @param [Class] klass the class of the scene.
163
167
  def scene(name, klass = Scene, &block)
164
- @game_registred_scenes[name] = klass.new(&block)
168
+ scene = @game_registred_scenes[name] = klass.new(&block)
169
+
170
+ scene.game = self
171
+ scene.event_runner = event_runner
172
+ scene.window = @game_window
173
+ end
174
+
175
+ # @return [Ray::Scene] scene register for a given name
176
+ def registred_scene(name)
177
+ @game_registred_scenes[name]
165
178
  end
166
179
 
167
180
  # Runs the game until the last scene gets popped.
168
181
  # Will call Ray.stop.
169
182
  def run
170
- until @game_scenes.empty?
171
- create_event_runner
183
+ while running?
184
+ event_runner.clear
172
185
  register
173
186
 
174
- @game_scenes.each do |scene|
175
- scene.game = self
176
- scene.window = @game_window
177
- scene.event_runner = event_runner
178
- scene.scene_arguments = @game_scene_arguments
179
- end
180
-
181
- scene = @game_scenes.last
182
-
183
- scene.setup(*@game_scene_arguments)
184
- scene.register_events
185
- scene.need_render!
186
- scene.run
187
+ @game_scenes.run_current
187
188
  end
189
+ end
188
190
 
189
- Ray.stop
191
+ # @param [true, false] True if the scene list isn't empty
192
+ # and the user hasn't exited from the game.
193
+ def running?
194
+ !@game_exited && !@game_scenes.empty?
190
195
  end
191
196
 
192
197
  # Registers a block to listen to events
@@ -201,44 +206,60 @@ module Ray
201
206
 
202
207
  # Removes the current scene of this game
203
208
  def exit
204
- return if @game_scenes.empty?
205
-
206
- @game_scenes.last.exit
207
- pop_scene
209
+ @game_scenes.exit_current
208
210
  end
209
211
 
210
212
  # Kills the game, removing all the scenes it contains.
211
213
  def exit!
212
- return if @game_scenes.empty?
214
+ @game_exited = true
213
215
 
214
- @game_scenes.last.exit
216
+ @game_scenes.exit_current
215
217
  @game_scenes.clear
216
218
  end
217
-
219
+
218
220
  # Resizes the window and raises a window_resize event
219
221
  def resize_window(w, h)
220
- @game_window = Ray.create_window(@game_last_mode.merge!(:w => w,
221
- :h => h))
222
- @game_scenes.each do |scene|
223
- scene.window = @game_window
224
- end
225
-
226
- @game_scenes.last.need_render!
227
-
228
- raise_event(:window_resize, Ray::Rect.new(0, 0, w, h))
222
+ Ray.create_window(@game_last_mode.merge!(:w => w, :h => h))
223
+ @game_window.fill Ray::Color.none
224
+
225
+ @game_scenes.current.need_render!
226
+
227
+ raise_event(:window_resize, Ray::Vector2[w, h])
229
228
  end
230
229
 
231
230
  def title
232
231
  @game_title
233
232
  end
234
233
 
234
+ def scenes
235
+ @game_scenes
236
+ end
237
+
238
+ def scenes=(list)
239
+ @game_scenes = list
240
+
241
+ unless running?
242
+ @game_scenes.exit_current
243
+ end
244
+ end
245
+
246
+ def event_runner=(runner)
247
+ super
248
+
249
+ @game_registred_scenes.each do |name, scene|
250
+ scene.event_runner = runner
251
+ end
252
+ end
253
+
235
254
  def inspect
236
255
  "game(#{title.inspect})"
237
256
  end
238
257
  end
239
258
 
240
259
  # @see Ray::Game#initialize
241
- def self.game(title, opts = {}, &block)
260
+ def game(title, opts = {}, &block)
242
261
  Ray::Game.new(title, opts, &block)
243
262
  end
263
+
264
+ module_function :game
244
265
  end
@@ -1,9 +1,74 @@
1
1
  module Ray
2
- # Module including many helpful modules and methods.
2
+ #
3
+ # == Event handling
4
+ # This module is used to allow both raising events and handling thereof.
5
+ # You can raise them simply with raise_event:
6
+ # raise_event(:event_name, arugments)
7
+ # (You can add as many extra arguments as you want/need to)
8
+ #
9
+ # Those events can trigger a block.
10
+ # on :event_name do
11
+ # # Do something useful
12
+ # end
13
+ #
14
+ # If you want your block to be triggered only when a condition is
15
+ # met, you can pass extra arguments to on. They will be compared
16
+ # to those that were passed to raise_event using === or == if
17
+ # === returned false.
18
+ # on :event_name, /foo/ do |str| puts str end
19
+ # raise_event "foobar" # Will trigger the above
20
+ #
21
+ # Handlers can be grouped:
22
+ # event_group :name do
23
+ # # Register for some events
24
+ # end
25
+ #
26
+ # Notice event groups cannot be nested. The following code:
27
+ # event_group :name do
28
+ # event_group :foo do
29
+ # # ...
30
+ # end
31
+ #
32
+ # # ...
33
+ # end
34
+ # Will create two totally unrelated event groups (:name and :foo).
35
+ #
36
+ # An event group is enabled by default, but it can be disabled:
37
+ # disable_event_group :name
38
+ # And re-enabled afterwards:
39
+ # enable_event_group :name
40
+ # It is also possible to remove the handlers that belong to a group
41
+ # when they're not needed anymore:
42
+ # remove_event_group :name
43
+ #
44
+ # == Shared resources
45
+ #
46
+ # This module contains helper methods that provide access to shared resources,
47
+ # i.e. they will always return the same object until the cache is cleared:
48
+ # obj = image "test.png"
49
+ # other_obj = image "test.png"
50
+ # obj.equal? other_obj # => true
51
+ #
52
+ # Notice different resources sets are used. This allows to get resources from
53
+ # other places than from the disk. For instance, Ray provides a way to get
54
+ # resources from the network using open-uri:
55
+ # obj = image "http://www.example.com/some_image.png" # Download, takes some time
56
+ # other_image = image "http://www.example.com/some_image.png" # Doesn't download
57
+ # obj.equal? other_image
58
+ #
59
+ # You can define your own resource sets pretty easily:
60
+ # Ray.image_set(/some (regexp)/) do |capture| # Captures are yielded
61
+ # # Block that returns an image
62
+ # end
63
+ #
64
+ # Be careful when using a shared resource. Call #dup instead of mutating it (even
65
+ # though that would not raise an error).
66
+ #
3
67
  module Helper
4
68
  include Ray::DSL::EventRaiser
5
69
  include Ray::DSL::EventListener
6
70
 
71
+ include Ray::Input
7
72
  include Ray::Matchers
8
73
 
9
74
  # Sets the event runner for this object.
@@ -22,6 +87,23 @@ module Ray
22
87
  self.event_runner = Ray::DSL::EventRunner.new
23
88
  end
24
89
 
90
+ # Enables an event group
91
+ def enable_event_group(name)
92
+ event_runner.enable_group(name)
93
+ end
94
+
95
+ # Disables an event group
96
+ def disable_event_group(name)
97
+ event_runner.disable_group(name)
98
+ end
99
+
100
+ # Removes all the handlers belonging to an event group
101
+ def remove_event_group(name)
102
+ event_runner.remove_group(name)
103
+ end
104
+
105
+ module_function
106
+
25
107
  # @see Ray::ImageSet.[]
26
108
  def image(name)
27
109
  Ray::ImageSet[name]
@@ -32,24 +114,37 @@ module Ray
32
114
  Ray::Sprite.new(image, opts)
33
115
  end
34
116
 
35
- # @see Ray::Channel#initialize
36
- def channel(id)
37
- Ray::Channel.new(id)
38
- end
39
-
40
- # @see Ray::MusicSet.[]
117
+ # @see Ray::SoundSet.[]
41
118
  def sound(file)
42
119
  Ray::SoundSet[file]
43
120
  end
44
-
121
+
45
122
  # @see Ray::MusicSet.[]
46
123
  def music(file)
47
124
  Ray::MusicSet[file]
48
125
  end
49
126
 
50
127
  # @see Ray::FontSet.[]
51
- def font(name, size)
52
- Ray::FontSet[name, size]
128
+ def font(name)
129
+ Ray::FontSet[name]
130
+ end
131
+
132
+ # @see Ray::Text#initialize
133
+ def text(content, opts = {})
134
+ Ray::Text.new(content, opts)
135
+ end
136
+
137
+ # @param [Symbol, Key, Integer] val A symbol to find the key (its name),
138
+ # a Key object, or one of the KEY_* constant.
139
+ # @return [true, false] True if the user is holding key.
140
+ def holding?(val)
141
+ if val.is_a? Integer
142
+ Ray::Input.holding? val
143
+ else
144
+ key(val.to_sym).to_a.any? do |key|
145
+ Ray::Input.holding? key
146
+ end
147
+ end
53
148
  end
54
149
  end
55
150
  end