ruby2d 0.11.3 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (221) hide show
  1. checksums.yaml +4 -4
  2. data/assets/include/GLES2/gl2.h +656 -0
  3. data/assets/include/GLES2/gl2ext.h +3949 -0
  4. data/assets/include/GLES2/gl2ext_angle.h +701 -0
  5. data/assets/include/GLES2/gl2platform.h +27 -0
  6. data/assets/include/GLES3/gl3.h +1192 -0
  7. data/assets/include/GLES3/gl31.h +1507 -0
  8. data/assets/include/GLES3/gl32.h +1808 -0
  9. data/assets/include/GLES3/gl3platform.h +27 -0
  10. data/assets/include/KHR/khrplatform.h +290 -0
  11. data/assets/include/SDL2/SDL.h +1 -0
  12. data/assets/include/SDL2/SDL_assert.h +4 -2
  13. data/assets/include/SDL2/SDL_atomic.h +20 -0
  14. data/assets/include/SDL2/SDL_audio.h +40 -4
  15. data/assets/include/SDL2/SDL_blendmode.h +4 -6
  16. data/assets/include/SDL2/SDL_clipboard.h +47 -0
  17. data/assets/include/SDL2/SDL_config.h +6 -2
  18. data/assets/include/SDL2/SDL_config_android.h +2 -0
  19. data/assets/include/SDL2/SDL_config_emscripten.h +2 -0
  20. data/assets/include/SDL2/SDL_config_iphoneos.h +3 -1
  21. data/assets/include/SDL2/SDL_config_macosx.h +3 -6
  22. data/assets/include/SDL2/SDL_config_minimal.h +18 -11
  23. data/assets/include/SDL2/SDL_config_ngage.h +89 -0
  24. data/assets/include/SDL2/SDL_config_os2.h +5 -3
  25. data/assets/include/SDL2/SDL_config_pandora.h +1 -0
  26. data/assets/include/SDL2/SDL_config_windows.h +71 -45
  27. data/assets/include/SDL2/SDL_config_wingdk.h +253 -0
  28. data/assets/include/SDL2/SDL_config_winrt.h +11 -49
  29. data/assets/include/SDL2/SDL_config_xbox.h +235 -0
  30. data/assets/include/SDL2/SDL_cpuinfo.h +39 -4
  31. data/assets/include/SDL2/SDL_egl.h +59 -9
  32. data/assets/include/SDL2/SDL_endian.h +34 -3
  33. data/assets/include/SDL2/SDL_events.h +32 -1
  34. data/assets/include/SDL2/SDL_filesystem.h +5 -1
  35. data/assets/include/SDL2/SDL_gamecontroller.h +78 -5
  36. data/assets/include/SDL2/SDL_guid.h +100 -0
  37. data/assets/include/SDL2/SDL_hints.h +645 -43
  38. data/assets/include/SDL2/SDL_image.h +2045 -33
  39. data/assets/include/SDL2/SDL_joystick.h +127 -7
  40. data/assets/include/SDL2/SDL_keyboard.h +38 -1
  41. data/assets/include/SDL2/SDL_keycode.h +6 -1
  42. data/assets/include/SDL2/SDL_log.h +2 -2
  43. data/assets/include/SDL2/SDL_main.h +42 -2
  44. data/assets/include/SDL2/SDL_metal.h +2 -1
  45. data/assets/include/SDL2/SDL_mixer.h +2529 -396
  46. data/assets/include/SDL2/SDL_mouse.h +12 -1
  47. data/assets/include/SDL2/SDL_opengl.h +0 -51
  48. data/assets/include/SDL2/SDL_opengl_glext.h +2260 -231
  49. data/assets/include/SDL2/SDL_opengles2_gl2.h +374 -339
  50. data/assets/include/SDL2/SDL_opengles2_gl2ext.h +3479 -1496
  51. data/assets/include/SDL2/SDL_opengles2_gl2platform.h +6 -9
  52. data/assets/include/SDL2/SDL_opengles2_khrplatform.h +43 -14
  53. data/assets/include/SDL2/SDL_platform.h +32 -6
  54. data/assets/include/SDL2/SDL_rect.h +154 -2
  55. data/assets/include/SDL2/SDL_render.h +46 -17
  56. data/assets/include/SDL2/SDL_revision.h +4 -0
  57. data/assets/include/SDL2/SDL_rwops.h +1 -15
  58. data/assets/include/SDL2/SDL_scancode.h +46 -21
  59. data/assets/include/SDL2/SDL_sensor.h +24 -3
  60. data/assets/include/SDL2/SDL_stdinc.h +119 -8
  61. data/assets/include/SDL2/SDL_surface.h +3 -1
  62. data/assets/include/SDL2/SDL_system.h +66 -6
  63. data/assets/include/SDL2/SDL_syswm.h +2 -0
  64. data/assets/include/SDL2/SDL_test_common.h +1 -0
  65. data/assets/include/SDL2/SDL_test_font.h +90 -3
  66. data/assets/include/SDL2/SDL_thread.h +3 -3
  67. data/assets/include/SDL2/SDL_touch.h +8 -0
  68. data/assets/include/SDL2/SDL_ttf.h +2084 -155
  69. data/assets/include/SDL2/SDL_version.h +19 -3
  70. data/assets/include/SDL2/SDL_video.h +71 -9
  71. data/assets/include/SDL2/begin_code.h +4 -4
  72. data/assets/include/mrbconf.h +15 -17
  73. data/assets/include/mruby/array.h +8 -21
  74. data/assets/include/mruby/boxing_nan.h +115 -86
  75. data/assets/include/mruby/boxing_word.h +104 -78
  76. data/assets/include/mruby/common.h +6 -0
  77. data/assets/include/mruby/compile.h +3 -4
  78. data/assets/include/mruby/debug.h +4 -2
  79. data/assets/include/mruby/dump.h +5 -2
  80. data/assets/include/mruby/error.h +12 -2
  81. data/assets/include/mruby/gc.h +2 -0
  82. data/assets/include/mruby/hash.h +1 -3
  83. data/assets/include/mruby/irep.h +4 -4
  84. data/assets/include/mruby/numeric.h +21 -13
  85. data/assets/include/mruby/opcode.h +30 -0
  86. data/assets/include/mruby/ops.h +99 -101
  87. data/assets/include/mruby/presym/scanning.h +15 -9
  88. data/assets/include/mruby/proc.h +4 -2
  89. data/assets/include/mruby/string.h +3 -24
  90. data/assets/include/mruby/value.h +80 -40
  91. data/assets/include/mruby/variable.h +0 -15
  92. data/assets/include/mruby/version.h +5 -5
  93. data/assets/include/mruby.h +86 -16
  94. data/assets/macos/universal/bin/mrbc +0 -0
  95. data/assets/macos/universal/lib/libFLAC.a +0 -0
  96. data/assets/macos/universal/lib/libSDL2.a +0 -0
  97. data/assets/macos/universal/lib/libSDL2_image.a +0 -0
  98. data/assets/macos/universal/lib/libSDL2_mixer.a +0 -0
  99. data/assets/macos/universal/lib/libSDL2_ttf.a +0 -0
  100. data/assets/macos/universal/lib/libavif.a +0 -0
  101. data/assets/macos/universal/lib/libbrotlicommon-static.a +0 -0
  102. data/assets/macos/universal/lib/libbrotlidec-static.a +0 -0
  103. data/assets/macos/universal/lib/libfreetype.a +0 -0
  104. data/assets/macos/universal/lib/libgraphite2.a +0 -0
  105. data/assets/macos/universal/lib/libharfbuzz.a +0 -0
  106. data/assets/macos/universal/lib/libhwy.a +0 -0
  107. data/assets/macos/universal/lib/libjpeg.a +0 -0
  108. data/assets/macos/universal/lib/libjxl.a +0 -0
  109. data/assets/macos/universal/lib/libmodplug.a +0 -0
  110. data/assets/macos/universal/lib/libmpg123.a +0 -0
  111. data/assets/macos/universal/lib/libmruby.a +0 -0
  112. data/assets/macos/universal/lib/libogg.a +0 -0
  113. data/assets/macos/universal/lib/libpng.a +0 -0
  114. data/assets/macos/universal/lib/libtiff.a +0 -0
  115. data/assets/macos/universal/lib/libvorbis.a +0 -0
  116. data/assets/macos/universal/lib/libvorbisfile.a +0 -0
  117. data/assets/macos/universal/lib/libwebp.a +0 -0
  118. data/assets/macos/universal/lib/libzstd.a +0 -0
  119. data/assets/wasm/template.html +2 -1
  120. data/assets/windows/mingw-w64-ucrt-x86_64/bin/mrbc.exe +0 -0
  121. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libFLAC.a +0 -0
  122. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libLerc.a +0 -0
  123. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libSDL2.a +0 -0
  124. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libSDL2_image.a +0 -0
  125. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libSDL2_mixer.a +0 -0
  126. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libSDL2_ttf.a +0 -0
  127. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libbrotlicommon.a +0 -0
  128. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libbrotlidec.a +0 -0
  129. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libdeflate.a +0 -0
  130. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libfreetype.a +0 -0
  131. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libglew32.a +0 -0
  132. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libharfbuzz.a +0 -0
  133. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libhwy.a +0 -0
  134. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libjpeg.a +0 -0
  135. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libjxl.a +0 -0
  136. data/assets/windows/mingw-w64-ucrt-x86_64/lib/liblzma.a +0 -0
  137. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libmpg123.a +0 -0
  138. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libmruby.a +0 -0
  139. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libopus.a +0 -0
  140. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libpng.a +0 -0
  141. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libsndfile.a +0 -0
  142. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libssp.a +0 -0
  143. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libstdc++.a +0 -0
  144. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libtiff.a +0 -0
  145. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libwebp.a +0 -0
  146. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libz.a +0 -0
  147. data/assets/windows/mingw-w64-x86_64/bin/mrbc.exe +0 -0
  148. data/assets/windows/mingw-w64-x86_64/lib/libFLAC.a +0 -0
  149. data/assets/windows/mingw-w64-x86_64/lib/libLerc.a +0 -0
  150. data/assets/windows/mingw-w64-x86_64/lib/libSDL2.a +0 -0
  151. data/assets/windows/mingw-w64-x86_64/lib/libSDL2_image.a +0 -0
  152. data/assets/windows/mingw-w64-x86_64/lib/libSDL2_mixer.a +0 -0
  153. data/assets/windows/mingw-w64-x86_64/lib/libSDL2_ttf.a +0 -0
  154. data/assets/windows/mingw-w64-x86_64/lib/libbrotlicommon.a +0 -0
  155. data/assets/windows/mingw-w64-x86_64/lib/libbrotlidec.a +0 -0
  156. data/assets/windows/mingw-w64-x86_64/lib/libdeflate.a +0 -0
  157. data/assets/windows/mingw-w64-x86_64/lib/libfreetype.a +0 -0
  158. data/assets/windows/mingw-w64-x86_64/lib/libglew32.a +0 -0
  159. data/assets/windows/mingw-w64-x86_64/lib/libharfbuzz.a +0 -0
  160. data/assets/windows/mingw-w64-x86_64/lib/libhwy.a +0 -0
  161. data/assets/windows/mingw-w64-x86_64/lib/libjpeg.a +0 -0
  162. data/assets/windows/mingw-w64-x86_64/lib/libjxl.a +0 -0
  163. data/assets/windows/mingw-w64-x86_64/lib/liblzma.a +0 -0
  164. data/assets/windows/mingw-w64-x86_64/lib/libmpg123.a +0 -0
  165. data/assets/windows/mingw-w64-x86_64/lib/libmruby.a +0 -0
  166. data/assets/windows/mingw-w64-x86_64/lib/libopus.a +0 -0
  167. data/assets/windows/mingw-w64-x86_64/lib/libpng.a +0 -0
  168. data/assets/windows/mingw-w64-x86_64/lib/libsndfile.a +0 -0
  169. data/assets/windows/mingw-w64-x86_64/lib/libssp.a +0 -0
  170. data/assets/windows/mingw-w64-x86_64/lib/libstdc++.a +0 -0
  171. data/assets/windows/mingw-w64-x86_64/lib/libtiff.a +0 -0
  172. data/assets/windows/mingw-w64-x86_64/lib/libwebp.a +0 -0
  173. data/assets/windows/mingw-w64-x86_64/lib/libz.a +0 -0
  174. data/assets/windows/mingw-w64-x86_64/lib/libzstd.a +0 -0
  175. data/ext/ruby2d/canvas.c +540 -0
  176. data/ext/ruby2d/extconf.rb +33 -20
  177. data/ext/ruby2d/gl.c +3 -3
  178. data/ext/ruby2d/image.c +7 -7
  179. data/ext/ruby2d/ruby2d.c +741 -24
  180. data/ext/ruby2d/ruby2d.h +66 -3
  181. data/ext/ruby2d/sound.c +16 -2
  182. data/lib/ruby2d/canvas.rb +315 -0
  183. data/lib/ruby2d/circle.rb +30 -15
  184. data/lib/ruby2d/cli/build.rb +5 -3
  185. data/lib/ruby2d/cli/enable_console.rb +3 -1
  186. data/lib/ruby2d/color.rb +133 -77
  187. data/lib/ruby2d/core.rb +32 -0
  188. data/lib/ruby2d/dsl.rb +38 -32
  189. data/lib/ruby2d/exceptions.rb +2 -1
  190. data/lib/ruby2d/font.rb +97 -62
  191. data/lib/ruby2d/image.rb +48 -27
  192. data/lib/ruby2d/line.rb +84 -45
  193. data/lib/ruby2d/music.rb +33 -23
  194. data/lib/ruby2d/pixel.rb +10 -9
  195. data/lib/ruby2d/pixmap.rb +39 -0
  196. data/lib/ruby2d/pixmap_atlas.rb +56 -0
  197. data/lib/ruby2d/quad.rb +98 -49
  198. data/lib/ruby2d/rectangle.rb +35 -36
  199. data/lib/ruby2d/renderable.rb +7 -6
  200. data/lib/ruby2d/sound.rb +23 -17
  201. data/lib/ruby2d/sprite.rb +181 -140
  202. data/lib/ruby2d/square.rb +21 -20
  203. data/lib/ruby2d/text.rb +47 -19
  204. data/lib/ruby2d/texture.rb +13 -3
  205. data/lib/ruby2d/tileset.rb +97 -44
  206. data/lib/ruby2d/triangle.rb +91 -42
  207. data/lib/ruby2d/version.rb +3 -1
  208. data/lib/ruby2d/vertices.rb +81 -45
  209. data/lib/ruby2d/window.rb +508 -371
  210. data/lib/ruby2d.rb +11 -29
  211. metadata +51 -16
  212. data/assets/include/SDL2/SDL_config_psp.h +0 -165
  213. data/assets/include/SDL2/SDL_config_wiz.h +0 -154
  214. data/assets/include/glew.h +0 -23686
  215. data/assets/macos/universal/lib/libpng16.a +0 -0
  216. data/assets/windows/glew/README.md +0 -10
  217. data/assets/windows/glew/glew.h +0 -23686
  218. data/assets/windows/glew/libglew32.a +0 -0
  219. data/assets/windows/mingw-w64-ucrt-x86_64/lib/libpng16.a +0 -0
  220. data/assets/windows/mingw-w64-x86_64/lib/libpng16.a +0 -0
  221. data/lib/ruby2d/entity.rb +0 -17
