ruby2d 0.9.3 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. checksums.yaml +4 -4
  2. data/assets/README.md +7 -9
  3. data/assets/Rakefile +85 -0
  4. data/assets/include/SDL2/SDL.h +4 -1
  5. data/assets/include/SDL2/SDL_assert.h +3 -1
  6. data/assets/include/SDL2/SDL_atomic.h +20 -2
  7. data/assets/include/SDL2/SDL_audio.h +47 -14
  8. data/assets/include/SDL2/SDL_bits.h +10 -1
  9. data/assets/include/SDL2/SDL_blendmode.h +10 -7
  10. data/assets/include/SDL2/SDL_clipboard.h +1 -1
  11. data/assets/include/SDL2/SDL_config.h +24 -390
  12. data/assets/include/SDL2/SDL_config_android.h +182 -0
  13. data/assets/include/SDL2/SDL_config_iphoneos.h +207 -0
  14. data/assets/include/SDL2/SDL_config_macosx.h +266 -0
  15. data/assets/include/SDL2/SDL_config_minimal.h +85 -0
  16. data/assets/include/SDL2/SDL_config_os2.h +188 -0
  17. data/assets/include/SDL2/SDL_config_pandora.h +135 -0
  18. data/assets/include/SDL2/SDL_config_psp.h +165 -0
  19. data/assets/include/SDL2/SDL_config_windows.h +288 -0
  20. data/assets/include/SDL2/SDL_config_winrt.h +243 -0
  21. data/assets/include/SDL2/SDL_config_wiz.h +149 -0
  22. data/assets/include/SDL2/SDL_copying.h +20 -0
  23. data/assets/include/SDL2/SDL_cpuinfo.h +119 -8
  24. data/assets/include/SDL2/SDL_egl.h +4 -1
  25. data/assets/include/SDL2/SDL_endian.h +6 -3
  26. data/assets/include/SDL2/SDL_error.h +38 -2
  27. data/assets/include/SDL2/SDL_events.h +67 -28
  28. data/assets/include/SDL2/SDL_filesystem.h +1 -1
  29. data/assets/include/SDL2/SDL_gamecontroller.h +160 -9
  30. data/assets/include/SDL2/SDL_gesture.h +1 -1
  31. data/assets/include/SDL2/SDL_haptic.h +10 -1
  32. data/assets/include/SDL2/SDL_hints.h +460 -17
  33. data/assets/include/SDL2/SDL_image.h +2 -2
  34. data/assets/include/SDL2/SDL_joystick.h +115 -24
  35. data/assets/include/SDL2/SDL_keyboard.h +1 -1
  36. data/assets/include/SDL2/SDL_keycode.h +11 -9
  37. data/assets/include/SDL2/SDL_loadso.h +1 -1
  38. data/assets/include/SDL2/SDL_locale.h +101 -0
  39. data/assets/include/SDL2/SDL_log.h +3 -3
  40. data/assets/include/SDL2/SDL_main.h +28 -16
  41. data/assets/include/SDL2/SDL_messagebox.h +6 -4
  42. data/assets/include/SDL2/SDL_metal.h +117 -0
  43. data/assets/include/SDL2/SDL_misc.h +75 -0
  44. data/assets/include/SDL2/SDL_mouse.h +1 -1
  45. data/assets/include/SDL2/SDL_mutex.h +1 -1
  46. data/assets/include/SDL2/SDL_name.h +1 -1
  47. data/assets/include/SDL2/SDL_opengl.h +1 -1
  48. data/assets/include/SDL2/SDL_opengl_glext.h +3 -0
  49. data/assets/include/SDL2/SDL_opengles.h +1 -1
  50. data/assets/include/SDL2/SDL_opengles2.h +1 -1
  51. data/assets/include/SDL2/SDL_pixels.h +27 -18
  52. data/assets/include/SDL2/SDL_platform.h +1 -1
  53. data/assets/include/SDL2/SDL_power.h +1 -1
  54. data/assets/include/SDL2/SDL_quit.h +1 -1
  55. data/assets/include/SDL2/SDL_rect.h +29 -3
  56. data/assets/include/SDL2/SDL_render.h +230 -3
  57. data/assets/include/SDL2/SDL_revision.h +2 -2
  58. data/assets/include/SDL2/SDL_rwops.h +51 -22
  59. data/assets/include/SDL2/SDL_scancode.h +2 -2
  60. data/assets/include/SDL2/SDL_sensor.h +28 -12
  61. data/assets/include/SDL2/SDL_shape.h +1 -1
  62. data/assets/include/SDL2/SDL_stdinc.h +44 -4
  63. data/assets/include/SDL2/SDL_surface.h +12 -2
  64. data/assets/include/SDL2/SDL_system.h +50 -4
  65. data/assets/include/SDL2/SDL_syswm.h +39 -9
  66. data/assets/include/SDL2/SDL_test.h +1 -1
  67. data/assets/include/SDL2/SDL_test_assert.h +1 -1
  68. data/assets/include/SDL2/SDL_test_common.h +32 -2
  69. data/assets/include/SDL2/SDL_test_compare.h +1 -1
  70. data/assets/include/SDL2/SDL_test_crc32.h +1 -1
  71. data/assets/include/SDL2/SDL_test_font.h +1 -1
  72. data/assets/include/SDL2/SDL_test_fuzzer.h +1 -1
  73. data/assets/include/SDL2/SDL_test_harness.h +1 -1
  74. data/assets/include/SDL2/SDL_test_images.h +1 -1
  75. data/assets/include/SDL2/SDL_test_log.h +1 -1
  76. data/assets/include/SDL2/SDL_test_md5.h +1 -1
  77. data/assets/include/SDL2/SDL_test_memory.h +3 -3
  78. data/assets/include/SDL2/SDL_test_random.h +1 -1
  79. data/assets/include/SDL2/SDL_thread.h +34 -11
  80. data/assets/include/SDL2/SDL_timer.h +1 -1
  81. data/assets/include/SDL2/SDL_touch.h +17 -1
  82. data/assets/include/SDL2/SDL_types.h +1 -1
  83. data/assets/include/SDL2/SDL_version.h +2 -2
  84. data/assets/include/SDL2/SDL_video.h +11 -5
  85. data/assets/include/SDL2/SDL_vulkan.h +9 -11
  86. data/assets/include/SDL2/begin_code.h +8 -9
  87. data/assets/include/SDL2/close_code.h +4 -1
  88. data/assets/macos/lib/libFLAC.a +0 -0
  89. data/assets/macos/lib/libSDL2.a +0 -0
  90. data/assets/macos/lib/libSDL2_image.a +0 -0
  91. data/assets/macos/lib/libSDL2_mixer.a +0 -0
  92. data/assets/macos/lib/libSDL2_ttf.a +0 -0
  93. data/assets/macos/lib/libfreetype.a +0 -0
  94. data/assets/macos/lib/libjpeg.a +0 -0
  95. data/assets/macos/lib/libmpg123.a +0 -0
  96. data/assets/macos/lib/libogg.a +0 -0
  97. data/assets/macos/lib/libpng16.a +0 -0
  98. data/assets/macos/lib/libtiff.a +0 -0
  99. data/assets/macos/lib/libvorbis.a +0 -0
  100. data/assets/macos/lib/libvorbisfile.a +0 -0
  101. data/assets/macos/lib/libwebp.a +0 -0
  102. data/assets/mingw/bin/SDL2.dll +0 -0
  103. data/assets/mingw/bin/SDL2_image.dll +0 -0
  104. data/assets/mingw/bin/libpng16-16.dll +0 -0
  105. data/assets/mingw/bin/libtiff-5.dll +0 -0
  106. data/assets/mingw/bin/libwebp-7.dll +0 -0
  107. data/assets/mingw/lib/libSDL2.a +0 -0
  108. data/assets/mingw/lib/libSDL2.dll.a +0 -0
  109. data/assets/mingw/lib/libSDL2_image.a +0 -0
  110. data/assets/mingw/lib/libSDL2_image.dll.a +0 -0
  111. data/assets/mingw/lib/libSDL2_test.a +0 -0
  112. data/assets/mingw/lib/libSDL2main.a +0 -0
  113. data/bin/ruby2d +1 -0
  114. data/{assets/linux/simple2d/src/simple2d.c → ext/ruby2d/common.c} +32 -32
  115. data/{assets/linux/simple2d/src → ext/ruby2d}/controllers.c +17 -17
  116. data/ext/ruby2d/extconf.rb +36 -58
  117. data/ext/ruby2d/font.c +35 -0
  118. data/{assets/linux/simple2d/src → ext/ruby2d}/gl.c +72 -100
  119. data/ext/ruby2d/gl2.c +86 -0
  120. data/ext/ruby2d/gl3.c +305 -0
  121. data/{assets/linux/simple2d/src → ext/ruby2d}/gles.c +22 -81
  122. data/ext/ruby2d/image.c +58 -0
  123. data/{assets/linux/simple2d/src → ext/ruby2d}/input.c +8 -8
  124. data/{assets/linux/simple2d/src → ext/ruby2d}/music.c +30 -17
  125. data/ext/ruby2d/ruby2d.c +449 -439
  126. data/ext/ruby2d/ruby2d.h +652 -0
  127. data/{assets/linux/simple2d/src → ext/ruby2d}/shapes.c +18 -18
  128. data/ext/ruby2d/sound.c +118 -0
  129. data/ext/ruby2d/text.c +22 -0
  130. data/{assets/linux/simple2d/src → ext/ruby2d}/window.c +73 -62
  131. data/lib/ruby2d/circle.rb +21 -3
  132. data/lib/ruby2d/cli/build.rb +4 -7
  133. data/lib/ruby2d/dsl.rb +16 -9
  134. data/lib/ruby2d/entity.rb +17 -0
  135. data/lib/ruby2d/font.rb +23 -3
  136. data/lib/ruby2d/image.rb +35 -7
  137. data/lib/ruby2d/line.rb +23 -1
  138. data/lib/ruby2d/music.rb +5 -0
  139. data/lib/ruby2d/pixel.rb +17 -0
  140. data/lib/ruby2d/quad.rb +21 -1
  141. data/lib/ruby2d/rectangle.rb +12 -3
  142. data/lib/ruby2d/renderable.rb +4 -20
  143. data/lib/ruby2d/sound.rb +30 -0
  144. data/lib/ruby2d/sprite.rb +57 -75
  145. data/lib/ruby2d/square.rb +10 -1
  146. data/lib/ruby2d/text.rb +55 -12
  147. data/lib/ruby2d/texture.rb +28 -0
  148. data/lib/ruby2d/tileset.rb +87 -0
  149. data/lib/ruby2d/triangle.rb +19 -1
  150. data/lib/ruby2d/version.rb +1 -1
  151. data/lib/ruby2d/vertices.rb +84 -0
  152. data/lib/ruby2d/window.rb +259 -22
  153. data/lib/ruby2d.rb +6 -1
  154. metadata +41 -26
  155. data/assets/include/simple2d.h +0 -735
  156. data/assets/linux/simple2d/Makefile +0 -257
  157. data/assets/linux/simple2d/bin/simple2d.sh +0 -1312
  158. data/assets/linux/simple2d/include/simple2d.h +0 -735
  159. data/assets/linux/simple2d/src/gl2.c +0 -146
  160. data/assets/linux/simple2d/src/gl3.c +0 -275
  161. data/assets/linux/simple2d/src/image.c +0 -138
  162. data/assets/linux/simple2d/src/sound.c +0 -56
  163. data/assets/linux/simple2d/src/sprite.c +0 -147
  164. data/assets/linux/simple2d/src/text.c +0 -129
  165. data/assets/macos/lib/libsimple2d.a +0 -0
  166. data/assets/mingw/lib/libsimple2d.a +0 -0
