ruby2d 0.7.0 → 0.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: befe5ad5a05dfdab4e9da310b4a016b138692dbd372f68b0fb9e5fa2c8836e78
4
- data.tar.gz: 89523d76e354169eff325ea485b3afde30b085a1a9431d9b6135ef90eadee53a
3
+ metadata.gz: daade2a60f6c1e24974a8ca77aafb4cd83e1c59cdf4aff49b8178ee7c880f0f1
4
+ data.tar.gz: d07549d541ebe08e984b48373b96b890848dcf98373cc565b146c9d97fdcdb5a
5
5
  SHA512:
6
- metadata.gz: 6e47726fb332a184bc026898f5b50d67f7463f0cc57350a37200e71f74d35e66902aaae6c61704a8ef467de461f7c8335c59feb890dcd319de2ac000ddfa265a
7
- data.tar.gz: eb887567fc2c23d9ea7d10c12b79b6392f1e572da82d2fdc0e1bb791298de33b5a1c942c125965b3cf002d6bffdbb1dd0f150023919b300503aaf577e0dbbbc1
6
+ metadata.gz: 379a8eee502d3599654094b625c8e3ca8039982f3d9a1857bf5783e3627b10b1bc9ccc4d31efeb1a13c00234cd35762f0d658890797ed8fc5242ba7f52395e99
7
+ data.tar.gz: bb7683a054c5e6e9848f2f791dc3e3f5bbb33266a2f798a4e06b3f1df299919cd248c1d4f19e5c31d0fabe4518fadf9cc7f5e8d7e975f49c79fff53e001e9390
data/bin/ruby2d CHANGED
@@ -1,296 +1,38 @@
1
1
  #!/usr/bin/env ruby
2
+
2
3
  require 'ruby2d/colorize'
3
4
  require 'ruby2d/version'
4
- require 'fileutils'
5
-
6
- # The installed gem directory
7
- @gem_dir = "#{Gem::Specification.find_by_name('ruby2d').gem_dir}"
8
-
9
- # The Ruby 2D library files
10
- @lib_files = [
11
- 'renderable',
12
- 'exceptions',
13
- 'color',
14
- 'window',
15
- 'dsl',
16
- 'quad',
17
- 'line',
18
- 'circle',
19
- 'rectangle',
20
- 'square',
21
- 'triangle',
22
- 'image',
23
- 'sprite',
24
- 'font',
25
- 'text',
26
- 'sound',
27
- 'music'
28
- ]
29
5
 
30
6
  # Debugging command-line flag
31
7
  @debug = false
32
8
 
33
-
34
- # Check if source file provided is good
35
- def check_build_src_file(rb_file)
36
- if !rb_file
37
- puts "Please provide a Ruby file to build"
38
- exit
39
- elsif !File.exists? rb_file
40
- puts "Can't find file: #{rb_file}"
41
- exit
42
- end
43
- end
44
-
45
-
46
- # Assemble the Ruby 2D library in one `.rb` file
47
- def make_lib
48
- FileUtils.mkdir_p 'build'
49
-
50
- lib_dir = "#{@gem_dir}/lib/ruby2d/"
51
-
52
- lib = ""
53
- @lib_files.each do |f|
54
- lib << File.read("#{lib_dir + f}.rb") + "\n\n"
55
- end
56
-
57
- lib << "
58
- include Ruby2D
59
- extend Ruby2D::DSL\n"
60
-
61
- File.write('build/lib.rb', lib)
62
- end
63
-
64
-
65
- # Remove `require 'ruby2d'` from source file
66
- def strip_require(file)
67
- output = ''
68
- File.foreach(file) do |line|
69
- output << line unless line =~ /require ('|")ruby2d('|")/
70
- end
71
- return output
72
- end
73
-
74
-
75
- # Build a native version of the provided Ruby application
76
- def build_native(rb_file)
77
- check_build_src_file(rb_file)
78
-
79
- # Check if MRuby exists; if not, quit
80
- if `which mruby`.empty?
81
- puts "#{'Error:'.error} Can't find MRuby, which is needed to build native Ruby 2D applications.\n"
82
- exit
83
- end
84
-
85
- # Add debugging information to produce backtrace
86
- if @debug then debug_flag = '-g' end
87
-
88
- # Assemble the Ruby 2D library in one `.rb` file and compile to bytecode
89
- make_lib
90
- `mrbc #{debug_flag} -Bruby2d_lib -obuild/lib.c build/lib.rb`
91
-
92
- # Read the provided Ruby source file, copy to build dir and compile to bytecode
93
- File.open('build/src.rb', 'w') { |file| file << strip_require(rb_file) }
94
- `mrbc #{debug_flag} -Bruby2d_app -obuild/src.c build/src.rb`
95
-
96
- # Combine contents of C source files and bytecode into one file
97
- open('build/app.c', 'w') do |f|
98
- f << "#define MRUBY 1" << "\n\n"
99
- f << File.read("build/lib.c") << "\n\n"
100
- f << File.read("build/src.c") << "\n\n"
101
- f << File.read("#{@gem_dir}/ext/ruby2d/ruby2d.c")
102
- end
103
-
104
- # Compile to a native executable
105
- `cc build/app.c -lmruby \`simple2d --libs\` -o build/app`
106
-
107
- # Clean up
108
- clean_up unless @debug
109
-
110
- # Success!
111
- puts "Native app created at `build/app`"
112
- end
113
-
114
-
115
- # Build a web-based version of the provided Ruby application
116
- def build_web(rb_file)
117
- puts "Warning: ".warn + "This feature is currently disabled while it's being upgraded."
118
- return
119
-
120
- check_build_src_file(rb_file)
121
-
122
- # Assemble the Ruby 2D library in one `.rb` file and compile to JS
123
- make_lib
124
- `opal --compile --no-opal build/lib.rb > build/lib.js`
125
-
126
- # Read the provided Ruby source file, copy to build dir, and compile to JS
127
- File.open('build/src.rb', 'w') { |file| file << strip_require(rb_file) }
128
- `opal --compile --no-opal build/src.rb > build/src.js`
129
- FileUtils.cp "#{@gem_dir}/ext/ruby2d/ruby2d-opal.rb", "build/"
130
- `opal --compile --no-opal build/ruby2d-opal.rb > build/ruby2d-opal.js`
131
-
132
- # Combine contents of JS source files and compiled JS into one file
133
- open('build/app.js', 'w') do |f|
134
- f << File.read("#{@gem_dir}/assets/simple2d.js") << "\n\n"
135
- f << File.read("#{@gem_dir}/assets/opal.js") << "\n\n"
136
- f << File.read("build/lib.js") << "\n\n"
137
- f << File.read("build/ruby2d-opal.js") << "\n\n"
138
- f << File.read("build/src.js") << "\n\n"
139
- end
140
-
141
- # Copy over HTML template
142
- FileUtils.cp "#{@gem_dir}/assets/template.html", "build/app.html"
143
-
144
- # Clean up
145
- clean_up unless @debug
146
-
147
- # Success!
148
- puts "Web app created at `build/app.js`",
149
- " Run by opening `build/app.html`"
150
- end
151
-
152
-
153
- # Build an iOS or tvOS app
154
- def build_apple(rb_file, device)
155
- check_build_src_file(rb_file)
156
-
157
- # Check for Simple 2D framework,
158
- unless File.exists?('/usr/local/Frameworks/Simple2D/iOS/Simple2D.framework') && File.exists?('/usr/local/Frameworks/Simple2D/tvOS/Simple2D.framework')
159
- puts "#{'Error:'.error} Simple 2D iOS and tvOS frameworks not found. Install them and try again.\n"
160
- exit
161
- end
162
-
163
- # Check if MRuby exists; if not, quit
164
- if `which mruby`.empty?
165
- puts "#{'Error:'.error} Can't find MRuby, which is needed to build native Ruby 2D applications.\n"
166
- exit
167
- end
168
-
169
- # Add debugging information to produce backtrace
170
- if @debug then debug_flag = '-g' end
171
-
172
- # Assemble the Ruby 2D library in one `.rb` file and compile to bytecode
173
- make_lib
174
- `mrbc #{debug_flag} -Bruby2d_lib -obuild/lib.c build/lib.rb`
175
-
176
- # Read the provided Ruby source file, copy to build dir and compile to bytecode
177
- File.open('build/src.rb', 'w') { |file| file << strip_require(rb_file) }
178
- `mrbc #{debug_flag} -Bruby2d_app -obuild/src.c build/src.rb`
179
-
180
- # Copy over iOS project
181
- FileUtils.cp_r "#{@gem_dir}/assets/#{device}", "build"
182
-
183
- # Combine contents of C source files and bytecode into one file
184
- File.open("build/#{device}/main.c", 'w') do |f|
185
- f << "#define RUBY2D_IOS_TVOS 1" << "\n\n"
186
- f << "#define MRUBY 1" << "\n\n"
187
- f << File.read("build/lib.c") << "\n\n"
188
- f << File.read("build/src.c") << "\n\n"
189
- f << File.read("#{@gem_dir}/ext/ruby2d/ruby2d.c")
190
- end
191
-
192
- # Build the Xcode project
193
- `simple2d build --#{device} build/#{device}/MyApp.xcodeproj`
194
-
195
- # Clean up
196
- clean_up unless @debug
197
-
198
- # Success!
199
- puts "App created at `build/#{device}`"
200
- end
201
-
202
-
203
- # Clean up unneeded build files
204
- def clean_up(cmd = nil)
205
- FileUtils.rm(
206
- Dir.glob('build/{src,lib}.{rb,c,js}') +
207
- Dir.glob('build/ruby2d-opal.{rb,js}') +
208
- Dir.glob('build/app.c')
209
- )
210
- if cmd == :all
211
- puts "cleaning up..."
212
- FileUtils.rm_f 'build/app'
213
- FileUtils.rm_f 'build/app.js'
214
- FileUtils.rm_f 'build/app.html'
215
- FileUtils.rm_rf 'build/ios'
216
- FileUtils.rm_rf 'build/tvos'
217
- end
218
- end
219
-
220
-
221
- # Launch a native app
222
- def launch_native
223
- if !File.exists? 'build/app'
224
- puts "No native app built!"
225
- exit
226
- end
227
- `( cd build && ./app )`
228
- end
229
-
230
-
231
- # Launch a web app
232
- def launch_web
233
- if !File.exists? 'build/app.html'
234
- puts "No web app built!"
235
- exit
236
- end
237
- open_cmd = 'open'
238
- case RUBY_PLATFORM
239
- when /linux/
240
- open_cmd = "xdg-#{open_cmd}"
241
- when /mingw/
242
- open_cmd = "start"
243
- end
244
- system "#{open_cmd} build/app.html"
245
- end
246
-
247
-
248
- # Launch an iOS or tvOS app in a simulator
249
- def launch_apple(device)
250
- case device
251
- when 'ios'
252
- if !File.exists? 'build/ios/build/Release-iphonesimulator/MyApp.app'
253
- puts "No iOS app built!"
254
- exit
255
- end
256
- puts `simple2d simulator --open "iPhone XR" &&
257
- simple2d simulator --install "build/ios/build/Release-iphonesimulator/MyApp.app" &&
258
- simple2d simulator --launch "Ruby2D.MyApp"`
259
- when 'tvos'
260
- if !File.exists? 'build/tvos/build/Release-appletvsimulator/MyApp.app'
261
- puts "No tvOS app built!"
262
- exit
263
- end
264
- puts `simple2d simulator --open "Apple TV 4K" &&
265
- simple2d simulator --install "build/tvos/build/Release-appletvsimulator/MyApp.app" &&
266
- simple2d simulator --launch "Ruby2D.MyApp"`
267
- end
268
- end
269
-
270
-
271
- # Check Command-line Arguments #################################################
9
+ # Usage ########################################################################
272
10
 
273
11
  usage = "Ruby 2D: Make cross-platform 2D applications in Ruby".bold + "\n
274
12
  Usage: ruby2d <command> <options>
275
13
  [-v|--version]
276
14
 
277
15
  Summary of commands and options:
16
+ console Run script in an interactive console
278
17
  build Build a Ruby source file
279
18
  launch Launch a built Ruby 2D application
280
19
  simulator Interact with iOS and tvOS simulators
281
20
  -v|--version Prints the installed version\n\n"
282
21
 
283
22
  usage_build = "
284
- Use the #{"build".bold} command to compile or build Ruby 2D apps.
23
+ Use the #{'build'.bold} command to compile or build Ruby 2D apps.
285
24
 
286
25
  To compile and create a native executable, use:
287
26
  build --native <ruby_file>
288
27
 
289
28
  To build for the web, creating a JavaScript and HTML package, use:
290
29
  build --web <ruby_file>
291
- #{"Warning:".warn} #{"This feature is currently disabled while it's being upgraded.".bold}
30
+ #{'Warning:'.warn} #{"This feature is currently disabled while it's being upgraded.".bold}
31
+
32
+ To build a macOS app bundle, use:
33
+ build --macos <ruby_file>
292
34
 
293
- To build an iOS or tvOS app, use the following:
35
+ To build an iOS or tvOS app, use:
294
36
  build --ios <ruby_file>
295
37
  build --tvos <ruby_file>
296
38
 
@@ -300,11 +42,11 @@ To build for every platform supported by the current system, use:
300
42
  To clean up the build directory, use:
301
43
  build --clean
302
44
 
303
- Add the #{"--debug".bold} option to print debugging info
45
+ Add the #{'--debug'.bold} option to print debugging info
304
46
  and keep all intermediate files generated.\n\n"
305
47
 
306
48
  usage_launch = "
307
- Use the #{"launch".bold} command to run a Ruby 2D app that has been built.
49
+ Use the #{'launch'.bold} command to run a Ruby 2D app that has been built.
308
50
  Choose one of the following options to select the kind of app to run:
309
51
 
310
52
  --native
@@ -313,7 +55,7 @@ Choose one of the following options to select the kind of app to run:
313
55
  --tvos\n\n"
314
56
 
315
57
  usage_simulator = "
316
- Choose an option with the #{"simulator".bold} command:
58
+ Choose an option with the #{'simulator'.bold} command:
317
59
 
318
60
  --list List available devices
319
61
  --booted Show currently booted devices
@@ -330,29 +72,37 @@ Choose an option with the #{"simulator".bold} command:
330
72
  --log <app> Stream log for the app only, given the app name
331
73
  --log-errors Stream log containing only error messages\n\n"
332
74
 
75
+ # Check Command-line Arguments #################################################
76
+
333
77
  case ARGV[0]
78
+ when 'console'
79
+ require 'ruby2d/cli/console'
334
80
  when 'build'
81
+ require 'ruby2d/cli/build'
335
82
  if ARGV.delete '--debug' then @debug = true end
336
83
  case ARGV[1]
337
84
  when '--native'
338
85
  build_native(ARGV[2])
339
86
  when '--web'
340
87
  build_web(ARGV[2])
88
+ when '--macos'
89
+ build_macos(ARGV[2])
341
90
  when '--ios'
342
- build_apple(ARGV[2], 'ios')
91
+ build_ios_tvos(ARGV[2], 'ios')
343
92
  when '--tvos'
344
- build_apple(ARGV[2], 'tvos')
93
+ build_ios_tvos(ARGV[2], 'tvos')
345
94
  when '--all'
346
95
  build_native(ARGV[2])
347
96
  build_web(ARGV[2])
348
- build_apple(ARGV[2], 'ios')
349
- build_apple(ARGV[2], 'tvos')
97
+ build_ios_tvos(ARGV[2], 'ios')
98
+ build_ios_tvos(ARGV[2], 'tvos')
350
99
  when '--clean'
351
100
  clean_up(:all)
352
101
  else
353
102
  puts usage_build
354
103
  end
355
104
  when 'launch'
105
+ require 'ruby2d/cli/launch'
356
106
  case ARGV[1]
357
107
  when '--native'
358
108
  launch_native
@@ -1,7 +1,7 @@
1
1
  require 'mkmf'
2
2
  require_relative '../../lib/ruby2d/colorize'
3
3
 
4
- S2D_VERSION = '1.0.1' # Simple 2D minimum version required
4
+ S2D_VERSION = '1.1.0' # Simple 2D minimum version required
5
5
  $errors = []
6
6
 
7
7
  def print_errors
@@ -154,6 +154,33 @@ double normalize_controller_axis(int val) {
154
154
  }
155
155
 
156
156
 
157
+ /*
158
+ * Ruby2D#self.ext_base_path
159
+ */
160
+ #if MRUBY
161
+ static R_VAL ruby2d_ext_base_path(mrb_state* mrb, R_VAL self) {
162
+ #else
163
+ static R_VAL ruby2d_ext_base_path(R_VAL self) {
164
+ #endif
165
+ return r_str_new(SDL_GetBasePath());
166
+ }
167
+
168
+
169
+ /*
170
+ * Ruby2D#self.ext_screenshot
171
+ */
172
+ #if MRUBY
173
+ static R_VAL ruby2d_ext_screenshot(mrb_state* mrb, R_VAL self) {
174
+ mrb_value path;
175
+ mrb_get_args(mrb, "o", &path);
176
+ #else
177
+ static R_VAL ruby2d_ext_screenshot(R_VAL self, R_VAL path) {
178
+ #endif
179
+ S2D_Screenshot(window, RSTRING_PTR(path));
180
+ return R_NIL;
181
+ }
182
+
183
+
157
184
  /*
158
185
  * Ruby2D::Triangle#ext_render
159
186
  */
@@ -429,6 +456,12 @@ static R_VAL ruby2d_sprite_ext_render(R_VAL self) {
429
456
 
430
457
  S2D_RotateSprite(spr, NUM2DBL(r_iv_get(self, "@rotate")), S2D_CENTER);
431
458
 
459
+ R_VAL c = r_iv_get(self, "@color");
460
+ spr->color.r = NUM2DBL(r_iv_get(c, "@r"));
461
+ spr->color.g = NUM2DBL(r_iv_get(c, "@g"));
462
+ spr->color.b = NUM2DBL(r_iv_get(c, "@b"));
463
+ spr->color.a = NUM2DBL(r_iv_get(c, "@a"));
464
+
432
465
  S2D_ClipSprite(
433
466
  spr,
434
467
  NUM2INT(r_iv_get(self, "@clip_x")),
@@ -672,17 +705,29 @@ static R_VAL ruby2d_music_ext_stop(R_VAL self) {
672
705
 
673
706
 
674
707
  /*
675
- * Ruby2D::Music#ext_volume
708
+ * Ruby2D::Music#ext_get_volume
709
+ */
710
+ #if MRUBY
711
+ static R_VAL ruby2d_music_ext_get_volume(mrb_state* mrb, R_VAL self) {
712
+ #else
713
+ static R_VAL ruby2d_music_ext_get_volume(R_VAL self) {
714
+ #endif
715
+ return INT2NUM(S2D_GetMusicVolume());
716
+ }
717
+
718
+
719
+ /*
720
+ * Ruby2D::Music#ext_set_volume
676
721
  */
677
722
  #if MRUBY
678
- static R_VAL ruby2d_music_ext_volume(mrb_state* mrb, R_VAL self) {
679
- mrb_value vol;
680
- mrb_get_args(mrb, "o", &vol);
723
+ static R_VAL ruby2d_music_ext_set_volume(mrb_state* mrb, R_VAL self) {
724
+ mrb_value volume;
725
+ mrb_get_args(mrb, "o", &volume);
681
726
  #else
682
- static R_VAL ruby2d_music_ext_volume(R_VAL self, R_VAL vol) {
727
+ static R_VAL ruby2d_music_ext_set_volume(R_VAL self, R_VAL volume) {
683
728
  #endif
684
- int mix_vol = NUM2INT(vol) == -1 ? -1 : MIX_MAX_VOLUME * NUM2INT(vol) / 100.0;
685
- return INT2NUM(ceil(Mix_VolumeMusic(mix_vol) * 100.0 / MIX_MAX_VOLUME));
729
+ S2D_SetMusicVolume(NUM2INT(volume));
730
+ return R_NIL;
686
731
  }
687
732
 
688
733
 
@@ -1069,6 +1114,9 @@ void Init_ruby2d() {
1069
1114
  // Ruby2D
1070
1115
  R_CLASS ruby2d_module = r_define_module("Ruby2D");
1071
1116
 
1117
+ // Ruby2D#self.ext_base_path
1118
+ r_define_class_method(ruby2d_module, "ext_base_path", ruby2d_ext_base_path, r_args_none);
1119
+
1072
1120
  // Ruby2D::Triangle
1073
1121
  R_CLASS ruby2d_triangle_class = r_define_class(ruby2d_module, "Triangle");
1074
1122
 
@@ -1150,8 +1198,11 @@ void Init_ruby2d() {
1150
1198
  // Ruby2D::Music#ext_stop
1151
1199
  r_define_method(ruby2d_music_class, "ext_stop", ruby2d_music_ext_stop, r_args_none);
1152
1200
 
1153
- // Ruby2D::Music#self.ext_volume
1154
- r_define_class_method(ruby2d_music_class, "ext_volume", ruby2d_music_ext_volume, r_args_req(1));
1201
+ // Ruby2D::Music#self.ext_get_volume
1202
+ r_define_class_method(ruby2d_music_class, "ext_get_volume", ruby2d_music_ext_get_volume, r_args_none);
1203
+
1204
+ // Ruby2D::Music#self.ext_set_volume
1205
+ r_define_class_method(ruby2d_music_class, "ext_set_volume", ruby2d_music_ext_set_volume, r_args_req(1));
1155
1206
 
1156
1207
  // Ruby2D::Music#ext_fadeout
1157
1208
  r_define_method(ruby2d_music_class, "ext_fadeout", ruby2d_music_ext_fadeout, r_args_req(1));
@@ -1168,6 +1219,9 @@ void Init_ruby2d() {
1168
1219
  // Ruby2D::Window#ext_show
1169
1220
  r_define_method(ruby2d_window_class, "ext_show", ruby2d_window_ext_show, r_args_none);
1170
1221
 
1222
+ // Ruby2D::Window#ext_screenshot
1223
+ r_define_method(ruby2d_window_class, "ext_screenshot", ruby2d_ext_screenshot, r_args_req(1));
1224
+
1171
1225
  // Ruby2D::Window#ext_close
1172
1226
  r_define_method(ruby2d_window_class, "ext_close", ruby2d_window_ext_close, r_args_none);
1173
1227