ruby2d 0.8.0 → 0.8.1

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: daade2a60f6c1e24974a8ca77aafb4cd83e1c59cdf4aff49b8178ee7c880f0f1
4
- data.tar.gz: d07549d541ebe08e984b48373b96b890848dcf98373cc565b146c9d97fdcdb5a
3
+ metadata.gz: e0d000122e993678b9da511dd2dd84a53c2e2762cb9aec666c2410b89c0848b3
4
+ data.tar.gz: 7cdbf45866165115566642aae3af535b222cfe05a3aeec82f7cc565f4b8f1179
5
5
  SHA512:
6
- metadata.gz: 379a8eee502d3599654094b625c8e3ca8039982f3d9a1857bf5783e3627b10b1bc9ccc4d31efeb1a13c00234cd35762f0d658890797ed8fc5242ba7f52395e99
7
- data.tar.gz: bb7683a054c5e6e9848f2f791dc3e3f5bbb33266a2f798a4e06b3f1df299919cd248c1d4f19e5c31d0fabe4518fadf9cc7f5e8d7e975f49c79fff53e001e9390
6
+ metadata.gz: 62aae16a50902e30da1647ea49b545aaa164cae20ac85d6b74eb9f18071eafa712b1b5460550413371cc9485cb1888fec2f9b4757c86b58f2ad15ce5d092775b
7
+ data.tar.gz: 98741f6e78b577d64fb8b63ab98a938d3388dae0d3d9389cd0793dc48611fc95c753f0d3b37ae35ce671f326fb246df9866c2e7e8788e9be46dd758c8bbaa0f7
@@ -166,21 +166,6 @@ static R_VAL ruby2d_ext_base_path(R_VAL self) {
166
166
  }
167
167
 
168
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
-
184
169
  /*
185
170
  * Ruby2D::Triangle#ext_render
186
171
  */
