ruby2d 0.10.0 → 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.
data/lib/ruby2d/image.rb CHANGED
@@ -7,26 +7,34 @@ module Ruby2D
7
7
  attr_reader :path
8
8
  attr_accessor :x, :y, :width, :height, :rotate, :data
9
9
 
10
- def initialize(path, opts = {})
10
+ def self.load_image(path)
11
11
  unless File.exist? path
12
12
  raise Error, "Cannot find image file `#{path}`"
13
13
  end
14
+
15
+ ext_load_image(path)
16
+ end
17
+
18
+ def initialize(path, opts = {})
14
19
  @path = path
20
+
21
+ @texture = Texture.new(*Image.load_image(@path))
22
+ @width = opts[:width] || @texture.width
23
+ @height = opts[:height] || @texture.height
24
+
15
25
  @x = opts[:x] || 0
16
26
  @y = opts[:y] || 0
17
27
  @z = opts[:z] || 0
18
- @width = opts[:width] || nil
19
- @height = opts[:height] || nil
20
28
  @rotate = opts[:rotate] || 0
21
29
  self.color = opts[:color] || 'white'
22
- self.opacity = opts[:opacity] if opts[:opacity]
23
- unless ext_init(@path)
24
- raise Error, "Image `#{@path}` cannot be created"
25
- end
30
+ self.color.opacity = opts[:opacity] if opts[:opacity]
31
+
26
32
  unless opts[:show] == false then add end
27
33
  end
28
34
 
29
35
  def draw(opts = {})
36
+ Window.render_ready_check
37
+
30
38
  opts[:width] = opts[:width] || @width
31
39
  opts[:height] = opts[:height] || @height
32
40
  opts[:rotate] = opts[:rotate] || @rotate
@@ -34,20 +42,17 @@ module Ruby2D
34
42
  opts[:color] = [1.0, 1.0, 1.0, 1.0]
35
43
  end
36
44
 
37
- self.class.ext_draw([
38
- self, opts[:x], opts[:y], opts[:width], opts[:height], opts[:rotate],
39
- opts[:color][0], opts[:color][1], opts[:color][2], opts[:color][3]
40
- ])
45
+ render(x: opts[:x], y: opts[:y], width: opts[:width], height: opts[:height], color: Color.new(opts[:color]), rotate: opts[:rotate])
41
46
  end
42
47
 
43
48
  private
44
49
 
45
- def render
46
- self.class.ext_draw([
47
- self, @x, @y, @width, @height, @rotate,
48
- @color.r, @color.g, @color.b, @color.a
49
- ])
50
- end
50
+ def render(x: @x, y: @y, width: @width, height: @height, color: @color, rotate: @rotate)
51
+ vertices = Vertices.new(x, y, width, height, rotate)
51
52
 
53
+ @texture.draw(
54
+ vertices.coordinates, vertices.texture_coordinates, color
55
+ )
56
+ end
52
57
  end
53
58
  end
data/lib/ruby2d/line.rb CHANGED
@@ -14,7 +14,7 @@ module Ruby2D
14
14
  @z = opts[:z] || 0
15
15
  @width = opts[:width] || 2
16
16
  self.color = opts[:color] || 'white'
17
- self.opacity = opts[:opacity] if opts[:opacity]
17
+ self.color.opacity = opts[:opacity] if opts[:opacity]
18
18
  add
19
19
  end
20
20
 
@@ -39,6 +39,8 @@ module Ruby2D
39
39
  end
40
40
 
41
41
  def self.draw(opts = {})