data/lib/ruby2d/sound.rb CHANGED
@@ -21,5 +21,35 @@ 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
+
29
+ # Get the volume of the sound
30
+ def volume
31
+ ext_get_volume
32
+ end
33
+
34
+ # Set the volume of the sound
35
+ def volume=(v)
36
+ # 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)
40
+ end
41
+
42
+ # Get the volume of the sound mixer
43
+ def self.mix_volume
44
+ ext_get_mix_volume
45
+ end
46
+
47
+ # Set the volume of the sound mixer
48
+ def self.mix_volume=(v)
49
+ # 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)
53
+ end
24
54
  end
25
55
  end
data/lib/ruby2d/sprite.rb CHANGED
@@ -5,32 +5,27 @@ module Ruby2D
5
5
  include Renderable
6
6
 
7
7
  attr_reader :path
8
- attr_accessor :rotate, :loop, :clip_x, :clip_y, :clip_width, :clip_height, :data
8
+ attr_accessor :rotate, :loop, :clip_x, :clip_y, :clip_width, :clip_height, :data, :x, :y, :width, :height
9
9
 
10
10
  def initialize(path, opts = {})
11
- unless File.exist? path
12
- raise Error, "Cannot find sprite image file `#{path}`"
13
- end
14
-
15
11
  # Sprite image file path