@@ -348,8 +333,8 @@ static R_VAL ruby2d_image_ext_init(mrb_state* mrb, R_VAL self) {
348
333
  #else
349
334
  static R_VAL ruby2d_image_ext_init(R_VAL self, R_VAL path) {
350
335
  #endif
351
- S2D_Log(S2D_INFO, "Init image: %s", RSTRING_PTR(path));
352
336
  S2D_Image *img = S2D_CreateImage(RSTRING_PTR(path));
337
+ if (!img) return R_FALSE;
353
338
 
354
339
  // Get width and height from Ruby class. If set, use it, else choose the
355
340
  // native dimensions of the image.
@@ -357,9 +342,9 @@ static R_VAL ruby2d_image_ext_init(R_VAL self, R_VAL path) {
357
342
  R_VAL h = r_iv_get(self, "@height");
358
343
  r_iv_set(self, "@width" , r_test(w) ? w : INT2NUM(img->width));
359
344
  r_iv_set(self, "@height", r_test(h) ? h : INT2NUM(img->height));
360
-
361
345
  r_iv_set(self, "@data", r_data_wrap_struct(image, img));
362
- return R_NIL;
346
+
347
+ return R_TRUE;
363
348
  }
364
349
 
365
350
 
@@ -405,7 +390,6 @@ static void free_image(mrb_state *mrb, void *p_) {
405
390
  #else
406
391
  static void free_image(S2D_Image *img) {
407
392
  #endif
408
- S2D_Log(S2D_INFO, "Free image `%s` at %i, %i", img->path, img->x, img->y);
409
393
  S2D_FreeImage(img);
410
394
  }
411
395
 
@@ -421,14 +405,14 @@ static R_VAL ruby2d_sprite_ext_init(mrb_state* mrb, R_VAL self) {
421
405
  #else
422
406
  static R_VAL ruby2d_sprite_ext_init(R_VAL self, R_VAL path) {
423
407
  #endif
424
- S2D_Log(S2D_INFO, "Init sprite: %s", RSTRING_PTR(path));
425
408
  S2D_Sprite *spr = S2D_CreateSprite(RSTRING_PTR(path));
409
+ if (!spr) return R_FALSE;
426
410
 
427
411
  r_iv_set(self, "@img_width" , INT2NUM(spr->width));
428
412
  r_iv_set(self, "@img_height", INT2NUM(spr->height));
429
413
  r_iv_set(self, "@data", r_data_wrap_struct(sprite, spr));
430
414
 
431
- return R_NIL;
415
+ return R_TRUE;
432
416
  }
433
417
 
434
418
 
@@ -485,7 +469,6 @@ static void free_sprite(mrb_state *mrb, void *p_) {
485
469
  #else
486
470
  static void free_sprite(S2D_Sprite *spr) {
487
471
  #endif
488
- S2D_Log(S2D_INFO, "Free sprite `%s` at %i, %i", spr->path, spr->x, spr->y);
489
472
  S2D_FreeSprite(spr);
490
473
  }
491
474
 
@@ -499,8 +482,6 @@ static R_VAL ruby2d_text_ext_init(mrb_state* mrb, R_VAL self) {
499
482
  #else
500
483
  static R_VAL ruby2d_text_ext_init(R_VAL self) {
501
484
  #endif
502
- S2D_Log(S2D_INFO, "Init text: %s", RSTRING_PTR(r_iv_get(self, "@text")));
503
-
504
485
  // Trim the font file string to its actual length on MRuby
505
486
  #if MRUBY
506
487
  mrb_value s = r_iv_get(self, "@font");
@@ -512,12 +493,13 @@ static R_VAL ruby2d_text_ext_init(R_VAL self) {
512
493
  RSTRING_PTR(r_iv_get(self, "@text")),
513
494
  NUM2DBL(r_iv_get(self, "@size"))
514
495
  );
496
+ if (!txt) return R_FALSE;
515
497
 
516
498
  r_iv_set(self, "@width", INT2NUM(txt->width));
517
499
  r_iv_set(self, "@height", INT2NUM(txt->height));
518
-
519
500
  r_iv_set(self, "@data", r_data_wrap_struct(text, txt));
520
- return R_NIL;
501
+
502
+ return R_TRUE;
521
503
  }
522
504
 
523
505
 
@@ -580,7 +562,6 @@ static void free_text(mrb_state *mrb, void *p_) {
580
562
  #else
581
563
  static void free_text(S2D_Text *txt) {
582
564
  #endif
583
- S2D_Log(S2D_INFO, "Free text \"%s\" with font `%s`", txt->msg, txt->font);
584
565
  S2D_FreeText(txt);
585
566
  }
586
567
 
@@ -596,10 +577,10 @@ static R_VAL ruby2d_sound_ext_init(mrb_state* mrb, R_VAL self) {
596
577
  #else
597
578
  static R_VAL ruby2d_sound_ext_init(R_VAL self, R_VAL path) {
598
579
  #endif
599
- S2D_Log(S2D_INFO, "Init sound: %s", RSTRING_PTR(path));
600
580
  S2D_Sound *snd = S2D_CreateSound(RSTRING_PTR(path));
581
+ if (!snd) return R_FALSE;
601
582
  r_iv_set(self, "@data", r_data_wrap_struct(sound, snd));
602
- return R_NIL;
583
+ return R_TRUE;
603
584
  }
604
585
 
605
586
 
@@ -627,7 +608,6 @@ static void free_sound(mrb_state *mrb, void *p_) {
627
608
  #else
628
609
  static void free_sound(S2D_Sound *snd) {
629
610
  #endif
630
- S2D_Log(S2D_INFO, "Free sound `%s`", snd->path);
631
611
  S2D_FreeSound(snd);
632
612
  }
633
613
 
@@ -643,10 +623,10 @@ static R_VAL ruby2d_music_ext_init(mrb_state* mrb, R_VAL self) {
643
623
  #else
644
624
  static R_VAL ruby2d_music_ext_init(R_VAL self, R_VAL path) {
645
625
  #endif
646
- S2D_Log(S2D_INFO, "Init music: %s", RSTRING_PTR(path));
647
626
  S2D_Music *mus = S2D_CreateMusic(RSTRING_PTR(path));
627
+ if (!mus) return R_FALSE;
648
628
  r_iv_set(self, "@data", r_data_wrap_struct(music, mus));
649
- return R_NIL;
629
+ return R_TRUE;
650
630
  }
651
631
 
652
632
 
@@ -755,7 +735,6 @@ static void free_music(mrb_state *mrb, void *p_) {
755
735
  #else
756
736
  static void free_music(S2D_Music *mus) {
757
737
  #endif
758
- S2D_Log(S2D_INFO, "Free music `%s`", mus->path);
759
738
  S2D_FreeMusic(mus);
760
739
  }
761
740
 
@@ -978,6 +957,22 @@ static void render() {
978
957
  }
979
958
 
980
959
 
960
+ /*
961
+ * Ruby2D::Window#ext_diagnostics
962
+ */
963
+ #if MRUBY
964
+ static R_VAL ruby2d_ext_diagnostics(mrb_state* mrb, R_VAL self) {
965
+ mrb_value enable;
966
+ mrb_get_args(mrb, "o", &enable);
967
+ #else
968
+ static R_VAL ruby2d_ext_diagnostics(R_VAL self, R_VAL enable) {
969
+ #endif
970
+ // Set Simple 2D diagnostics
971
+ S2D_Diagnostics(r_test(enable));
972
+ return R_TRUE;
973
+ }
974
+
975
+
981
976
  /*
982
977
  * Ruby2D::Window#ext_get_display_dimensions
983
978
  */
@@ -1020,11 +1015,6 @@ static R_VAL ruby2d_window_ext_show(R_VAL self) {
1020
1015
  #endif
1021
1016
  ruby2d_window = self;
1022
1017
 
1023
- // Set Simple 2D diagnostics
1024
- if (r_test(r_iv_get(self, "@diagnostics"))) {
1025
- S2D_Diagnostics(true);
1026
- }
1027
-
1028
1018
  // Add controller mappings from file
1029
1019
  r_funcall(self, "add_controller_mappings", 0);
1030
1020
 
@@ -1083,6 +1073,26 @@ static R_VAL ruby2d_window_ext_show(R_VAL self) {
1083
1073
  }
1084
1074
 
1085
1075
 
1076
+ /*
1077
+ * Ruby2D::Window#ext_screenshot
1078
+ */
1079
+ #if MRUBY
1080
+ static R_VAL ruby2d_ext_screenshot(mrb_state* mrb, R_VAL self) {
1081
+ mrb_value path;
1082
+ mrb_get_args(mrb, "o", &path);
1083
+ #else
1084
+ static R_VAL ruby2d_ext_screenshot(R_VAL self, R_VAL path) {
1085
+ #endif
1086
+ if (window) {
1087
+ S2D_Screenshot(window, RSTRING_PTR(path));
1088
+ return path;
1089
+ } else {
1090
+ S2D_Log(S2D_WARN, "Cannot take screenshot before window is shown");
1091
+ return R_FALSE;
1092
+ }
1093
+ }
1094
+
1095
+
1086
1096
  /*
1087
1097
  * Ruby2D::Window#ext_close
1088
1098
  */
@@ -1210,6 +1220,9 @@ void Init_ruby2d() {
1210
1220
  // Ruby2D::Window
1211
1221
  R_CLASS ruby2d_window_class = r_define_class(ruby2d_module, "Window");
1212
1222
 
1223
+ // Ruby2D::Window#ext_diagnostics
1224
+ r_define_method(ruby2d_window_class, "ext_diagnostics", ruby2d_ext_diagnostics, r_args_req(1));
1225
+
1213
1226
  // Ruby2D::Window#ext_get_display_dimensions
1214
1227
  r_define_method(ruby2d_window_class, "ext_get_display_dimensions", ruby2d_window_ext_get_display_dimensions, r_args_none);
1215
1228
 
@@ -4,7 +4,7 @@ module Ruby2D
4
4
  class Circle
5
5
  include Renderable
6
6
 
7
- attr_accessor :radius, :sectors
7
+ attr_accessor :x, :y, :radius, :sectors
8
8
 
9
9
  def initialize(opts = {})
10
10
  @x = opts[:x] || 25
@@ -1,3 +1,6 @@
1
+ # Interactive Ruby 2D console
2
+
3
+ # Save the Ruby file from the command-line arguments
1
4
  rb_file = ARGV[1]
2
5
 
3
6
  # Check if source file provided is good
@@ -14,11 +17,12 @@ require 'open3'
14
17
  require 'readline'
15
18
  require 'io/wait'
16
19
 
17
- line = 1 # the current line number, to be incremented
20
+ line = 1 # the current line number
18
21
 
19
- # Open a new process for the Ruby file
22
+ # Open a new process for the Ruby file
20
23
  stdin, stdout, stderr, wait_thr = Open3.popen3("ruby -r 'ruby2d/cli/enable_console' #{rb_file}")
21
24
 
25
+ # Request input and send commands
22
26
  loop do
23
27
 
24
28
  # Read the next command
@@ -31,19 +35,31 @@ loop do
31
35
  exit
32
36
  end
33
37
 
34
- # Skip if command is an empty string
35
- unless cmd.empty?
38
+ # Try sending commands
39
+ begin
40
+
41
+ # Skip if command is an empty string
42
+ unless cmd.empty?
36
43
 
37
- # Send command to the Ruby file
38
- stdin.puts cmd
44
+ # Send command to the Ruby file
45
+ stdin.puts cmd
39
46
 
40
- # Read and print output from the Ruby file
41
- puts stdout.gets
42
- while stdout.ready? do
47
+ # Read and print output from the Ruby file
43
48
  puts stdout.gets
49
+ while stdout.ready? do
50
+ puts stdout.gets
51
+ end
52
+
44
53
  end
54
+
55
+ # Increment the line number
56
+ line += 1
57
+
58
+ # Rescue exception if can't send commands to the Ruby 2D window
59
+ rescue Errno::EPIPE
60
+ puts "Can't connect to the window (was it closed?)",
61
+ "For help, see: ruby2d.com/learn/console"
62
+ exit 1
45
63
  end
46
64
 
47
- # Advance to next line
48
- line += 1
49
65
  end
@@ -20,7 +20,9 @@ module Ruby2D
20
20
  @rotate = opts[:rotate] || 0
21
21
  self.color = opts[:color] || 'white'
22
22
  self.opacity = opts[:opacity] if opts[:opacity]
23
- ext_init(@path)
23
+ unless ext_init(@path)
24
+ raise Error, "Image `#{@path}` cannot be created"
25
+ end
24
26
  add
25
27
  end
26
28
 
@@ -4,15 +4,15 @@ 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, :width
8
8
 
9
9
  def initialize(opts = {})
10
10
  @x1 = opts[:x1] || 0
11
11
  @y1 = opts[:y1] || 0
12
12
  @x2 = opts[:x2] || 100
13
13
  @y2 = opts[:y2] || 100
14
- @width = opts[:width] || 2
15
14
  @z = opts[:z] || 0
15
+ @width = opts[:width] || 2
16
16
  self.color = opts[:color] || 'white'
17
17
  self.opacity = opts[:opacity] if opts[:opacity]
18
18
  add
@@ -53,7 +53,7 @@ module Ruby2D
53
53
  @c3 = c[2]
54
54
  @c4 = c[3]
55
55
  else
56
- raise ArgumentError, "Lines require 4 colors, one for each vertex. #{c.length} were given."
56
+ raise ArgumentError, "`#{self.class}` requires 4 colors, one for each vertex. #{c.length} were given."
57
57
  end
58
58
  else
59
59
  @c1 = c
@@ -6,13 +6,15 @@ module Ruby2D
6
6
  attr_reader :path
7
7
  attr_accessor :loop, :data
8
8
 
9
- def initialize(path)
9
+ def initialize(path, opts = {})
10
10
  unless File.exist? path
11
11
  raise Error, "Cannot find audio file `#{path}`"
12
12
  end
13
13
  @path = path
14
- @loop = false
15
- ext_init(path)
14
+ @loop = opts[:loop] || false
15
+ unless ext_init(@path)
16
+ raise Error, "Music `#{@path}` cannot be created"
17
+ end
16
18
  end
17
19
 
18
20
  # Play the music
@@ -62,7 +62,7 @@ module Ruby2D
62
62
  @c3 = c[2]
63
63
  @c4 = c[3]
64
64
  else
65
- raise ArgumentError, "Quads require 4 colors, one for each vertex. #{c.length} were given."
65
+ raise ArgumentError, "`#{self.class}` requires 4 colors, one for each vertex. #{c.length} were given."
66
66
  end
67
67
  else
68
68
  @c1 = c
@@ -49,7 +49,7 @@ module Ruby2D
49
49
 
50
50
  # Add a contains method stub
51
51
  def contains?(x, y)
52
- x > @x && x < (@x + @width) && y > @y && y < (@y + @height)
52
+ x >= @x && x <= (@x + @width) && y >= @y && y <= (@y + @height)
53
53
  end
54
54
 
55
55
  end
@@ -11,7 +11,9 @@ module Ruby2D
11
11
  raise Error, "Cannot find audio file `#{path}`"
12
12
  end
13
13
  @path = path
14
- ext_init(path)
14
+ unless ext_init(@path)
15
+ raise Error, "Sound `#{@path}` cannot be created"
16
+ end
15
17
  end
16
18
 
17
19
  # Play the sound
@@ -4,6 +4,7 @@ module Ruby2D
4
4
  class Sprite
5
5
  include Renderable
6
6
 
7
+ attr_reader :path
7
8
  attr_accessor :rotate, :loop, :clip_x, :clip_y, :clip_width, :clip_height, :data
8
9
 
9
10
  def initialize(path, opts = {})
@@ -45,7 +46,9 @@ module Ruby2D
45
46
  @img_width = nil; @img_height = nil
46
47
 
47
48
  # Initialize the sprite
48
- ext_init(@path)
49
+ unless ext_init(@path)
50
+ raise Error, "Sprite image `#{@path}` cannot be created"
51
+ end
49
52
 
50
53
  # The clipping rectangle
51
54
  @clip_x = opts[:clip_x] || 0
@@ -4,8 +4,8 @@ module Ruby2D
4
4
  class Text
5
5
  include Renderable
6
6
 
7
- attr_reader :text, :size, :font
8
- attr_accessor :x, :y, :rotate, :data
7
+ attr_reader :text, :font
8
+ attr_accessor :x, :y, :size, :rotate, :data
9
9
 
10
10
  def initialize(text, opts = {})
11
11
  @x = opts[:x] || 0
@@ -20,7 +20,9 @@ module Ruby2D
20
20
  unless File.exist? @font
21
21
  raise Error, "Cannot find font file `#{@font}`"
22
22
  end
23
- ext_init
23
+ unless ext_init
24
+ raise Error, "Text `#{@text}` cannot be created"
25
+ end
24
26
  add
25
27
  end
26
28
 
@@ -26,9 +26,8 @@ module Ruby2D
26
26
  update_color(@color)
27
27
  end
28
28
 
29
- # Point is inside a triangle if
30
- # the area of 3 triangles, constructed from triangle sides and that point
31
- # is equal to the area of triangle.
29
+ # A point is inside a triangle if the area of 3 triangles, constructed from
30
+ # triangle sides and the given point, is equal to the area of triangle.
32
31
  def contains?(x, y)
33
32
  self_area = triangle_area(@x1, @y1, @x2, @y2, @x3, @y3)
34
33
  questioned_area =
@@ -52,7 +51,7 @@ module Ruby2D
52
51
  @c2 = c[1]
53
52
  @c3 = c[2]
54
53
  else
55
- raise ArgumentError, "Triangles require 3 colors, one for each vertex. #{c.length} were given."
54
+ raise ArgumentError, "`#{self.class}` requires 3 colors, one for each vertex. #{c.length} were given."
56
55
  end
57
56
  else
58
57
  @c1 = c
@@ -1,5 +1,5 @@
1
1
  # Ruby2D::VERSION
2
2
 
3
3
  module Ruby2D
4
- VERSION = '0.8.0'
4
+ VERSION = '0.8.1'
5
5
  end
@@ -87,7 +87,11 @@ module Ruby2D
87
87
  @diagnostics = false
88
88
 
89
89
  # Console mode, enabled at command line
90
- @console = $ruby2d_console_mode || false
90
+ if RUBY_ENGINE == 'ruby'
91
+ @console = defined?($ruby2d_console_mode) ? true : false
92
+ else
93
+ @console = false
94
+ end
91
95
  end
92
96
 
93
97
  # Class methods for convenient access to properties
@@ -204,7 +208,10 @@ module Ruby2D
204
208
  @borderless = opts[:borderless] || @borderless
205
209
  @fullscreen = opts[:fullscreen] || @fullscreen
206
210
  @highdpi = opts[:highdpi] || @highdpi
207
- @diagnostics = opts[:diagnostics] || @diagnostics
211
+ unless opts[:diagnostics].nil?
212
+ @diagnostics = opts[:diagnostics]
213
+ ext_diagnostics(@diagnostics)
214
+ end
208
215
  end
209
216
 
210
217
  # Add an object to the window
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby2d
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Black
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-05 00:00:00.000000000 Z
11
+ date: 2019-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec