ruby2d 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/assets/opal.js +42 -42
- data/assets/simple2d.js +422 -223
- data/bin/ruby2d +26 -24
- data/ext/ruby2d/extconf.rb +6 -6
- data/ext/ruby2d/ruby2d-opal.rb +133 -69
- data/ext/ruby2d/ruby2d.c +246 -114
- data/lib/ruby2d/application.rb +14 -18
- data/lib/ruby2d/color.rb +50 -12
- data/lib/ruby2d/dsl.rb +12 -16
- data/lib/ruby2d/exceptions.rb +0 -9
- data/lib/ruby2d/image.rb +25 -28
- data/lib/ruby2d/line.rb +64 -0
- data/lib/ruby2d/music.rb +30 -5
- data/lib/ruby2d/quad.rb +52 -44
- data/lib/ruby2d/rectangle.rb +24 -13
- data/lib/ruby2d/renderable.rb +35 -0
- data/lib/ruby2d/sound.rb +15 -5
- data/lib/ruby2d/sprite.rb +21 -19
- data/lib/ruby2d/square.rb +14 -9
- data/lib/ruby2d/text.rb +36 -32
- data/lib/ruby2d/triangle.rb +46 -44
- data/lib/ruby2d/version.rb +1 -1
- data/lib/ruby2d/window.rb +148 -120
- data/lib/ruby2d.rb +2 -0
- metadata +5 -3
data/bin/ruby2d
CHANGED
@@ -14,12 +14,14 @@ end
|
|
14
14
|
|
15
15
|
# The Ruby 2D library files
|
16
16
|
@lib_files = [
|
17
|
+
'renderable',
|
17
18
|
'exceptions',
|
18
19
|
'color',
|
19
20
|
'window',
|
20
21
|
'application',
|
21
22
|
'dsl',
|
22
23
|
'quad',
|
24
|
+
'line',
|
23
25
|
'rectangle',
|
24
26
|
'square',
|
25
27
|
'triangle',
|
@@ -46,18 +48,18 @@ end
|
|
46
48
|
# Assemble the Ruby 2D library in one `.rb` file
|
47
49
|
def make_lib
|
48
50
|
FileUtils.mkdir_p 'build'
|
49
|
-
|
51
|
+
|
50
52
|
lib_dir = "#{@gem_dir}/lib/ruby2d/"
|
51
|
-
|
53
|
+
|
52
54
|
lib = ""
|
53
55
|
@lib_files.each do |f|
|
54
56
|
lib << File.read("#{lib_dir + f}.rb") + "\n\n"
|
55
57
|
end
|
56
|
-
|
58
|
+
|
57
59
|
lib << "
|
58
60
|
include Ruby2D::DSL
|
59
61
|
include Ruby2D\n"
|
60
|
-
|
62
|
+
|
61
63
|
File.write('build/lib.rb', lib)
|
62
64
|
end
|
63
65
|
|
@@ -74,25 +76,25 @@ end
|
|
74
76
|
|
75
77
|
# Build a native version of the provided Ruby application
|
76
78
|
def build_native(rb_file)
|
77
|
-
|
79
|
+
|
78
80
|
# Check if MRuby exists; if not, quit
|
79
81
|
if `which mruby`.empty?
|
80
82
|
puts "#{'Error:'.error} Can't find MRuby, which is needed to build native Ruby 2D applications.\n"
|
81
83
|
exit
|
82
84
|
end
|
83
|
-
|
85
|
+
|
84
86
|
# Assemble the Ruby 2D library in one `.rb` file and compile to bytecode
|
85
87
|
make_lib
|
86
88
|
`mrbc -Bruby2d_lib -obuild/lib.c build/lib.rb`
|
87
|
-
|
89
|
+
|
88
90
|
# Read the provided Ruby source file, copy to build dir and compile to bytecode
|
89
|
-
|
91
|
+
|
90
92
|
File.open('build/src.rb', 'w') do |file|
|
91
93
|
file << strip_require(rb_file)
|
92
94
|
end
|
93
|
-
|
95
|
+
|
94
96
|
`mrbc -Bruby2d_app -obuild/src.c build/src.rb`
|
95
|
-
|
97
|
+
|
96
98
|
# Combine contents of C source files and bytecode into one file
|
97
99
|
open('build/app.c', 'w') do |f|
|
98
100
|
f << "#define MRUBY 1" << "\n\n"
|
@@ -100,10 +102,10 @@ def build_native(rb_file)
|
|
100
102
|
f << File.read("build/src.c") << "\n\n"
|
101
103
|
f << File.read("#{@gem_dir}/ext/ruby2d/ruby2d.c")
|
102
104
|
end
|
103
|
-
|
105
|
+
|
104
106
|
# Compile to native executable
|
105
107
|
`cc build/app.c -lmruby \`simple2d --libs\` -o build/app`
|
106
|
-
|
108
|
+
|
107
109
|
# Success!
|
108
110
|
puts "Native app created at `build/app`"
|
109
111
|
end
|
@@ -111,22 +113,22 @@ end
|
|
111
113
|
|
112
114
|
# Build a web-based version of the provided Ruby application
|
113
115
|
def build_web(rb_file)
|
114
|
-
|
116
|
+
|
115
117
|
# Assemble the Ruby 2D library in one `.rb` file and compile to JS
|
116
118
|
make_lib
|
117
119
|
`opal --compile --no-opal build/lib.rb > build/lib.js`
|
118
|
-
|
120
|
+
|
119
121
|
# Read the provided Ruby source file, copy to build dir, and compile to JS
|
120
|
-
|
122
|
+
|
121
123
|
File.open('build/src.rb', 'w') do |file|
|
122
124
|
file << strip_require(rb_file)
|
123
125
|
end
|
124
|
-
|
126
|
+
|
125
127
|
`opal --compile --no-opal build/src.rb > build/src.js`
|
126
|
-
|
128
|
+
|
127
129
|
FileUtils.cp "#{@gem_dir}/ext/ruby2d/ruby2d-opal.rb", "build/"
|
128
130
|
`opal --compile --no-opal build/ruby2d-opal.rb > build/ruby2d-opal.js`
|
129
|
-
|
131
|
+
|
130
132
|
# Combine contents of JS source files and compiled JS into one file
|
131
133
|
open('build/app.js', 'w') do |f|
|
132
134
|
f << File.read("#{@gem_dir}/assets/simple2d.js") << "\n\n"
|
@@ -135,10 +137,10 @@ def build_web(rb_file)
|
|
135
137
|
f << File.read("build/ruby2d-opal.js") << "\n\n"
|
136
138
|
f << File.read("build/src.js") << "\n\n"
|
137
139
|
end
|
138
|
-
|
140
|
+
|
139
141
|
# Copy over HTML template
|
140
142
|
FileUtils.cp "#{@gem_dir}/assets/template.html", "build/app.html"
|
141
|
-
|
143
|
+
|
142
144
|
# Success!
|
143
145
|
puts "Web app created at `build/app.js`",
|
144
146
|
" Run by opening `build/app.html`"
|
@@ -148,13 +150,13 @@ end
|
|
148
150
|
# Build an application package for the current platform
|
149
151
|
def build_package
|
150
152
|
require 'fileutils'
|
151
|
-
|
153
|
+
|
152
154
|
icon_path = "#{@gem_dir}/assets/app.icns"
|
153
|
-
|
155
|
+
|
154
156
|
FileUtils.mkpath "build/App.app/Contents/MacOS"
|
155
157
|
FileUtils.mkpath "build/App.app/Contents/Resources"
|
156
158
|
FileUtils.cp icon_path, "build/App.app/Contents/Resources"
|
157
|
-
|
159
|
+
|
158
160
|
info_plist = %(
|
159
161
|
<?xml version="1.0" encoding="UTF-8"?>
|
160
162
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
@@ -175,7 +177,7 @@ def build_package
|
|
175
177
|
</dict>
|
176
178
|
</plist>
|
177
179
|
)
|
178
|
-
|
180
|
+
|
179
181
|
File.open("build/App.app/Contents/Info.plist", 'w') { |f| f.write(info_plist) }
|
180
182
|
FileUtils.cp "build/app", "build/App.app/Contents/MacOS/"
|
181
183
|
puts "App written to `build/App.app`."
|
data/ext/ruby2d/extconf.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'mkmf'
|
2
2
|
|
3
|
-
S2D_VERSION = '0.
|
3
|
+
S2D_VERSION = '0.6.0' # Simple 2D minimum version required
|
4
4
|
$errors = []
|
5
5
|
|
6
6
|
class String
|
@@ -32,10 +32,10 @@ case RUBY_PLATFORM
|
|
32
32
|
|
33
33
|
# macOS
|
34
34
|
when /darwin/
|
35
|
-
|
35
|
+
|
36
36
|
# Simple 2D not installed
|
37
37
|
if `which simple2d`.empty?
|
38
|
-
|
38
|
+
|
39
39
|
# Homebrew not installed
|
40
40
|
if `which brew`.empty?
|
41
41
|
$errors << "Ruby 2D uses a native library called Simple 2D." <<
|
@@ -46,7 +46,7 @@ when /darwin/
|
|
46
46
|
" brew install simple2d".bold
|
47
47
|
print_errors
|
48
48
|
exit
|
49
|
-
|
49
|
+
|
50
50
|
# Homebrew installed, instruct to install Simple 2D
|
51
51
|
else
|
52
52
|
$errors << "Ruby 2D uses a native library called Simple 2D." <<
|
@@ -57,10 +57,10 @@ when /darwin/
|
|
57
57
|
exit
|
58
58
|
end
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
# Linux and Windows / MinGW
|
62
62
|
when /linux|mingw/
|
63
|
-
|
63
|
+
|
64
64
|
# Simple 2D not installed
|
65
65
|
if `which simple2d`.empty?
|
66
66
|
$errors << "Ruby 2D uses a native library called Simple 2D.\n" <<
|
data/ext/ruby2d/ruby2d-opal.rb
CHANGED
@@ -12,30 +12,75 @@ $R2D_WINDOW = nil
|
|
12
12
|
// @type_id values for rendering
|
13
13
|
const $R2D_TRIANGLE = 1;
|
14
14
|
const $R2D_QUAD = 2;
|
15
|
-
const $
|
16
|
-
const $
|
17
|
-
const $
|
15
|
+
const $R2D_LINE = 3;
|
16
|
+
const $R2D_IMAGE = 4;
|
17
|
+
const $R2D_SPRITE = 5;
|
18
|
+
const $R2D_TEXT = 6;
|
18
19
|
|
19
20
|
|
20
|
-
function on_key(e
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
function on_key(e) {
|
22
|
+
|
23
|
+
switch (e.type) {
|
24
|
+
case S2D.KEY_DOWN:
|
25
|
+
#{type = :down};
|
24
26
|
break;
|
25
|
-
|
26
|
-
|
27
|
-
#{$R2D_WINDOW.key_callback(`key`)};
|
27
|
+
case S2D.KEY_HELD:
|
28
|
+
#{type = :held};
|
28
29
|
break;
|
29
|
-
|
30
|
-
|
31
|
-
#{$R2D_WINDOW.key_up_callback(`key`)};
|
30
|
+
case S2D.KEY_UP:
|
31
|
+
#{type = :up};
|
32
32
|
break;
|
33
33
|
}
|
34
|
+
|
35
|
+
#{$R2D_WINDOW.key_callback(type, `e.key`)};
|
34
36
|
}
|
35
37
|
|
36
38
|
|
37
|
-
function on_mouse(
|
38
|
-
|
39
|
+
function on_mouse(e) {
|
40
|
+
|
41
|
+
#{direction = nil}
|
42
|
+
#{button = nil}
|
43
|
+
|
44
|
+
switch (e.type) {
|
45
|
+
case S2D.MOUSE_DOWN:
|
46
|
+
#{type = :down};
|
47
|
+
break;
|
48
|
+
case S2D.MOUSE_UP:
|
49
|
+
#{type = :up};
|
50
|
+
break;
|
51
|
+
case S2D.MOUSE_SCROLL:
|
52
|
+
#{type = :scroll};
|
53
|
+
#{direction} = e.direction == S2D.MOUSE_SCROLL_NORMAL ? #{:normal} : #{:inverted};
|
54
|
+
break;
|
55
|
+
case S2D.MOUSE_MOVE:
|
56
|
+
#{type = :move};
|
57
|
+
break;
|
58
|
+
}
|
59
|
+
|
60
|
+
if (e.type == S2D.MOUSE_DOWN || e.type == S2D.MOUSE_UP) {
|
61
|
+
switch (e.button) {
|
62
|
+
case S2D.MOUSE_LEFT:
|
63
|
+
#{button = :left};
|
64
|
+
break;
|
65
|
+
case S2D.MOUSE_MIDDLE:
|
66
|
+
#{button = :middle};
|
67
|
+
break;
|
68
|
+
case S2D.MOUSE_RIGHT:
|
69
|
+
#{button = :right};
|
70
|
+
break;
|
71
|
+
case S2D.MOUSE_X1:
|
72
|
+
#{button = :x1};
|
73
|
+
break;
|
74
|
+
case S2D.MOUSE_X2:
|
75
|
+
#{button = :x2};
|
76
|
+
break;
|
77
|
+
}
|
78
|
+
}
|
79
|
+
|
80
|
+
#{$R2D_WINDOW.mouse_callback(
|
81
|
+
type, button, direction,
|
82
|
+
`e.x`, `e.y`, `e.delta_x`, `e.delta_y`
|
83
|
+
)};
|
39
84
|
}
|
40
85
|
|
41
86
|
|
@@ -49,30 +94,30 @@ function update() {
|
|
49
94
|
|
50
95
|
|
51
96
|
function render() {
|
52
|
-
|
97
|
+
|
53
98
|
// Set background color
|
54
99
|
win.background.r = #{$R2D_WINDOW.get(:background).r};
|
55
100
|
win.background.g = #{$R2D_WINDOW.get(:background).g};
|
56
101
|
win.background.b = #{$R2D_WINDOW.get(:background).b};
|
57
102
|
win.background.a = #{$R2D_WINDOW.get(:background).a};
|
58
|
-
|
103
|
+
|
59
104
|
var objects = #{$R2D_WINDOW.objects};
|
60
|
-
|
105
|
+
|
61
106
|
for (var i = 0; i < objects.length; i++) {
|
62
|
-
|
107
|
+
|
63
108
|
var el = objects[i];
|
64
|
-
|
109
|
+
|
65
110
|
switch (el.type_id) {
|
66
|
-
|
111
|
+
|
67
112
|
case $R2D_TRIANGLE:
|
68
|
-
|
113
|
+
|
69
114
|
S2D.DrawTriangle(
|
70
115
|
el.x1, el.y1, el.c1.r, el.c1.g, el.c1.b, el.c1.a,
|
71
116
|
el.x2, el.y2, el.c2.r, el.c2.g, el.c2.b, el.c2.a,
|
72
117
|
el.x3, el.y3, el.c3.r, el.c3.g, el.c3.b, el.c3.a
|
73
118
|
);
|
74
119
|
break;
|
75
|
-
|
120
|
+
|
76
121
|
case $R2D_QUAD:
|
77
122
|
S2D.DrawQuad(
|
78
123
|
el.x1, el.y1, el.c1.r, el.c1.g, el.c1.b, el.c1.a,
|
@@ -81,26 +126,36 @@ function render() {
|
|
81
126
|
el.x4, el.y4, el.c4.r, el.c4.g, el.c4.b, el.c4.a
|
82
127
|
);
|
83
128
|
break;
|
84
|
-
|
129
|
+
|
130
|
+
case $R2D_LINE:
|
131
|
+
S2D.DrawLine(
|
132
|
+
el.x1, el.y1, el.x2, el.y2, el.width,
|
133
|
+
el.c1.r, el.c1.g, el.c1.b, el.c1.a,
|
134
|
+
el.c2.r, el.c2.g, el.c2.b, el.c2.a,
|
135
|
+
el.c3.r, el.c3.g, el.c3.b, el.c3.a,
|
136
|
+
el.c4.r, el.c4.g, el.c4.b, el.c4.a
|
137
|
+
);
|
138
|
+
break;
|
139
|
+
|
85
140
|
case $R2D_IMAGE:
|
86
141
|
el.data.x = el.x;
|
87
142
|
el.data.y = el.y;
|
88
|
-
|
143
|
+
|
89
144
|
if (el.width != Opal.nil) el.data.width = el.width;
|
90
145
|
if (el.height != Opal.nil) el.data.height = el.height;
|
91
|
-
|
146
|
+
|
92
147
|
el.data.color.r = el.color.r;
|
93
148
|
el.data.color.g = el.color.g;
|
94
149
|
el.data.color.b = el.color.b;
|
95
150
|
el.data.color.a = el.color.a;
|
96
|
-
|
151
|
+
|
97
152
|
S2D.DrawImage(el.data);
|
98
153
|
break;
|
99
|
-
|
154
|
+
|
100
155
|
case $R2D_SPRITE:
|
101
156
|
el.data.x = el.x;
|
102
157
|
el.data.y = el.y;
|
103
|
-
|
158
|
+
|
104
159
|
S2D.ClipSprite(
|
105
160
|
el.data,
|
106
161
|
el.clip_x,
|
@@ -108,111 +163,120 @@ function render() {
|
|
108
163
|
el.clip_w,
|
109
164
|
el.clip_h
|
110
165
|
);
|
111
|
-
|
166
|
+
|
112
167
|
S2D.DrawSprite(el.data);
|
113
168
|
break;
|
114
|
-
|
169
|
+
|
115
170
|
case $R2D_TEXT:
|
116
171
|
el.data.x = el.x;
|
117
172
|
el.data.y = el.y;
|
118
|
-
|
173
|
+
|
119
174
|
el.data.color.r = el.color.r;
|
120
175
|
el.data.color.g = el.color.g;
|
121
176
|
el.data.color.b = el.color.b;
|
122
177
|
el.data.color.a = el.color.a;
|
123
|
-
|
178
|
+
|
124
179
|
S2D.DrawText(el.data);
|
125
180
|
break;
|
126
181
|
}
|
127
|
-
|
182
|
+
|
128
183
|
}
|
129
184
|
}`
|
130
185
|
|
131
186
|
|
132
187
|
module Ruby2D
|
133
|
-
|
134
188
|
class Image
|
135
|
-
def
|
136
|
-
`#{self}.data = S2D.CreateImage(path)
|
189
|
+
def ext_image_init(path)
|
190
|
+
`#{self}.data = S2D.CreateImage(path, function() {
|
191
|
+
if (#{@width} == Opal.nil) {
|
192
|
+
#{@width} = #{self}.data.width;
|
193
|
+
}
|
194
|
+
if (#{@height} == Opal.nil) {
|
195
|
+
#{@height} = #{self}.data.height;
|
196
|
+
}
|
197
|
+
});`
|
137
198
|
end
|
138
199
|
end
|
139
|
-
|
200
|
+
|
140
201
|
class Sprite
|
141
|
-
def
|
202
|
+
def ext_sprite_init(path)
|
142
203
|
`#{self}.data = S2D.CreateSprite(path);`
|
143
204
|
end
|
144
205
|
end
|
145
|
-
|
206
|
+
|
146
207
|
class Text
|
147
|
-
def
|
208
|
+
def ext_text_init
|
148
209
|
`#{self}.data = S2D.CreateText(#{self}.font, #{self}.text, #{self}.size);`
|
210
|
+
@width = `#{self}.data.width;`
|
211
|
+
@height = `#{self}.data.height;`
|
149
212
|
end
|
150
|
-
|
151
|
-
def
|
152
|
-
|
153
|
-
|
213
|
+
|
214
|
+
def ext_text_set(msg)
|
215
|
+
`S2D.SetText(#{self}.data, #{msg});`
|
216
|
+
@width = `#{self}.data.width;`
|
217
|
+
@height = `#{self}.data.height;`
|
154
218
|
end
|
155
219
|
end
|
156
|
-
|
220
|
+
|
157
221
|
class Sound
|
158
|
-
def
|
222
|
+
def ext_sound_init(path)
|
159
223
|
`#{self}.data = S2D.CreateSound(path);`
|
160
224
|
end
|
161
|
-
|
162
|
-
def
|
225
|
+
|
226
|
+
def ext_sound_play
|
163
227
|
`S2D.PlaySound(#{self}.data);`
|
164
228
|
end
|
165
229
|
end
|
166
|
-
|
230
|
+
|
167
231
|
class Music
|
168
|
-
def
|
232
|
+
def ext_music_init(path)
|
169
233
|
`#{self}.data = S2D.CreateMusic(path);`
|
170
234
|
end
|
171
|
-
|
172
|
-
def
|
235
|
+
|
236
|
+
def ext_music_play
|
173
237
|
`S2D.PlayMusic(#{self}.data, #{self}.loop);`
|
174
238
|
end
|
175
|
-
|
176
|
-
def
|
239
|
+
|
240
|
+
def ext_music_pause
|
177
241
|
`S2D.PauseMusic();`
|
178
242
|
end
|
179
|
-
|
180
|
-
def
|
243
|
+
|
244
|
+
def ext_music_resume
|
181
245
|
`S2D.ResumeMusic();`
|
182
246
|
end
|
183
|
-
|
184
|
-
def
|
247
|
+
|
248
|
+
def ext_music_stop
|
185
249
|
`S2D.StopMusic();`
|
186
250
|
end
|
187
|
-
|
188
|
-
def
|
251
|
+
|
252
|
+
def ext_music_fadeout(ms)
|
189
253
|
`S2D.FadeOutMusic(ms);`
|
190
254
|
end
|
191
255
|
end
|
192
|
-
|
256
|
+
|
193
257
|
class Window
|
194
|
-
def
|
258
|
+
def ext_window_show
|
195
259
|
$R2D_WINDOW = self
|
196
|
-
|
260
|
+
|
197
261
|
`
|
198
262
|
var width = #{$R2D_WINDOW.get(:width)};
|
199
263
|
var height = #{$R2D_WINDOW.get(:height)};
|
200
|
-
|
264
|
+
|
201
265
|
var vp_w = #{$R2D_WINDOW.get(:viewport_width)};
|
202
266
|
var viewport_width = vp_w != Opal.nil ? vp_w : width;
|
203
|
-
|
267
|
+
|
204
268
|
var vp_h = #{$R2D_WINDOW.get(:viewport_height)};
|
205
269
|
var viewport_height = vp_h != Opal.nil ? vp_h : height;
|
206
|
-
|
270
|
+
|
207
271
|
win = S2D.CreateWindow(
|
208
272
|
#{$R2D_WINDOW.get(:title)}, width, height, update, render, "ruby2d-app", {}
|
209
273
|
);
|
210
|
-
|
274
|
+
|
211
275
|
win.viewport.width = viewport_width;
|
212
276
|
win.viewport.height = viewport_height;
|
213
277
|
win.on_key = on_key;
|
214
278
|
win.on_mouse = on_mouse;
|
215
|
-
|
279
|
+
|
216
280
|
S2D.Show(win);`
|
217
281
|
end
|
218
282
|
end
|