ruby2d 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/ruby2d +24 -274
- data/ext/ruby2d/extconf.rb +1 -1
- data/ext/ruby2d/ruby2d.c +63 -9
- data/lib/ruby2d.rb +51 -28
- data/lib/ruby2d/circle.rb +2 -6
- data/lib/ruby2d/cli/build.rb +226 -0
- data/lib/ruby2d/cli/console.rb +49 -0
- data/lib/ruby2d/cli/enable_console.rb +5 -0
- data/lib/ruby2d/cli/launch.rb +50 -0
- data/lib/ruby2d/color.rb +14 -12
- data/lib/ruby2d/dsl.rb +2 -2
- data/lib/ruby2d/image.rb +5 -17
- data/lib/ruby2d/line.rb +6 -5
- data/lib/ruby2d/music.rb +5 -9
- data/lib/ruby2d/quad.rb +2 -3
- data/lib/ruby2d/rectangle.rb +1 -6
- data/lib/ruby2d/renderable.rb +11 -6
- data/lib/ruby2d/sound.rb +2 -6
- data/lib/ruby2d/sprite.rb +4 -7
- data/lib/ruby2d/square.rb +1 -0
- data/lib/ruby2d/text.rb +5 -16
- data/lib/ruby2d/triangle.rb +2 -2
- data/lib/ruby2d/version.rb +1 -1
- data/lib/ruby2d/window.rb +43 -7
- metadata +7 -19
- data/ext/ruby2d/ruby2d-opal.rb +0 -289
data/lib/ruby2d/dsl.rb
CHANGED
data/lib/ruby2d/image.rb
CHANGED
@@ -4,18 +4,14 @@ module Ruby2D
|
|
4
4
|
class Image
|
5
5
|
include Renderable
|
6
6
|
|
7
|
-
attr_reader :path
|
7
|
+
attr_reader :path
|
8
8
|
attr_accessor :x, :y, :width, :height, :rotate, :data
|
9
9
|
|
10
10
|
def initialize(path, opts = {})
|
11
|
-
|
12
|
-
|
13
|
-
unless RUBY_ENGINE == 'opal'
|
14
|
-
unless File.exists? @path
|
15
|
-
raise Error, "Cannot find image file `#{@path}`"
|
16
|
-
end
|
11
|
+
unless File.exist? path
|
12
|
+
raise Error, "Cannot find image file `#{path}`"
|
17
13
|
end
|
18
|
-
|
14
|
+
@path = path
|
19
15
|
@x = opts[:x] || 0
|
20
16
|
@y = opts[:y] || 0
|
21
17
|
@z = opts[:z] || 0
|
@@ -23,18 +19,10 @@ module Ruby2D
|
|
23
19
|
@height = opts[:height] || nil
|
24
20
|
@rotate = opts[:rotate] || 0
|
25
21
|
self.color = opts[:color] || 'white'
|
26
|
-
|
22
|
+
self.opacity = opts[:opacity] if opts[:opacity]
|
27
23
|
ext_init(@path)
|
28
24
|
add
|
29
25
|
end
|
30
26
|
|
31
|
-
def color=(c)
|
32
|
-
@color = Color.new(c)
|
33
|
-
end
|
34
|
-
|
35
|
-
def contains?(x, y)
|
36
|
-
@x < x and @x + @width > x and @y < y and @y + @height > y
|
37
|
-
end
|
38
|
-
|
39
27
|
end
|
40
28
|
end
|
data/lib/ruby2d/line.rb
CHANGED
@@ -4,7 +4,7 @@ module Ruby2D
|
|
4
4
|
class Line
|
5
5
|
include Renderable
|
6
6
|
|
7
|
-
attr_accessor :x1, :x2, :y1, :y2
|
7
|
+
attr_accessor :x1, :x2, :y1, :y2
|
8
8
|
|
9
9
|
def initialize(opts = {})
|
10
10
|
@x1 = opts[:x1] || 0
|
@@ -14,11 +14,12 @@ module Ruby2D
|
|
14
14
|
@width = opts[:width] || 2
|
15
15
|
@z = opts[:z] || 0
|
16
16
|
self.color = opts[:color] || 'white'
|
17
|
+
self.opacity = opts[:opacity] if opts[:opacity]
|
17
18
|
add
|
18
19
|
end
|
19
20
|
|
20
21
|
def color=(c)
|
21
|
-
@color = Color.
|
22
|
+
@color = Color.set(c)
|
22
23
|
update_color(@color)
|
23
24
|
end
|
24
25
|
|
@@ -32,9 +33,9 @@ module Ruby2D
|
|
32
33
|
# the width. For reference:
|
33
34
|
# https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
|
34
35
|
def contains?(x, y)
|
35
|
-
points_distance(x1, y1, x, y)
|
36
|
-
points_distance(x2, y2, x, y)
|
37
|
-
(((@y2 - @y1) * x - (@x2 - @x1) * y + @x2 * @y1 - @y2 * @x1).abs / length)
|
36
|
+
points_distance(x1, y1, x, y) <= length &&
|
37
|
+
points_distance(x2, y2, x, y) <= length &&
|
38
|
+
(((@y2 - @y1) * x - (@x2 - @x1) * y + @x2 * @y1 - @y2 * @x1).abs / length) <= 0.5 * @width
|
38
39
|
end
|
39
40
|
|
40
41
|
private
|
data/lib/ruby2d/music.rb
CHANGED
@@ -7,13 +7,9 @@ module Ruby2D
|
|
7
7
|
attr_accessor :loop, :data
|
8
8
|
|
9
9
|
def initialize(path)
|
10
|
-
|
11
|
-
|
12
|
-
unless File.exists? path
|
13
|
-
raise Error, "Cannot find audio file `#{path}`"
|
14
|
-
end
|
10
|
+
unless File.exist? path
|
11
|
+
raise Error, "Cannot find audio file `#{path}`"
|
15
12
|
end
|
16
|
-
|
17
13
|
@path = path
|
18
14
|
@loop = false
|
19
15
|
ext_init(path)
|
@@ -39,16 +35,16 @@ module Ruby2D
|
|
39
35
|
ext_stop
|
40
36
|
end
|
41
37
|
|
42
|
-
# Returns the
|
38
|
+
# Returns the volume, in percentage
|
43
39
|
def self.volume
|
44
|
-
self.
|
40
|
+
self.ext_get_volume
|
45
41
|
end
|
46
42
|
|
47
43
|
# Set music volume, 0 to 100%
|
48
44
|
def self.volume=(v)
|
49
45
|
# If a negative value, volume will be 0
|
50
46
|
if v < 0 then v = 0 end
|
51
|
-
self.
|
47
|
+
self.ext_set_volume(v)
|
52
48
|
end
|
53
49
|
|
54
50
|
# Alias instance methods to class methods
|
data/lib/ruby2d/quad.rb
CHANGED
@@ -4,8 +4,6 @@ module Ruby2D
|
|
4
4
|
class Quad
|
5
5
|
include Renderable
|
6
6
|
|
7
|
-
attr_reader :color
|
8
|
-
|
9
7
|
# Coordinates in clockwise order, starting at top left:
|
10
8
|
# x1,y1 == top left
|
11
9
|
# x2,y2 == top right
|
@@ -27,11 +25,12 @@ module Ruby2D
|
|
27
25
|
@y4 = opts[:y4] || 100
|
28
26
|
@z = opts[:z] || 0
|
29
27
|
self.color = opts[:color] || 'white'
|
28
|
+
self.opacity = opts[:opacity] if opts[:opacity]
|
30
29
|
add
|
31
30
|
end
|
32
31
|
|
33
32
|
def color=(c)
|
34
|
-
@color = Color.
|
33
|
+
@color = Color.set(c)
|
35
34
|
update_color(@color)
|
36
35
|
end
|
37
36
|
|
data/lib/ruby2d/rectangle.rb
CHANGED
@@ -3,8 +3,6 @@
|
|
3
3
|
module Ruby2D
|
4
4
|
class Rectangle < Quad
|
5
5
|
|
6
|
-
attr_reader :x, :y, :width, :height
|
7
|
-
|
8
6
|
def initialize(opts = {})
|
9
7
|
@x = opts[:x] || 0
|
10
8
|
@y = opts[:y] || 0
|
@@ -12,6 +10,7 @@ module Ruby2D
|
|
12
10
|
@width = opts[:width] || 200
|
13
11
|
@height = opts[:height] || 100
|
14
12
|
self.color = opts[:color] || 'white'
|
13
|
+
self.opacity = opts[:opacity] if opts[:opacity]
|
15
14
|
update_coords(@x, @y, @width, @height)
|
16
15
|
add
|
17
16
|
end
|
@@ -40,10 +39,6 @@ module Ruby2D
|
|
40
39
|
update_coords(@x, @y, @width, h)
|
41
40
|
end
|
42
41
|
|
43
|
-
def contains?(x, y)
|
44
|
-
@x < x and @x + @width > x and @y < y and @y + @height > y
|
45
|
-
end
|
46
|
-
|
47
42
|
private
|
48
43
|
|
49
44
|
def update_coords(x, y, w, h)
|
data/lib/ruby2d/renderable.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
module Ruby2D
|
4
4
|
module Renderable
|
5
5
|
|
6
|
-
attr_reader :z
|
6
|
+
attr_reader :x, :y, :z, :width, :height, :color
|
7
7
|
|
8
8
|
# Set the z position (depth) of the object
|
9
9
|
def z=(z)
|
@@ -26,6 +26,15 @@ module Ruby2D
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
# Set the color value
|
30
|
+
def color=(c)
|
31
|
+
@color = Color.new(c)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Allow British English spelling of color
|
35
|
+
def colour; self.color end
|
36
|
+
def colour=(c); self.color = c end
|
37
|
+
|
29
38
|
# Allow shortcuts for setting color values
|
30
39
|
def r; self.color.r end
|
31
40
|
def g; self.color.g end
|
@@ -38,13 +47,9 @@ module Ruby2D
|
|
38
47
|
def opacity; self.color.opacity end
|
39
48
|
def opacity=(val); self.color.opacity = val end
|
40
49
|
|
41
|
-
# Allow British English spelling of color
|
42
|
-
def colour; self.color end
|
43
|
-
def colour=(c); self.color = c end
|
44
|
-
|
45
50
|
# Add a contains method stub
|
46
51
|
def contains?(x, y)
|
47
|
-
|
52
|
+
x > @x && x < (@x + @width) && y > @y && y < (@y + @height)
|
48
53
|
end
|
49
54
|
|
50
55
|
end
|
data/lib/ruby2d/sound.rb
CHANGED
@@ -7,13 +7,9 @@ module Ruby2D
|
|
7
7
|
attr_accessor :data
|
8
8
|
|
9
9
|
def initialize(path)
|
10
|
-
|
11
|
-
|
12
|
-
unless File.exists? path
|
13
|
-
raise Error, "Cannot find audio file `#{path}`"
|
14
|
-
end
|
10
|
+
unless File.exist? path
|
11
|
+
raise Error, "Cannot find audio file `#{path}`"
|
15
12
|
end
|
16
|
-
|
17
13
|
@path = path
|
18
14
|
ext_init(path)
|
19
15
|
end
|
data/lib/ruby2d/sprite.rb
CHANGED
@@ -4,16 +4,11 @@ module Ruby2D
|
|
4
4
|
class Sprite
|
5
5
|
include Renderable
|
6
6
|
|
7
|
-
attr_reader :x, :y, :width, :height
|
8
7
|
attr_accessor :rotate, :loop, :clip_x, :clip_y, :clip_width, :clip_height, :data
|
9
8
|
|
10
9
|
def initialize(path, opts = {})
|
11
|
-
|
12
|
-
|
13
|
-
unless RUBY_ENGINE == 'opal'
|
14
|
-
unless File.exists? path
|
15
|
-
raise Error, "Cannot find sprite image file `#{path}`"
|
16
|
-
end
|
10
|
+
unless File.exist? path
|
11
|
+
raise Error, "Cannot find sprite image file `#{path}`"
|
17
12
|
end
|
18
13
|
|
19
14
|
# Sprite image file path
|
@@ -26,6 +21,8 @@ module Ruby2D
|
|
26
21
|
@width = opts[:width] || nil
|
27
22
|
@height = opts[:height] || nil
|
28
23
|
@rotate = opts[:rotate] || 0
|
24
|
+
self.color = opts[:color] || 'white'
|
25
|
+
self.opacity = opts[:opacity] if opts[:opacity]
|
29
26
|
|
30
27
|
# Flipping status, coordinates, and size, used internally
|
31
28
|
@flip = nil
|
data/lib/ruby2d/square.rb
CHANGED
data/lib/ruby2d/text.rb
CHANGED
@@ -4,7 +4,7 @@ module Ruby2D
|
|
4
4
|
class Text
|
5
5
|
include Renderable
|
6
6
|
|
7
|
-
attr_reader :text, :size, :
|
7
|
+
attr_reader :text, :size, :font
|
8
8
|
attr_accessor :x, :y, :rotate, :data
|
9
9
|
|
10
10
|
def initialize(text, opts = {})
|
@@ -14,15 +14,12 @@ module Ruby2D
|
|
14
14
|
@text = text.to_s
|
15
15
|
@size = opts[:size] || 20
|
16
16
|
@rotate = opts[:rotate] || 0
|
17
|
+
self.color = opts[:color] || 'white'
|
18
|
+
self.opacity = opts[:opacity] if opts[:opacity]
|
17
19
|
@font = opts[:font] || Font.default
|
18
|
-
|
19
|
-
|
20
|
-
unless File.exists? @font
|
21
|
-
raise Error, "Cannot find font file `#{@font}`"
|
22
|
-
end
|
20
|
+
unless File.exist? @font
|
21
|
+
raise Error, "Cannot find font file `#{@font}`"
|
23
22
|
end
|
24
|
-
|
25
|
-
self.color = opts[:color] || 'white'
|
26
23
|
ext_init
|
27
24
|
add
|
28
25
|
end
|
@@ -32,13 +29,5 @@ module Ruby2D
|
|
32
29
|
ext_set(@text)
|
33
30
|
end
|
34
31
|
|
35
|
-
def color=(c)
|
36
|
-
@color = Color.new(c)
|
37
|
-
end
|
38
|
-
|
39
|
-
def contains?(x, y)
|
40
|
-
@x < x and @x + @width > x and @y < y and @y + @height > y
|
41
|
-
end
|
42
|
-
|
43
32
|
end
|
44
33
|
end
|
data/lib/ruby2d/triangle.rb
CHANGED
@@ -4,7 +4,6 @@ module Ruby2D
|
|
4
4
|
class Triangle
|
5
5
|
include Renderable
|
6
6
|
|
7
|
-
attr_reader :color
|
8
7
|
attr_accessor :x1, :y1, :c1,
|
9
8
|
:x2, :y2, :c2,
|
10
9
|
:x3, :y3, :c3
|
@@ -18,11 +17,12 @@ module Ruby2D
|
|
18
17
|
@y3 = opts[:y3] || 100
|
19
18
|
@z = opts[:z] || 0
|
20
19
|
self.color = opts[:color] || 'white'
|
20
|
+
self.opacity = opts[:opacity] if opts[:opacity]
|
21
21
|
add
|
22
22
|
end
|
23
23
|
|
24
24
|
def color=(c)
|
25
|
-
@color = Color.
|
25
|
+
@color = Color.set(c)
|
26
26
|
update_color(@color)
|
27
27
|
end
|
28
28
|
|
data/lib/ruby2d/version.rb
CHANGED
data/lib/ruby2d/window.rb
CHANGED
@@ -85,6 +85,9 @@ module Ruby2D
|
|
85
85
|
|
86
86
|
# Whether diagnostic messages should be printed
|
87
87
|
@diagnostics = false
|
88
|
+
|
89
|
+
# Console mode, enabled at command line
|
90
|
+
@console = $ruby2d_console_mode || false
|
88
91
|
end
|
89
92
|
|
90
93
|
# Class methods for convenient access to properties
|
@@ -108,9 +111,10 @@ module Ruby2D
|
|
108
111
|
def mouse_x; get(:mouse_x) end
|
109
112
|
def mouse_y; get(:mouse_y) end
|
110
113
|
def diagnostics; get(:diagnostics) end
|
114
|
+
def screenshot(opts = nil); get(:screenshot, opts) end
|
111
115
|
|
112
|
-
def get(sym)
|
113
|
-
@@window.get(sym)
|
116
|
+
def get(sym, opts = nil)
|
117
|
+
@@window.get(sym, opts)
|
114
118
|
end
|
115
119
|
|
116
120
|
def set(opts)
|
@@ -153,7 +157,7 @@ module Ruby2D
|
|
153
157
|
# Public instance methods
|
154
158
|
|
155
159
|
# Retrieve an attribute of the window
|
156
|
-
def get(sym)
|
160
|
+
def get(sym, opts = nil)
|
157
161
|
case sym
|
158
162
|
when :window; self
|
159
163
|
when :title; @title
|
@@ -179,6 +183,7 @@ module Ruby2D
|
|
179
183
|
when :mouse_x; @mouse_x
|
180
184
|
when :mouse_y; @mouse_y
|
181
185
|
when :diagnostics; @diagnostics
|
186
|
+
when :screenshot; screenshot(opts)
|
182
187
|
end
|
183
188
|
end
|
184
189
|
|
@@ -320,10 +325,8 @@ module Ruby2D
|
|
320
325
|
|
321
326
|
# Add controller mappings from file
|
322
327
|
def add_controller_mappings
|
323
|
-
|
324
|
-
|
325
|
-
ext_add_controller_mappings(@controller_mappings)
|
326
|
-
end
|
328
|
+
if File.exist? @controller_mappings
|
329
|
+
ext_add_controller_mappings(@controller_mappings)
|
327
330
|
end
|
328
331
|
end
|
329
332
|
|
@@ -356,6 +359,25 @@ module Ruby2D
|
|
356
359
|
# Update callback method, called by the native and web extentions
|
357
360
|
def update_callback
|
358
361
|
@update_proc.call
|
362
|
+
|
363
|
+
# Accept and eval commands if in console mode
|
364
|
+
if @console
|
365
|
+
if STDIN.ready?
|
366
|
+
cmd = STDIN.gets
|
367
|
+
begin
|
368
|
+
res = eval(cmd, TOPLEVEL_BINDING)
|
369
|
+
STDOUT.puts "=> #{res.inspect}"
|
370
|
+
STDOUT.flush
|
371
|
+
rescue SyntaxError => se
|
372
|
+
STDOUT.puts se
|
373
|
+
STDOUT.flush
|
374
|
+
rescue Exception => e
|
375
|
+
STDOUT.puts e
|
376
|
+
STDOUT.flush
|
377
|
+
end
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
359
381
|
end
|
360
382
|
|
361
383
|
# Show the window
|
@@ -363,6 +385,20 @@ module Ruby2D
|
|
363
385
|
ext_show
|
364
386
|
end
|
365
387
|
|
388
|
+
# Take screenshot
|
389
|
+
def screenshot(path)
|
390
|
+
if path
|
391
|
+
ext_screenshot(path)
|
392
|
+
else
|
393
|
+
if RUBY_ENGINE == 'ruby'
|
394
|
+
time = Time.now.utc.strftime '%Y-%m-%d--%H-%M-%S'
|
395
|
+
else
|
396
|
+
time = Time.now.utc.to_i
|
397
|
+
end
|
398
|
+
ext_screenshot("./screenshot-#{time}.png")
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
366
402
|
# Close the window
|
367
403
|
def close
|
368
404
|
ext_close
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby2d
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Black
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: opal
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0.11'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0.11'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rspec
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -148,10 +134,13 @@ files:
|
|
148
134
|
- assets/tvos/main.c
|
149
135
|
- bin/ruby2d
|
150
136
|
- ext/ruby2d/extconf.rb
|
151
|
-
- ext/ruby2d/ruby2d-opal.rb
|
152
137
|
- ext/ruby2d/ruby2d.c
|
153
138
|
- lib/ruby2d.rb
|
154
139
|
- lib/ruby2d/circle.rb
|
140
|
+
- lib/ruby2d/cli/build.rb
|
141
|
+
- lib/ruby2d/cli/console.rb
|
142
|
+
- lib/ruby2d/cli/enable_console.rb
|
143
|
+
- lib/ruby2d/cli/launch.rb
|
155
144
|
- lib/ruby2d/color.rb
|
156
145
|
- lib/ruby2d/colorize.rb
|
157
146
|
- lib/ruby2d/dsl.rb
|
@@ -189,8 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
189
178
|
- !ruby/object:Gem::Version
|
190
179
|
version: '0'
|
191
180
|
requirements: []
|
192
|
-
|
193
|
-
rubygems_version: 2.7.7
|
181
|
+
rubygems_version: 3.0.2
|
194
182
|
signing_key:
|
195
183
|
specification_version: 4
|
196
184
|
summary: Ruby 2D
|