16
12
  @path = path
17
13
 
14
+ # Initialize the sprite texture
15
+ @texture = Texture.new(*Image.load_image(@path))
16
+ @img_width = @texture.width
17
+ @img_height = @texture.height
18
+
18
19
  # Coordinates, size, and rotation of the sprite
19
20
  @x = opts[:x] || 0
20
21
  @y = opts[:y] || 0
21
22
  @z = opts[:z] || 0
22
- @width = opts[:width] || nil
23
- @height = opts[:height] || nil
24
23
  @rotate = opts[:rotate] || 0
25
24
  self.color = opts[:color] || 'white'
26
- self.opacity = opts[:opacity] if opts[:opacity]
25
+ self.color.opacity = opts[:opacity] if opts[:opacity]
27
26
 
28
- # Flipping status, coordinates, and size, used internally
27
+ # Flipping status
29
28
  @flip = nil
30
- @flip_x = @x
31
- @flip_y = @y
32
- @flip_width = @width
33
- @flip_height = @height
34
29
 
35
30
  # Animation attributes
36
31
  @start_time = 0.0
@@ -42,20 +37,16 @@ module Ruby2D
42
37
  @last_frame = 0
43
38
  @done_proc = nil
44
39
 
45
- # The sprite image size set by the native extension `ext_init()`
46
- @img_width = nil; @img_height = nil
47
-
48
- # Initialize the sprite
49
- unless ext_init(@path)
50
- raise Error, "Sprite image `#{@path}` cannot be created"
51
- end
52
-
53
40
  # The clipping rectangle