@@ -1,18 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Ruby2D::Rectangle
2
4
 
3
5
  module Ruby2D
6
+ # A rectangle
4
7
  class Rectangle < Quad
5
-
6
- def initialize(opts = {})
7
- @x = opts[:x] || 0
8
- @y = opts[:y] || 0
9
- @z = opts[:z] || 0
10
- @width = opts[:width] || 200
11
- @height = opts[:height] || 100
12
- self.color = opts[:color] || 'white'
13
- self.color.opacity = opts[:opacity] if opts[:opacity]
14
- update_coords(@x, @y, @width, @height)
15
- add
8
+ # Create an rectangle
9
+ # @param [Numeric] x
10
+ # @param [Numeric] y
11
+ # @param [Numeric] width
12
+ # @param [Numeric] height
13
+ # @param [Numeric] z
14
+ # @param [String, Array] color A single colour or an array of exactly 4 colours
15
+ # @param [Numeric] opacity Opacity of the image when rendering
16
+ # @raise [ArgumentError] if an array of colours does not have 4 entries
17
+ def initialize(x: 0, y: 0, width: 200, height: 100, z: 0, color: nil, colour: nil, opacity: nil)
18
+ @width = width
19
+ @height = height
20
+ super(x1: @x = x, y1: @y = y,
21
+ x2: x + width, y2: y,
22
+ x3: x + width, y3: y + height,
23
+ x4: x, y4: y + height, z: z, color: color, colour: colour, opacity: opacity)
16
24
  end
