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
data/lib/ruby2d/color.rb CHANGED
@@ -1,23 +1,45 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Ruby2D::Color
2
4
 
3
5
  module Ruby2D
6
+ # Colors are represented by the Color class. Colors can be created from keywords
7
+ # (based on css), a hexadecimal value or an array containing a collection of
8
+ # red, green, blue, and alpha (transparency) values expressed as a Float from 0.0 to 1.0.
4
9
  class Color
5
-
6
- # Color::Set represents an array of colors
10
+ # Color::Set represents an array of colors.
7
11
  class Set
12
+ include Enumerable
13
+
8
14
  def initialize(colors)
9
15
  @colors = colors.map { |c| Color.new(c) }
10
16
  end
11
17
 
12
- def [](i)
13
- @colors[i]
18
+ def [](index)
19
+ @colors[index]
14
20
  end
15
21
 
16
22
  def length
17
23
  @colors.length
18
24
  end
19
25
 
20
- def opacity; @colors[0].opacity end
26
+ alias count length
27
+
28
+ def each(&block)
29
+ @colors.each(&block)
30
+ end
31
+
32
+ def first
33
+ @colors.first
34
+ end
35
+
36
+ def last
37
+ @colors.last
38
+ end
39
+
40
+ def opacity
41
+ @colors.first.opacity
42
+ end
21
43
 
22
44
  def opacity=(opacity)
23
45
  @colors.each do |color|
@@ -29,106 +51,140 @@ module Ruby2D
29
51
  attr_accessor :r, :g, :b, :a
30
52
 
31
53
  # Based on clrs.cc
32
- @@colors = {
33
- 'navy' => '#001F3F',
34
- 'blue' => '#0074D9',
35
- 'aqua' => '#7FDBFF',
36
- 'teal' => '#39CCCC',
37
- 'olive' => '#3D9970',
38
- 'green' => '#2ECC40',
39
- 'lime' => '#01FF70',
40
- 'yellow' => '#FFDC00',
41
- 'orange' => '#FF851B',
42
- 'red' => '#FF4136',
43
- 'brown' => '#663300',
54
+ NAMED_COLORS = {
55
+ 'navy' => '#001F3F',
56
+ 'blue' => '#0074D9',
57
+ 'aqua' => '#7FDBFF',
58
+ 'teal' => '#39CCCC',
59
+ 'olive' => '#3D9970',
60
+ 'green' => '#2ECC40',
61
+ 'lime' => '#01FF70',
62
+ 'yellow' => '#FFDC00',
63
+ 'orange' => '#FF851B',
64
+ 'red' => '#FF4136',
65
+ 'brown' => '#663300',
44
66
  'fuchsia' => '#F012BE',
45
- 'purple' => '#B10DC9',
46
- 'maroon' => '#85144B',
47
- 'white' => '#FFFFFF',
48
- 'silver' => '#DDDDDD',
49
- 'gray' => '#AAAAAA',
50
- 'black' => '#111111',
51
- 'random' => ''
52
- }
53
-
54
- def initialize(c)
55
- if !self.class.is_valid? c
56
- raise Error, "`#{c}` is not a valid color"
57
- else
58
- case c
59
- when String
60
- if c == 'random'
61
- @r, @g, @b, @a = rand, rand, rand, 1.0
62
- elsif self.class.is_hex?(c)
63
- @r, @g, @b, @a = hex_to_f(c)
64
- else
65
- @r, @g, @b, @a = hex_to_f(@@colors[c])
66
- end
67
- when Array
68
- @r, @g, @b, @a = [c[0], c[1], c[2], c[3]]
69
- when Color
70
- @r, @g, @b, @a = [c.r, c.g, c.b, c.a]
71
- end
67
+ 'purple' => '#B10DC9',
68
+ 'maroon' => '#85144B',
69
+ 'white' => '#FFFFFF',
70
+ 'silver' => '#DDDDDD',
71
+ 'gray' => '#AAAAAA',
72
+ 'black' => '#111111',
73
+ 'random' => ''
74
+ }.freeze
75
+
76
+ def initialize(color)
77
+ raise Error, "`#{color}` is not a valid color" unless self.class.valid? color
78
+
79
+ case color
80
+ when String
81
+ init_from_string color
82
+ when Array
83
+ @r = color[0]
84
+ @g = color[1]
85
+ @b = color[2]
86
+ @a = color[3]
87
+ when Color
88
+ @r = color.r
89
+ @g = color.g
90
+ @b = color.b
91
+ @a = color.a
72
92
  end
