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/ext/ruby2d/ruby2d.c
CHANGED
@@ -59,10 +59,12 @@
|
|
59
59
|
#define r_data_wrap_struct(name, data) mrb_obj_value(Data_Wrap_Struct(mrb, mrb->object_class, &name##_data_type, data))
|
60
60
|
#define r_data_get_struct(self, var, mrb_type, rb_type, data) Data_Get_Struct(mrb, r_iv_get(self, var), mrb_type, data)
|
61
61
|
#define r_define_module(name) mrb_module_get(mrb, name)
|
62
|
-
#define r_define_class(module, name) mrb_class_get_under(mrb, module, name)
|
62
|
+
#define r_define_class(module, name) mrb_class_get_under(mrb, module, name)
|
63
63
|
#define r_define_method(class, name, function, args) mrb_define_method(mrb, class, name, function, args)
|
64
64
|
#define r_args_none (MRB_ARGS_NONE())
|
65
65
|
#define r_args_req(n) MRB_ARGS_REQ(n)
|
66
|
+
// Helpers
|
67
|
+
#define r_char_to_sym(str) mrb_check_intern_cstr(mrb, str)
|
66
68
|
#else
|
67
69
|
// Ruby
|
68
70
|
#define R_VAL VALUE
|
@@ -79,18 +81,21 @@
|
|
79
81
|
#define r_data_wrap_struct(name, data) Data_Wrap_Struct(rb_cObject, NULL, (free_##name), data)
|
80
82
|
#define r_data_get_struct(self, var, mrb_type, rb_type, data) Data_Get_Struct(r_iv_get(self, var), rb_type, data)
|
81
83
|
#define r_define_module(name) rb_define_module(name)
|
82
|
-
#define r_define_class(module, name) rb_define_class_under(module, name, rb_cObject)
|
84
|
+
#define r_define_class(module, name) rb_define_class_under(module, name, rb_cObject)
|
83
85
|
#define r_define_method(class, name, function, args) rb_define_method(class, name, function, args)
|
84
86
|
#define r_args_none 0
|
85
87
|
#define r_args_req(n) n
|
88
|
+
// Helpers
|
89
|
+
#define r_char_to_sym(str) ID2SYM(rb_intern(str))
|
86
90
|
#endif
|
87
91
|
|
88
92
|
// @type_id values for rendering
|
89
93
|
#define R2D_TRIANGLE 1
|
90
94
|
#define R2D_QUAD 2
|
91
|
-
#define
|
92
|
-
#define
|
93
|
-
#define
|
95
|
+
#define R2D_LINE 3
|
96
|
+
#define R2D_IMAGE 4
|
97
|
+
#define R2D_SPRITE 5
|
98
|
+
#define R2D_TEXT 6
|
94
99
|
|
95
100
|
// Create the MRuby context
|
96
101
|
#if MRUBY
|
@@ -143,6 +148,18 @@ static void free_window() {
|
|
143
148
|
}
|
144
149
|
|
145
150
|
|
151
|
+
/*
|
152
|
+
* File#exists? for MRuby
|
153
|
+
*/
|
154
|
+
#if MRUBY
|
155
|
+
static R_VAL file_exists(mrb_state* mrb, R_VAL self) {
|
156
|
+
mrb_value path;
|
157
|
+
mrb_get_args(mrb, "o", &path);
|
158
|
+
return S2D_FileExists(RSTRING_PTR(path)) ? R_TRUE : R_FALSE;
|
159
|
+
}
|
160
|
+
#endif
|
161
|
+
|
162
|
+
|
146
163
|
/*
|
147
164
|
* Ruby2D::Image#init
|
148
165
|
* Initialize image structure data
|
@@ -157,6 +174,8 @@ static R_VAL ruby2d_image_init(R_VAL self, R_VAL path) {
|
|
157
174
|
sprintf(S2D_msg, "Init image: %s", RSTRING_PTR(path));
|
158
175
|
S2D_Log(S2D_msg, S2D_INFO);
|
159
176
|
S2D_Image *img = S2D_CreateImage(RSTRING_PTR(path));
|
177
|
+
r_iv_set(self, "@width", INT2NUM(img->width));
|
178
|
+
r_iv_set(self, "@height", INT2NUM(img->height));
|
160
179
|
r_iv_set(self, "@data", r_data_wrap_struct(image, img));
|
161
180
|
return R_NIL;
|
162
181
|
}
|
@@ -222,37 +241,39 @@ static R_VAL ruby2d_text_init(R_VAL self) {
|
|
222
241
|
#endif
|
223
242
|
sprintf(S2D_msg, "Init text: %s", RSTRING_PTR(r_iv_get(self, "@text")));
|
224
243
|
S2D_Log(S2D_msg, S2D_INFO);
|
225
|
-
|
244
|
+
|
226
245
|
S2D_Text *txt = S2D_CreateText(
|
227
246
|
RSTRING_PTR(r_iv_get(self, "@font")),
|
228
247
|
RSTRING_PTR(r_iv_get(self, "@text")),
|
229
248
|
NUM2DBL(r_iv_get(self, "@size"))
|
230
249
|
);
|
231
|
-
|
250
|
+
|
251
|
+
r_iv_set(self, "@width", INT2NUM(txt->width));
|
252
|
+
r_iv_set(self, "@height", INT2NUM(txt->height));
|
253
|
+
|
232
254
|
r_iv_set(self, "@data", r_data_wrap_struct(text, txt));
|
233
255
|
return R_NIL;
|
234
256
|
}
|
235
257
|
|
236
258
|
|
237
259
|
/*
|
238
|
-
* Ruby2D::Text#
|
260
|
+
* Ruby2D::Text#ext_text_set
|
239
261
|
*/
|
240
262
|
#if MRUBY
|
241
|
-
static R_VAL
|
263
|
+
static R_VAL ruby2d_text_set(mrb_state* mrb, R_VAL self) {
|
242
264
|
mrb_value text;
|
243
265
|
mrb_get_args(mrb, "o", &text);
|
244
266
|
#else
|
245
|
-
static R_VAL
|
246
|
-
r_iv_set(self, "@text", text);
|
267
|
+
static R_VAL ruby2d_text_set(R_VAL self, R_VAL text) {
|
247
268
|
#endif
|
248
|
-
|
249
|
-
// If called before window is shown, return
|
250
|
-
if (!r_test(ruby2d_window)) return R_NIL;
|
251
|
-
|
252
269
|
S2D_Text *txt;
|
253
270
|
r_data_get_struct(self, "@data", &text_data_type, S2D_Text, txt);
|
254
|
-
|
271
|
+
|
255
272
|
S2D_SetText(txt, RSTRING_PTR(text));
|
273
|
+
|
274
|
+
r_iv_set(self, "@width", INT2NUM(txt->width));
|
275
|
+
r_iv_set(self, "@height", INT2NUM(txt->height));
|
276
|
+
|
256
277
|
return R_NIL;
|
257
278
|
}
|
258
279
|
|
@@ -427,39 +448,106 @@ static void free_music(S2D_Music *mus) {
|
|
427
448
|
/*
|
428
449
|
* Simple 2D `on_key` input callback function
|
429
450
|
*/
|
430
|
-
static void on_key(S2D_Event e
|
431
|
-
|
432
|
-
|
433
|
-
|
451
|
+
static void on_key(S2D_Event e) {
|
452
|
+
|
453
|
+
R_VAL type;
|
454
|
+
|
455
|
+
switch (e.type) {
|
456
|
+
case S2D_KEY_DOWN:
|
457
|
+
type = r_char_to_sym("down");
|
434
458
|
break;
|
435
|
-
|
436
|
-
|
437
|
-
r_funcall(ruby2d_window, "key_callback", 1, r_str_new(key));
|
459
|
+
case S2D_KEY_HELD:
|
460
|
+
type = r_char_to_sym("held");
|
438
461
|
break;
|
439
|
-
|
440
|
-
|
441
|
-
r_funcall(ruby2d_window, "key_up_callback", 1, r_str_new(key));
|
462
|
+
case S2D_KEY_UP:
|
463
|
+
type = r_char_to_sym("up");
|
442
464
|
break;
|
443
465
|
}
|
466
|
+
|
467
|
+
r_funcall(ruby2d_window, "key_callback", 2, type, r_str_new(e.key));
|
444
468
|
}
|
445
469
|
|
446
470
|
|
447
471
|
/*
|
448
472
|
* Simple 2D `on_mouse` input callback function
|
449
473
|
*/
|
450
|
-
void on_mouse(
|
451
|
-
|
474
|
+
void on_mouse(S2D_Event e) {
|
475
|
+
|
476
|
+
R_VAL type = R_NIL; R_VAL button = R_NIL; R_VAL direction = R_NIL;
|
477
|
+
|
478
|
+
switch (e.type) {
|
479
|
+
case S2D_MOUSE_DOWN:
|
480
|
+
// type, button, x, y
|
481
|
+
type = r_char_to_sym("down");
|
482
|
+
break;
|
483
|
+
case S2D_MOUSE_UP:
|
484
|
+
// type, button, x, y
|
485
|
+
type = r_char_to_sym("up");
|
486
|
+
break;
|
487
|
+
case S2D_MOUSE_SCROLL:
|
488
|
+
// type, direction, delta_x, delta_y
|
489
|
+
type = r_char_to_sym("scroll");
|
490
|
+
direction = e.direction == S2D_MOUSE_SCROLL_NORMAL ?
|
491
|
+
r_str_new("normal") : r_str_new("inverted");
|
492
|
+
break;
|
493
|
+
case S2D_MOUSE_MOVE:
|
494
|
+
// type, x, y, delta_x, delta_y
|
495
|
+
type = r_char_to_sym("move");
|
496
|
+
break;
|
497
|
+
}
|
498
|
+
|
499
|
+
if (e.type == S2D_MOUSE_DOWN || e.type == S2D_MOUSE_UP) {
|
500
|
+
switch (e.button) {
|
501
|
+
case S2D_MOUSE_LEFT:
|
502
|
+
button = r_str_new("left");
|
503
|
+
break;
|
504
|
+
case S2D_MOUSE_MIDDLE:
|
505
|
+
button = r_str_new("middle");
|
506
|
+
break;
|
507
|
+
case S2D_MOUSE_RIGHT:
|
508
|
+
button = r_str_new("right");
|
509
|
+
break;
|
510
|
+
case S2D_MOUSE_X1:
|
511
|
+
button = r_str_new("x1");
|
512
|
+
break;
|
513
|
+
case S2D_MOUSE_X2:
|
514
|
+
button = r_str_new("x2");
|
515
|
+
break;
|
516
|
+
}
|
517
|
+
}
|
518
|
+
|
519
|
+
// Bug in MRuby: If `button` or `direction` are symbols (created with
|
520
|
+
// r_char_to_sym), they will always both be `nil`. Use `r_str_new` for now.
|
521
|
+
|
522
|
+
r_funcall(
|
523
|
+
ruby2d_window, "mouse_callback", 7, type, button, direction,
|
524
|
+
INT2NUM(e.x), INT2NUM(e.y), INT2NUM(e.delta_x), INT2NUM(e.delta_y)
|
525
|
+
);
|
452
526
|
}
|
453
527
|
|
454
528
|
|
455
529
|
/*
|
456
530
|
* Simple 2D `on_controller` input callback function
|
457
531
|
*/
|
458
|
-
static void on_controller(
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
532
|
+
static void on_controller(S2D_Event e) {
|
533
|
+
|
534
|
+
R_VAL type = R_NIL;
|
535
|
+
|
536
|
+
switch (e.type) {
|
537
|
+
case S2D_AXIS:
|
538
|
+
type = r_char_to_sym("axis");
|
539
|
+
break;
|
540
|
+
case S2D_BUTTON_DOWN:
|
541
|
+
type = r_char_to_sym("button_down");
|
542
|
+
break;
|
543
|
+
case S2D_BUTTON_UP:
|
544
|
+
type = r_char_to_sym("button_up");
|
545
|
+
break;
|
546
|
+
}
|
547
|
+
|
548
|
+
r_funcall(
|
549
|
+
ruby2d_window, "controller_callback", 5, INT2NUM(e.which), type,
|
550
|
+
INT2NUM(e.axis), INT2NUM(e.value), INT2NUM(e.button)
|
463
551
|
);
|
464
552
|
}
|
465
553
|
|
@@ -468,17 +556,17 @@ static void on_controller(int which, bool is_axis, int axis, int val, bool is_bt
|
|
468
556
|
* Simple 2D `update` callback function
|
469
557
|
*/
|
470
558
|
static void update() {
|
471
|
-
|
559
|
+
|
472
560
|
// Set the cursor
|
473
561
|
r_iv_set(ruby2d_window, "@mouse_x", INT2NUM(window->mouse.x));
|
474
562
|
r_iv_set(ruby2d_window, "@mouse_y", INT2NUM(window->mouse.y));
|
475
|
-
|
563
|
+
|
476
564
|
// Store frames
|
477
565
|
r_iv_set(ruby2d_window, "@frames", DBL2NUM(window->frames));
|
478
|
-
|
566
|
+
|
479
567
|
// Store frame rate
|
480
568
|
r_iv_set(ruby2d_window, "@fps", DBL2NUM(window->fps));
|
481
|
-
|
569
|
+
|
482
570
|
// Call update proc, `window.update`
|
483
571
|
r_funcall(ruby2d_window, "update_callback", 0);
|
484
572
|
}
|
@@ -488,32 +576,32 @@ static void update() {
|
|
488
576
|
* Simple 2D `render` callback function
|
489
577
|
*/
|
490
578
|
static void render() {
|
491
|
-
|
579
|
+
|
492
580
|
// Set background color
|
493
581
|
R_VAL bc = r_iv_get(ruby2d_window, "@background");
|
494
582
|
window->background.r = NUM2DBL(r_iv_get(bc, "@r"));
|
495
583
|
window->background.g = NUM2DBL(r_iv_get(bc, "@g"));
|
496
584
|
window->background.b = NUM2DBL(r_iv_get(bc, "@b"));
|
497
585
|
window->background.a = NUM2DBL(r_iv_get(bc, "@a"));
|
498
|
-
|
586
|
+
|
499
587
|
// Read window objects
|
500
588
|
R_VAL objects = r_iv_get(ruby2d_window, "@objects");
|
501
589
|
int num_objects = NUM2INT(r_funcall(objects, "length", 0));
|
502
|
-
|
590
|
+
|
503
591
|
// Switch on each object type
|
504
592
|
for (int i = 0; i < num_objects; ++i) {
|
505
|
-
|
593
|
+
|
506
594
|
R_VAL el = r_ary_entry(objects, i);
|
507
595
|
int type_id = NUM2INT(r_iv_get(el, "@type_id"));
|
508
|
-
|
596
|
+
|
509
597
|
// Switch on the object's type_id
|
510
598
|
switch(type_id) {
|
511
|
-
|
599
|
+
|
512
600
|
case R2D_TRIANGLE: {
|
513
601
|
R_VAL c1 = r_iv_get(el, "@c1");
|
514
602
|
R_VAL c2 = r_iv_get(el, "@c2");
|
515
603
|
R_VAL c3 = r_iv_get(el, "@c3");
|
516
|
-
|
604
|
+
|
517
605
|
S2D_DrawTriangle(
|
518
606
|
NUM2DBL(r_iv_get(el, "@x1")),
|
519
607
|
NUM2DBL(r_iv_get(el, "@y1")),
|
@@ -521,14 +609,14 @@ static void render() {
|
|
521
609
|
NUM2DBL(r_iv_get(c1, "@g")),
|
522
610
|
NUM2DBL(r_iv_get(c1, "@b")),
|
523
611
|
NUM2DBL(r_iv_get(c1, "@a")),
|
524
|
-
|
612
|
+
|
525
613
|
NUM2DBL(r_iv_get(el, "@x2")),
|
526
614
|
NUM2DBL(r_iv_get(el, "@y2")),
|
527
615
|
NUM2DBL(r_iv_get(c2, "@r")),
|
528
616
|
NUM2DBL(r_iv_get(c2, "@g")),
|
529
617
|
NUM2DBL(r_iv_get(c2, "@b")),
|
530
618
|
NUM2DBL(r_iv_get(c2, "@a")),
|
531
|
-
|
619
|
+
|
532
620
|
NUM2DBL(r_iv_get(el, "@x3")),
|
533
621
|
NUM2DBL(r_iv_get(el, "@y3")),
|
534
622
|
NUM2DBL(r_iv_get(c3, "@r")),
|
@@ -538,13 +626,13 @@ static void render() {
|
|
538
626
|
);
|
539
627
|
}
|
540
628
|
break;
|
541
|
-
|
629
|
+
|
542
630
|
case R2D_QUAD: {
|
543
631
|
R_VAL c1 = r_iv_get(el, "@c1");
|
544
632
|
R_VAL c2 = r_iv_get(el, "@c2");
|
545
633
|
R_VAL c3 = r_iv_get(el, "@c3");
|
546
634
|
R_VAL c4 = r_iv_get(el, "@c4");
|
547
|
-
|
635
|
+
|
548
636
|
S2D_DrawQuad(
|
549
637
|
NUM2DBL(r_iv_get(el, "@x1")),
|
550
638
|
NUM2DBL(r_iv_get(el, "@y1")),
|
@@ -552,21 +640,21 @@ static void render() {
|
|
552
640
|
NUM2DBL(r_iv_get(c1, "@g")),
|
553
641
|
NUM2DBL(r_iv_get(c1, "@b")),
|
554
642
|
NUM2DBL(r_iv_get(c1, "@a")),
|
555
|
-
|
643
|
+
|
556
644
|
NUM2DBL(r_iv_get(el, "@x2")),
|
557
645
|
NUM2DBL(r_iv_get(el, "@y2")),
|
558
646
|
NUM2DBL(r_iv_get(c2, "@r")),
|
559
647
|
NUM2DBL(r_iv_get(c2, "@g")),
|
560
648
|
NUM2DBL(r_iv_get(c2, "@b")),
|
561
649
|
NUM2DBL(r_iv_get(c2, "@a")),
|
562
|
-
|
650
|
+
|
563
651
|
NUM2DBL(r_iv_get(el, "@x3")),
|
564
652
|
NUM2DBL(r_iv_get(el, "@y3")),
|
565
653
|
NUM2DBL(r_iv_get(c3, "@r")),
|
566
654
|
NUM2DBL(r_iv_get(c3, "@g")),
|
567
655
|
NUM2DBL(r_iv_get(c3, "@b")),
|
568
656
|
NUM2DBL(r_iv_get(c3, "@a")),
|
569
|
-
|
657
|
+
|
570
658
|
NUM2DBL(r_iv_get(el, "@x4")),
|
571
659
|
NUM2DBL(r_iv_get(el, "@y4")),
|
572
660
|
NUM2DBL(r_iv_get(c4, "@r")),
|
@@ -576,36 +664,72 @@ static void render() {
|
|
576
664
|
);
|
577
665
|
}
|
578
666
|
break;
|
579
|
-
|
667
|
+
|
668
|
+
case R2D_LINE: {
|
669
|
+
R_VAL c1 = r_iv_get(el, "@c1");
|
670
|
+
R_VAL c2 = r_iv_get(el, "@c2");
|
671
|
+
R_VAL c3 = r_iv_get(el, "@c3");
|
672
|
+
R_VAL c4 = r_iv_get(el, "@c4");
|
673
|
+
|
674
|
+
S2D_DrawLine(
|
675
|
+
NUM2DBL(r_iv_get(el, "@x1")),
|
676
|
+
NUM2DBL(r_iv_get(el, "@y1")),
|
677
|
+
NUM2DBL(r_iv_get(el, "@x2")),
|
678
|
+
NUM2DBL(r_iv_get(el, "@y2")),
|
679
|
+
NUM2DBL(r_iv_get(el, "@width")),
|
680
|
+
|
681
|
+
NUM2DBL(r_iv_get(c1, "@r")),
|
682
|
+
NUM2DBL(r_iv_get(c1, "@g")),
|
683
|
+
NUM2DBL(r_iv_get(c1, "@b")),
|
684
|
+
NUM2DBL(r_iv_get(c1, "@a")),
|
685
|
+
|
686
|
+
NUM2DBL(r_iv_get(c2, "@r")),
|
687
|
+
NUM2DBL(r_iv_get(c2, "@g")),
|
688
|
+
NUM2DBL(r_iv_get(c2, "@b")),
|
689
|
+
NUM2DBL(r_iv_get(c2, "@a")),
|
690
|
+
|
691
|
+
NUM2DBL(r_iv_get(c3, "@r")),
|
692
|
+
NUM2DBL(r_iv_get(c3, "@g")),
|
693
|
+
NUM2DBL(r_iv_get(c3, "@b")),
|
694
|
+
NUM2DBL(r_iv_get(c3, "@a")),
|
695
|
+
|
696
|
+
NUM2DBL(r_iv_get(c4, "@r")),
|
697
|
+
NUM2DBL(r_iv_get(c4, "@g")),
|
698
|
+
NUM2DBL(r_iv_get(c4, "@b")),
|
699
|
+
NUM2DBL(r_iv_get(c4, "@a"))
|
700
|
+
);
|
701
|
+
}
|
702
|
+
break;
|
703
|
+
|
580
704
|
case R2D_IMAGE: {
|
581
705
|
S2D_Image *img;
|
582
706
|
r_data_get_struct(el, "@data", &image_data_type, S2D_Image, img);
|
583
|
-
|
707
|
+
|
584
708
|
img->x = NUM2DBL(r_iv_get(el, "@x"));
|
585
709
|
img->y = NUM2DBL(r_iv_get(el, "@y"));
|
586
|
-
|
710
|
+
|
587
711
|
R_VAL w = r_iv_get(el, "@width");
|
588
712
|
R_VAL h = r_iv_get(el, "@height");
|
589
713
|
if (r_test(w)) img->width = NUM2INT(w);
|
590
714
|
if (r_test(h)) img->height = NUM2INT(h);
|
591
|
-
|
715
|
+
|
592
716
|
R_VAL c = r_iv_get(el, "@color");
|
593
717
|
img->color.r = NUM2DBL(r_iv_get(c, "@r"));
|
594
718
|
img->color.g = NUM2DBL(r_iv_get(c, "@g"));
|
595
719
|
img->color.b = NUM2DBL(r_iv_get(c, "@b"));
|
596
720
|
img->color.a = NUM2DBL(r_iv_get(c, "@a"));
|
597
|
-
|
721
|
+
|
598
722
|
S2D_DrawImage(img);
|
599
723
|
}
|
600
724
|
break;
|
601
|
-
|
725
|
+
|
602
726
|
case R2D_SPRITE: {
|
603
727
|
S2D_Sprite *spr;
|
604
728
|
r_data_get_struct(el, "@data", &sprite_data_type, S2D_Sprite, spr);
|
605
|
-
|
729
|
+
|
606
730
|
spr->x = NUM2DBL(r_iv_get(el, "@x"));
|
607
731
|
spr->y = NUM2DBL(r_iv_get(el, "@y"));
|
608
|
-
|
732
|
+
|
609
733
|
S2D_ClipSprite(
|
610
734
|
spr,
|
611
735
|
NUM2INT(r_iv_get(el, "@clip_x")),
|
@@ -613,24 +737,24 @@ static void render() {
|
|
613
737
|
NUM2INT(r_iv_get(el, "@clip_w")),
|
614
738
|
NUM2INT(r_iv_get(el, "@clip_h"))
|
615
739
|
);
|
616
|
-
|
740
|
+
|
617
741
|
S2D_DrawSprite(spr);
|
618
742
|
}
|
619
743
|
break;
|
620
|
-
|
744
|
+
|
621
745
|
case R2D_TEXT: {
|
622
746
|
S2D_Text *txt;
|
623
747
|
r_data_get_struct(el, "@data", &text_data_type, S2D_Text, txt);
|
624
|
-
|
748
|
+
|
625
749
|
txt->x = NUM2DBL(r_iv_get(el, "@x"));
|
626
750
|
txt->y = NUM2DBL(r_iv_get(el, "@y"));
|
627
|
-
|
751
|
+
|
628
752
|
R_VAL c = r_iv_get(el, "@color");
|
629
753
|
txt->color.r = NUM2DBL(r_iv_get(c, "@r"));
|
630
754
|
txt->color.g = NUM2DBL(r_iv_get(c, "@g"));
|
631
755
|
txt->color.b = NUM2DBL(r_iv_get(c, "@b"));
|
632
756
|
txt->color.a = NUM2DBL(r_iv_get(c, "@a"));
|
633
|
-
|
757
|
+
|
634
758
|
S2D_DrawText(txt);
|
635
759
|
}
|
636
760
|
break;
|
@@ -648,17 +772,17 @@ static R_VAL ruby2d_show(mrb_state* mrb, R_VAL self) {
|
|
648
772
|
static R_VAL ruby2d_show(R_VAL self) {
|
649
773
|
#endif
|
650
774
|
ruby2d_window = self;
|
651
|
-
|
775
|
+
|
652
776
|
if (r_test(r_iv_get(self, "@diagnostics"))) {
|
653
777
|
S2D_Diagnostics(true);
|
654
778
|
}
|
655
|
-
|
779
|
+
|
656
780
|
// Get window attributes
|
657
781
|
char *title = RSTRING_PTR(r_iv_get(self, "@title"));
|
658
782
|
int width = NUM2INT(r_iv_get(self, "@width"));
|
659
783
|
int height = NUM2INT(r_iv_get(self, "@height"));
|
660
784
|
int flags = 0;
|
661
|
-
|
785
|
+
|
662
786
|
// Get window flags
|
663
787
|
if (r_test(r_iv_get(self, "@resizable"))) {
|
664
788
|
flags = flags | S2D_RESIZABLE;
|
@@ -672,29 +796,29 @@ static R_VAL ruby2d_show(R_VAL self) {
|
|
672
796
|
if (r_test(r_iv_get(self, "@highdpi"))) {
|
673
797
|
flags = flags | S2D_HIGHDPI;
|
674
798
|
}
|
675
|
-
|
799
|
+
|
676
800
|
// Check viewport size and set
|
677
|
-
|
801
|
+
|
678
802
|
R_VAL vp_w = r_iv_get(self, "@viewport_width");
|
679
803
|
int viewport_width = r_test(vp_w) ? NUM2INT(vp_w) : width;
|
680
|
-
|
804
|
+
|
681
805
|
R_VAL vp_h = r_iv_get(self, "@viewport_height");
|
682
806
|
int viewport_height = r_test(vp_h) ? NUM2INT(vp_h) : height;
|
683
|
-
|
807
|
+
|
684
808
|
// Create and show window
|
685
|
-
|
809
|
+
|
686
810
|
window = S2D_CreateWindow(
|
687
811
|
title, width, height, update, render, flags
|
688
812
|
);
|
689
|
-
|
813
|
+
|
690
814
|
window->viewport.width = viewport_width;
|
691
815
|
window->viewport.height = viewport_height;
|
692
816
|
window->on_key = on_key;
|
693
817
|
window->on_mouse = on_mouse;
|
694
818
|
window->on_controller = on_controller;
|
695
|
-
|
819
|
+
|
696
820
|
S2D_Show(window);
|
697
|
-
|
821
|
+
|
698
822
|
atexit(free_window);
|
699
823
|
return R_NIL;
|
700
824
|
}
|
@@ -717,83 +841,91 @@ int main(void) {
|
|
717
841
|
// Open the MRuby environment
|
718
842
|
mrb = mrb_open();
|
719
843
|
if (!mrb) { /* handle error */ }
|
720
|
-
|
844
|
+
|
721
845
|
// Load the Ruby 2D library
|
722
846
|
mrb_load_irep(mrb, ruby2d_lib);
|
847
|
+
|
848
|
+
// Add missing MRuby classes, methods
|
849
|
+
R_CLASS file_class = mrb_define_class(mrb, "File", mrb->object_class);
|
850
|
+
mrb_define_class_method(mrb, file_class, "exists?", file_exists, r_args_req(1));
|
851
|
+
|
723
852
|
#else
|
724
853
|
/*
|
725
854
|
* Ruby C extension init
|
726
855
|
*/
|
727
856
|
void Init_ruby2d() {
|
728
857
|
#endif
|
729
|
-
|
858
|
+
|
730
859
|
// Ruby2D
|
731
860
|
R_CLASS ruby2d_module = r_define_module("Ruby2D");
|
732
|
-
|
861
|
+
|
733
862
|
// Ruby2D::Image
|
734
863
|
R_CLASS ruby2d_image_class = r_define_class(ruby2d_module, "Image");
|
735
|
-
|
864
|
+
|
736
865
|
// Ruby2D::Image#init
|
737
|
-
r_define_method(ruby2d_image_class, "
|
738
|
-
|
866
|
+
r_define_method(ruby2d_image_class, "ext_image_init", ruby2d_image_init, r_args_req(1));
|
867
|
+
|
739
868
|
// Ruby2D::Sprite
|
740
869
|
R_CLASS ruby2d_sprite_class = r_define_class(ruby2d_module, "Sprite");
|
741
|
-
|
870
|
+
|
742
871
|
// Ruby2D::Sprite#init
|
743
|
-
r_define_method(ruby2d_sprite_class, "
|
744
|
-
|
872
|
+
r_define_method(ruby2d_sprite_class, "ext_sprite_init", ruby2d_sprite_init, r_args_req(1));
|
873
|
+
|
745
874
|
// Ruby2D::Text
|
746
875
|
R_CLASS ruby2d_text_class = r_define_class(ruby2d_module, "Text");
|
747
|
-
|
876
|
+
|
748
877
|
// Ruby2D::Text#init
|
749
|
-
r_define_method(ruby2d_text_class, "
|
750
|
-
|
751
|
-
// Ruby2D::Text#
|
752
|
-
r_define_method(ruby2d_text_class, "
|
753
|
-
|
878
|
+
r_define_method(ruby2d_text_class, "ext_text_init", ruby2d_text_init, r_args_none);
|
879
|
+
|
880
|
+
// Ruby2D::Text#ext_text_set
|
881
|
+
r_define_method(ruby2d_text_class, "ext_text_set", ruby2d_text_set, r_args_req(1));
|
882
|
+
|
754
883
|
// Ruby2D::Sound
|
755
884
|
R_CLASS ruby2d_sound_class = r_define_class(ruby2d_module, "Sound");
|
756
|
-
|
885
|
+
|
757
886
|
// Ruby2D::Sound#init
|
758
|
-
r_define_method(ruby2d_sound_class, "
|
759
|
-
|
887
|
+
r_define_method(ruby2d_sound_class, "ext_sound_init", ruby2d_sound_init, r_args_req(1));
|
888
|
+
|
760
889
|
// Ruby2D::Sound#play
|
761
|
-
r_define_method(ruby2d_sound_class, "
|
762
|
-
|
890
|
+
r_define_method(ruby2d_sound_class, "ext_sound_play", ruby2d_sound_play, r_args_none);
|
891
|
+
|
763
892
|
// Ruby2D::Music
|
764
893
|
R_CLASS ruby2d_music_class = r_define_class(ruby2d_module, "Music");
|
765
|
-
|
894
|
+
|
766
895
|
// Ruby2D::Music#init
|
767
|
-
r_define_method(ruby2d_music_class, "
|
768
|
-
|
896
|
+
r_define_method(ruby2d_music_class, "ext_music_init", ruby2d_music_init, r_args_req(1));
|
897
|
+
|
769
898
|
// Ruby2D::Music#play
|
770
|
-
r_define_method(ruby2d_music_class, "
|
771
|
-
|
899
|
+
r_define_method(ruby2d_music_class, "ext_music_play", ruby2d_music_play, r_args_none);
|
900
|
+
|
772
901
|
// Ruby2D::Music#pause
|
773
|
-
r_define_method(ruby2d_music_class, "
|
774
|
-
|
902
|
+
r_define_method(ruby2d_music_class, "ext_music_pause", ruby2d_music_pause, r_args_none);
|
903
|
+
|
775
904
|
// Ruby2D::Music#resume
|
776
|
-
r_define_method(ruby2d_music_class, "
|
777
|
-
|
905
|
+
r_define_method(ruby2d_music_class, "ext_music_resume", ruby2d_music_resume, r_args_none);
|
906
|
+
|
778
907
|
// Ruby2D::Music#stop
|
779
|
-
r_define_method(ruby2d_music_class, "
|
780
|
-
|
908
|
+
r_define_method(ruby2d_music_class, "ext_music_stop", ruby2d_music_stop, r_args_none);
|
909
|
+
|
781
910
|
// Ruby2D::Music#fadeout
|
782
|
-
r_define_method(ruby2d_music_class, "
|
783
|
-
|
911
|
+
r_define_method(ruby2d_music_class, "ext_music_fadeout", ruby2d_music_fadeout, r_args_req(1));
|
912
|
+
|
784
913
|
// Ruby2D::Window
|
785
914
|
R_CLASS ruby2d_window_class = r_define_class(ruby2d_module, "Window");
|
786
|
-
|
915
|
+
|
787
916
|
// Ruby2D::Window#show
|
788
|
-
r_define_method(ruby2d_window_class, "
|
789
|
-
|
917
|
+
r_define_method(ruby2d_window_class, "ext_window_show", ruby2d_show, r_args_none);
|
918
|
+
|
790
919
|
// Ruby2D::Window#close
|
791
|
-
r_define_method(ruby2d_window_class, "
|
792
|
-
|
920
|
+
r_define_method(ruby2d_window_class, "ext_window_close", ruby2d_close, r_args_none);
|
921
|
+
|
793
922
|
#if MRUBY
|
794
923
|
// Load the Ruby 2D app
|
795
924
|
mrb_load_irep(mrb, ruby2d_app);
|
796
|
-
|
925
|
+
|
926
|
+
// If an exception, print error
|
927
|
+
if (mrb->exc) mrb_print_error(mrb);
|
928
|
+
|
797
929
|
// Close the MRuby environment
|
798
930
|
mrb_close(mrb);
|
799
931
|
#endif
|