42
+ Window.render_ready_check
43
+
42
44
  ext_draw([
43
45
  opts[:x1], opts[:y1], opts[:x2], opts[:y2], opts[:width],
44
46
  opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
data/lib/ruby2d/quad.rb CHANGED
@@ -25,7 +25,7 @@ module Ruby2D
25
25
  @y4 = opts[:y4] || 100
26
26
  @z = opts[:z] || 0
27
27
  self.color = opts[:color] || 'white'
28
- self.opacity = opts[:opacity] if opts[:opacity]
28
+ self.color.opacity = opts[:opacity] if opts[:opacity]
29
29
  add
30
30
  end
31
31
 
@@ -49,6 +49,8 @@ module Ruby2D
49
49
  end
50
50
 
51
51
  def self.draw(opts = {})
52
+ Window.render_ready_check
53
+
52
54
  ext_draw([
53
55
  opts[:x1], opts[:y1], opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
54
56
  opts[:x2], opts[:y2], opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
@@ -10,7 +10,7 @@ module Ruby2D
10
10
  @width = opts[:width] || 200
11
11
  @height = opts[:height] || 100
12
12
  self.color = opts[:color] || 'white'
13
- self.opacity = opts[:opacity] if opts[:opacity]
13
+ self.color.opacity = opts[:opacity] if opts[:opacity]
14
14
  update_coords(@x, @y, @width, @height)
15
15
  add
16
16
  end
@@ -31,18 +31,6 @@ module Ruby2D
31
31
  alias_method :colour, :color
32
32
  alias_method :colour=, :color=
33
33
 
34
- # Allow shortcuts for setting color values
35
- def r; self.color.r end
36
- def g; self.color.g end
37
- def b; self.color.b end
38
- def a; self.color.a end
39
- def r=(c); self.color.r = c end
40
- def g=(c); self.color.g = c end
41
- def b=(c); self.color.b = c end
42
- def a=(c); self.color.a = c end
43
- def opacity; self.color.opacity end
44
- def opacity=(val); self.color.opacity = val end
45
-
46
34
  # Add a contains method stub
47
35
  def contains?(x, y)
48
36
  x >= @x && x <= (@x + @width) && y >= @y && y <= (@y + @height)
data/lib/ruby2d/sound.rb CHANGED
@@ -26,5 +26,30 @@ module Ruby2D
26
26
  ext_length
27
27
  end
28
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
29
54
  end
30
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
 
@@ -75,38 +66,6 @@ module Ruby2D
75
66
  unless opts[:show] == false then add end
76
67
  end
77
68
 
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
108
- end
109
-
110
69
  # Play an animation
111
70
  def play(opts = {}, &done_proc)
112
71
 
@@ -165,26 +124,6 @@ module Ruby2D
165
124
  end
166
125
 
167
126
  @flip = flip
168
-
169
- # Reset flip values
170
- @flip_x = @x
171
- @flip_y = @y
172
- @flip_width = @width
173
- @flip_height = @height
174
-
175
- case flip
176
- when :horizontal
177
- @flip_x = @x + @width
178
- @flip_width = -@width
179
- when :vertical
180
- @flip_y = @y + @height
181
- @flip_height = -@height
182
- when :both # horizontal and vertical
183
- @flip_x = @x + @width
184
- @flip_width = -@width
185
- @flip_y = @y + @height
186
- @flip_height = -@height
187
- end
188
127
  end
189
128
 
190
129
  # Reset frame to defaults
@@ -248,8 +187,10 @@ module Ruby2D
248
187
  end
249
188
 
250
189
  def draw(opts = {})
251
- opts[:width] = opts[:width] || @flip_width
252
- opts[:height] = opts[:height] || @flip_height
190
+ Window.render_ready_check
191
+
192
+ opts[:width] = opts[:width] || (@width || @clip_width)
193
+ opts[:height] = opts[:height] || (@height || @clip_height)
253
194
  opts[:rotate] = opts[:rotate] || @rotate
254
195
  opts[:clip_x] = opts[:clip_x] || @clip_x
255
196
  opts[:clip_y] = opts[:clip_y] || @clip_y
@@ -259,24 +200,33 @@ module Ruby2D
259
200
  opts[:color] = [1.0, 1.0, 1.0, 1.0]
260
201
  end
261
202
 
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
- ])
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
+ })
267
211
  end
268
212
 
269
213
  private
270
214
 
271
- def render
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
+ })
272
223
  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
224
 
225
+ vertices = Vertices.new(x, y, width, height, rotate, crop: crop, flip: flip)
280
226
 
227
+ @texture.draw(
228
+ vertices.coordinates, vertices.texture_coordinates, color
229
+ )
230
+ end
281
231
  end
282
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
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,43 +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
19
+ self.color.opacity = opts[:opacity] if opts[:opacity]
20
+ @font_path = opts[:font] || Font.default
21
+ create_font
22
+ create_texture
23
+
26
24
  unless opts[:show] == false then add end
27
25
  end
28
26
 
27
+ # Returns the path of the font as a string
28
+ def font
29
+ @font_path
30
+ end
31
+
29
32
  def text=(msg)
30
33
  @text = msg.to_s
31
- ext_set(@text)
34
+ create_texture
35
+ end
36
+
37
+ def size=(size)
38
+ @size = size
39
+ create_font
40
+ create_texture
32
41
  end
33
42
 
34
43
  def draw(opts = {})
44
+ Window.render_ready_check
45
+
35
46
  opts[:rotate] = opts[:rotate] || @rotate
36
47
  unless opts[:color]
37
48
  opts[:color] = [1.0, 1.0, 1.0, 1.0]
38
49
  end
39
50
 
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
- ])
51
+ render(x: opts[:x], y: opts[:y], color: Color.new(opts[:color]), rotate: opts[:rotate])
44
52
  end
45
53
 
46
54
  private
47
55
 
48
- def render
49
- self.class.ext_draw([
50
- self, @x, @y, @rotate,
51
- @color.r, @color.g, @color.b, @color.a
52
- ])
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)
53
66
  end
54
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
55
77
  end
56
78
  end