73
93
  end
74
94
 
75
- # Return a color set if an array of valid colors
76
- def self.set(colors)
77
- # If a valid array of colors, return a `Color::Set` with those colors
78
- if colors.is_a?(Array) && colors.all? { |el| Color.is_valid? el }
79
- Color::Set.new(colors)
80
- # Otherwise, return single color
81
- else
82
- Color.new(colors)
95
+ class << self
96
+ # Return a color set if an array of valid colors
97
+ def set(colors)
98
+ # If a valid array of colors, return a `Color::Set` with those colors
99
+ if colors.is_a?(Array) && colors.all? { |el| Color.valid? el }
100
+ Color::Set.new(colors)
101
+ # Otherwise, return single color
102
+ else
103
+ Color.new(colors)
104
+ end
83
105
  end
84
- end
85
106
 
86
- # Check if string is a proper hex value
87
- def self.is_hex?(s)
88
- # MRuby doesn't support regex, otherwise we'd do:
89
- # !(/^#[0-9A-F]{6}$/i.match(a).nil?)
90
- s.class == String && s[0] == '#' && s.length == 7
91
- end
107
+ # Check if string is a proper hex value
108
+ def hex?(color_string)
109
+ # MRuby doesn't support regex, otherwise we'd do:
110
+ # !(/^#[0-9A-F]{6}$/i.match(a).nil?)
111
+ color_string.instance_of?(String) &&
112
+ color_string[0] == '#' &&
113
+ color_string.length == 7
114
+ end
92
115
 
93
- # Check if the color is valid
94
- def self.is_valid?(c)
95
- c.is_a?(Color) || # color object
96
- @@colors.key?(c) || # keyword
97
- self.is_hex?(c) || # hexadecimal value
116
+ # Check if the color is valid
117
+ def valid?(color)
118
+ color.is_a?(Color) || # color object
119
+ NAMED_COLORS.key?(color) || # keyword
120
+ hex?(color) || # hexadecimal value
121
+ ( # Array of Floats from 0.0..1.0
122
+ color.instance_of?(Array) &&
123
+ color.length == 4 &&
124
+ color.all? { |el| el.is_a?(Numeric) }
125
+ )
126
+ end
98
127
 
99
- # Array of Floats from 0.0..1.0
100
- c.class == Array && c.length == 4 &&
101
- c.all? { |el| el.is_a?(Numeric) }
128
+ #
129
+ # Backwards compatibility
130
+ alias is_valid? valid?
131
+ alias is_hex? valid?
102
132
  end
103
133
 
104
134
  # Convenience methods to alias `opacity` to `@a`
105
- def opacity; @a end
106
- def opacity=(opacity); @a = opacity end
135
+ def opacity
136
+ @a
137
+ end
138
+
139
+ def opacity=(opacity)
140
+ @a = opacity
141
+ end
142
+
143
+ # Return colour components as an array +[r, g, b, a]+
144
+ def to_a
145
+ [@r, @g, @b, @a]
146
+ end
107
147
 
108
148
  private
109
149
 
150
+ def init_from_string(color)
151
+ if color == 'random'
152
+ init_random_color
153
+ elsif self.class.hex?(color)
154
+ @r, @g, @b, @a = hex_to_f(color)
155
+ else
156
+ @r, @g, @b, @a = hex_to_f(NAMED_COLORS[color])
157
+ end
158
+ end
159
+
160
+ def init_random_color
161
+ @r = rand
162
+ @g = rand
163
+ @b = rand
164
+ @a = 1.0
165
+ end
166
+
110
167
  # Convert from Fixnum (0..255) to Float (0.0..1.0)
111
- def to_f(a)
168
+ def i_to_f(color_array)
112
169
  b = []
