ruby2d 0.9.5 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +4 -4
  2. data/assets/include/SDL2/SDL.h +4 -1
  3. data/assets/include/SDL2/SDL_assert.h +3 -1
  4. data/assets/include/SDL2/SDL_atomic.h +1 -1
  5. data/assets/include/SDL2/SDL_audio.h +1 -1
  6. data/assets/include/SDL2/SDL_bits.h +1 -1
  7. data/assets/include/SDL2/SDL_blendmode.h +4 -1
  8. data/assets/include/SDL2/SDL_clipboard.h +1 -1
  9. data/assets/include/SDL2/SDL_config.h +1 -1
  10. data/assets/include/SDL2/SDL_config_android.h +7 -3
  11. data/assets/include/SDL2/SDL_config_iphoneos.h +16 -10
  12. data/assets/include/SDL2/SDL_config_macosx.h +40 -14
  13. data/assets/include/SDL2/SDL_config_minimal.h +1 -1
  14. data/assets/include/SDL2/SDL_config_os2.h +26 -8
  15. data/assets/include/SDL2/SDL_config_pandora.h +4 -2
  16. data/assets/include/SDL2/SDL_config_psp.h +2 -1
  17. data/assets/include/SDL2/SDL_config_windows.h +40 -9
  18. data/assets/include/SDL2/SDL_config_winrt.h +5 -2
  19. data/assets/include/SDL2/SDL_config_wiz.h +5 -2
  20. data/assets/include/SDL2/SDL_copying.h +1 -1
  21. data/assets/include/SDL2/SDL_cpuinfo.h +32 -3
  22. data/assets/include/SDL2/SDL_egl.h +4 -1
  23. data/assets/include/SDL2/SDL_endian.h +6 -3
  24. data/assets/include/SDL2/SDL_error.h +38 -2
  25. data/assets/include/SDL2/SDL_events.h +64 -28
  26. data/assets/include/SDL2/SDL_filesystem.h +1 -1
  27. data/assets/include/SDL2/SDL_gamecontroller.h +160 -9
  28. data/assets/include/SDL2/SDL_gesture.h +1 -1
  29. data/assets/include/SDL2/SDL_haptic.h +10 -1
  30. data/assets/include/SDL2/SDL_hints.h +316 -4
  31. data/assets/include/SDL2/SDL_joystick.h +115 -24
  32. data/assets/include/SDL2/SDL_keyboard.h +1 -1
  33. data/assets/include/SDL2/SDL_keycode.h +11 -9
  34. data/assets/include/SDL2/SDL_loadso.h +1 -1
  35. data/assets/include/SDL2/SDL_locale.h +101 -0
  36. data/assets/include/SDL2/SDL_log.h +3 -3
  37. data/assets/include/SDL2/SDL_main.h +1 -1
  38. data/assets/include/SDL2/SDL_messagebox.h +6 -4
  39. data/assets/include/SDL2/SDL_metal.h +117 -0
  40. data/assets/include/SDL2/SDL_misc.h +75 -0
  41. data/assets/include/SDL2/SDL_mouse.h +1 -1
  42. data/assets/include/SDL2/SDL_mutex.h +1 -1
  43. data/assets/include/SDL2/SDL_name.h +1 -1
  44. data/assets/include/SDL2/SDL_opengl.h +1 -1
  45. data/assets/include/SDL2/SDL_opengl_glext.h +3 -0
  46. data/assets/include/SDL2/SDL_opengles.h +1 -1
  47. data/assets/include/SDL2/SDL_opengles2.h +1 -1
  48. data/assets/include/SDL2/SDL_pixels.h +25 -16
  49. data/assets/include/SDL2/SDL_platform.h +1 -1
  50. data/assets/include/SDL2/SDL_power.h +1 -1
  51. data/assets/include/SDL2/SDL_quit.h +1 -1
  52. data/assets/include/SDL2/SDL_rect.h +1 -1
  53. data/assets/include/SDL2/SDL_render.h +63 -3
  54. data/assets/include/SDL2/SDL_revision.h +2 -2
  55. data/assets/include/SDL2/SDL_rwops.h +2 -10
  56. data/assets/include/SDL2/SDL_scancode.h +1 -1
  57. data/assets/include/SDL2/SDL_sensor.h +25 -9
  58. data/assets/include/SDL2/SDL_shape.h +1 -1
  59. data/assets/include/SDL2/SDL_stdinc.h +44 -4
  60. data/assets/include/SDL2/SDL_surface.h +11 -2
  61. data/assets/include/SDL2/SDL_system.h +50 -4
  62. data/assets/include/SDL2/SDL_syswm.h +29 -2
  63. data/assets/include/SDL2/SDL_test.h +1 -1
  64. data/assets/include/SDL2/SDL_test_assert.h +1 -1
  65. data/assets/include/SDL2/SDL_test_common.h +14 -1
  66. data/assets/include/SDL2/SDL_test_compare.h +1 -1
  67. data/assets/include/SDL2/SDL_test_crc32.h +1 -1
  68. data/assets/include/SDL2/SDL_test_font.h +1 -1
  69. data/assets/include/SDL2/SDL_test_fuzzer.h +1 -1
  70. data/assets/include/SDL2/SDL_test_harness.h +1 -1
  71. data/assets/include/SDL2/SDL_test_images.h +1 -1
  72. data/assets/include/SDL2/SDL_test_log.h +1 -1
  73. data/assets/include/SDL2/SDL_test_md5.h +1 -1
  74. data/assets/include/SDL2/SDL_test_memory.h +1 -1
  75. data/assets/include/SDL2/SDL_test_random.h +1 -1
  76. data/assets/include/SDL2/SDL_thread.h +34 -11
  77. data/assets/include/SDL2/SDL_timer.h +1 -1
  78. data/assets/include/SDL2/SDL_touch.h +1 -1
  79. data/assets/include/SDL2/SDL_types.h +1 -1
  80. data/assets/include/SDL2/SDL_version.h +2 -2
  81. data/assets/include/SDL2/SDL_video.h +11 -5
  82. data/assets/include/SDL2/SDL_vulkan.h +6 -8
  83. data/assets/include/SDL2/begin_code.h +5 -9
  84. data/assets/include/SDL2/close_code.h +1 -1
  85. data/assets/macos/lib/libFLAC.a +0 -0
  86. data/assets/macos/lib/libSDL2.a +0 -0
  87. data/assets/macos/lib/libSDL2_image.a +0 -0
  88. data/assets/macos/lib/libSDL2_mixer.a +0 -0
  89. data/assets/macos/lib/libSDL2_ttf.a +0 -0
  90. data/assets/macos/lib/libfreetype.a +0 -0
  91. data/assets/macos/lib/libjpeg.a +0 -0
  92. data/assets/macos/lib/libmpg123.a +0 -0
  93. data/assets/macos/lib/libogg.a +0 -0
  94. data/assets/macos/lib/libpng16.a +0 -0
  95. data/assets/macos/lib/libtiff.a +0 -0
  96. data/assets/macos/lib/libvorbis.a +0 -0
  97. data/assets/macos/lib/libvorbisfile.a +0 -0
  98. data/assets/macos/lib/libwebp.a +0 -0
  99. data/assets/mingw/bin/SDL2.dll +0 -0
  100. data/assets/mingw/lib/libSDL2.a +0 -0
  101. data/assets/mingw/lib/libSDL2.dll.a +0 -0
  102. data/assets/mingw/lib/libSDL2_test.a +0 -0
  103. data/assets/mingw/lib/libSDL2main.a +0 -0
  104. data/ext/ruby2d/extconf.rb +1 -1
  105. data/ext/ruby2d/gl.c +17 -0
  106. data/ext/ruby2d/gl2.c +15 -0
  107. data/ext/ruby2d/gl3.c +15 -0
  108. data/ext/ruby2d/gles.c +15 -0
  109. data/ext/ruby2d/music.c +13 -0
  110. data/ext/ruby2d/ruby2d.c +306 -168
  111. data/ext/ruby2d/ruby2d.h +32 -0
  112. data/ext/ruby2d/sound.c +25 -0
  113. data/ext/ruby2d/tileset.c +30 -0
  114. data/lib/ruby2d.rb +4 -1
  115. data/lib/ruby2d/circle.rb +18 -2
  116. data/lib/ruby2d/dsl.rb +16 -9
  117. data/lib/ruby2d/entity.rb +17 -0
  118. data/lib/ruby2d/font.rb +4 -3
  119. data/lib/ruby2d/image.rb +24 -1
  120. data/lib/ruby2d/line.rb +20 -0
  121. data/lib/ruby2d/music.rb +5 -0
  122. data/lib/ruby2d/pixel.rb +17 -0
  123. data/lib/ruby2d/quad.rb +18 -0
  124. data/lib/ruby2d/rectangle.rb +11 -2
  125. data/lib/ruby2d/renderable.rb +4 -8
  126. data/lib/ruby2d/sound.rb +5 -0
  127. data/lib/ruby2d/sprite.rb +35 -3
  128. data/lib/ruby2d/square.rb +9 -0
  129. data/lib/ruby2d/text.rb +22 -1
  130. data/lib/ruby2d/tileset.rb +69 -0
  131. data/lib/ruby2d/triangle.rb +16 -0
  132. data/lib/ruby2d/version.rb +1 -1
  133. data/lib/ruby2d/window.rb +249 -22
  134. metadata +10 -3