17
25
 
18
26
  def x=(x)
@@ -29,37 +37,28 @@ module Ruby2D
29
37
  @y4 = y + @height
30
38
  end
31
39
 
32
- def width=(w)
33
- @width = w
34
- update_coords(@x, @y, w, @height)
40
+ def width=(width)
41
+ @width = width
42
+ @x2 = @x1 + width
43
+ @x3 = @x1 + width
35
44
  end
36
45
 
37
- def height=(h)
38
- @height = h
39
- update_coords(@x, @y, @width, h)
46
+ def height=(height)
47
+ @height = height
48
+ @y3 = @y1 + height
49
+ @y4 = @y1 + height
40
50
  end
41
51
 
42
- def self.draw(opts = {})
43
- ext_draw([
44
- opts[:x] , opts[:y] , opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
45
- opts[:x] + opts[:width], opts[:y] , opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
46
- opts[:x] + opts[:width], opts[:y] + opts[:height], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3],
47
- opts[:x] , opts[:y] + opts[:height], opts[:color][3][0], opts[:color][3][1], opts[:color][3][2], opts[:color][3][3]
48
- ])
52
+ def self.draw(x:, y:, width:, height:, color:)
53
+ super(x1: x, y1: y,
54
+ x2: x + width, y2: y,
55
+ x3: x + width, y3: y + height,
56
+ x4: x, y4: y + height, color: color)
49
57
  end
