ruby2d 0.9.4 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) 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/libmodplug.a +0 -0
  93. data/assets/macos/lib/libmpg123.a +0 -0
  94. data/assets/macos/lib/libogg.a +0 -0
  95. data/assets/macos/lib/libpng16.a +0 -0
  96. data/assets/macos/lib/libtiff.a +0 -0
  97. data/assets/macos/lib/libvorbis.a +0 -0
  98. data/assets/macos/lib/libvorbisfile.a +0 -0
  99. data/assets/macos/lib/libwebp.a +0 -0
  100. data/assets/mingw/bin/SDL2.dll +0 -0
  101. data/assets/mingw/lib/libSDL2.a +0 -0
  102. data/assets/mingw/lib/libSDL2.dll.a +0 -0
  103. data/assets/mingw/lib/libSDL2_test.a +0 -0
  104. data/assets/mingw/lib/libSDL2main.a +0 -0
  105. data/bin/ruby2d +1 -0
  106. data/{assets/linux/simple2d/src/simple2d.c → ext/ruby2d/common.c} +32 -32
  107. data/{assets/linux/simple2d/src → ext/ruby2d}/controllers.c +17 -17
  108. data/ext/ruby2d/extconf.rb +37 -59
  109. data/ext/ruby2d/font.c +35 -0
  110. data/{assets/linux/simple2d/src → ext/ruby2d}/gl.c +66 -98
  111. data/ext/ruby2d/gl2.c +86 -0
  112. data/{assets/linux/simple2d/src → ext/ruby2d}/gl3.c +69 -112
  113. data/{assets/linux/simple2d/src → ext/ruby2d}/gles.c +22 -81
  114. data/ext/ruby2d/image.c +58 -0
  115. data/{assets/linux/simple2d/src → ext/ruby2d}/input.c +8 -8
  116. data/{assets/linux/simple2d/src → ext/ruby2d}/music.c +30 -17
  117. data/ext/ruby2d/ruby2d.c +449 -439
  118. data/ext/ruby2d/ruby2d.h +652 -0
  119. data/{assets/linux/simple2d/src → ext/ruby2d}/shapes.c +18 -18
  120. data/{assets/linux/simple2d/src → ext/ruby2d}/sound.c +39 -14
  121. data/ext/ruby2d/text.c +22 -0
  122. data/{assets/linux/simple2d/src → ext/ruby2d}/window.c +63 -63
  123. data/lib/ruby2d/circle.rb +21 -3
  124. data/lib/ruby2d/cli/build.rb +4 -7
  125. data/lib/ruby2d/dsl.rb +16 -9
  126. data/lib/ruby2d/entity.rb +17 -0
  127. data/lib/ruby2d/font.rb +23 -3
  128. data/lib/ruby2d/image.rb +35 -7
  129. data/lib/ruby2d/line.rb +23 -1
  130. data/lib/ruby2d/music.rb +5 -0
  131. data/lib/ruby2d/pixel.rb +17 -0
  132. data/lib/ruby2d/quad.rb +21 -1
  133. data/lib/ruby2d/rectangle.rb +12 -3
  134. data/lib/ruby2d/renderable.rb +4 -20
  135. data/lib/ruby2d/sound.rb +30 -0
  136. data/lib/ruby2d/sprite.rb +57 -75
  137. data/lib/ruby2d/square.rb +10 -1
  138. data/lib/ruby2d/text.rb +55 -12
  139. data/lib/ruby2d/texture.rb +28 -0
  140. data/lib/ruby2d/tileset.rb +87 -0
  141. data/lib/ruby2d/triangle.rb +19 -1
  142. data/lib/ruby2d/version.rb +1 -1
  143. data/lib/ruby2d/vertices.rb +84 -0
  144. data/lib/ruby2d/window.rb +259 -22
  145. data/lib/ruby2d.rb +6 -1
  146. metadata +30 -26
  147. data/assets/include/simple2d.h +0 -757
  148. data/assets/linux/simple2d/Makefile +0 -260
  149. data/assets/linux/simple2d/bin/simple2d.sh +0 -1318
  150. data/assets/linux/simple2d/include/simple2d.h +0 -757
  151. data/assets/linux/simple2d/src/gl2.c +0 -146
  152. data/assets/linux/simple2d/src/image.c +0 -138
  153. data/assets/linux/simple2d/src/sprite.c +0 -147
  154. data/assets/linux/simple2d/src/text.c +0 -129
  155. data/assets/macos/lib/libsimple2d.a +0 -0
  156. data/assets/mingw/lib/libsimple2d.a +0 -0