data/lib/ruby2d/sound.rb CHANGED
@@ -21,5 +21,10 @@ module Ruby2D
21
21
  ext_play
22
22
  end
23
23
 
24
+ # Returns the length in seconds
25
+ def length
26
+ ext_length
27
+ end
28
+
24
29
  end
25
30
  end
data/lib/ruby2d/sprite.rb CHANGED
@@ -42,7 +42,7 @@ module Ruby2D
42
42
  @last_frame = 0
43
43
  @done_proc = nil
44
44
 
45
- # The sprite image size set by the native extension `ext_init()`
45
+ # The sprite image size set by the native extension `ext_init`
46
46
  @img_width = nil; @img_height = nil
47
47
 
48
48
  # Initialize the sprite
@@ -72,7 +72,7 @@ module Ruby2D
72
72
  }
73
73
 
74
74
  # Add the sprite to the window
75
- add
75
+ unless opts[:show] == false then add end
76
76
  end
77
77
 
78
78
  # Set the x coordinate
@@ -141,6 +141,7 @@ module Ruby2D
141
141
  set_frame
142
142
  restart_time
143
143
  end
144
+ self
144
145
  end
145
146
 
146
147
  # Stop the current animation and set to the default frame
@@ -221,7 +222,7 @@ module Ruby2D
221
222
  @start_time = Time.now.to_f