113
- a.each do |n|
170
+ color_array.each do |n|
114
171
  b.push(n / 255.0)
115
172
  end
116
- return b
173
+ b
117
174
  end
118
175
 
119
176
  # Convert from hex value (e.g. #FFF000) to Float (0.0..1.0)
120
- def hex_to_f(h)
121
- h = (h[1..-1]).chars.each_slice(2).map(&:join)
177
+ def hex_to_f(hex_color)
178
+ hex_color = (hex_color[1..]).chars.each_slice(2).map(&:join)
122
179
  a = []
123
180
 
124
- h.each do |el|
181
+ hex_color.each do |el|
125
182
  a.push(el.to_i(16))
126
183
  end
127
184
 
128
185
  a.push(255)
129
- return to_f(a)
186
+ i_to_f(a)
130
187
  end
131
-
132
188
  end
133
189
 
134
190
  # Allow British English spelling of color
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Ruby2D module and native extension loader
4
+
5
+ unless RUBY_ENGINE == 'mruby'
6
+ require 'ruby2d/cli/colorize'
7
+ require 'ruby2d/exceptions'
8
+ require 'ruby2d/renderable'
9
+ require 'ruby2d/color'
10
+ require 'ruby2d/window'
11
+ require 'ruby2d/dsl'
12
+ require 'ruby2d/quad'
13
+ require 'ruby2d/line'
14
+ require 'ruby2d/circle'
15
+ require 'ruby2d/rectangle'
16
+ require 'ruby2d/square'
17
+ require 'ruby2d/triangle'
18
+ require 'ruby2d/pixel'
19
+ require 'ruby2d/pixmap'
20
+ require 'ruby2d/pixmap_atlas'
21
+ require 'ruby2d/image'
22
+ require 'ruby2d/sprite'
23
+ require 'ruby2d/tileset'
24
+ require 'ruby2d/font'
25
+ require 'ruby2d/text'
26
+ require 'ruby2d/canvas'
27
+ require 'ruby2d/sound'
28
+ require 'ruby2d/music'
29
+ require 'ruby2d/texture'
30
+ require 'ruby2d/vertices'
31
+ require 'ruby2d/ruby2d' # load native extension
32
+ end
data/lib/ruby2d/dsl.rb CHANGED
@@ -1,46 +1,52 @@
1
- # Ruby2D::DSL
1
+ # frozen_string_literal: true
2
2
 
3
- module Ruby2D::DSL
3
+ module Ruby2D
4
+ # Ruby2D::DSL
5
+ module DSL
6
+ @window = Ruby2D::Window.new
4
7
 
5
- @window = Ruby2D::Window.new
8
+ def self.window
9
+ @window
10
+ end
6
11
 
7
- def self.window; @window end
8
- def self.window=(w); @window = w end
12
+ def self.window=(window)
13
+ @window = window
14
+ end
9
15
 
10
- def get(sym, opts = nil)
11
- DSL.window.get(sym, opts)
12
- end
16
+ def get(sym, opts = nil)
17
+ DSL.window.get(sym, opts)
18
+ end
13
19
 
14
- def set(opts)
15
- DSL.window.set(opts)
16
- end
20
+ def set(opts)
21
+ DSL.window.set(opts)
22
+ end
17
23
 
18
- def on(event, &proc)
19
- DSL.window.on(event, &proc)
20
- end
24
+ def on(event, &proc)
25
+ DSL.window.on(event, &proc)
26
+ end
21
27
 
22
- def off(event_descriptor)
23
- DSL.window.off(event_descriptor)
24
- end
28
+ def off(event_descriptor)
29
+ DSL.window.off(event_descriptor)
30
+ end
25
31
 
26
- def update(&proc)
27
- DSL.window.update(&proc)
28
- end
32
+ def update(&proc)
33
+ DSL.window.update(&proc)
34
+ end
29
35
 
30
- def render(&proc)
31
- DSL.window.render(&proc)
32
- end
36
+ def render(&proc)
37
+ DSL.window.render(&proc)
38
+ end
33
39
 