@@ -25,6 +25,8 @@ require 'fileutils'
25
25
  'text',
26
26
  'sound',
27
27
  'music',
28
+ 'texture',
29
+ 'vertices',
28
30
  '../ruby2d'
29
31
  ]
30
32
 
@@ -96,7 +98,7 @@ def build_native(rb_file)
96
98
  end
97
99
 
98
100
  # Compile to a native executable
99
- `cc build/app.c -lmruby \`simple2d --libs\` -o build/app`
101
+ `cc build/app.c -lmruby -o build/app`
100
102
 
101
103
  # Clean up
102
104
  clean_up unless @debug
@@ -162,12 +164,6 @@ end
162
164
  def build_ios_tvos(rb_file, device)
163
165
  check_build_src_file(rb_file)
164
166
 
165
- # Check for Simple 2D framework,
166
- unless File.exist?('/usr/local/Frameworks/Simple2D/iOS/Simple2D.framework') && File.exist?('/usr/local/Frameworks/Simple2D/tvOS/Simple2D.framework')
167
- puts "#{'Error:'.error} Simple 2D iOS and tvOS frameworks not found. Install them and try again.\n"
168
- exit
169
- end
170
-
171
167
  # Check if MRuby exists; if not, quit
172
168
  if `which mruby`.empty?
173
169
  puts "#{'Error:'.error} Can't find MRuby, which is needed to build native Ruby 2D applications.\n"
@@ -197,6 +193,7 @@ def build_ios_tvos(rb_file, device)
197
193
  f << File.read("#{@gem_dir}/ext/ruby2d/ruby2d.c")
198
194
  end
199
195
 
196
+ # TODO: Need add this functionality to the gem
200
197
  # Build the Xcode project
201
198
  `simple2d build --#{device} build/#{device}/MyApp.xcodeproj`
202
199
 
data/lib/ruby2d/dsl.rb CHANGED
@@ -2,38 +2,45 @@
2
2
 
3
3
  module Ruby2D::DSL
4
4
 
5
- Ruby2D::Window.new
5
+ @window = Ruby2D::Window.new
6
+
7
+ def self.window; @window end
8
+ def self.window=(w); @window = w end
6
9
 
7
10
  def get(sym, opts = nil)
8
- Window.get(sym, opts)
11
+ DSL.window.get(sym, opts)
9
12
  end
10
13
 
11
14
  def set(opts)
12
- Window.set(opts)
15
+ DSL.window.set(opts)
13
16
  end
14
17
 
15
18
  def on(event, &proc)
16
- Window.on(event, &proc)
19
+ DSL.window.on(event, &proc)
17
20
  end
18
21
 
19
22
  def off(event_descriptor)
20
- Window.off(event_descriptor)
23
+ DSL.window.off(event_descriptor)
21
24
  end
22
25
 
23
26
  def update(&proc)
24
- Window.update(&proc)
27
+ DSL.window.update(&proc)
28
+ end
29
+
30
+ def render(&proc)
31
+ DSL.window.render(&proc)
25
32
  end
26
33
 
27
34
  def clear
28
- Window.clear
35
+ DSL.window.clear
29
36
  end
30
37
 
31
38
  def show
32
- Window.show
39
+ DSL.window.show
33
40
  end
34
41
 
35
42
  def close
36
- Window.close
43
+ DSL.window.close
37
44
  end
38
45
 
39
46
  end
@@ -0,0 +1,17 @@
1
+ # Ruby2D::Entity
2
+
3
+ module Ruby2D
4
+ class Entity
5
+
6
+ # Add the entity to the window
7
+ def add
8
+ Window.add(self)
9
+ end
10
+
11
+ # Remove the entity from the window
12
+ def remove
13
+ Window.remove(self)
14
+ end
15
+
16
+ end
17
+ end
data/lib/ruby2d/font.rb CHANGED
@@ -2,8 +2,22 @@
2
2
 
3
3
  module Ruby2D
4
4
  class Font
5
+ @@loaded_fonts = {}
6
+
7
+ attr_reader :ttf_font
8
+
9
+ def initialize(path, size, style=nil)
10
+ @ttf_font = Font.ext_load(path, size, style.to_s)
11
+ end
5
12
 
