ruby2d 0.11.2 → 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/libmruby.a +0 -0
- data/assets/wasm/template.html +52 -4
- 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/libbz2.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/libgraphite2.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/libjbig.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/libmodplug.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/libogg.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/libopusfile.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 +1 -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/libvorbis.a +0 -0
- data/assets/windows/mingw-w64-ucrt-x86_64/lib/libvorbisfile.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-ucrt-x86_64/lib/libzstd.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 +39 -21
- 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 +11 -4
- 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 +84 -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/wasm/build_config.rb +0 -13
- 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-x86_64/lib/libpng16.a +0 -0
- data/lib/ruby2d/entity.rb +0 -17
data/lib/ruby2d/rectangle.rb
CHANGED
@@ -1,18 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Ruby2D::Rectangle
|
2
4
|
|
3
5
|
module Ruby2D
|
6
|
+
# A rectangle
|
4
7
|
class Rectangle < Quad
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
# Create an rectangle
|
9
|
+
# @param [Numeric] x
|
10
|
+
# @param [Numeric] y
|
11
|
+
# @param [Numeric] width
|
12
|
+
# @param [Numeric] height
|
13
|
+
# @param [Numeric] z
|
14
|
+
# @param [String, Array] color A single colour or an array of exactly 4 colours
|
15
|
+
# @param [Numeric] opacity Opacity of the image when rendering
|
16
|
+
# @raise [ArgumentError] if an array of colours does not have 4 entries
|
17
|
+
def initialize(x: 0, y: 0, width: 200, height: 100, z: 0, color: nil, colour: nil, opacity: nil)
|
18
|
+
@width = width
|
19
|
+
@height = height
|
20
|
+
super(x1: @x = x, y1: @y = y,
|
21
|
+
x2: x + width, y2: y,
|
22
|
+
x3: x + width, y3: y + height,
|
23
|
+
x4: x, y4: y + height, z: z, color: color, colour: colour, opacity: opacity)
|
16
24
|
end
|
17
25
|
|
18
26
|
def x=(x)
|
@@ -29,37 +37,28 @@ module Ruby2D
|
|
29
37
|
@y4 = y + @height
|
30
38
|
end
|
31
39
|
|
32
|
-
def width=(
|
33
|
-
@width =
|
34
|
-
|
40
|
+
def width=(width)
|
41
|
+
@width = width
|
42
|
+
@x2 = @x1 + width
|
43
|
+
@x3 = @x1 + width
|
35
44
|
end
|
36
45
|
|
37
|
-
def height=(
|
38
|
-
@height =
|
39
|
-
|
46
|
+
def height=(height)
|
47
|
+
@height = height
|
48
|
+
@y3 = @y1 + height
|
49
|
+
@y4 = @y1 + height
|
40
50
|
end
|
41
51
|
|
42
|
-
def self.draw(
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
opts[:x] , opts[:y] + opts[:height], opts[:color][3][0], opts[:color][3][1], opts[:color][3][2], opts[:color][3][3]
|
48
|
-
])
|
52
|
+
def self.draw(x:, y:, width:, height:, color:)
|
53
|
+
super(x1: x, y1: y,
|
54
|
+
x2: x + width, y2: y,
|
55
|
+
x3: x + width, y3: y + height,
|
56
|
+
x4: x, y4: y + height, color: color)
|
49
57
|
end
|
50
58
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
@x1 = x
|
55
|
-
@y1 = y
|
56
|
-
@x2 = x + w
|
57
|
-
@y2 = y
|
58
|
-
@x3 = x + w
|
59
|
-
@y3 = y + h
|
60
|
-
@x4 = x
|
61
|
-
@y4 = y + h
|
59
|
+
def contains?(x, y)
|
60
|
+
# Override the check from Quad since this is faster
|
61
|
+
x >= @x && x <= (@x + @width) && y >= @y && y <= (@y + @height)
|
62
62
|
end
|
63
|
-
|
64
63
|
end
|
65
64
|
end
|
data/lib/ruby2d/renderable.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Ruby2D::Renderable
|
2
4
|
|
3
5
|
module Ruby2D
|
6
|
+
# Base class for all renderable shapes
|
4
7
|
module Renderable
|
5
|
-
|
6
8
|
attr_reader :x, :y, :z, :width, :height, :color
|
7
9
|
|
8
10
|
# Set the z position (depth) of the object
|
@@ -23,18 +25,17 @@ module Ruby2D
|
|
23
25
|
end
|
24
26
|
|
25
27
|
# Set the color value
|
26
|
-
def color=(
|
27
|
-
@color = Color.new(
|
28
|
+
def color=(color)
|
29
|
+
@color = Color.new(color)
|
28
30
|
end
|
29
31
|
|
30
32
|
# Allow British English spelling of color
|
31
|
-
|
32
|
-
|
33
|
+
alias colour color
|
34
|
+
alias colour= color=
|
33
35
|
|
34
36
|
# Add a contains method stub
|
35
37
|
def contains?(x, y)
|
36
38
|
x >= @x && x <= (@x + @width) && y >= @y && y <= (@y + @height)
|
37
39
|
end
|
38
|
-
|
39
40
|
end
|
40
41
|
end
|
data/lib/ruby2d/sound.rb
CHANGED
@@ -1,19 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Ruby2D::Sound
|
2
4
|
|
3
5
|
module Ruby2D
|
6
|
+
# Sounds are intended to be short samples, played without interruption, like an effect.
|
4
7
|
class Sound
|
5
|
-
|
6
8
|
attr_reader :path
|
7
|
-
attr_accessor :data
|
9
|
+
attr_accessor :loop, :data
|
10
|
+
|
11
|
+
#
|
12
|
+
# Load a sound from a file
|
13
|
+
# @param [String] path File to load the sound from
|
14
|
+
# @param [true, false] loop If +true+ playback will loop automatically, default is +false+
|
15
|
+
# @raise [Error] if file cannot be found or music could not be successfully loaded.
|
16
|
+
def initialize(path, loop: false)
|
17
|
+
raise Error, "Cannot find audio file `#{path}`" unless File.exist? path
|
8
18
|
|
9
|
-
def initialize(path)
|
10
|
-
unless File.exist? path
|
11
|
-
raise Error, "Cannot find audio file `#{path}`"
|
12
|
-
end
|
13
19
|
@path = path
|
14
|
-
|
15
|
-
|
16
|
-
end
|
20
|
+
@loop = loop
|
21
|
+
raise Error, "Sound `#{@path}` cannot be created" unless ext_init(@path)
|
17
22
|
end
|
18
23
|
|
19
24
|
# Play the sound
|
@@ -21,6 +26,11 @@ module Ruby2D
|
|
21
26
|
ext_play
|
22
27
|
end
|
23
28
|
|
29
|
+
# Stop the sound
|
30
|
+
def stop
|
31
|
+
ext_stop
|
32
|
+
end
|
33
|
+
|
24
34
|
# Returns the length in seconds
|
25
35
|
def length
|
26
36
|
ext_length
|
@@ -32,11 +42,9 @@ module Ruby2D
|
|
32
42
|
end
|
33
43
|
|
34
44
|
# Set the volume of the sound
|
35
|
-
def volume=(
|
45
|
+
def volume=(volume)
|
36
46
|
# Clamp value to between 0-100
|
37
|
-
|
38
|
-
if v > 100 then v = 100 end
|
39
|
-
ext_set_volume(v)
|
47
|
+
ext_set_volume(volume.clamp(0, 100))
|
40
48
|
end
|
41
49
|
|
42
50
|
# Get the volume of the sound mixer
|
@@ -45,11 +53,9 @@ module Ruby2D
|
|
45
53
|
end
|
46
54
|
|
47
55
|
# Set the volume of the sound mixer
|
48
|
-
def self.mix_volume=(
|
56
|
+
def self.mix_volume=(volume)
|
49
57
|
# Clamp value to between 0-100
|
50
|
-
|
51
|
-
if v > 100 then v = 100 end
|
52
|
-
ext_set_mix_volume(v)
|
58
|
+
ext_set_mix_volume(volume.clamp(0, 100))
|
53
59
|
end
|
54
60
|
end
|
55
61
|
end
|
data/lib/ruby2d/sprite.rb
CHANGED
@@ -1,101 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Ruby2D::Sprite
|
2
4
|
|
3
5
|
module Ruby2D
|
6
|
+
# Sprites are special images that can be used to create animations, kind of like a flip book.
|
7
|
+
# To create a sprite animation, first you’ll need an image which contains each frame of your animation.
|
4
8
|
class Sprite
|
5
9
|
include Renderable
|
6
10
|
|
7
11
|
attr_reader :path
|
8
12
|
attr_accessor :rotate, :loop, :clip_x, :clip_y, :clip_width, :clip_height, :data, :x, :y, :width, :height
|
9
13
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
# Create a sprite via single image or sprite-sheet.
|
15
|
+
# @param path [#to_s] The location of the file to load as an image.
|
16
|
+
# @param width [Numeric] The +width+ of drawn image, or default is width from image file
|
17
|
+
# @param height [Numeric] The +height+ of drawn image, or default is height from image file
|
18
|
+
# @param x [Numeric]
|
19
|
+
# @param y [Numeric]
|
20
|
+
# @param z [Numeric]
|
21
|
+
# @param rotate [Numeric] Angle, default is 0
|
22
|
+
# @param color [Numeric] (or +colour+) Tint the image when rendering
|
23
|
+
# @param opacity [Numeric] Opacity of the image when rendering
|
24
|
+
# @param show [Boolean] If +true+ the image is added to +Window+ automatically.
|
25
|
+
# @param loop [Boolean] Sets whether animations loop by default when played (can be overridden by +play+ method)
|
26
|
+
# @param time [Numeric] The default time in millisecond for each frame (unless overridden animation within frames)
|
27
|
+
# @param animations [Hash{String => Range}, Hash{String=>Array<Hash>}] Sprite sheet map to animations using
|
28
|
+
# frame ranges or individual frames
|
29
|
+
# @param default [Numeric] The default initial frame for the sprite
|
30
|
+
# @param clip_x [Numeric]
|
31
|
+
# @param clip_x [Numeric]
|
32
|
+
# @param clip_width [Numeric]
|
33
|
+
# @param clip_height [Numeric]
|
34
|
+
def initialize(path, atlas: nil, show: true, width: nil, height: nil,
|
35
|
+
x: 0, y: 0, z: 0, rotate: 0, color: nil, colour: nil, opacity: nil,
|
36
|
+
loop: false, time: 300, animations: {}, default: 0,
|
37
|
+
clip_x: 0, clip_y: 0, clip_width: nil, clip_height: nil)
|
38
|
+
@path = path.to_s
|
18
39
|
|
19
40
|
# Coordinates, size, and rotation of the sprite
|
20
|
-
@x =
|
21
|
-
@y =
|
22
|
-
@z =
|
23
|
-
@rotate =
|
24
|
-
self.color =
|
25
|
-
self.color.opacity =
|
41
|
+
@x = x
|
42
|
+
@y = y
|
43
|
+
@z = z
|
44
|
+
@rotate = rotate
|
45
|
+
self.color = color || colour || 'white'
|
46
|
+
self.color.opacity = opacity unless opacity.nil?
|
47
|
+
|
48
|
+
# Dimensions
|
49
|
+
@width = width
|
50
|
+
@height = height
|
26
51
|
|
27
52
|
# Flipping status
|
28
53
|
@flip = nil
|
29
54
|
|
30
55
|
# Animation attributes
|
31
|
-
@
|
32
|
-
@
|
33
|
-
@
|
34
|
-
@
|
35
|
-
@playing = false
|
36
|
-
@current_frame = opts[:default] || 0
|
37
|
-
@last_frame = 0
|
38
|
-
@done_proc = nil
|
39
|
-
|
40
|
-
# The clipping rectangle
|
41
|
-
@clip_x = opts[:clip_x] || 0
|
42
|
-
@clip_y = opts[:clip_y] || 0
|
43
|
-
@clip_width = opts[:clip_width] || @img_width
|
44
|
-
@clip_height = opts[:clip_height] || @img_height
|
45
|
-
|
46
|
-
# Dimensions
|
47
|
-
@width = opts[:width] || nil
|
48
|
-
@height = opts[:height] || nil
|
49
|
-
|
50
|
-
# Set the default animation
|
51
|
-
@animations[:default] = 0..(@img_width / @clip_width) - 1
|
56
|
+
@loop = loop
|
57
|
+
@frame_time = time
|
58
|
+
@animations = animations
|
59
|
+
@current_frame = default
|
52
60
|
|
53
|
-
|
54
|
-
|
55
|
-
animation: @animations.first[0],
|
56
|
-
frame: @current_frame,
|
57
|
-
frame_time: @frame_time,
|
58
|
-
clip_x: @clip_x,
|
59
|
-
clip_y: @clip_y,
|
60
|
-
clip_width: @clip_width,
|
61
|
-
clip_height: @clip_height,
|
62
|
-
loop: @loop
|
63
|
-
}
|
61
|
+
_setup_texture_and_clip_box atlas, clip_x, clip_y, clip_width, clip_height
|
62
|
+
_setup_animation
|
64
63
|
|
65
64
|
# Add the sprite to the window
|
66
|
-
|
65
|
+
add if show
|
67
66
|
end
|
68
67
|
|
69
|
-
#
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
flip
|
75
|
-
|
76
|
-
if !@playing || (animation != @playing_animation && animation != nil) || flip != @flip
|
77
|
-
|
68
|
+
# Start playing an animation
|
69
|
+
# @param animation [String] Name of the animation to play
|
70
|
+
# @param loop [Boolean] Set the animation to loop or not
|
71
|
+
# @param flip [nil, :vertical, :horizontal, :both] Direction to flip the sprite if desired
|
72
|
+
def play(animation: :default, loop: nil, flip: nil, &done_proc)
|
73
|
+
unless @playing && animation == @playing_animation && flip == @flip
|
78
74
|
@playing = true
|
79
75
|
@playing_animation = animation || :default
|
80
|
-
frames = @animations[@playing_animation]
|
81
|
-
flip_sprite(flip)
|
82
76
|
@done_proc = done_proc
|
83
77
|
|
84
|
-
|
85
|
-
|
86
|
-
when Range
|
87
|
-
@first_frame = frames.first || @defaults[:frame]
|
88
|
-
@current_frame = frames.first || @defaults[:frame]
|
89
|
-
@last_frame = frames.last
|
90
|
-
# When array...
|
91
|
-
when Array
|
92
|
-
@first_frame = 0
|
93
|
-
@current_frame = 0
|
94
|
-
@last_frame = frames.length - 1
|
95
|
-
end
|
78
|
+
flip_sprite(flip)
|
79
|
+
_reset_playing_animation
|
96
80
|
|
97
|
-
|
98
|
-
@loop = loop
|
81
|
+
loop = @defaults[:loop] if loop.nil?
|
82
|
+
@loop = loop ? true : false
|
99
83
|
|
100
84
|
set_frame
|
101
85
|
restart_time
|
@@ -105,22 +89,22 @@ module Ruby2D
|
|
105
89
|
|
106
90
|
# Stop the current animation and set to the default frame
|
107
91
|
def stop(animation = nil)
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
92
|
+
return unless !animation || animation == @playing_animation
|
93
|
+
|
94
|
+
@playing = false
|
95
|
+
@playing_animation = @defaults[:animation]
|
96
|
+
@current_frame = @defaults[:frame]
|
97
|
+
set_frame
|
114
98
|
end
|
115
99
|
|
116
100
|
# Flip the sprite
|
101
|
+
# @param flip [nil, :vertical, :horizontal, :both] Direction to flip the sprite if desired
|
117
102
|
def flip_sprite(flip)
|
118
|
-
|
119
103
|
# The sprite width and height must be set for it to be flipped correctly
|
120
104
|
if (!@width || !@height) && flip
|
121
105
|
raise Error,
|
122
|
-
|
123
|
-
|
106
|
+
"Sprite width/height required to flip; occured playing animation `:#{@playing_animation}`
|
107
|
+
with image `#{@path}`"
|
124
108
|
end
|
125
109
|
|
126
110
|
@flip = flip
|
@@ -142,12 +126,7 @@ module Ruby2D
|
|
142
126
|
reset_clipping_rect
|
143
127
|
@clip_x = @current_frame * @clip_width
|
144
128
|
when Array
|
145
|
-
|
146
|
-
@clip_x = f[:x] || @defaults[:clip_x]
|
147
|
-
@clip_y = f[:y] || @defaults[:clip_y]
|
148
|
-
@clip_width = f[:width] || @defaults[:clip_width]
|
149
|
-
@clip_height = f[:height] || @defaults[:clip_height]
|
150
|
-
@frame_time = f[:time] || @defaults[:frame_time]
|
129
|
+
_set_explicit_frame frames[@current_frame]
|
151
130
|
end
|
152
131
|
end
|
153
132
|
|
@@ -163,75 +142,137 @@ module Ruby2D
|
|
163
142
|
|
164
143
|
# Update the sprite animation, called by `render`
|
165
144
|
def update
|
166
|
-
|
145
|
+
return unless @playing
|
167
146
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
147
|
+
# Advance the frame
|
148
|
+
unless elapsed_time <= (@frame_time || @defaults[:frame_time])
|
149
|
+
@current_frame += 1
|
150
|
+
restart_time
|
151
|
+
end
|
173
152
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
end
|
153
|
+
# Reset to the starting frame if all frames played
|
154
|
+
if @current_frame > @last_frame
|
155
|
+
@current_frame = @first_frame
|
156
|
+
unless @loop
|
157
|
+
# Stop animation and play block, if provided
|
158
|
+
stop
|
159
|
+
if @done_proc
|
160
|
+
# allow proc to make nested `play/do` calls to sequence multiple
|
161
|
+
# animations by clearing `@done_proc` before the call
|
162
|
+
kept_done_proc = @done_proc
|
163
|
+
@done_proc = nil
|
164
|
+
kept_done_proc.call
|
187
165
|
end
|
188
166
|
end
|
189
|
-
|
190
|
-
set_frame
|
191
167
|
end
|
168
|
+
|
169
|
+
set_frame
|
192
170
|
end
|
193
171
|
|
194
|
-
|
172
|
+
# @param width [Numeric] The +width+ of drawn image
|
173
|
+
# @param height [Numeric] The +height+ of drawn image
|
174
|
+
# @param x [Numeric]
|
175
|
+
# @param y [Numeric]
|
176
|
+
# @param rotate [Numeric] Angle, default is 0
|
177
|
+
# @param color [Numeric] (or +colour+) Tint the image when rendering
|
178
|
+
# @param clip_x [Numeric]
|
179
|
+
# @param clip_x [Numeric]
|
180
|
+
# @param clip_width [Numeric]
|
181
|
+
# @param clip_height [Numeric]
|
182
|
+
def draw(x:, y:, width: (@width || @clip_width), height: (@height || @clip_height), rotate: @rotate,
|
183
|
+
clip_x: @clip_x, clip_y: @clip_y, clip_width: @clip_width, clip_height: @clip_height,
|
184
|
+
color: [1.0, 1.0, 1.0, 1.0])
|
185
|
+
|
195
186
|
Window.render_ready_check
|
187
|
+
render(x: x, y: y, width: width, height: height, color: Color.new(color), rotate: rotate, crop: {
|
188
|
+
x: clip_x,
|
189
|
+
y: clip_y,
|
190
|
+
width: clip_width,
|
191
|
+
height: clip_height,
|
192
|
+
image_width: @img_width,
|
193
|
+
image_height: @img_height
|
194
|
+
})
|
195
|
+
end
|
196
|
+
|
197
|
+
private
|
198
|
+
|
199
|
+
def render(x: @x, y: @y, width: (@width || @clip_width), height: (@height || @clip_height),
|
200
|
+
color: @color, rotate: @rotate, flip: @flip,
|
201
|
+
crop: {
|
202
|
+
x: @clip_x,
|
203
|
+
y: @clip_y,
|
204
|
+
width: @clip_width,
|
205
|
+
height: @clip_height,
|
206
|
+
image_width: @img_width,
|
207
|
+
image_height: @img_height
|
208
|
+
})
|
209
|
+
update
|
210
|
+
vertices = Vertices.new(x, y, width, height, rotate, crop: crop, flip: flip)
|
211
|
+
@texture.draw vertices.coordinates, vertices.texture_coordinates, color
|
212
|
+
end
|
196
213
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
214
|
+
# Reset the playing animation to the first frame
|
215
|
+
def _reset_playing_animation
|
216
|
+
frames = @animations[@playing_animation]
|
217
|
+
case frames
|
218
|
+
# When animation is a range, play through frames horizontally
|
219
|
+
when Range
|
220
|
+
@first_frame = frames.first || @defaults[:frame]
|
221
|
+
@current_frame = frames.first || @defaults[:frame]
|
222
|
+
@last_frame = frames.last
|
223
|
+
# When array...
|
224
|
+
when Array
|
225
|
+
@first_frame = 0
|
226
|
+
@current_frame = 0
|
227
|
+
@last_frame = frames.length - 1
|
206
228
|
end
|
229
|
+
end
|
207
230
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
})
|
231
|
+
# Set the current frame based on the region/portion of image
|
232
|
+
def _set_explicit_frame(frame)
|
233
|
+
@clip_x = frame[:x] || @defaults[:clip_x]
|
234
|
+
@clip_y = frame[:y] || @defaults[:clip_y]
|
235
|
+
@clip_width = frame[:width] || @defaults[:clip_width]
|
236
|
+
@clip_height = frame[:height] || @defaults[:clip_height]
|
237
|
+
@frame_time = frame[:time] || @defaults[:frame_time]
|
216
238
|
end
|
217
239
|
|
218
|
-
|
240
|
+
# initialize texture and clipping, called by constructor
|
241
|
+
def _setup_texture_and_clip_box(atlas, clip_x, clip_y, clip_width, clip_height)
|
242
|
+
# Initialize the sprite texture
|
243
|
+
# Consider input pixmap atlas if supplied to load image file
|
244
|
+
@texture = Image.load_image_as_texture @path, atlas: atlas
|
245
|
+
@img_width = @texture.width
|
246
|
+
@img_height = @texture.height
|
219
247
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
image_height: @img_height,
|
227
|
-
})
|
228
|
-
update
|
248
|
+
# The clipping rectangle
|
249
|
+
@clip_x = clip_x
|
250
|
+
@clip_y = clip_y
|
251
|
+
@clip_width = clip_width || @img_width
|
252
|
+
@clip_height = clip_height || @img_height
|
253
|
+
end
|
229
254
|
|
230
|
-
|
255
|
+
# initialize animation, called by constructor
|
256
|
+
def _setup_animation
|
257
|
+
@start_time = 0.0
|
258
|
+
@playing = false
|
259
|
+
@last_frame = 0
|
260
|
+
@done_proc = nil
|
261
|
+
|
262
|
+
# Set the default animation
|
263
|
+
@animations[:default] = 0..(@img_width / @clip_width) - 1
|
231
264
|
|
232
|
-
|
233
|
-
|
234
|
-
|
265
|
+
# Set the sprite defaults
|
266
|
+
@defaults = {
|
267
|
+
animation: @animations.first[0],
|
268
|
+
frame: @current_frame,
|
269
|
+
frame_time: @frame_time,
|
270
|
+
clip_x: @clip_x,
|
271
|
+
clip_y: @clip_y,
|
272
|
+
clip_width: @clip_width,
|
273
|
+
clip_height: @clip_height,
|
274
|
+
loop: @loop
|
275
|
+
}
|
235
276
|
end
|
236
277
|
end
|
237
278
|
end
|
data/lib/ruby2d/square.rb
CHANGED
@@ -1,37 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Ruby2D::Square
|
2
4
|
|
3
5
|
module Ruby2D
|
6
|
+
# A square
|
4
7
|
class Square < Rectangle
|
5
|
-
|
6
8
|
attr_reader :size
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
10
|
+
# Create an square
|
11
|
+
# @param [Numeric] x
|
12
|
+
# @param [Numeric] y
|
13
|
+
# @param [Numeric] size is width and height
|
14
|
+
# @param [Numeric] z
|
15
|
+
# @param [String, Array] color A single colour or an array of exactly 4 colours
|
16
|
+
# @param [Numeric] opacity Opacity of the image when rendering
|
17
|
+
# @raise [ArgumentError] if an array of colours does not have 4 entries
|
18
|
+
def initialize(x: 0, y: 0, size: 100, z: 0, color: nil, colour: nil, opacity: nil)
|
19
|
+
@size = size
|
20
|
+
super(x: x, y: y, width: size, height: size, z: z,
|
21
|
+
color: color, colour: colour, opacity: opacity)
|
17
22
|
end
|
18
23
|
|
19
24
|
# Set the size of the square
|
20
|
-
def size=(
|
21
|
-
self.width = self.height = @size =
|
25
|
+
def size=(size)
|
26
|
+
self.width = self.height = @size = size
|
22
27
|
end
|
23
28
|
|
24
|
-
def self.draw(
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
])
|
29
|
+
def self.draw(x:, y:, size:, color:)
|
30
|
+
super(x: x, y: y,
|
31
|
+
width: size, height: size,
|
32
|
+
color: color)
|
31
33
|
end
|
32
34
|
|
33
35
|
# Make the inherited width and height attribute accessors private
|
34
36
|
private :width=, :height=
|
35
|
-
|
36
37
|
end
|
37
38
|
end
|