54
41
  @clip_x = opts[:clip_x] || 0
55
42
  @clip_y = opts[:clip_y] || 0
56
43
  @clip_width = opts[:clip_width] || @img_width
57
44
  @clip_height = opts[:clip_height] || @img_height
58
45
 
46
+ # Dimensions
47
+ @width = opts[:width] || nil
48
+ @height = opts[:height] || nil
49
+
59
50
  # Set the default animation
60
51
  @animations[:default] = 0..(@img_width / @clip_width) - 1
61
52
 
@@ -72,39 +63,7 @@ module Ruby2D
72
63
  }
73
64
 
74
65
  # Add the sprite to the window
75
- add
76
- end
77
-
78
- # Set the x coordinate
79
- def x=(x)
80
- @x = @flip_x = x
81
- if @flip == :horizontal || @flip == :both
82
- @flip_x = x + @width
83
- end
84
- end
85
-
86
- # Set the y coordinate
87
- def y=(y)
88
- @y = @flip_y = y
89
- if @flip == :vertical || @flip == :both
90
- @flip_y = y + @height
91
- end
92
- end
93
-
94
- # Set the width
95
- def width=(width)
96
- @width = @flip_width = width
97
- if @flip == :horizontal || @flip == :both
98
- @flip_width = -width
99
- end
100
- end
101
-
102
- # Set the height
103
- def height=(height)
104
- @height = @flip_height = height
105
- if @flip == :vertical || @flip == :both
106
- @flip_height = -height
107
- end
66
+ unless opts[:show] == false then add end
108
67
  end