6
13
  class << self
14
+ def load(path, size, style=nil)
15
+ unless File.exist? path
16
+ raise Error, "Cannot find font file `#{path}`"
17
+ end
18
+
19
+ @@loaded_fonts[[path, size, style]] ||= Font.new(path, size, style)
20
+ end
7
21
 
8
22
  # List all fonts, names only
9
23
  def all
@@ -50,6 +64,7 @@ module Ruby2D
50
64
  macos_font_path = '/Library/Fonts'
51
65
  linux_font_path = '/usr/share/fonts'
52
66
  windows_font_path = 'C:/Windows/Fonts'
67
+ openbsd_font_path = '/usr/X11R6/lib/X11/fonts'
53
68
 
54
69
  # If MRI and/or non-Bash shell (like cmd.exe)
55
70
  if Object.const_defined? :RUBY_PLATFORM
@@ -60,15 +75,20 @@ module Ruby2D
60
75
  linux_font_path
61
76
  when /mingw/
62
77
  windows_font_path
78
+ when /openbsd/
79
+ openbsd_font_path
63
80
  end
64
81
  # If MRuby
65
82
  else
66
- if `uname`.include? 'Darwin' # macOS
83
+ uname = `uname`
84
+ if uname.include? 'Darwin' # macOS
67
85
  macos_font_path
68
- elsif `uname`.include? 'Linux'
86
+ elsif uname.include? 'Linux'
69
87
  linux_font_path
70
- elsif `uname`.include? 'MINGW'
88
+ elsif uname.include? 'MINGW'
71
89
  windows_font_path
90
+ elsif uname.include? 'OpenBSD'
91
+ openbsd_font_path
72
92
  end
73
93
  end
74
94
  end
data/lib/ruby2d/image.rb CHANGED
@@ -7,24 +7,52 @@ 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"
30
+ self.color.opacity = opts[:opacity] if opts[:opacity]
31
+
32
+ unless opts[:show] == false then add end
33
+ end
34
+
35
+ def draw(opts = {})
36
+ Window.render_ready_check
37
+
38
+ opts[:width] = opts[:width] || @width
39
+ opts[:height] = opts[:height] || @height
40
+ opts[:rotate] = opts[:rotate] || @rotate
41
+ unless opts[:color]
42
+ opts[:color] = [1.0, 1.0, 1.0, 1.0]
25
43
  end
26
- add
44
+
45
+ render(x: opts[:x], y: opts[:y], width: opts[:width], height: opts[:height], color: Color.new(opts[:color]), rotate: opts[:rotate])
27
46
  end
28
47
 
48
+ private
49
+
50
+ def render(x: @x, y: @y, width: @width, height: @height, color: @color, rotate: @rotate)
51
+ vertices = Vertices.new(x, y, width, height, rotate)
52
+
53
+ @texture.draw(
54
+ vertices.coordinates, vertices.texture_coordinates, color
55
+ )
56
+ end
29
57
  end
30
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
 
@@ -38,8 +38,30 @@ module Ruby2D
38
38
  (((@y2 - @y1) * x - (@x2 - @x1) * y + @x2 * @y1 - @y2 * @x1).abs / length) <= 0.5 * @width
39
39
  end
40
40
 
41
+ def self.draw(opts = {})
42
+ Window.render_ready_check
43
+
44
+ ext_draw([
45
+ opts[:x1], opts[:y1], opts[:x2], opts[:y2], opts[:width],
46
+ opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
47
+ opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
48
+ opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3],
49
+ opts[:color][3][0], opts[:color][3][1], opts[:color][3][2], opts[:color][3][3]
50
+ ])
51
+ end
52
+
41
53
  private
42
54
 
55
+ def render
56
+ self.class.ext_draw([
57
+ @x1, @y1, @x2, @y2, @width,
58
+ @c1.r, @c1.g, @c1.b, @c1.a,
59
+ @c2.r, @c2.g, @c2.b, @c2.a,
60
+ @c3.r, @c3.g, @c3.b, @c3.a,
61
+ @c4.r, @c4.g, @c4.b, @c4.a
62
+ ])
63
+ end
64
+
43
65
  # Calculate the distance between two points
44
66
  def points_distance(x1, y1, x2, y2)
45
67
  Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