34
- def clear
35
- DSL.window.clear
36
- end
40
+ def clear
41
+ DSL.window.clear
42
+ end
37
43
 
38
- def show
39
- DSL.window.show
40
- end
44
+ def show
45
+ DSL.window.show
46
+ end
41
47
 
42
- def close
43
- DSL.window.close
48
+ def close
49
+ DSL.window.close
50
+ end
44
51
  end
45
-
46
52
  end
@@ -1,6 +1,7 @@
1
- # Ruby2D::Error
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Ruby2D
4
+ # Ruby2D::Error
4
5
  class Error < StandardError
5
6
  end
6
7
  end
data/lib/ruby2d/font.rb CHANGED
@@ -1,27 +1,33 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Ruby2D::Font
2
4
 
3
5
  module Ruby2D
6
+ #
7
+ # Font represents a single typeface in a specific size and style.
8
+ # Use +Font.load+ to load/retrieve a +Font+ instance by path, size and style.
4
9
  class Font
5
10
  FONT_CACHE_LIMIT = 100
6
11
 
7
- @@loaded_fonts = {}
8
-
9
12
  attr_reader :ttf_font
10
13
 
11
- def initialize(path, size, style=nil)
12
- @ttf_font = Font.ext_load(path, size, style.to_s)
13
- end
14
+ # Cache loaded fonts in a class variable
15
+ @loaded_fonts = {}
14
16
 
15
17
  class << self
16
- def load(path, size, style=nil)
17
- unless File.exist? path
18
- raise Error, "Cannot find font file `#{path}`"
19
- end
20
-
21
- (@@loaded_fonts[[path, size, style]] ||= Font.new(path, size, style)).tap do |font|
22
- if @@loaded_fonts.size > FONT_CACHE_LIMIT
23
- @@loaded_fonts.shift
24
- end
18
+ # Return a font by +path+, +size+ and +style+, loading the font if not already in the cache.
19
+ #
20
+ # @param path [#to_s] Full path to the font file
21
+ # @param size [Numeric] Size of font to setup
22
+ # @param style [String] Font style
23
+ #
24
+ # @return [Font]
25
+ def load(path, size, style = nil)
26
+ path = path.to_s
27
+ raise Error, "Cannot find font file `#{path}`" unless File.exist? path
28
+
29
+ (@loaded_fonts[[path, size, style]] ||= Font.send(:new, path, size, style)).tap do |_font|
30
+ @loaded_fonts.shift if @loaded_fonts.size > FONT_CACHE_LIMIT
25
31
  end
26
32
  end
27
33
 
@@ -30,76 +36,105 @@ module Ruby2D
30
36
  all_paths.map { |path| path.split('/').last.chomp('.ttf').downcase }.uniq.sort
31
37
  end
32
38
 
33
- # Find a font file path from its name
39
+ # Find a font file path from its name, if it exists in the platforms list of fonts
40
+ # @return [String] full path if +font_name+ is known
41
+ # @return [nil] if +font_name+ is unknown
34
42
  def path(font_name)
35
43
  all_paths.find { |path| path.downcase.include?(font_name) }
36
44
  end
37
45
 
38
- # Get all fonts with full file paths
39
- def all_paths
40
- # MRuby does not have `Dir` defined
41
- if RUBY_ENGINE == 'mruby'
42
- fonts = `find #{directory} -name *.ttf`.split("\n")
43
- # If MRI and/or non-Bash shell (like cmd.exe)
46
+ # Get full path to the default font
47
+ def default
48
+ if all.include? 'arial'
49
+ path 'arial'
44
50
  else
45
- fonts = Dir["#{directory}/**/*.ttf"]
51
+ all_paths.first
46
52
  end
53
+ end
47
54
 
48
- fonts = fonts.reject do |f|
49
- f.downcase.include?('bold') ||
50
- f.downcase.include?('italic') ||
51
- f.downcase.include?('oblique') ||
52
- f.downcase.include?('narrow') ||
53
- f.downcase.include?('black')
54
- end
55
+ # Font cannot be instantiated directly; use +Font.load+ instead.
56
+ private :new
57
+
58
+ private
59
+
60
+ # Get all fonts with full file paths
61
+ def all_paths
62
+ # memoize so we only calculate once
63
+ @all_paths ||= platform_font_paths
64
+ end
55
65
 