109
68
 
110
69
  # Play an animation
@@ -141,6 +100,7 @@ module Ruby2D
141
100
  set_frame
142
101
  restart_time
143
102
  end
103
+ self
144
104
  end
145
105
 
146
106
  # Stop the current animation and set to the default frame
@@ -164,26 +124,6 @@ module Ruby2D
164
124
  end
165
125
 
166
126
  @flip = flip
167
-
168
- # Reset flip values
169
- @flip_x = @x
170
- @flip_y = @y
171
- @flip_width = @width
172
- @flip_height = @height
173
-
174
- case flip
175
- when :horizontal
176
- @flip_x = @x + @width
177
- @flip_width = -@width
178
- when :vertical
179
- @flip_y = @y + @height
180
- @flip_height = -@height
181
- when :both # horizontal and vertical
182
- @flip_x = @x + @width
183
- @flip_width = -@width
184
- @flip_y = @y + @height
185
- @flip_height = -@height
186
- end
187
127
  end
188
128
 
189
129
  # Reset frame to defaults
@@ -221,7 +161,7 @@ module Ruby2D
221
161
  @start_time = Time.now.to_f
222
162
  end
223
163
 
224
- # Update the sprite animation, called by `Sprite#ext_render`
164
+ # Update the sprite animation, called by `render`
225
165
  def update
226
166
  if @playing
227
167
 
@@ -246,5 +186,47 @@ module Ruby2D
246
186
  end
247
187
  end
248
188
 
189
+ def draw(opts = {})
190
+ Window.render_ready_check
191
+
192
+ opts[:width] = opts[:width] || (@width || @clip_width)
193
+ opts[:height] = opts[:height] || (@height || @clip_height)
194
+ opts[:rotate] = opts[:rotate] || @rotate
195
+ opts[:clip_x] = opts[:clip_x] || @clip_x
196
+ opts[:clip_y] = opts[:clip_y] || @clip_y
197
+ opts[:clip_width] = opts[:clip_width] || @clip_width
198
+ opts[:clip_height] = opts[:clip_height] || @clip_height
199
+ unless opts[:color]
200
+ opts[:color] = [1.0, 1.0, 1.0, 1.0]
201
+ end
202
+
203
+ render(x: opts[:x], y: opts[:y], width: opts[:width], height: opts[:height], color: Color.new(color), rotate: opts[:rotate], crop: {
204
+ x: opts[:clip_x],
205
+ y: opts[:clip_y],
206
+ width: opts[:clip_width],
207
+ height: opts[:clip_height],
208
+ image_width: @img_width,
209
+ image_height: @img_height,
210
+ })
211
+ end
212
+
213
+ private
214
+
215
+ def render(x: @x, y: @y, width: (@width || @clip_width), height: (@height || @clip_height) , color: @color, rotate: @rotate, flip: @flip, crop: {
216
+ x: @clip_x,
217
+ y: @clip_y,
218
+ width: @clip_width,
219
+ height: @clip_height,
220
+ image_width: @img_width,
221
+ image_height: @img_height,
222
+ })
223
+ update
224
+
225
+ vertices = Vertices.new(x, y, width, height, rotate, crop: crop, flip: flip)
226
+
227
+ @texture.draw(
228
+ vertices.coordinates, vertices.texture_coordinates, color
229
+ )
230
+ end
249
231
  end