222
223
  end
223
224
 
224
- # Update the sprite animation, called by `Sprite#ext_render`
225
+ # Update the sprite animation, called by `render`
225
226
  def update
226
227
  if @playing
227
228
 
@@ -246,5 +247,36 @@ module Ruby2D
246
247
  end
247
248
  end
248
249
 
250
+ def draw(opts = {})
251
+ opts[:width] = opts[:width] || @flip_width
252
+ opts[:height] = opts[:height] || @flip_height
253
+ opts[:rotate] = opts[:rotate] || @rotate
254
+ opts[:clip_x] = opts[:clip_x] || @clip_x
255
+ opts[:clip_y] = opts[:clip_y] || @clip_y
256
+ opts[:clip_width] = opts[:clip_width] || @clip_width
257
+ opts[:clip_height] = opts[:clip_height] || @clip_height
258
+ unless opts[:color]
259
+ opts[:color] = [1.0, 1.0, 1.0, 1.0]
260
+ end
261
+
262
+ self.class.ext_draw([
263
+ self, opts[:x], opts[:y], opts[:width], opts[:height], opts[:rotate],
264
+ opts[:clip_x], opts[:clip_y], opts[:clip_width], opts[:clip_height],
265
+ opts[:color][0], opts[:color][1], opts[:color][2], opts[:color][3]
266
+ ])
267
+ end
268
+
269
+ private
270
+
271
+ def render
272
+ update
273
+ self.class.ext_draw([
274
+ self, @flip_x, @flip_y, @flip_width, @flip_height, @rotate,
275
+ @clip_x, @clip_y, @clip_width, @clip_height,
276
+ @color.r, @color.g, @color.b, @color.a
277
+ ])
278
+ end
279
+
280
+
249
281
  end
250
282
  end