66
+ # Compute and return all platform font file paths, removing variants by style
67
+ def platform_font_paths
68
+ fonts = find_os_font_files.reject do |f|
69
+ f.downcase.include?('bold') ||
70
+ f.downcase.include?('italic') ||
71
+ f.downcase.include?('oblique') ||
72
+ f.downcase.include?('narrow') ||
73
+ f.downcase.include?('black')
74
+ end
56
75
  fonts.sort_by { |f| f.downcase.chomp '.ttf' }
57
76
  end
58
77
 
59
- # Get the default font
60
- def default
61
- if all.include? 'arial'
62
- path 'arial'
78
+ # Return all font files in the platform's font location
79
+ def find_os_font_files
80
+ if RUBY_ENGINE == 'mruby'
81
+ # MRuby does not have `Dir` defined
82
+ `find #{directory} -name *.ttf`.split("\n")
63
83
  else
64
- all_paths.first
84
+ # If MRI and/or non-Bash shell (like cmd.exe)
85
+ Dir["#{directory}/**/*.ttf"]
65
86
  end
66
87
  end
67
88
 
89
+ # Mapping of OS names to platform-specific font file locations
90
+ OS_FONT_PATHS = {
91
+ macos: '/Library/Fonts',
92
+ linux: '/usr/share/fonts',
93
+ windows: 'C:/Windows/Fonts',
94
+ openbsd: '/usr/X11R6/lib/X11/fonts'
95
+ }.freeze
96
+
68
97
  # Get the fonts directory for the current platform
69
98
  def directory
70
- macos_font_path = '/Library/Fonts'
71
- linux_font_path = '/usr/share/fonts'
72
- windows_font_path = 'C:/Windows/Fonts'
73
- openbsd_font_path = '/usr/X11R6/lib/X11/fonts'
74
-
75
99
  # If MRI and/or non-Bash shell (like cmd.exe)
76
- if Object.const_defined? :RUBY_PLATFORM
77
- case RUBY_PLATFORM
78
- when /darwin/ # macOS
79
- macos_font_path
80
- when /linux/
81
- linux_font_path
82
- when /mingw/
83
- windows_font_path
84
- when /openbsd/
85
- openbsd_font_path
86
- end
87
- # If MRuby
88
- else
89
- uname = `uname`
90
- if uname.include? 'Darwin' # macOS
91
- macos_font_path
92
- elsif uname.include? 'Linux'
93
- linux_font_path
94
- elsif uname.include? 'MINGW'
95
- windows_font_path
96
- elsif uname.include? 'OpenBSD'
97
- openbsd_font_path
98
- end
100
+ # memoize so we only calculate once
101
+ @directory ||= OS_FONT_PATHS[ruby_platform_osname || sys_uname_osname]
102
+ end
103
+
104
+ # Uses RUBY_PLATFORM to identify OS
105
+ def ruby_platform_osname
106
+ return unless Object.const_defined? :RUBY_PLATFORM
107
+
108
+ case RUBY_PLATFORM
109
+ when /darwin/ # macOS
110
+ :macos
111
+ when /linux/
112
+ :linux
113
+ when /mingw/
114
+ :windows
115
+ when /openbsd/
116
+ :openbsd
99
117
  end
100
118
  end
101
119
 
120
+ # Uses uname command to identify OS
121
+ def sys_uname_osname
122
+ uname = `uname`
123
+ if uname.include? 'Darwin' # macOS
124
+ :macos
125
+ elsif uname.include? 'Linux'
126
+ :linux
127
+ elsif uname.include? 'MINGW'
128
+ :windows
129
+ elsif uname.include? 'OpenBSD'
130
+ :openbsd
131
+ end
132
+ end
102
133
  end
103
134
 
135
+ # Private constructor, called internally using +Font.send(:new,...)+
136
+ def initialize(path, size, style = nil)
137
+ @ttf_font = Font.ext_load(path.to_s, size, style.to_s)
138
+ end
104
139
  end
105
140
  end