50
58
 
51
- private
52
-
53
- def update_coords(x, y, w, h)
54
- @x1 = x
55
- @y1 = y
56
- @x2 = x + w
57
- @y2 = y
58
- @x3 = x + w
59
- @y3 = y + h
60
- @x4 = x
61
- @y4 = y + h
59
+ def contains?(x, y)
60
+ # Override the check from Quad since this is faster
61
+ x >= @x && x <= (@x + @width) && y >= @y && y <= (@y + @height)
62
62
  end
63
-
64
63
  end
65
64
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Ruby2D::Renderable
2
4
 
3
5
  module Ruby2D
6
+ # Base class for all renderable shapes
4
7
  module Renderable
5
-
6
8
  attr_reader :x, :y, :z, :width, :height, :color
7
9
 
8
10
  # Set the z position (depth) of the object
@@ -23,18 +25,17 @@ module Ruby2D
23
25
  end
24
26
 
25
27
  # Set the color value
26
- def color=(c)
27
- @color = Color.new(c)
28
+ def color=(color)
29
+ @color = Color.new(color)
28
30
  end
29
31
 
30
32
  # Allow British English spelling of color
31
- alias_method :colour, :color
32
- alias_method :colour=, :color=
33
+ alias colour color
34
+ alias colour= color=
33
35
 
34
36
  # Add a contains method stub
35
37
  def contains?(x, y)
36
38
  x >= @x && x <= (@x + @width) && y >= @y && y <= (@y + @height)
37
39
  end
38
-
39
40
  end
40
41
  end
data/lib/ruby2d/sound.rb CHANGED
@@ -1,19 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Ruby2D::Sound
2
4
 
3
5
  module Ruby2D
6
+ # Sounds are intended to be short samples, played without interruption, like an effect.
4
7
  class Sound