250
232
  end
data/lib/ruby2d/square.rb CHANGED
@@ -11,7 +11,7 @@ module Ruby2D
11
11
  @z = opts[:z] || 0
12
12
  @width = @height = @size = opts[:size] || 100
13
13
  self.color = opts[:color] || 'white'
14
- self.opacity = opts[:opacity] if opts[:opacity]
14
+ self.color.opacity = opts[:opacity] if opts[:opacity]
15
15
  update_coords(@x, @y, @size, @size)
16
16
  add
17
17
  end
@@ -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
@@ -4,8 +4,8 @@ module Ruby2D
4
4
  class Text
5
5
  include Renderable
6
6
 
7
- attr_reader :text, :font
8
- attr_accessor :x, :y, :size, :rotate, :data
7
+ attr_reader :text, :size
8
+ attr_accessor :x, :y, :rotate, :data
9
9
 
10
10
  def initialize(text, opts = {})
11
11
  @x = opts[:x] || 0
@@ -14,22 +14,65 @@ module Ruby2D
14
14
  @text = text.to_s
15
15
  @size = opts[:size] || 20
16
16
  @rotate = opts[:rotate] || 0
17
+ @style = opts[:style]
17
18
  self.color = opts[:color] || 'white'
18
- self.opacity = opts[:opacity] if opts[:opacity]
19
- @font = opts[:font] || Font.default
20
- unless File.exist? @font
21
- raise Error, "Cannot find font file `#{@font}`"
22
- end
23
- unless ext_init
24
- raise Error, "Text `#{@text}` cannot be created"
25
- end
26
- add
19
+ self.color.opacity = opts[:opacity] if opts[:opacity]
20
+ @font_path = opts[:font] || Font.default
21
+ create_font
22
+ create_texture
23
+
24
+ unless opts[:show] == false then add end
25
+ end
26
+
27
+ # Returns the path of the font as a string
28
+ def font
29
+ @font_path
27
30
  end
28
31
 
29
32
  def text=(msg)
30
33
  @text = msg.to_s
31
- ext_set(@text)
34
+ create_texture
32
35
  end
33
36
 
37
+ def size=(size)
38
+ @size = size
39
+ create_font
40
+ create_texture
41
+ end
42
+
43
+ def draw(opts = {})
44
+ Window.render_ready_check
45
+
46
+ opts[:rotate] = opts[:rotate] || @rotate
47
+ unless opts[:color]
48
+ opts[:color] = [1.0, 1.0, 1.0, 1.0]
49
+ end
50
+
51
+ render(x: opts[:x], y: opts[:y], color: Color.new(opts[:color]), rotate: opts[:rotate])
52
+ end
53
+
54
+ private
55
+
56
+ def render(x: @x, y: @y, color: @color, rotate: @rotate)
57
+ vertices = Vertices.new(x, y, @width, @height, rotate)
58
+
59
+ @texture.draw(
60
+ vertices.coordinates, vertices.texture_coordinates, color
61
+ )
62
+ end
63
+
64
+ def create_font
65
+ @font = Font.load(@font_path, @size, @style)
66
+ end
67
+
68
+ def create_texture
69
+ if defined?(@texture)
70
+ @texture.delete
71
+ end
72
+
73
+ @texture = Texture.new(*Text.ext_load_text(@font.ttf_font, @text))
74
+ @width = @texture.width
75
+ @height = @texture.height
76
+ end
34
77
  end
35
78
  end
