ruby2d 0.11.3 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/assets/include/GLES2/gl2.h +656 -0
- data/assets/include/GLES2/gl2ext.h +3949 -0
- data/assets/include/GLES2/gl2ext_angle.h +701 -0
- data/assets/include/GLES2/gl2platform.h +27 -0
- data/assets/include/GLES3/gl3.h +1192 -0
- data/assets/include/GLES3/gl31.h +1507 -0
- data/assets/include/GLES3/gl32.h +1808 -0
- data/assets/include/GLES3/gl3platform.h +27 -0
- data/assets/include/KHR/khrplatform.h +290 -0
- data/assets/include/SDL2/SDL.h +1 -0
- data/assets/include/SDL2/SDL_assert.h +4 -2
- data/assets/include/SDL2/SDL_atomic.h +20 -0
- data/assets/include/SDL2/SDL_audio.h +40 -4
- data/assets/include/SDL2/SDL_blendmode.h +4 -6
- data/assets/include/SDL2/SDL_clipboard.h +47 -0
- data/assets/include/SDL2/SDL_config.h +6 -2
- data/assets/include/SDL2/SDL_config_android.h +2 -0
- data/assets/include/SDL2/SDL_config_emscripten.h +2 -0
- data/assets/include/SDL2/SDL_config_iphoneos.h +3 -1
- data/assets/include/SDL2/SDL_config_macosx.h +3 -6
- data/assets/include/SDL2/SDL_config_minimal.h +18 -11
- data/assets/include/SDL2/SDL_config_ngage.h +89 -0
- data/assets/include/SDL2/SDL_config_os2.h +5 -3
- data/assets/include/SDL2/SDL_config_pandora.h +1 -0
- data/assets/include/SDL2/SDL_config_windows.h +71 -45
- data/assets/include/SDL2/SDL_config_wingdk.h +253 -0
- data/assets/include/SDL2/SDL_config_winrt.h +11 -49
- data/assets/include/SDL2/SDL_config_xbox.h +235 -0
- data/assets/include/SDL2/SDL_cpuinfo.h +39 -4
- data/assets/include/SDL2/SDL_egl.h +59 -9
- data/assets/include/SDL2/SDL_endian.h +34 -3
- data/assets/include/SDL2/SDL_events.h +32 -1
- data/assets/include/SDL2/SDL_filesystem.h +5 -1
- data/assets/include/SDL2/SDL_gamecontroller.h +78 -5
- data/assets/include/SDL2/SDL_guid.h +100 -0
- data/assets/include/SDL2/SDL_hints.h +645 -43
- data/assets/include/SDL2/SDL_image.h +2045 -33
- data/assets/include/SDL2/SDL_joystick.h +127 -7
- data/assets/include/SDL2/SDL_keyboard.h +38 -1
- data/assets/include/SDL2/SDL_keycode.h +6 -1
- data/assets/include/SDL2/SDL_log.h +2 -2
- data/assets/include/SDL2/SDL_main.h +42 -2
- data/assets/include/SDL2/SDL_metal.h +2 -1
- data/assets/include/SDL2/SDL_mixer.h +2529 -396
- data/assets/include/SDL2/SDL_mouse.h +12 -1
- data/assets/include/SDL2/SDL_opengl.h +0 -51
- data/assets/include/SDL2/SDL_opengl_glext.h +2260 -231
- data/assets/include/SDL2/SDL_opengles2_gl2.h +374 -339
- data/assets/include/SDL2/SDL_opengles2_gl2ext.h +3479 -1496
- data/assets/include/SDL2/SDL_opengles2_gl2platform.h +6 -9
- data/assets/include/SDL2/SDL_opengles2_khrplatform.h +43 -14
- data/assets/include/SDL2/SDL_platform.h +32 -6
- data/assets/include/SDL2/SDL_rect.h +154 -2
- data/assets/include/SDL2/SDL_render.h +46 -17
- data/assets/include/SDL2/SDL_revision.h +4 -0
- data/assets/include/SDL2/SDL_rwops.h +1 -15
- data/assets/include/SDL2/SDL_scancode.h +46 -21
- data/assets/include/SDL2/SDL_sensor.h +24 -3
- data/assets/include/SDL2/SDL_stdinc.h +119 -8
- data/assets/include/SDL2/SDL_surface.h +3 -1
- data/assets/include/SDL2/SDL_system.h +66 -6
- data/assets/include/SDL2/SDL_syswm.h +2 -0
- data/assets/include/SDL2/SDL_test_common.h +1 -0
- data/assets/include/SDL2/SDL_test_font.h +90 -3
- data/assets/include/SDL2/SDL_thread.h +3 -3
- data/assets/include/SDL2/SDL_touch.h +8 -0
- data/assets/include/SDL2/SDL_ttf.h +2084 -155
- data/assets/include/SDL2/SDL_version.h +19 -3
- data/assets/include/SDL2/SDL_video.h +71 -9
- data/assets/include/SDL2/begin_code.h +4 -4
- data/assets/include/mrbconf.h +15 -17
- data/assets/include/mruby/array.h +8 -21
- data/assets/include/mruby/boxing_nan.h +115 -86
- data/assets/include/mruby/boxing_word.h +104 -78
- data/assets/include/mruby/common.h +6 -0
- data/assets/include/mruby/compile.h +3 -4
- data/assets/include/mruby/debug.h +4 -2
- data/assets/include/mruby/dump.h +5 -2
- data/assets/include/mruby/error.h +12 -2
- data/assets/include/mruby/gc.h +2 -0
- data/assets/include/mruby/hash.h +1 -3
- data/assets/include/mruby/irep.h +4 -4
- data/assets/include/mruby/numeric.h +21 -13
- data/assets/include/mruby/opcode.h +30 -0
- data/assets/include/mruby/ops.h +99 -101
- data/assets/include/mruby/presym/scanning.h +15 -9
- data/assets/include/mruby/proc.h +4 -2
- data/assets/include/mruby/string.h +3 -24
- data/assets/include/mruby/value.h +80 -40
- data/assets/include/mruby/variable.h +0 -15
- data/assets/include/mruby/version.h +5 -5
- data/assets/include/mruby.h +86 -16
- data/assets/macos/universal/bin/mrbc +0 -0
- data/assets/macos/universal/lib/libFLAC.a +0 -0
- data/assets/macos/universal/lib/libSDL2.a +0 -0
- data/assets/macos/universal/lib/libSDL2_image.a +0 -0
- data/assets/macos/universal/lib/libSDL2_mixer.a +0 -0
- data/assets/macos/universal/lib/libSDL2_ttf.a +0 -0
- data/assets/macos/universal/lib/libavif.a +0 -0
- data/assets/macos/universal/lib/libbrotlicommon-static.a +0 -0
- data/assets/macos/universal/lib/libbrotlidec-static.a +0 -0
- data/assets/macos/universal/lib/libfreetype.a +0 -0
- data/assets/macos/universal/lib/libgraphite2.a +0 -0
- data/assets/macos/universal/lib/libharfbuzz.a +0 -0
- data/assets/macos/universal/lib/libhwy.a +0 -0
- data/assets/macos/universal/lib/libjpeg.a +0 -0
- data/assets/macos/universal/lib/libjxl.a +0 -0
- data/assets/macos/universal/lib/libmodplug.a +0 -0
- data/assets/macos/universal/lib/libmpg123.a +0 -0
- data/assets/macos/universal/lib/libmruby.a +0 -0
- data/assets/macos/universal/lib/libogg.a +0 -0
- data/assets/macos/universal/lib/libpng.a +0 -0
- data/assets/macos/universal/lib/libtiff.a +0 -0
- data/assets/macos/universal/lib/libvorbis.a +0 -0
- data/assets/macos/universal/lib/libvorbisfile.a +0 -0
- data/assets/macos/universal/lib/libwebp.a +0 -0
- data/assets/macos/universal/lib/libzstd.a +0 -0
- data/assets/wasm/template.html +2 -1
- data/assets/windows/mingw-w64-ucrt-x86_64/bin/mrbc.exe +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libFLAC.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libLerc.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libSDL2.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libSDL2_image.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libSDL2_mixer.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libSDL2_ttf.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libbrotlicommon.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libbrotlidec.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libdeflate.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libfreetype.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libglew32.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libharfbuzz.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libhwy.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libjpeg.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libjxl.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/liblzma.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libmpg123.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libmruby.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libopus.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libpng.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libsndfile.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libssp.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libstdc++.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libtiff.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libwebp.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libz.a +0 -0
- data/assets/windows/mingw-w64-x86_64/bin/mrbc.exe +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libFLAC.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libLerc.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libSDL2.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libSDL2_image.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libSDL2_mixer.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libSDL2_ttf.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libbrotlicommon.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libbrotlidec.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libdeflate.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libfreetype.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libglew32.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libharfbuzz.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libhwy.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libjpeg.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libjxl.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/liblzma.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libmpg123.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libmruby.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libopus.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libpng.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libsndfile.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libssp.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libstdc++.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libtiff.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libwebp.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libz.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libzstd.a +0 -0
- data/ext/ruby2d/canvas.c +540 -0
- data/ext/ruby2d/extconf.rb +33 -20
- data/ext/ruby2d/gl.c +3 -3
- data/ext/ruby2d/image.c +7 -7
- data/ext/ruby2d/ruby2d.c +741 -24
- data/ext/ruby2d/ruby2d.h +66 -3
- data/ext/ruby2d/sound.c +16 -2
- data/lib/ruby2d/canvas.rb +315 -0
- data/lib/ruby2d/circle.rb +30 -15
- data/lib/ruby2d/cli/build.rb +5 -3
- data/lib/ruby2d/cli/enable_console.rb +3 -1
- data/lib/ruby2d/color.rb +133 -77
- data/lib/ruby2d/core.rb +32 -0
- data/lib/ruby2d/dsl.rb +38 -32
- data/lib/ruby2d/exceptions.rb +2 -1
- data/lib/ruby2d/font.rb +97 -62
- data/lib/ruby2d/image.rb +48 -27
- data/lib/ruby2d/line.rb +84 -45
- data/lib/ruby2d/music.rb +33 -23
- data/lib/ruby2d/pixel.rb +10 -9
- data/lib/ruby2d/pixmap.rb +39 -0
- data/lib/ruby2d/pixmap_atlas.rb +56 -0
- data/lib/ruby2d/quad.rb +98 -49
- data/lib/ruby2d/rectangle.rb +35 -36
- data/lib/ruby2d/renderable.rb +7 -6
- data/lib/ruby2d/sound.rb +23 -17
- data/lib/ruby2d/sprite.rb +181 -140
- data/lib/ruby2d/square.rb +21 -20
- data/lib/ruby2d/text.rb +47 -19
- data/lib/ruby2d/texture.rb +13 -3
- data/lib/ruby2d/tileset.rb +97 -44
- data/lib/ruby2d/triangle.rb +91 -42
- data/lib/ruby2d/version.rb +3 -1
- data/lib/ruby2d/vertices.rb +81 -45
- data/lib/ruby2d/window.rb +508 -371
- data/lib/ruby2d.rb +11 -29
- metadata +51 -16
- data/assets/include/SDL2/SDL_config_psp.h +0 -165
- data/assets/include/SDL2/SDL_config_wiz.h +0 -154
- data/assets/include/glew.h +0 -23686
- data/assets/macos/universal/lib/libpng16.a +0 -0
- data/assets/windows/glew/README.md +0 -10
- data/assets/windows/glew/glew.h +0 -23686
- data/assets/windows/glew/libglew32.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libpng16.a +0 -0
- data/assets/windows/mingw-w64-x86_64/lib/libpng16.a +0 -0
- 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 [](
|
13
|
-
@colors[
|
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
|
-
|
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
|
-
|
33
|
-
'navy'
|
34
|
-
'blue'
|
35
|
-
'aqua'
|
36
|
-
'teal'
|
37
|
-
'olive'
|
38
|
-
'green'
|
39
|
-
'lime'
|
40
|
-
'yellow'
|
41
|
-
'orange'
|
42
|
-
'red'
|
43
|
-
'brown'
|
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'
|
46
|
-
'maroon'
|
47
|
-
'white'
|
48
|
-
'silver'
|
49
|
-
'gray'
|
50
|
-
'black'
|
51
|
-
'random'
|
52
|
-
}
|
53
|
-
|
54
|
-
def initialize(
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
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
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
-
#
|
100
|
-
|
101
|
-
|
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
|
106
|
-
|
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
|
168
|
+
def i_to_f(color_array)
|
112
169
|
b = []
|
113
|
-
|
170
|
+
color_array.each do |n|
|
114
171
|
b.push(n / 255.0)
|
115
172
|
end
|
116
|
-
|
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(
|
121
|
-
|
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
|
-
|
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
|
-
|
186
|
+
i_to_f(a)
|
130
187
|
end
|
131
|
-
|
132
188
|
end
|
133
189
|
|
134
190
|
# Allow British English spelling of color
|
data/lib/ruby2d/core.rb
ADDED
@@ -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
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module Ruby2D
|
3
|
+
module Ruby2D
|
4
|
+
# Ruby2D::DSL
|
5
|
+
module DSL
|
6
|
+
@window = Ruby2D::Window.new
|
4
7
|
|
5
|
-
|
8
|
+
def self.window
|
9
|
+
@window
|
10
|
+
end
|
6
11
|
|
7
|
-
|
8
|
-
|
12
|
+
def self.window=(window)
|
13
|
+
@window = window
|
14
|
+
end
|
9
15
|
|
10
|
-
|
11
|
-
|
12
|
-
|
16
|
+
def get(sym, opts = nil)
|
17
|
+
DSL.window.get(sym, opts)
|
18
|
+
end
|
13
19
|
|
14
|
-
|
15
|
-
|
16
|
-
|
20
|
+
def set(opts)
|
21
|
+
DSL.window.set(opts)
|
22
|
+
end
|
17
23
|
|
18
|
-
|
19
|
-
|
20
|
-
|
24
|
+
def on(event, &proc)
|
25
|
+
DSL.window.on(event, &proc)
|
26
|
+
end
|
21
27
|
|
22
|
-
|
23
|
-
|
24
|
-
|
28
|
+
def off(event_descriptor)
|
29
|
+
DSL.window.off(event_descriptor)
|
30
|
+
end
|
25
31
|
|
26
|
-
|
27
|
-
|
28
|
-
|
32
|
+
def update(&proc)
|
33
|
+
DSL.window.update(&proc)
|
34
|
+
end
|
29
35
|
|
30
|
-
|
31
|
-
|
32
|
-
|
36
|
+
def render(&proc)
|
37
|
+
DSL.window.render(&proc)
|
38
|
+
end
|
33
39
|
|
34
|
-
|
35
|
-
|
36
|
-
|
40
|
+
def clear
|
41
|
+
DSL.window.clear
|
42
|
+
end
|
37
43
|
|
38
|
-
|
39
|
-
|
40
|
-
|
44
|
+
def show
|
45
|
+
DSL.window.show
|
46
|
+
end
|
41
47
|
|
42
|
-
|
43
|
-
|
48
|
+
def close
|
49
|
+
DSL.window.close
|
50
|
+
end
|
44
51
|
end
|
45
|
-
|
46
52
|
end
|
data/lib/ruby2d/exceptions.rb
CHANGED
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
|
-
|
12
|
-
|
13
|
-
end
|
14
|
+
# Cache loaded fonts in a class variable
|
15
|
+
@loaded_fonts = {}
|
14
16
|
|
15
17
|
class << self
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
39
|
-
def
|
40
|
-
|
41
|
-
|
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
|
-
|
51
|
+
all_paths.first
|
46
52
|
end
|
53
|
+
end
|
47
54
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
-
#
|
60
|
-
def
|
61
|
-
if
|
62
|
-
|
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
|
-
|
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
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|