5
-
6
8
  attr_reader :path
7
- attr_accessor :data
9
+ attr_accessor :loop, :data
10
+
11
+ #
12
+ # Load a sound from a file
13
+ # @param [String] path File to load the sound from
14
+ # @param [true, false] loop If +true+ playback will loop automatically, default is +false+
15
+ # @raise [Error] if file cannot be found or music could not be successfully loaded.
16
+ def initialize(path, loop: false)
17
+ raise Error, "Cannot find audio file `#{path}`" unless File.exist? path
8
18
 
9
- def initialize(path)
10
- unless File.exist? path
11
- raise Error, "Cannot find audio file `#{path}`"
12
- end
13
19
  @path = path
14
- unless ext_init(@path)
15
- raise Error, "Sound `#{@path}` cannot be created"
16
- end
20
+ @loop = loop
21
+ raise Error, "Sound `#{@path}` cannot be created" unless ext_init(@path)
17
22
  end
18
23
 
19
24
  # Play the sound
@@ -21,6 +26,11 @@ module Ruby2D
21
26
  ext_play
22
27
  end
23
28
 
29
+ # Stop the sound
30
+ def stop
31
+ ext_stop
32
+ end
33
+
24
34
  # Returns the length in seconds
25
35
  def length
26
36
  ext_length
@@ -32,11 +42,9 @@ module Ruby2D
32
42
  end
33
43
 
34
44
  # Set the volume of the sound
35
- def volume=(v)
45
+ def volume=(volume)
36
46
  # Clamp value to between 0-100
37
- if v < 0 then v = 0 end
38
- if v > 100 then v = 100 end
39
- ext_set_volume(v)
47
+ ext_set_volume(volume.clamp(0, 100))
40
48
  end
41
49
 
42
50
  # Get the volume of the sound mixer
@@ -45,11 +53,9 @@ module Ruby2D
45
53
  end
46
54
 
47
55
  # Set the volume of the sound mixer
48
- def self.mix_volume=(v)
56
+ def self.mix_volume=(volume)
49
57
  # Clamp value to between 0-100
50
- if v < 0 then v = 0 end
51
- if v > 100 then v = 100 end
52
- ext_set_mix_volume(v)
58
+ ext_set_mix_volume(volume.clamp(0, 100))
53
59
  end
54
60
  end
55
61
  end
data/lib/ruby2d/sprite.rb CHANGED
@@ -1,101 +1,85 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Ruby2D::Sprite
2
4
 
3
5
  module Ruby2D
6
+ # Sprites are special images that can be used to create animations, kind of like a flip book.
7
+ # To create a sprite animation, first you’ll need an image which contains each frame of your animation.
4
8
  class Sprite
5
9
  include Renderable
6
10
 
7
11
  attr_reader :path
8
12
  attr_accessor :rotate, :loop, :clip_x, :clip_y, :clip_width, :clip_height, :data, :x, :y, :width, :height
9
13
 
10
- def initialize(path, opts = {})
11
- # Sprite image file path
12
- @path = path
13
-
14
- # Initialize the sprite texture
15
- @texture = Texture.new(*Image.load_image(@path))
16
- @img_width = @texture.width
17
- @img_height = @texture.height
14
+ # Create a sprite via single image or sprite-sheet.
15
+ # @param path [#to_s] The location of the file to load as an image.
16
+ # @param width [Numeric] The +width+ of drawn image, or default is width from image file
17
+ # @param height [Numeric] The +height+ of drawn image, or default is height from image file
18
+ # @param x [Numeric]
19
+ # @param y [Numeric]
20
+ # @param z [Numeric]
21
+ # @param rotate [Numeric] Angle, default is 0
22
+ # @param color [Numeric] (or +colour+) Tint the image when rendering
23
+ # @param opacity [Numeric] Opacity of the image when rendering
24
+ # @param show [Boolean] If +true+ the image is added to +Window+ automatically.
25
+ # @param loop [Boolean] Sets whether animations loop by default when played (can be overridden by +play+ method)
26
+ # @param time [Numeric] The default time in millisecond for each frame (unless overridden animation within frames)
27
+ # @param animations [Hash{String => Range}, Hash{String=>Array<Hash>}] Sprite sheet map to animations using
28
+ # frame ranges or individual frames
29
+ # @param default [Numeric] The default initial frame for the sprite
30
+ # @param clip_x [Numeric]
31
+ # @param clip_x [Numeric]
32
+ # @param clip_width [Numeric]
33
+ # @param clip_height [Numeric]
34
+ def initialize(path, atlas: nil, show: true, width: nil, height: nil,
35
+ x: 0, y: 0, z: 0, rotate: 0, color: nil, colour: nil, opacity: nil,
36
+ loop: false, time: 300, animations: {}, default: 0,
37
+ clip_x: 0, clip_y: 0, clip_width: nil, clip_height: nil)
38
+ @path = path.to_s
18
39
 
19
40
  # Coordinates, size, and rotation of the sprite