data/lib/ruby2d/square.rb CHANGED
@@ -21,6 +21,15 @@ module Ruby2D
21
21
  self.width = self.height = @size = s
22
22
  end
23
23
 
24
+ def self.draw(opts = {})
25
+ ext_draw([
26
+ opts[:x] , opts[:y] , opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
27
+ opts[:x] + opts[:size], opts[:y] , opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
28
+ opts[:x] + opts[:size], opts[:y] + opts[:size], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3],
29
+ opts[:x] , opts[:y] + opts[:size], opts[:color][3][0], opts[:color][3][1], opts[:color][3][2], opts[:color][3][3]
30
+ ])
31
+ end
32
+
24
33
  # Make the inherited width and height attribute accessors private
25
34
  private :width=, :height=
26
35
 
data/lib/ruby2d/text.rb CHANGED
@@ -23,7 +23,7 @@ module Ruby2D
23
23
  unless ext_init
24
24
  raise Error, "Text `#{@text}` cannot be created"
25
25
  end
26
- add
26
+ unless opts[:show] == false then add end
27
27
  end
28
28
 
29
29
  def text=(msg)
@@ -31,5 +31,26 @@ module Ruby2D
31
31
  ext_set(@text)
32
32
  end
33
33
 
34
+ def draw(opts = {})
35
+ opts[:rotate] = opts[:rotate] || @rotate
36
+ unless opts[:color]
37
+ opts[:color] = [1.0, 1.0, 1.0, 1.0]
38
+ end
39
+
40
+ self.class.ext_draw([
41
+ self, opts[:x], opts[:y], opts[:rotate],
42
+ opts[:color][0], opts[:color][1], opts[:color][2], opts[:color][3]
43
+ ])
44
+ end
45
+
46
+ private
47
+
48
+ def render
49
+ self.class.ext_draw([
50
+ self, @x, @y, @rotate,
51
+ @color.r, @color.g, @color.b, @color.a
52
+ ])
53
+ end
54
+
34
55
  end
35
56
  end
@@ -0,0 +1,69 @@
1
+ # Ruby2D::Tileset
2
+
3
+ module Ruby2D
4
+ class Tileset
5
+ include Renderable
6
+
7
+ def initialize(path, opts = {})
8
+ unless File.exist? path
9
+ raise Error, "Cannot find tileset image file `#{path}`"
10
+ end
11
+ @path = path
12
+ @tiles = []
13
+ @defined_tiles = {}
14
+ @padding = opts[:padding] || 0
15
+ @spacing = opts[:spacing] || 0
16
+ @tile_width = opts[:tile_width]
17
+ @tile_height = opts[:tile_height]
18
+
19
+ unless ext_init(@path)
20
+ raise Error, "Tileset `#{@path}` cannot be created"
21
+ end
22
+
23
+ unless opts[:show] == false then add end
24
+ end
25
+
26
+ def define_tile(name, x, y)
27
+ @defined_tiles[name] = { x: x, y: y }
28
+ end
29
+
30
+ def set_tile(name, coordinates)
31
+ tile = @defined_tiles.fetch(name)
32
+
33
+ coordinates.each do |coordinate|
34
+ @tiles.push({
35
+ tile_x: tile.fetch(:x),
36
+ tile_y: tile.fetch(:y),
37
+ x: coordinate.fetch(:x),
38
+ y: coordinate.fetch(:y)
39
+ })
40
+ end
41
+ end
42
+
43
+ def clear_tiles
44
+ @tiles = []
45
+ end
46
+ end
47
+
48
+ def draw(opts = {})
49
+ render(opts)
50
+ end
51
+
52
+ private
53
+
54
+ def render(opts = {})
55
+ opts[:tile_width] = opts[:tile_width] || @tile_width
56
+ opts[:tile_height] = opts[:tile_height] || @tile_height
57
+ opts[:padding] = opts[:padding] || @padding
58
+ opts[:spacing] = opts[:spacing] || @spacing
59
+
60
+ @tiles.each do |tile|
61
+ self.class.ext_draw(
62
+ [
63
+ self, opts[:tile_width], opts[:tile_height], opts[:padding], opts[:spacing],
64
+ tile.fetch(:tile_x), tile.fetch(:tile_y), tile.fetch(:x),
65
+ tile.fetch(:y)
66
+ ])
67
+ end
68
+ end
69
+ end
@@ -38,8 +38,24 @@ module Ruby2D
38
38
  questioned_area <= self_area