@@ -0,0 +1,28 @@
1
+ # Ruby2D::Text
2
+
3
+ module Ruby2D
4
+ class Texture
5
+ attr_reader :width, :height, :texture_id
6
+
7
+ def initialize(pixel_data, width, height)
8
+ @pixel_data = pixel_data
9
+ @width = width
10
+ @height = height
11
+ @texture_id = 0
12
+ end
13
+
14
+ def draw(coordinates, texture_coordinates, color)
15
+ if @texture_id == 0
16
+ @texture_id = ext_create(@pixel_data, @width, @height)
17
+ @pixel_data = nil
18
+ end
19
+
20
+ color = [color.r, color.g, color.b, color.a]
21
+ ext_draw(coordinates, texture_coordinates, color, @texture_id)
22
+ end
23
+
24
+ def delete
25
+ ext_delete(@texture_id)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,87 @@
1
+ # Ruby2D::Tileset
2
+
3
+ module Ruby2D
4
+ class Tileset
5
+ include Renderable
6
+
7
+ def initialize(path, opts = {})
8
+ @path = path
9
+
10
+ # Initialize the tileset texture
11
+ @texture = Texture.new(*Image.load_image(@path))
12
+ @width = opts[:width] || @texture.width
13
+ @height = opts[:height] || @texture.height
14
+ @z = opts[:z] || 0
15
+
16
+ @tiles = []
17
+ @defined_tiles = {}
18
+ @padding = opts[:padding] || 0
19
+ @spacing = opts[:spacing] || 0
20
+ @tile_width = opts[:tile_width]
21
+ @tile_height = opts[:tile_height]
22
+ @scale = opts[:scale] || 1
23
+
24
+ unless opts[:show] == false then add end
25
+ end
26
+
27
+ def define_tile(name, x, y, rotate: 0, flip: nil)
28
+ @defined_tiles[name] = { x: x, y: y , rotate: rotate, flip: flip }
29
+ end
30
+
31
+ def set_tile(name, coordinates)
32
+ tile = @defined_tiles.fetch(name)
33
+
34
+ coordinates.each do |coordinate|
35
+ @tiles.push({
36
+ tile_x: tile.fetch(:x),
37
+ tile_y: tile.fetch(:y),
38
+ tile_rotate: tile.fetch(:rotate),
39
+ tile_flip: tile.fetch(:flip),
40
+ x: coordinate.fetch(:x),
41
+ y: coordinate.fetch(:y)
42
+ })
43
+ end
44
+ end
45
+
46
+ def clear_tiles
47
+ @tiles = []
48
+ end
49
+ end
50
+
51
+ def draw
52
+ Window.render_ready_check
53
+
54
+ render
55
+ end
56
+
57
+ private
58
+
59
+
60
+ def render
61
+ scaled_padding = @padding * @scale
62
+ scaled_spacing = @spacing * @scale
63
+ scaled_tile_width = @tile_width * @scale
64
+ scaled_tile_height = @tile_height * @scale
65
+ scaled_width = @width * @scale
66
+ scaled_height = @height * @scale
67
+
68
+ @tiles.each do |tile|
69
+ crop = {
70
+ x: scaled_padding + (tile.fetch(:tile_x) * (scaled_spacing + scaled_tile_width)),
71
+ y: scaled_padding + (tile.fetch(:tile_y) * (scaled_spacing + scaled_tile_height)),
72
+ width: scaled_tile_width,
73
+ height: scaled_tile_height,
74
+ image_width: scaled_width,
75
+ image_height: scaled_height,
76
+ }
77
+
78
+ color = defined?(@color) ? @color : Color.new([1.0, 1.0, 1.0, 1.0])
79
+
80
+ vertices = Vertices.new(tile.fetch(:x), tile.fetch(:y), scaled_tile_width, scaled_tile_height, tile.fetch(:tile_rotate), crop: crop, flip: tile.fetch(:tile_flip))
81
+
82
+ @texture.draw(
83
+ vertices.coordinates, vertices.texture_coordinates, color
84
+ )
85
+ end
86
+ end
87
+ end