data/lib/ruby2d/music.rb CHANGED
@@ -58,5 +58,10 @@ module Ruby2D
58
58
  ext_fadeout(ms)
59
59
  end
60
60
 
61
+ # Returns the length in seconds
62
+ def length
63
+ ext_length
64
+ end
65
+
61
66
  end
62
67
  end
@@ -0,0 +1,17 @@
1
+ # Ruby2D::Pixel
2
+
3
+ module Ruby2D
4
+ class Pixel
5
+
6
+ def self.draw(opts = {})
7
+ ext_draw([
8
+ opts[:x] , opts[:y],
9
+ opts[:x] + opts[:size], opts[:y],
10
+ opts[:x] + opts[:size], opts[:y] + opts[:size],
11
+ opts[:x] , opts[:y] + opts[:size],
12
+ opts[:color][0], opts[:color][1], opts[:color][2], opts[:color][3]
13
+ ])
14
+ end
15
+
16
+ end
17
+ end
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
 
@@ -48,8 +48,28 @@ module Ruby2D
48
48
  questioned_area <= self_area
49
49
  end
50
50
 
51
+ def self.draw(opts = {})
52
+ Window.render_ready_check
53
+
54
+ ext_draw([
55
+ opts[:x1], opts[:y1], opts[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
56
+ opts[:x2], opts[:y2], opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
57
+ opts[:x3], opts[:y3], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3],
58
+ opts[:x4], opts[:y4], opts[:color][3][0], opts[:color][3][1], opts[:color][3][2], opts[:color][3][3]
59
+ ])
60
+ end
61
+
51
62
  private
52
63
 
64
+ def render
65
+ self.class.ext_draw([
66
+ @x1, @y1, @c1.r, @c1.g, @c1.b, @c1.a,
67
+ @x2, @y2, @c2.r, @c2.g, @c2.b, @c2.a,
68
+ @x3, @y3, @c3.r, @c3.g, @c3.b, @c3.a,
69
+ @x4, @y4, @c4.r, @c4.g, @c4.b, @c4.a
70
+ ])
71
+ end
72
+
53
73
  def triangle_area(x1, y1, x2, y2, x3, y3)
54
74
  (x1*y2 + x2*y3 + x3*y1 - x3*y2 - x1*y3 - x2*y1).abs / 2
55
75
  end
@@ -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
@@ -39,6 +39,15 @@ module Ruby2D
39
39
  update_coords(@x, @y, @width, h)
40
40
  end
41
41
 
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
+ ])
49
+ end
50
+
42
51
  private
43
52
 
44
53
  def update_coords(x, y, w, h)
@@ -46,10 +55,10 @@ module Ruby2D
46
55
  @y1 = y
47
56
  @x2 = x + w
48
57
  @y2 = y
49
- @x4 = x
50
- @y4 = y + h
51
58
  @x3 = x + w
52
59
  @y3 = y + h
60
+ @x4 = x
61
+ @y4 = y + h
53
62
  end
54
63
 
55
64
  end
@@ -14,16 +14,12 @@ module Ruby2D
14
14
 
15
15
  # Add the object to the window
16
16
  def add
17
- if Module.const_defined? :DSL
18
- Window.add(self)
19
- end
17
+ Window.add(self)
20
18
  end
21
19
 
22
20
  # Remove the object from the window
23
21
  def remove
24
- if Module.const_defined? :DSL
25
- Window.remove(self)
26
- end
22
+ Window.remove(self)
27
23
  end
28
24
 
29
25
  # Set the color value
@@ -32,20 +28,8 @@ module Ruby2D
32
28
  end
33
29
 
34
30
  # Allow British English spelling of color
35
- def colour; self.color end
36
- def colour=(c); self.color = c end
37
-
38
- # Allow shortcuts for setting color values
39
- def r; self.color.r end
40
- def g; self.color.g end
41
- def b; self.color.b end
42
- def a; self.color.a end
43
- def r=(c); self.color.r = c end
44
- def g=(c); self.color.g = c end
45
- def b=(c); self.color.b = c end
46
- def a=(c); self.color.a = c end
47
- def opacity; self.color.opacity end
48
- def opacity=(val); self.color.opacity = val end
31
+ alias_method :colour, :color
32
+ alias_method :colour=, :color=
49
33
 
50
34
  # Add a contains method stub
51
35
  def contains?(x, y)
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