ruby2d 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/assets/README.md +1 -0
- data/assets/opal.js +19532 -0
- data/assets/simple2d.js +1180 -0
- data/assets/template.html +18 -0
- data/bin/ruby2d +161 -46
- data/ext/ruby2d/extconf.rb +17 -8
- data/ext/ruby2d/ruby2d-opal.rb +219 -0
- data/ext/ruby2d/ruby2d.c +665 -154
- data/lib/ruby2d.rb +7 -4
- data/lib/ruby2d/application.rb +14 -2
- data/lib/ruby2d/color.rb +56 -33
- data/lib/ruby2d/dsl.rb +21 -12
- data/lib/ruby2d/exceptions.rb +2 -3
- data/lib/ruby2d/image.rb +22 -9
- data/lib/ruby2d/music.rb +17 -0
- data/lib/ruby2d/quad.rb +24 -15
- data/lib/ruby2d/rectangle.rb +2 -5
- data/lib/ruby2d/sound.rb +6 -9
- data/lib/ruby2d/sprite.rb +91 -0
- data/lib/ruby2d/square.rb +2 -5
- data/lib/ruby2d/text.rb +19 -23
- data/lib/ruby2d/triangle.rb +29 -15
- data/lib/ruby2d/version.rb +1 -1
- data/lib/ruby2d/window.rb +121 -67
- metadata +27 -7
data/lib/ruby2d.rb
CHANGED
@@ -1,17 +1,20 @@
|
|
1
1
|
# ruby2d.rb
|
2
2
|
|
3
|
+
require 'ruby2d/exceptions'
|
4
|
+
require 'ruby2d/color'
|
3
5
|
require 'ruby2d/window'
|
4
6
|
require 'ruby2d/application'
|
5
|
-
require 'ruby2d/
|
7
|
+
require 'ruby2d/dsl'
|
6
8
|
require 'ruby2d/quad'
|
7
9
|
require 'ruby2d/rectangle'
|
8
10
|
require 'ruby2d/square'
|
9
11
|
require 'ruby2d/triangle'
|
10
12
|
require 'ruby2d/image'
|
13
|
+
require 'ruby2d/sprite'
|
11
14
|
require 'ruby2d/text'
|
12
|
-
require 'ruby2d/
|
13
|
-
require 'ruby2d/
|
15
|
+
require 'ruby2d/sound'
|
16
|
+
require 'ruby2d/music'
|
14
17
|
require 'ruby2d/ruby2d' # load native extension
|
15
18
|
|
16
|
-
include Ruby2D::DSL
|
17
19
|
include Ruby2D
|
20
|
+
extend Ruby2D::DSL
|
data/lib/ruby2d/application.rb
CHANGED
@@ -12,8 +12,16 @@ module Ruby2D::Application
|
|
12
12
|
@@window.set(opts)
|
13
13
|
end
|
14
14
|
|
15
|
-
def on(
|
16
|
-
@@window.on(
|
15
|
+
def on(args = {}, &proc)
|
16
|
+
@@window.on(args, &proc)
|
17
|
+
end
|
18
|
+
|
19
|
+
def on_key(&proc)
|
20
|
+
@@window.on_key(&proc)
|
21
|
+
end
|
22
|
+
|
23
|
+
def on_controller(&proc)
|
24
|
+
@@window.on_controller(&proc)
|
17
25
|
end
|
18
26
|
|
19
27
|
def add(o)
|
@@ -35,5 +43,9 @@ module Ruby2D::Application
|
|
35
43
|
def show
|
36
44
|
@@window.show
|
37
45
|
end
|
46
|
+
|
47
|
+
def close
|
48
|
+
@@window.close
|
49
|
+
end
|
38
50
|
end
|
39
51
|
end
|
data/lib/ruby2d/color.rb
CHANGED
@@ -5,26 +5,27 @@ module Ruby2D
|
|
5
5
|
|
6
6
|
attr_reader :r, :g, :b, :a
|
7
7
|
|
8
|
+
# Based on clrs.cc
|
8
9
|
@@colors = {
|
9
|
-
'
|
10
|
-
'
|
11
|
-
'
|
12
|
-
'
|
13
|
-
'
|
14
|
-
'
|
15
|
-
'
|
16
|
-
'
|
17
|
-
'
|
18
|
-
'
|
19
|
-
'
|
20
|
-
'
|
21
|
-
'
|
22
|
-
'
|
23
|
-
'
|
24
|
-
'
|
25
|
-
'
|
26
|
-
'
|
27
|
-
'random' =>
|
10
|
+
'navy' => '#001F3F',
|
11
|
+
'blue' => '#0074D9',
|
12
|
+
'aqua' => '#7FDBFF',
|
13
|
+
'teal' => '#39CCCC',
|
14
|
+
'olive' => '#3D9970',
|
15
|
+
'green' => '#2ECC40',
|
16
|
+
'lime' => '#01FF70',
|
17
|
+
'yellow' => '#FFDC00',
|
18
|
+
'orange' => '#FF851B',
|
19
|
+
'red' => '#FF4136',
|
20
|
+
'brown' => '#663300',
|
21
|
+
'fuchsia' => '#F012BE',
|
22
|
+
'purple' => '#B10DC9',
|
23
|
+
'maroon' => '#85144B',
|
24
|
+
'white' => '#FFFFFF',
|
25
|
+
'silver' => '#DDDDDD',
|
26
|
+
'gray' => '#AAAAAA',
|
27
|
+
'black' => '#111111',
|
28
|
+
'random' => ''
|
28
29
|
}
|
29
30
|
|
30
31
|
def initialize(c)
|
@@ -34,39 +35,61 @@ module Ruby2D
|
|
34
35
|
case c
|
35
36
|
when String
|
36
37
|
if c == 'random'
|
37
|
-
@r, @g, @b, @a = rand
|
38
|
+
@r, @g, @b, @a = rand, rand, rand, 1.0
|
39
|
+
elsif self.class.is_hex?(c)
|
40
|
+
@r, @g, @b, @a = hex_to_f(c)
|
38
41
|
else
|
39
|
-
@r, @g, @b, @a =
|
42
|
+
@r, @g, @b, @a = hex_to_f(@@colors[c])
|
40
43
|
end
|
41
44
|
when Array
|
42
|
-
@r, @g, @b, @a =
|
45
|
+
@r, @g, @b, @a = [c[0], c[1], c[2], c[3]]
|
43
46
|
end
|
44
47
|
end
|
45
48
|
end
|
46
49
|
|
47
|
-
#
|
50
|
+
# Check if string is a proper hex value
|
51
|
+
def self.is_hex?(s)
|
52
|
+
# MRuby doesn't support regex, otherwise we'd do:
|
53
|
+
# !(/^#[0-9A-F]{6}$/i.match(a).nil?)
|
54
|
+
s.class == String && s[0] == '#' && s.length == 7
|
55
|
+
end
|
56
|
+
|
57
|
+
# Check if the color is valid
|
48
58
|
def self.is_valid?(c)
|
49
|
-
|
50
|
-
(c
|
51
|
-
|
52
|
-
|
53
|
-
|
59
|
+
@@colors.key?(c) || # keyword
|
60
|
+
self.is_hex?(c) || # hexadecimal value
|
61
|
+
|
62
|
+
# Array of Floats from 0.0..1.0
|
63
|
+
c.class == Array && c.length == 4 &&
|
64
|
+
c.all? { |el|
|
65
|
+
el.is_a?(Numeric) && (0.0..1.0).include?(el)
|
66
|
+
}
|
54
67
|
end
|
55
68
|
|
56
69
|
private
|
57
70
|
|
71
|
+
# TODO: Only `Number` supported in JS
|
58
72
|
# Convert from Fixnum (0..255) to Float (0.0..1.0)
|
59
73
|
def to_f(a)
|
60
74
|
b = []
|
61
75
|
a.each do |n|
|
62
|
-
|
63
|
-
b.push(n / 255.0)
|
64
|
-
else
|
65
|
-
b.push(n)
|
66
|
-
end
|
76
|
+
b.push(n / 255.0)
|
67
77
|
end
|
68
78
|
return b
|
69
79
|
end
|
70
80
|
|
81
|
+
# Convert from hex value (e.g. #FFF000) to Float (0.0..1.0)
|
82
|
+
def hex_to_f(h)
|
83
|
+
h = (h[1..-1]).chars.each_slice(2).map(&:join)
|
84
|
+
a = []
|
85
|
+
|
86
|
+
h.each do |el|
|
87
|
+
a.push(el.to_i(16))
|
88
|
+
end
|
89
|
+
|
90
|
+
a.push(255)
|
91
|
+
return to_f(a)
|
92
|
+
end
|
93
|
+
|
71
94
|
end
|
72
95
|
end
|
data/lib/ruby2d/dsl.rb
CHANGED
@@ -1,31 +1,40 @@
|
|
1
1
|
# dsl.rb
|
2
2
|
|
3
3
|
module Ruby2D::DSL
|
4
|
-
def hello
|
5
|
-
puts "hi"
|
6
|
-
end
|
7
|
-
|
8
4
|
def get(sym)
|
9
|
-
|
5
|
+
Application.get(sym)
|
10
6
|
end
|
11
7
|
|
12
8
|
def set(opts)
|
13
|
-
|
9
|
+
Application.set(opts)
|
10
|
+
end
|
11
|
+
|
12
|
+
def on(args = {}, &proc)
|
13
|
+
Application.on(args, &proc)
|
14
|
+
end
|
15
|
+
|
16
|
+
def on_key(&proc)
|
17
|
+
Application.on_key(&proc)
|
14
18
|
end
|
15
19
|
|
16
|
-
def
|
17
|
-
|
20
|
+
def on_controller(&proc)
|
21
|
+
Application.on_controller(&proc)
|
18
22
|
end
|
19
23
|
|
20
24
|
def update(&proc)
|
21
|
-
|
25
|
+
Application.update(&proc)
|
26
|
+
end
|
27
|
+
|
28
|
+
def clear
|
29
|
+
Application.clear
|
22
30
|
end
|
23
31
|
|
24
32
|
def show
|
25
|
-
|
33
|
+
Application.show
|
26
34
|
end
|
27
35
|
|
28
|
-
def
|
29
|
-
|
36
|
+
def close
|
37
|
+
Application.close
|
30
38
|
end
|
39
|
+
|
31
40
|
end
|
data/lib/ruby2d/exceptions.rb
CHANGED
@@ -8,9 +8,8 @@ module Ruby2D
|
|
8
8
|
|
9
9
|
def initialize(msg)
|
10
10
|
super(msg)
|
11
|
-
puts error("\nRuby 2D Error:") << " " <<
|
12
|
-
|
13
|
-
puts bold(" " + caller.last), "\n"
|
11
|
+
puts error("\nRuby 2D Error:") << " #{msg}" <<
|
12
|
+
bold("\nOccurred in:\n #{caller.last}\n")
|
14
13
|
end
|
15
14
|
end
|
16
15
|
end
|
data/lib/ruby2d/image.rb
CHANGED
@@ -3,25 +3,38 @@
|
|
3
3
|
module Ruby2D
|
4
4
|
class Image
|
5
5
|
|
6
|
-
attr_accessor :x, :y
|
6
|
+
attr_accessor :x, :y, :width, :height, :data
|
7
|
+
attr_reader :path, :color
|
7
8
|
|
8
9
|
def initialize(x, y, path)
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
# TODO: Check if file exists
|
12
|
+
# `File.exists?` is not available in MRuby
|
13
|
+
# Ideally would do:
|
14
|
+
# unless File.exists? path
|
15
|
+
# raise Error, "Cannot find image file `#{path}`"
|
16
|
+
# end
|
13
17
|
|
14
18
|
@type_id = 3
|
15
19
|
@x, @y, @path = x, y, path
|
16
|
-
|
17
|
-
|
18
|
-
|
20
|
+
@color = Color.new([1, 1, 1, 1])
|
21
|
+
init(path)
|
22
|
+
add
|
23
|
+
end
|
24
|
+
|
25
|
+
def color=(c)
|
26
|
+
@color = Color.new(c)
|
27
|
+
end
|
28
|
+
|
29
|
+
def add
|
30
|
+
if Module.const_defined? :DSL
|
31
|
+
Application.add(self)
|
19
32
|
end
|
20
33
|
end
|
21
34
|
|
22
35
|
def remove
|
23
|
-
if
|
24
|
-
|
36
|
+
if Module.const_defined? :DSL
|
37
|
+
Application.remove(self)
|
25
38
|
end
|
26
39
|
end
|
27
40
|
|
data/lib/ruby2d/music.rb
ADDED
data/lib/ruby2d/quad.rb
CHANGED
@@ -19,10 +19,7 @@ module Ruby2D
|
|
19
19
|
@x1, @y1, @x2, @y2, @x3, @y3, @x4, @y4 = x1, y1, x2, y2, x3, y3, x4, y4
|
20
20
|
@color = c
|
21
21
|
update_color(c)
|
22
|
-
|
23
|
-
if defined? Ruby2D::DSL
|
24
|
-
Ruby2D::Application.add(self)
|
25
|
-
end
|
22
|
+
add
|
26
23
|
end
|
27
24
|
|
28
25
|
def color=(c)
|
@@ -30,9 +27,15 @@ module Ruby2D
|
|
30
27
|
update_color(c)
|
31
28
|
end
|
32
29
|
|
30
|
+
def add
|
31
|
+
if Module.const_defined? :DSL
|
32
|
+
Application.add(self)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
33
36
|
def remove
|
34
|
-
if
|
35
|
-
|
37
|
+
if Module.const_defined? :DSL
|
38
|
+
Application.remove(self)
|
36
39
|
end
|
37
40
|
end
|
38
41
|
|
@@ -42,21 +45,27 @@ module Ruby2D
|
|
42
45
|
|
43
46
|
# If a valid color, use it for each vertex
|
44
47
|
if Color.is_valid? c
|
45
|
-
@c1 =
|
46
|
-
@c2 =
|
47
|
-
@c3 =
|
48
|
-
@c4 =
|
49
|
-
|
48
|
+
@c1 = Color.new(c)
|
49
|
+
@c2 = Color.new(c)
|
50
|
+
@c3 = Color.new(c)
|
51
|
+
@c4 = Color.new(c)
|
52
|
+
|
53
|
+
elsif c.class == Array && c.length < 4
|
54
|
+
raise Error, "Quads require 4 colors, one for each vertex. Only " <<
|
55
|
+
"#{c.length} were given."
|
56
|
+
|
50
57
|
# If a valid array of colors, assign them to each vertex, respectively
|
51
58
|
elsif c.all? { |el| Color.is_valid? el }
|
52
|
-
@c1 =
|
53
|
-
@c2 =
|
54
|
-
@c3 =
|
55
|
-
@c4 =
|
59
|
+
@c1 = Color.new(c[0])
|
60
|
+
@c2 = Color.new(c[1])
|
61
|
+
@c3 = Color.new(c[2])
|
62
|
+
@c4 = Color.new(c[3])
|
63
|
+
|
56
64
|
else
|
57
65
|
raise Error, "Not a valid color for #{self.class}"
|
58
66
|
end
|
59
67
|
|
60
68
|
end
|
69
|
+
|
61
70
|
end
|
62
71
|
end
|
data/lib/ruby2d/rectangle.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# rectangle.rb
|
2
2
|
|
3
3
|
module Ruby2D
|
4
|
-
class Rectangle <
|
4
|
+
class Rectangle < Quad
|
5
5
|
|
6
6
|
attr_reader :x, :y, :width, :height
|
7
7
|
|
@@ -10,10 +10,7 @@ module Ruby2D
|
|
10
10
|
@x, @y, @width, @height, @color = x, y, w, h, c
|
11
11
|
update_coords(x, y, w, h)
|
12
12
|
update_color(c)
|
13
|
-
|
14
|
-
if defined? Ruby2D::DSL
|
15
|
-
Ruby2D::Application.add(self)
|
16
|
-
end
|
13
|
+
add
|
17
14
|
end
|
18
15
|
|
19
16
|
def x=(x)
|
data/lib/ruby2d/sound.rb
CHANGED
@@ -3,16 +3,13 @@
|
|
3
3
|
module Ruby2D
|
4
4
|
class Sound
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
raise Error, "Cannot find sound file!"
|
9
|
-
end
|
10
|
-
window.create_audio(self, path)
|
11
|
-
@window, @path = window, path
|
12
|
-
end
|
6
|
+
attr_accessor :data
|
7
|
+
attr_reader :path
|
13
8
|
|
14
|
-
def
|
15
|
-
|
9
|
+
def initialize(path)
|
10
|
+
# TODO: Check if file exists
|
11
|
+
init(path)
|
12
|
+
@path = path
|
16
13
|
end
|
17
14
|
|
18
15
|
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# sprite.rb
|
2
|
+
|
3
|
+
module Ruby2D
|
4
|
+
class Sprite
|
5
|
+
|
6
|
+
attr_accessor :x, :y, :clip_x, :clip_y, :clip_w, :clip_h, :data
|
7
|
+
|
8
|
+
def initialize(x, y, path)
|
9
|
+
|
10
|
+
# unless File.exists? path
|
11
|
+
# raise Error, "Cannot find image file `#{path}`"
|
12
|
+
# end
|
13
|
+
|
14
|
+
@type_id = 4
|
15
|
+
@x, @y, @path = x, y, path
|
16
|
+
@clip_x, @clip_y, @clip_w, @clip_h = 0, 0, 0, 0
|
17
|
+
@default = nil
|
18
|
+
@animations = {}
|
19
|
+
@current_animation = nil
|
20
|
+
@current_frame = 0
|
21
|
+
@current_frame_time = 0
|
22
|
+
|
23
|
+
init(path)
|
24
|
+
if Module.const_defined? :DSL
|
25
|
+
Application.add(self)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def start(x, y, w, h)
|
30
|
+
@default = [x, y, w, h]
|
31
|
+
clip(x, y, w, h)
|
32
|
+
end
|
33
|
+
|
34
|
+
def add(animations)
|
35
|
+
@animations.merge!(animations)
|
36
|
+
end
|
37
|
+
|
38
|
+
def animate(animation)
|
39
|
+
if @current_animation != animation
|
40
|
+
@current_frame = 0
|
41
|
+
@current_frame_time = 0
|
42
|
+
@current_animation = animation
|
43
|
+
end
|
44
|
+
animate_frames(@animations[animation])
|
45
|
+
end
|
46
|
+
|
47
|
+
def reset
|
48
|
+
clip(@default[0], @default[1], @default[2], @default[3])
|
49
|
+
@current_animation = nil
|
50
|
+
end
|
51
|
+
|
52
|
+
# TODO: Sprite already has an `add` method, have to reconsile
|
53
|
+
# def add
|
54
|
+
# if Module.const_defined? :DSL
|
55
|
+
# Application.add(self)
|
56
|
+
# end
|
57
|
+
# end
|
58
|
+
|
59
|
+
def remove
|
60
|
+
if Module.const_defined? :DSL
|
61
|
+
Application.remove(self)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def clip(x, y, w, h)
|
68
|
+
@clip_x, @clip_y, @clip_w, @clip_h = x, y, w, h
|
69
|
+
end
|
70
|
+
|
71
|
+
def animate_frames(frames)
|
72
|
+
if @current_frame_time < frames[@current_frame][4]
|
73
|
+
clip_with_current_frame(frames)
|
74
|
+
@current_frame_time += 1
|
75
|
+
else
|
76
|
+
@current_frame += 1
|
77
|
+
if @current_frame == frames.length
|
78
|
+
@current_frame = 0
|
79
|
+
end
|
80
|
+
clip_with_current_frame(frames)
|
81
|
+
@current_frame_time = 0
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def clip_with_current_frame(frames)
|
86
|
+
clip(frames[@current_frame][0], frames[@current_frame][1],
|
87
|
+
frames[@current_frame][2], frames[@current_frame][3])
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|