39
39
  end
40
40
 
41
+ def self.draw(opts = {})
42
+ ext_draw([
43
+ opts[:x1], opts[:y1], opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
44
+ opts[:x2], opts[:y2], opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
45
+ opts[:x3], opts[:y3], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3]
46
+ ])
47
+ end
48
+
41
49
  private
42
50
 
51
+ def render
52
+ self.class.ext_draw([
53
+ @x1, @y1, @c1.r, @c1.g, @c1.b, @c1.a,
54
+ @x2, @y2, @c2.r, @c2.g, @c2.b, @c2.a,
55
+ @x3, @y3, @c3.r, @c3.g, @c3.b, @c3.a
56
+ ])
57
+ end
58
+
43
59
  def triangle_area(x1, y1, x2, y2, x3, y3)
44
60
  (x1*y2 + x2*y3 + x3*y1 - x3*y2 - x1*y3 - x2*y1).abs / 2
45
61
  end
@@ -1,5 +1,5 @@
1
1
  # Ruby2D::VERSION
2
2
 
3
3
  module Ruby2D
4
- VERSION = '0.9.5'
4
+ VERSION = '0.10.0'
5
5
  end
data/lib/ruby2d/window.rb CHANGED
@@ -6,20 +6,17 @@ module Ruby2D
6
6
  class Window
7
7
 
8
8
  # Event structures
9
- EventDescriptor = Struct.new(:type, :id)
10
- MouseEvent = Struct.new(:type, :button, :direction, :x, :y, :delta_x, :delta_y)
11
- KeyEvent = Struct.new(:type, :key)
9
+ EventDescriptor = Struct.new(:type, :id)
10
+ MouseEvent = Struct.new(:type, :button, :direction, :x, :y, :delta_x, :delta_y)
11
+ KeyEvent = Struct.new(:type, :key)
12
12
  ControllerEvent = Struct.new(:which, :type, :axis, :value, :button)
13
13
  ControllerAxisEvent = Struct.new(:which, :axis, :value)
14
14
  ControllerButtonEvent = Struct.new(:which, :button)
15
15
 
16
16
  def initialize(args = {})
17
17
 
18
- # This window instance, stored so it can be called by the class methods
19
- @@window = self
20
-
21
18
  # Title of the window
22
- @title = args[:title] || "Ruby 2D"
19
+ @title = args[:title] || "Ruby 2D"
23
20
 
24
21
  # Window background color
25
22
  @background = Color.new([0.0, 0.0, 0.0, 1.0])
@@ -51,14 +48,39 @@ module Ruby2D
51
48
  # Vertical synchronization, set to prevent screen tearing (recommended)
52
49
  @vsync = args[:vsync] || true
53
50
 
51
+ # Renderable objects currently in the window, like a linear scene graph
52
+ @objects = []
53
+
54
+ # Entities currently in the window
55
+ @entities = []
56
+
54
57
  # Mouse X and Y position in the window
55
58
  @mouse_x, @mouse_y = 0, 0
56
59
 
57
60
  # Controller axis and button mappings file
58
61
  @controller_mappings = File.expand_path('~') + "/.ruby2d/controllers.txt"
59
62
 
60
- # Renderable objects currently in the window, like a linear scene graph
61
- @objects = []
63
+ # Event stores for class pattern
64
+ @keys_down = []
65
+ @keys_held = []
66
+ @keys_up = []
67
+ @mouse_buttons_down = []
68
+ @mouse_buttons_up = []
69
+ @mouse_scroll_event = false
70
+ @mouse_scroll_direction = nil
71
+ @mouse_scroll_delta_x = 0
72
+ @mouse_scroll_delta_y = 0
73
+ @mouse_move_event = false
74
+ @mouse_move_delta_x = 0
75
+ @mouse_move_delta_y = 0
76
+ @controller_id = nil
77
+ @controller_axes_moved = []
78
+ @controller_axis_left_x = 0
79
+ @controller_axis_left_y = 0
80
+ @controller_axis_right_x = 0
81
+ @controller_axis_right_y = 0
82
+ @controller_buttons_down = []
83
+ @controller_buttons_up = []
62
84
 
