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
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
@@ -17,7 +17,7 @@ module Ruby2D
17
17
  @y3 = opts[:y3] || 100
18
18
  @z = opts[:z] || 0
19
19
  self.color = opts[:color] || 'white'
20
- self.opacity = opts[:opacity] if opts[:opacity]
20
+ self.color.opacity = opts[:opacity] if opts[:opacity]
21
21
  add
22
22
  end
23
23
 
@@ -38,8 +38,26 @@ module Ruby2D
38
38
  questioned_area <= self_area
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[:color][0][0], opts[:color][0][1], opts[:color][0][2], opts[:color][0][3],
46
+ opts[:x2], opts[:y2], opts[:color][1][0], opts[:color][1][1], opts[:color][1][2], opts[:color][1][3],
47
+ opts[:x3], opts[:y3], opts[:color][2][0], opts[:color][2][1], opts[:color][2][2], opts[:color][2][3]
48
+ ])
49
+ end
50
+
41
51
  private
42
52
 
53
+ def render
54
+ self.class.ext_draw([
55
+ @x1, @y1, @c1.r, @c1.g, @c1.b, @c1.a,
56
+ @x2, @y2, @c2.r, @c2.g, @c2.b, @c2.a,
57
+ @x3, @y3, @c3.r, @c3.g, @c3.b, @c3.a
58
+ ])
59
+ end
60
+
43
61
  def triangle_area(x1, y1, x2, y2, x3, y3)
44
62
  (x1*y2 + x2*y3 + x3*y1 - x3*y2 - x1*y3 - x2*y1).abs / 2
45
63
  end
@@ -1,5 +1,5 @@
1
1
  # Ruby2D::VERSION
2
2
 
3
3
  module Ruby2D
4
- VERSION = '0.9.4'
4
+ VERSION = '0.11.1'
5
5
  end
@@ -0,0 +1,84 @@
1
+ # Ruby2D::Vertices
2
+
3
+ # This class generates a vertices array which are passed to the openGL rendering code.
4
+ # The vertices array is split up into 4 groups (1 - top left, 2 - top right, 3 - bottom right, 4 - bottom left)
5
+ # This class is responsible for transforming textures, it can scale / crop / rotate and flip textures
6
+
7
+ module Ruby2D
8
+ class Vertices
9
+ def initialize(x, y, width, height, rotate, crop: nil, flip: nil)
10
+ @flip = flip
11
+ @x = x
12
+ @y = y
13
+ @width = width.to_f
14
+ @height = height.to_f
15
+
16
+ if @flip == :horizontal || @flip == :both
17
+ @x = @x + @width
18
+ @width = -@width
19
+ end
20
+
21
+ if @flip == :vertical || @flip == :both
22
+ @y = y + @height
23
+ @height = -@height
24
+ end
25
+
26
+ @rotate = rotate
27
+ @rx = @x + (@width / 2.0)
28
+ @ry = @y + (@height / 2.0)
29
+ @crop = crop
30
+ end
31
+
32
+ def coordinates
33
+ x1, y1 = rotate(@x, @y); # Top left
34
+ x2, y2 = rotate(@x + @width, @y); # Top right
35
+ x3, y3 = rotate(@x + @width, @y + @height); # Bottom right
36
+ x4, y4 = rotate(@x, @y + @height); # Bottom left
37
+
38
+ [ x1, y1, x2, y2, x3, y3, x4, y4 ]
39
+ end
40
+
41
+ def texture_coordinates
42
+ if @crop.nil?
43
+ tx1 = 0.0; ty1 = 0.0 # Top left
44
+ tx2 = 1.0; ty2 = 0.0 # Top right
45
+ tx3 = 1.0; ty3 = 1.0 # Bottom right
46
+ tx4 = 0.0; ty4 = 1.0 # Bottom left
47
+ else
48
+ tx1 = @crop[:x] / @crop[:image_width].to_f; ty1 = @crop[:y] / @crop[:image_height].to_f # Top left
49
+ tx2 = tx1 + (@crop[:width] / @crop[:image_width].to_f); ty2 = ty1 # Top right
50
+ tx3 = tx2; ty3 = ty1 + (@crop[:height] / @crop[:image_height].to_f) # Botttom right
51
+ tx4 = tx1; ty4 = ty3 # Bottom left
52
+ end
53
+
54
+ [ tx1, ty1, tx2, ty2, tx3, ty3, tx4, ty4 ]
55
+ end
56
+
57
+ private
58
+
59
+ def rotate(x, y)
60
+ return [x, y] if @rotate == 0
61
+
62
+ # Convert from degrees to radians
63
+ angle = @rotate * Math::PI / 180.0
64
+
65
+ # Get the sine and cosine of the angle
66
+ sa = Math.sin(angle)
67
+ ca = Math.cos(angle)
68
+
69
+ # Translate point to origin
70
+ x -= @rx
71
+ y -= @ry
72
+
73
+ # Rotate point
74
+ xnew = x * ca - y * sa;
75
+ ynew = x * sa + y * ca;
76
+
77
+ # Translate point back
78
+ x = xnew + @rx;
79
+ y = ynew + @ry;
80
+
81
+ [x, y]
82
+ end
83
+ end
84
+ end