20
- @x = opts[:x] || 0
21
- @y = opts[:y] || 0
22
- @z = opts[:z] || 0
23
- @rotate = opts[:rotate] || 0
24
- self.color = opts[:color] || 'white'
25
- self.color.opacity = opts[:opacity] if opts[:opacity]
41
+ @x = x
42
+ @y = y
43
+ @z = z
44
+ @rotate = rotate
45
+ self.color = color || colour || 'white'
46
+ self.color.opacity = opacity unless opacity.nil?
47
+
48
+ # Dimensions
49
+ @width = width
50
+ @height = height
26
51
 
27
52
  # Flipping status
28
53
  @flip = nil
29
54
 
30
55
  # Animation attributes
31
- @start_time = 0.0
32
- @loop = opts[:loop] || false
33
- @frame_time = opts[:time] || 300
34
- @animations = opts[:animations] || {}
35
- @playing = false
36
- @current_frame = opts[:default] || 0
37
- @last_frame = 0
38
- @done_proc = nil
39
-
40
- # The clipping rectangle
41
- @clip_x = opts[:clip_x] || 0
42
- @clip_y = opts[:clip_y] || 0
43
- @clip_width = opts[:clip_width] || @img_width
44
- @clip_height = opts[:clip_height] || @img_height
45
-
46
- # Dimensions
47
- @width = opts[:width] || nil
48
- @height = opts[:height] || nil
49
-
50
- # Set the default animation
51
- @animations[:default] = 0..(@img_width / @clip_width) - 1
56
+ @loop = loop
57
+ @frame_time = time
58
+ @animations = animations
59
+ @current_frame = default
52
60
 
53
- # Set the sprite defaults
54
- @defaults = {
55
- animation: @animations.first[0],
56
- frame: @current_frame,
57
- frame_time: @frame_time,
58
- clip_x: @clip_x,
59
- clip_y: @clip_y,
60
- clip_width: @clip_width,
61
- clip_height: @clip_height,
62
- loop: @loop
63
- }
61
+ _setup_texture_and_clip_box atlas, clip_x, clip_y, clip_width, clip_height
62
+ _setup_animation
64
63
 
65
64
  # Add the sprite to the window
66
- unless opts[:show] == false then add end
65
+ add if show
67
66
  end
68
67
 
69
- # Play an animation
70
- def play(opts = {}, &done_proc)
71
-
72
- animation = opts[:animation]
73
- loop = opts[:loop]
74
- flip = opts[:flip]
75
-
76
- if !@playing || (animation != @playing_animation && animation != nil) || flip != @flip
77
-
68
+ # Start playing an animation
69
+ # @param animation [String] Name of the animation to play
70
+ # @param loop [Boolean] Set the animation to loop or not
71
+ # @param flip [nil, :vertical, :horizontal, :both] Direction to flip the sprite if desired
72
+ def play(animation: :default, loop: nil, flip: nil, &done_proc)
73
+ unless @playing && animation == @playing_animation && flip == @flip
78
74
  @playing = true
79
75
  @playing_animation = animation || :default
80
- frames = @animations[@playing_animation]
81
- flip_sprite(flip)
82
76
  @done_proc = done_proc
83
77
 
84
- case frames
85
- # When animation is a range, play through frames horizontally
86
- when Range
87
- @first_frame = frames.first || @defaults[:frame]
88
- @current_frame = frames.first || @defaults[:frame]
89
- @last_frame = frames.last
90
- # When array...
91
- when Array
92
- @first_frame = 0
93
- @current_frame = 0
94
- @last_frame = frames.length - 1
95
- end
78
+ flip_sprite(flip)
79
+ _reset_playing_animation
96
80
 
97
- # Set looping
98
- @loop = loop == true || @defaults[:loop] ? true : false
81
+ loop = @defaults[:loop] if loop.nil?
82
+ @loop = loop ? true : false
99
83
 
100
84
  set_frame
101
85
  restart_time
@@ -105,22 +89,22 @@ module Ruby2D
105
89
 
106
90
  # Stop the current animation and set to the default frame
107
91
  def stop(animation = nil)
108
- if !animation || animation == @playing_animation
109
- @playing = false
110
- @playing_animation = @defaults[:animation]
111
- @current_frame = @defaults[:frame]
112
- set_frame
113
- end
92
+ return unless !animation || animation == @playing_animation
93
+
94
+ @playing = false
95
+ @playing_animation = @defaults[:animation]
96
+ @current_frame = @defaults[:frame]
97
+ set_frame
114
98
  end
115
99
 
116
100
  # Flip the sprite
101
+ # @param flip [nil, :vertical, :horizontal, :both] Direction to flip the sprite if desired
117
102
  def flip_sprite(flip)
118
-
119
103
  # The sprite width and height must be set for it to be flipped correctly
120
104
  if (!@width || !@height) && flip
121
105
  raise Error,
122
- "Sprite width and height must be set in order to flip; " +
123
- "occured playing animation `:#{@playing_animation}` with image `#{@path}`"
106
+ "Sprite width/height required to flip; occured playing animation `:#{@playing_animation}`
107
+ with image `#{@path}`"
124
108
  end
125
109
 
126
110
  @flip = flip
@@ -142,12 +126,7 @@ module Ruby2D
142
126
  reset_clipping_rect