63
85
  # Unique ID for the input event being registered
64
86
  @event_key = 0
@@ -83,6 +105,14 @@ module Ruby2D
83
105
  # The window update block
84
106
  @update_proc = Proc.new {}
85
107
 
108
+ # The window render block
109
+ @render_proc = Proc.new {}
110
+
111
+ # Detect if window is being used through the DSL or as a class instance
112
+ unless method(:update).parameters.empty? || method(:render).parameters.empty?
113
+ @using_dsl = true
114
+ end
115
+
86
116
  # Whether diagnostic messages should be printed
87
117
  @diagnostics = false
88
118
 
@@ -118,43 +148,47 @@ module Ruby2D
118
148
  def screenshot(opts = nil); get(:screenshot, opts) end
119
149
 
120
150
  def get(sym, opts = nil)
121
- @@window.get(sym, opts)
151
+ DSL.window.get(sym, opts)
122
152
  end
123
153
 
124
154
  def set(opts)
125
- @@window.set(opts)
155
+ DSL.window.set(opts)
126
156
  end
127
157
 
128
158
  def on(event, &proc)
129
- @@window.on(event, &proc)
159
+ DSL.window.on(event, &proc)
130
160
  end
131
161
 
132
162
  def off(event_descriptor)
133
- @@window.off(event_descriptor)
163
+ DSL.window.off(event_descriptor)
134
164
  end
135
165
 
136
166
  def add(o)
137
- @@window.add(o)
167
+ DSL.window.add(o)
138
168
  end
139
169
 
140
170
  def remove(o)
141
- @@window.remove(o)
171
+ DSL.window.remove(o)
142
172
  end
143
173
 
144
174
  def clear
145
- @@window.clear
175
+ DSL.window.clear
146
176
  end
147
177
 
148
178
  def update(&proc)
149
- @@window.update(&proc)
179
+ DSL.window.update(&proc)
180
+ end
181
+
182
+ def render(&proc)
183
+ DSL.window.render(&proc)
150
184
  end
151
185
 
152
186
  def show
153
- @@window.show
187
+ DSL.window.show
154
188
  end
155
189
 
156
190
  def close
157
- @@window.close
191
+ DSL.window.close
158
192
  end
159
193
  end
160
194
 
@@ -219,6 +253,8 @@ module Ruby2D
219
253
  case o
220
254
  when nil
221
255
  raise Error, "Cannot add '#{o.class}' to window!"
256
+ when Entity
257
+ @entities.push(o)
222
258
  when Array
223
259
  o.each { |x| add_object(x) }
224
260
  else
@@ -232,8 +268,9 @@ module Ruby2D
232
268
  raise Error, "Cannot remove '#{o.class}' from window!"
233
269
  end
234
270
 
235
- if i = @objects.index(o)
236
- @objects.delete_at(i)
271
+ collection = o.class.ancestors.include?(Ruby2D::Entity) ? @entities : @objects
272
+ if i = collection.index(o)
273
+ collection.delete_at(i)
237
274
  true
238
275
  else
239
276
  false
@@ -243,14 +280,21 @@ module Ruby2D
243
280
  # Clear all objects from the window
244
281
  def clear
245
282
  @objects.clear
283
+ @entities.clear
246
284
  end
247
285
 
248
- # Set an update callback
286
+ # Set the update callback
249
287
  def update(&proc)
250
288
  @update_proc = proc
251
289
  true
252
290
  end
253
291
 
292
+ # Set the render callback
293
+ def render(&proc)
294
+ @render_proc = proc
295
+ true
296
+ end
297
+
254
298
  # Generate a new event key (ID)
255
299
  def new_event_key
256
300
  @event_key = @event_key.next
@@ -271,6 +315,21 @@ module Ruby2D
271
315
  @events[event_descriptor.type].delete(event_descriptor.id)
272
316
  end
273
317
 
318
+ # Key down event method for class pattern
319
+ def key_down(key)
320
+ @keys_down.include? key
321
+ end
322
+
323
+ # Key held event method for class pattern
324
+ def key_held(key)
325
+ @keys_held.include? key
326
+ end
327
+
328
+ # Key up event method for class pattern
329
+ def key_up(key)
330
+ @keys_up.include? key
331
+ end
332
+
274
333
  # Key callback method, called by the native and web extentions
275
334
  def key_callback(type, key)
276
335
  key = key.downcase
@@ -283,22 +342,66 @@ module Ruby2D
283
342
  case type
284
343
  # When key is pressed, fired once
285
344
  when :down
345
+ # For class pattern
346
+ unless @using_dsl
347
+ unless @keys_down.include? key
348
+ @keys_down << key
349
+ end
350
+ end
351
+
352
+ # Call event handler
286
353
  @events[:key_down].each do |id, e|
287
354
  e.call(KeyEvent.new(type, key))
288
355
  end
289
356
  # When key is being held down, fired every frame
290
357
  when :held
358
+ # For class pattern
359
+ unless @using_dsl
360
+ unless @keys_held.include? key
361
+ @keys_held << key
362
+ end
363
+ end
364
+
365
+ # Call event handler
291
366
  @events[:key_held].each do |id, e|
292
367
  e.call(KeyEvent.new(type, key))
293
368
  end
294
369
  # When key released, fired once
295
370
  when :up
371
+ # For class pattern
372
+ unless @using_dsl
373
+ unless @keys_up.include? key
374
+ @keys_up << key
375
+ end
376
+ end
377
+
378
+ # Call event handler
296
379
  @events[:key_up].each do |id, e|
297
380
  e.call(KeyEvent.new(type, key))
298
381
  end
299
382
  end
300
383
  end
301
384
 
385
+ # Mouse down event method for class pattern
386
+ def mouse_down(btn)
387
+ @mouse_buttons_down.include? btn
388
+ end
389
+
390
+ # Mouse up event method for class pattern
391
+ def mouse_up(btn)
392
+ @mouse_buttons_up.include? btn
393
+ end
394
+
395
+ # Mouse scroll event method for class pattern
396
+ def mouse_scroll
397
+ @mouse_scroll_event
398
+ end
399
+
400
+ # Mouse move event method for class pattern
401
+ def mouse_move
402
+ @mouse_move_event
403
+ end
404
+
302
405
  # Mouse callback method, called by the native and web extentions
303
406
  def mouse_callback(type, button, direction, x, y, delta_x, delta_y)
304
407
  # All mouse events
@@ -309,21 +412,54 @@ module Ruby2D
309
412
  case type
310
413
  # When mouse button pressed
311
414
  when :down
415
+ # For class pattern
416
+ unless @using_dsl
417
+ unless @mouse_buttons_down.include? button
418
+ @mouse_buttons_down << button
419
+ end
420
+ end
421
+
422
+ # Call event handler
312
423
  @events[:mouse_down].each do |id, e|
313
424
  e.call(MouseEvent.new(type, button, nil, x, y, nil, nil))
314
425
  end
315
426
  # When mouse button released
316
427
  when :up
428
+ # For class pattern
429
+ unless @using_dsl
430
+ unless @mouse_buttons_up.include? button
431
+ @mouse_buttons_up << button
432
+ end
433
+ end
434
+
435
+ # Call event handler
317
436
  @events[:mouse_up].each do |id, e|
318
437
  e.call(MouseEvent.new(type, button, nil, x, y, nil, nil))
319
438
  end
320
439
  # When mouse motion / movement
321
440
  when :scroll
441
+ # For class pattern
442
+ unless @using_dsl
443
+ @mouse_scroll_event = true
444
+ @mouse_scroll_direction = direction
445
+ @mouse_scroll_delta_x = delta_x
446
+ @mouse_scroll_delta_y = delta_y
447
+ end
448
+
449
+ # Call event handler
322
450
  @events[:mouse_scroll].each do |id, e|
323
451
  e.call(MouseEvent.new(type, nil, direction, nil, nil, delta_x, delta_y))
324
452
  end
325
453
  # When mouse scrolling, wheel or trackpad
326
454
  when :move
455
+ # For class pattern
456
+ unless @using_dsl
457
+ @mouse_move_event = true
458
+ @mouse_move_delta_x = delta_x
459
+ @mouse_move_delta_y = delta_y
460
+ end
461
+
462
+ # Call event handler
327
463
  @events[:mouse_move].each do |id, e|