143
127
  @clip_x = @current_frame * @clip_width
144
128
  when Array
145
- f = frames[@current_frame]
146
- @clip_x = f[:x] || @defaults[:clip_x]
147
- @clip_y = f[:y] || @defaults[:clip_y]
148
- @clip_width = f[:width] || @defaults[:clip_width]
149
- @clip_height = f[:height] || @defaults[:clip_height]
150
- @frame_time = f[:time] || @defaults[:frame_time]
129
+ _set_explicit_frame frames[@current_frame]
151
130
  end
152
131
  end
153
132
 
@@ -163,75 +142,137 @@ module Ruby2D
163
142
 
164
143
  # Update the sprite animation, called by `render`
165
144
  def update
166
- if @playing
145
+ return unless @playing
167
146
 
168
- # Advance the frame
169
- unless elapsed_time <= (@frame_time || @defaults[:frame_time])
170
- @current_frame += 1
171
- restart_time
172
- end
147
+ # Advance the frame
148
+ unless elapsed_time <= (@frame_time || @defaults[:frame_time])
149
+ @current_frame += 1
150
+ restart_time
151
+ end
173
152
 
174
- # Reset to the starting frame if all frames played
175
- if @current_frame > @last_frame
176
- @current_frame = @first_frame
177
- unless @loop
178
- # Stop animation and play block, if provided
179
- stop
180
- if @done_proc
181
- # allow proc to make nested `play/do` calls to sequence multiple
182
- # animations by clearing `@done_proc` before the call
183
- kept_done_proc = @done_proc
184
- @done_proc = nil
185
- kept_done_proc.call
186
- end
153
+ # Reset to the starting frame if all frames played
154
+ if @current_frame > @last_frame
155
+ @current_frame = @first_frame
156
+ unless @loop
157
+ # Stop animation and play block, if provided
158
+ stop
159
+ if @done_proc
160
+ # allow proc to make nested `play/do` calls to sequence multiple
161
+ # animations by clearing `@done_proc` before the call
162
+ kept_done_proc = @done_proc
163
+ @done_proc = nil
164
+ kept_done_proc.call
187
165
  end
188
166
  end
189
-
190
- set_frame
191
167
  end
168
+
169
+ set_frame
192
170
  end
193
171
 
194
- def draw(opts = {})
172
+ # @param width [Numeric] The +width+ of drawn image
173
+ # @param height [Numeric] The +height+ of drawn image
174
+ # @param x [Numeric]
175
+ # @param y [Numeric]
176
+ # @param rotate [Numeric] Angle, default is 0
177
+ # @param color [Numeric] (or +colour+) Tint the image when rendering
178
+ # @param clip_x [Numeric]
179
+ # @param clip_x [Numeric]
180
+ # @param clip_width [Numeric]
181
+ # @param clip_height [Numeric]
182
+ def draw(x:, y:, width: (@width || @clip_width), height: (@height || @clip_height), rotate: @rotate,
183
+ clip_x: @clip_x, clip_y: @clip_y, clip_width: @clip_width, clip_height: @clip_height,
184
+ color: [1.0, 1.0, 1.0, 1.0])
185
+
195
186
  Window.render_ready_check
187
+ render(x: x, y: y, width: width, height: height, color: Color.new(color), rotate: rotate, crop: {
188
+ x: clip_x,
189
+ y: clip_y,
190
+ width: clip_width,
191
+ height: clip_height,
192
+ image_width: @img_width,
193
+ image_height: @img_height
194
+ })
195
+ end
196
+
197
+ private
198
+
199
+ def render(x: @x, y: @y, width: (@width || @clip_width), height: (@height || @clip_height),
200
+ color: @color, rotate: @rotate, flip: @flip,
201
+ crop: {
202
+ x: @clip_x,
203
+ y: @clip_y,
204
+ width: @clip_width,
205
+ height: @clip_height,
206
+ image_width: @img_width,
207
+ image_height: @img_height
208
+ })
209
+ update
210
+ vertices = Vertices.new(x, y, width, height, rotate, crop: crop, flip: flip)
211
+ @texture.draw vertices.coordinates, vertices.texture_coordinates, color
212
+ end
196
213
 
197
- opts[:width] = opts[:width] || (@width || @clip_width)
198
- opts[:height] = opts[:height] || (@height || @clip_height)
199
- opts[:rotate] = opts[:rotate] || @rotate
200
- opts[:clip_x] = opts[:clip_x] || @clip_x
201
- opts[:clip_y] = opts[:clip_y] || @clip_y
202
- opts[:clip_width] = opts[:clip_width] || @clip_width
203
- opts[:clip_height] = opts[:clip_height] || @clip_height
204
- unless opts[:color]
205
- opts[:color] = [1.0, 1.0, 1.0, 1.0]
214
+ # Reset the playing animation to the first frame
215
+ def _reset_playing_animation
216
+ frames = @animations[@playing_animation]
217
+ case frames
218
+ # When animation is a range, play through frames horizontally
219
+ when Range
220
+ @first_frame = frames.first || @defaults[:frame]
221
+ @current_frame = frames.first || @defaults[:frame]
222
+ @last_frame = frames.last
223
+ # When array...
224
+ when Array
225
+ @first_frame = 0
226
+ @current_frame = 0
227
+ @last_frame = frames.length - 1
206
228
  end
229
+ end
207
230
 
208
- render(x: opts[:x], y: opts[:y], width: opts[:width], height: opts[:height], color: Color.new(color), rotate: opts[:rotate], crop: {
209
- x: opts[:clip_x],
210
- y: opts[:clip_y],
211
- width: opts[:clip_width],
212
- height: opts[:clip_height],
213
- image_width: @img_width,
214
- image_height: @img_height,
215
- })
231
+ # Set the current frame based on the region/portion of image
232
+ def _set_explicit_frame(frame)
233
+ @clip_x = frame[:x] || @defaults[:clip_x]
234
+ @clip_y = frame[:y] || @defaults[:clip_y]
235
+ @clip_width = frame[:width] || @defaults[:clip_width]
236
+ @clip_height = frame[:height] || @defaults[:clip_height]
237
+ @frame_time = frame[:time] || @defaults[:frame_time]
216
238
  end
217
239
 
218
- private
240
+ # initialize texture and clipping, called by constructor
241
+ def _setup_texture_and_clip_box(atlas, clip_x, clip_y, clip_width, clip_height)
242
+ # Initialize the sprite texture
243
+ # Consider input pixmap atlas if supplied to load image file
244
+ @texture = Image.load_image_as_texture @path, atlas: atlas
245
+ @img_width = @texture.width
246
+ @img_height = @texture.height
219
247
 
220
- def render(x: @x, y: @y, width: (@width || @clip_width), height: (@height || @clip_height) , color: @color, rotate: @rotate, flip: @flip, crop: {
221
- x: @clip_x,
222
- y: @clip_y,
223
- width: @clip_width,
224
- height: @clip_height,
225
- image_width: @img_width,
226
- image_height: @img_height,
227
- })
228
- update
248
+ # The clipping rectangle
249
+ @clip_x = clip_x
250
+ @clip_y = clip_y
251
+ @clip_width = clip_width || @img_width
252
+ @clip_height = clip_height || @img_height
253
+ end
229
254
 
230
- vertices = Vertices.new(x, y, width, height, rotate, crop: crop, flip: flip)
255
+ # initialize animation, called by constructor
256
+ def _setup_animation
257
+ @start_time = 0.0
258
+ @playing = false
259
+ @last_frame = 0
260
+ @done_proc = nil
261
+
262
+ # Set the default animation
263
+ @animations[:default] = 0..(@img_width / @clip_width) - 1
231
264
 
232
- @texture.draw(
233
- vertices.coordinates, vertices.texture_coordinates, color
234
- )
265
+ # Set the sprite defaults
266
+ @defaults = {
267
+ animation: @animations.first[0],
268
+ frame: @current_frame,
269
+ frame_time: @frame_time,
270
+ clip_x: @clip_x,
271
+ clip_y: @clip_y,
272
+ clip_width: @clip_width,
273
+ clip_height: @clip_height,
274
+ loop: @loop
275
+ }
235
276
  end
236
277
  end
237
278
  end
data/lib/ruby2d/square.rb CHANGED
@@ -1,37 +1,38 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Ruby2D::Square
2
4
 
3
5
  module Ruby2D
6
+ # A square
4
7
  class Square < Rectangle
5
-
6
8
  attr_reader :size
7
9
 
8
- def initialize(opts = {})
9
- @x = opts[:x] || 0
10
- @y = opts[:y] || 0
11
- @z = opts[:z] || 0
12
- @width = @height = @size = opts[:size] || 100
13
- self.color = opts[:color] || 'white'
14
- self.color.opacity = opts[:opacity] if opts[:opacity]
15
- update_coords(@x, @y, @size, @size)
16
- add
10
+ # Create an square
11
+ # @param [Numeric] x
12
+ # @param [Numeric] y
13
+ # @param [Numeric] size is width and height
14
+ # @param [Numeric] z
15
+ # @param [String, Array] color A single colour or an array of exactly 4 colours
16
+ # @param [Numeric] opacity Opacity of the image when rendering
17
+ # @raise [ArgumentError] if an array of colours does not have 4 entries
18
+ def initialize(x: 0, y: 0, size: 100, z: 0, color: nil, colour: nil, opacity: nil)
19
+ @size = size
20
+ super(x: x, y: y, width: size, height: size, z: z,
21
+ color: color, colour: colour, opacity: opacity)
17
22
  end
18
23
 
19
24
  # Set the size of the square
20
- def size=(s)
21
- self.width = self.height = @size = s
25
+ def size=(size)
26
+ self.width = self.height = @size = size
22
27
  end
23
28
 
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
- ])
29
+ def self.draw(x:, y:, size:, color:)
30
+ super(x: x, y: y,
31
+ width: size, height: size,
32
+ color: color)
31
33
  end
32
34
 
33
35
  # Make the inherited width and height attribute accessors private
34
36
  private :width=, :height=
35
-
36
37
  end
37
38
  end