328
464
  e.call(MouseEvent.new(type, nil, nil, x, y, delta_x, delta_y))
329
465
  end
@@ -337,6 +473,21 @@ module Ruby2D
337
473
  end
338
474
  end
339
475
 
476
+ # Controller axis event method for class pattern
477
+ def controller_axis(axis)
478
+ @controller_axes_moved.include? axis
479
+ end
480
+
481
+ # Controller button down event method for class pattern
482
+ def controller_button_down(btn)
483
+ @controller_buttons_down.include? btn
484
+ end
485
+
486
+ # Controller button up event method for class pattern
487
+ def controller_button_up(btn)
488
+ @controller_buttons_up.include? btn
489
+ end
490
+
340
491
  # Controller callback method, called by the native and web extentions
341
492
  def controller_callback(which, type, axis, value, button)
342
493
  # All controller events
@@ -347,16 +498,56 @@ module Ruby2D
347
498
  case type
348
499
  # When controller axis motion, like analog sticks
349
500
  when :axis
501
+
502
+ # For class pattern
503
+ unless @using_dsl
504
+ @controller_id = which
505
+
506
+ unless @controller_axes_moved.include? axis
507
+ @controller_axes_moved << axis
508
+ end
509
+
510
+ case axis
511
+ when :left_x
512
+ @controller_axis_left_x = value
513
+ when :left_y
514
+ @controller_axis_left_y = value
515
+ when :right_x
516
+ @controller_axis_right_x = value
517
+ when :right_y
518
+ @controller_axis_right_y = value
519
+ end
520
+ end
521
+
522
+ # Call event handler
350
523
  @events[:controller_axis].each do |id, e|
351
524
  e.call(ControllerAxisEvent.new(which, axis, value))
352
525
  end
353
526
  # When controller button is pressed
354
527
  when :button_down
528
+ # For class pattern
529
+ unless @using_dsl
530
+ @controller_id = which
531
+ unless @controller_buttons_down.include? button
532
+ @controller_buttons_down << button
533
+ end
534
+ end
535
+
536
+ # Call event handler
355
537
  @events[:controller_button_down].each do |id, e|
356
538
  e.call(ControllerButtonEvent.new(which, button))
357
539
  end
358
540
  # When controller button is released
359
541
  when :button_up
542
+ # For class pattern
543
+ unless @using_dsl
544
+ @controller_id = which
545
+ unless @controller_buttons_up.include? button
546
+ @controller_buttons_up << button
547
+ end
548
+ end
549
+
550
+ # Call event handler
360
551
  @events[:controller_button_up].each do |id, e|
361
552
  e.call(ControllerButtonEvent.new(which, button))
362
553
  end
@@ -365,8 +556,17 @@ module Ruby2D
365
556
 
366
557
  # Update callback method, called by the native and web extentions
367
558
  def update_callback
559
+ unless @using_dsl
560
+ update
561
+ end
562
+
368
563
  @update_proc.call
369
564
 
565
+ # Run update method on all entities
566
+ @entities.each do |e|
567
+ e.update
568
+ end
569
+
370
570
  # Accept and eval commands if in console mode
371
571
  if @console
372
572
  if STDIN.ready?
@@ -385,6 +585,33 @@ module Ruby2D
385
585
  end
386
586
  end
387
587
 
588
+ # Clear inputs if using class pattern
589
+ unless @using_dsl
590
+ @keys_down.clear
591
+ @keys_held.clear
592
+ @keys_up.clear
593
+ @mouse_buttons_down.clear
594
+ @mouse_buttons_up.clear
595
+ @mouse_scroll_event = false
596
+ @mouse_move_event = false
597
+ @controller_axes_moved.clear
598
+ @controller_buttons_down.clear
599
+ @controller_buttons_up.clear
600
+ end
601
+ end
602
+
603
+ # Render callback method, called by the native and web extentions
604
+ def render_callback
605
+ unless @using_dsl
606
+ render
607
+ end
608
+
609
+ @render_proc.call
610
+
611
+ # Run render method on all entities
612
+ @entities.each do |e|
613
+ e.render
614
+ end
388
615
  end
389
616
 
390
617
  # Show the window