ext/ruby2d/ruby2d.c in ruby2d-0.2.1 vs ext/ruby2d/ruby2d.c in ruby2d-0.3.0

- old
+ new

@@ -1,289 +1,800 @@ -#include <ruby.h> +// ruby2d.c – Native C extension for Ruby and MRuby + +// Simple 2D includes #include <simple2d.h> +// Ruby includes +#if MRUBY + #include <mruby.h> + #include <mruby/array.h> + #include <mruby/string.h> + #include <mruby/variable.h> + #include <mruby/numeric.h> + #include <mruby/data.h> + #include <mruby/irep.h> +#else + #include <ruby.h> +#endif + +// Define Ruby type conversions in MRuby +#if MRUBY + // C to MRuby types + #define INT2FIX(val) (mrb_fixnum_value(val)) + #define INT2NUM(val) (mrb_fixnum_value((mrb_int)(val))) + #define UINT2NUM(val) (INT2NUM((unsigned mrb_int)(val))) + #define LL2NUM(val) (INT2NUM(val)) + #define ULL2NUM(val) (INT2NUM((unsigned __int64)(val))) + #define DBL2NUM(val) (mrb_float_value(mrb, (mrb_float)(val))) + // MRuby to C types + #define TO_PDT(val) ((mrb_type(val) == MRB_TT_FLOAT) ? mrb_float(val) : mrb_int(mrb, (val))) + #define FIX2INT(val) (mrb_fixnum(val)) + #define NUM2INT(val) ((mrb_int)TO_PDT(val)) + #define NUM2UINT(val) ((unsigned mrb_int)TO_PDT(val)) + #define NUM2LONG(val) (mrb_int(mrb, (val))) + #define NUM2LL(val) ((__int64)(TO_PDT(val))) + #define NUM2ULL(val) ((unsigned __int64)(TO_PDT(val))) + #define NUM2DBL(val) (mrb_to_flo(mrb, (val))) + #define NUM2CHR(val) ((mrb_string_p(val) && (RSTRING_LEN(val)>=1)) ? RSTRING_PTR(val)[0] : (char)(NUM2INT(val) & 0xff)) + // Memory management + #define ALLOC(type) ((type *)mrb_malloc(mrb, sizeof(type))) + #define ALLOC_N(type, n) ((type *)mrb_malloc(mrb, sizeof(type) * (n))) + #define ALLOCA_N(type, n) ((type *)alloca(sizeof(type) * (n))) + #define MEMCMP(p1, p2, type, n) (memcmp((p1), (p2), sizeof(type)*(n))) +#endif + +// Define common types and API calls, mapping to both Ruby and MRuby APIs +#if MRUBY + // MRuby + #define R_VAL mrb_value + #define R_NIL (mrb_nil_value()) + #define R_TRUE (mrb_true_value()) + #define R_FALSE (mrb_false_value()) + #define R_CLASS struct RClass * + #define r_iv_get(self, var) mrb_iv_get(mrb, self, mrb_intern_lit(mrb, var)) + #define r_iv_set(self, var, val) mrb_iv_set(mrb, self, mrb_intern_lit(mrb, var), val) + #define r_funcall(self, method, num_args, ...) mrb_funcall(mrb, self, method, num_args, ##__VA_ARGS__) + #define r_str_new(str) mrb_str_new(mrb, str, strlen(str)) + #define r_test(val) (mrb_test(val) == true) + #define r_ary_entry(ary, pos) mrb_ary_entry(ary, pos) + #define r_data_wrap_struct(name, data) mrb_obj_value(Data_Wrap_Struct(mrb, mrb->object_class, &name##_data_type, data)) + #define r_data_get_struct(self, var, mrb_type, rb_type, data) Data_Get_Struct(mrb, r_iv_get(self, var), mrb_type, data) + #define r_define_module(name) mrb_module_get(mrb, name) + #define r_define_class(module, name) mrb_class_get_under(mrb, module, name); + #define r_define_method(class, name, function, args) mrb_define_method(mrb, class, name, function, args) + #define r_args_none (MRB_ARGS_NONE()) + #define r_args_req(n) MRB_ARGS_REQ(n) +#else + // Ruby + #define R_VAL VALUE + #define R_NIL Qnil + #define R_TRUE Qtrue + #define R_FALSE Qfalse + #define R_CLASS R_VAL + #define r_iv_get(self, var) rb_iv_get(self, var) + #define r_iv_set(self, var, val) rb_iv_set(self, var, val) + #define r_funcall(self, method, num_args, ...) rb_funcall(self, rb_intern(method), num_args, ##__VA_ARGS__) + #define r_str_new(str) rb_str_new2(str) + #define r_test(val) (val != Qfalse && val != Qnil) + #define r_ary_entry(ary, pos) rb_ary_entry(ary, pos) + #define r_data_wrap_struct(name, data) Data_Wrap_Struct(rb_cObject, NULL, (free_##name), data) + #define r_data_get_struct(self, var, mrb_type, rb_type, data) Data_Get_Struct(r_iv_get(self, var), rb_type, data) + #define r_define_module(name) rb_define_module(name) + #define r_define_class(module, name) rb_define_class_under(module, name, rb_cObject); + #define r_define_method(class, name, function, args) rb_define_method(class, name, function, args) + #define r_args_none 0 + #define r_args_req(n) n +#endif + // @type_id values for rendering -#define TRIANGLE 1 -#define QUAD 2 -#define IMAGE 3 -#define TEXT 4 +#define R2D_TRIANGLE 1 +#define R2D_QUAD 2 +#define R2D_IMAGE 3 +#define R2D_SPRITE 4 +#define R2D_TEXT 5 +// Create the MRuby context +#if MRUBY + static mrb_state *mrb; +#endif + // Ruby 2D window -VALUE self; +static R_VAL ruby2d_window; // Simple 2D window -S2D_Window *window; +static S2D_Window *window; -// Ruby data types -static VALUE ruby2d_module; -static VALUE ruby2d_window_klass; -static VALUE c_data_klass; -// Structures for Ruby 2D classes -struct image_data { - S2D_Image *img; -}; -struct text_data { - S2D_Text *txt; -}; -struct sound_data { - S2D_Sound *snd; -}; +// Method signatures and structures for Ruby 2D classes +#if MRUBY + static void free_image(mrb_state *mrb, void *p_); + static const struct mrb_data_type image_data_type = { + "image", free_image + }; + static void free_sprite(mrb_state *mrb, void *p_); + static const struct mrb_data_type sprite_data_type = { + "sprite", free_sprite + }; + static void free_text(mrb_state *mrb, void *p_); + static const struct mrb_data_type text_data_type = { + "text", free_text + }; + static void free_sound(mrb_state *mrb, void *p_); + static const struct mrb_data_type sound_data_type = { + "sound", free_sound + }; + static void free_music(mrb_state *mrb, void *p_); + static const struct mrb_data_type music_data_type = { + "music", free_music + }; +#else + static void free_image(S2D_Image *img); + static void free_sprite(S2D_Sprite *spr); + static void free_text(S2D_Text *txt); + static void free_sound(S2D_Sound *snd); + static void free_music(S2D_Music *mus); +#endif /* - * Function pointer to close the Simple 2D window + * Function pointer to free the Simple 2D window */ -static void close_window() { - S2D_Close(window); +static void free_window() { + S2D_FreeWindow(window); } /* + * Ruby2D::Image#init + * Initialize image structure data + */ +#if MRUBY +static R_VAL ruby2d_image_init(mrb_state* mrb, R_VAL self) { + mrb_value path; + mrb_get_args(mrb, "o", &path); +#else +static R_VAL ruby2d_image_init(R_VAL self, R_VAL path) { +#endif + sprintf(S2D_msg, "Init image: %s", RSTRING_PTR(path)); + S2D_Log(S2D_msg, S2D_INFO); + S2D_Image *img = S2D_CreateImage(RSTRING_PTR(path)); + r_iv_set(self, "@data", r_data_wrap_struct(image, img)); + return R_NIL; +} + + +/* * Free image structure attached to Ruby 2D `Image` class */ -static void free_image(struct image_data *data) { - S2D_FreeImage(data->img); - xfree(data); +#if MRUBY +static void free_image(mrb_state *mrb, void *p_) { + S2D_Image *img = (S2D_Image *)p_; +#else +static void free_image(S2D_Image *img) { +#endif + sprintf(S2D_msg, "Free image: %i, %i", img->x, img->y); + S2D_Log(S2D_msg, S2D_INFO); + S2D_FreeImage(img); } /* - * Initialize image structure data + * Ruby2D::Sprite#init + * Initialize sprite structure data */ -static VALUE init_image(char *path) { - struct image_data *data = ALLOC(struct image_data); - data->img = S2D_CreateImage(path); - return Data_Wrap_Struct(c_data_klass, NULL, free_image, data); +#if MRUBY +static R_VAL ruby2d_sprite_init(mrb_state* mrb, R_VAL self) { + mrb_value path; + mrb_get_args(mrb, "o", &path); +#else +static R_VAL ruby2d_sprite_init(R_VAL self, R_VAL path) { +#endif + sprintf(S2D_msg, "Init sprite: %s", RSTRING_PTR(path)); + S2D_Log(S2D_msg, S2D_INFO); + S2D_Sprite *spr = S2D_CreateSprite(RSTRING_PTR(path)); + r_iv_set(self, "@data", r_data_wrap_struct(sprite, spr)); + return R_NIL; } /* - * Free text structure attached to Ruby 2D `Text` class + * Free sprite structure attached to Ruby 2D `Sprite` class */ -static void free_text(struct text_data *data) { - S2D_FreeText(data->txt); - xfree(data); +#if MRUBY +static void free_sprite(mrb_state *mrb, void *p_) { + S2D_Sprite *spr = (S2D_Sprite *)p_; +#else +static void free_sprite(S2D_Sprite *spr) { +#endif + sprintf(S2D_msg, "Free sprite: %i, %i", spr->x, spr->y); + S2D_Log(S2D_msg, S2D_INFO); + S2D_FreeSprite(spr); } /* + * Ruby2D::Text#init * Initialize text structure data */ -static VALUE init_text(char *font, char *msg, int size) { - struct text_data *data = ALLOC(struct text_data); - data->txt = S2D_CreateText(font, msg, size); - return Data_Wrap_Struct(c_data_klass, NULL, free_text, data); +#if MRUBY +static R_VAL ruby2d_text_init(mrb_state* mrb, R_VAL self) { +#else +static R_VAL ruby2d_text_init(R_VAL self) { +#endif + sprintf(S2D_msg, "Init text: %s", RSTRING_PTR(r_iv_get(self, "@text"))); + S2D_Log(S2D_msg, S2D_INFO); + + S2D_Text *txt = S2D_CreateText( + RSTRING_PTR(r_iv_get(self, "@font")), + RSTRING_PTR(r_iv_get(self, "@text")), + NUM2DBL(r_iv_get(self, "@size")) + ); + + r_iv_set(self, "@data", r_data_wrap_struct(text, txt)); + return R_NIL; } /* + * Ruby2D::Text#text= + */ +#if MRUBY +static R_VAL ruby2d_text_equals(mrb_state* mrb, R_VAL self) { + mrb_value text; + mrb_get_args(mrb, "o", &text); +#else +static R_VAL ruby2d_text_equals(R_VAL self, R_VAL text) { + r_iv_set(self, "@text", text); +#endif + + // If called before window is shown, return + if (!r_test(ruby2d_window)) return R_NIL; + + S2D_Text *txt; + r_data_get_struct(self, "@data", &text_data_type, S2D_Text, txt); + + S2D_SetText(txt, RSTRING_PTR(text)); + return R_NIL; +} + + +/* + * Free text structure attached to Ruby 2D `Text` class + */ +#if MRUBY +static void free_text(mrb_state *mrb, void *p_) { + S2D_Text *txt = (S2D_Text *)p_; +#else +static void free_text(S2D_Text *txt) { +#endif + sprintf(S2D_msg, "Free text: %s", txt->msg); + S2D_Log(S2D_msg, S2D_INFO); + S2D_FreeText(txt); +} + + +/* + * Ruby2D::Sound#init + * Initialize sound structure data + */ +#if MRUBY +static R_VAL ruby2d_sound_init(mrb_state* mrb, R_VAL self) { + mrb_value path; + mrb_get_args(mrb, "o", &path); +#else +static R_VAL ruby2d_sound_init(R_VAL self, R_VAL path) { +#endif + sprintf(S2D_msg, "Init sound: %s", RSTRING_PTR(path)); + S2D_Log(S2D_msg, S2D_INFO); + S2D_Sound *snd = S2D_CreateSound(RSTRING_PTR(path)); + r_iv_set(self, "@data", r_data_wrap_struct(sound, snd)); + return R_NIL; +} + + +/* + * Ruby2D::Sound#play + */ +#if MRUBY +static R_VAL ruby2d_sound_play(mrb_state* mrb, R_VAL self) { +#else +static R_VAL ruby2d_sound_play(R_VAL self) { +#endif + S2D_Sound *snd; + r_data_get_struct(self, "@data", &sound_data_type, S2D_Sound, snd); + S2D_PlaySound(snd); + return R_NIL; +} + + +/* + * Free sound structure attached to Ruby 2D `Sound` class + */ +#if MRUBY +static void free_sound(mrb_state *mrb, void *p_) { + S2D_Sound *snd = (S2D_Sound *)p_; +#else +static void free_sound(S2D_Sound *snd) { +#endif + sprintf(S2D_msg, "Free sound"); + S2D_Log(S2D_msg, S2D_INFO); + S2D_FreeSound(snd); +} + + +/* + * Ruby2D::Music#init + * Initialize music structure data + */ +#if MRUBY +static R_VAL ruby2d_music_init(mrb_state* mrb, R_VAL self) { + mrb_value path; + mrb_get_args(mrb, "o", &path); +#else +static R_VAL ruby2d_music_init(R_VAL self, R_VAL path) { +#endif + sprintf(S2D_msg, "Init music: %s", RSTRING_PTR(path)); + S2D_Log(S2D_msg, S2D_INFO); + S2D_Music *mus = S2D_CreateMusic(RSTRING_PTR(path)); + r_iv_set(self, "@data", r_data_wrap_struct(music, mus)); + return R_NIL; +} + + +/* + * Ruby2D::Music#play + */ +#if MRUBY +static R_VAL ruby2d_music_play(mrb_state* mrb, R_VAL self) { +#else +static R_VAL ruby2d_music_play(R_VAL self) { +#endif + S2D_Music *mus; + r_data_get_struct(self, "@data", &music_data_type, S2D_Music, mus); + S2D_PlayMusic(mus, r_test(r_iv_get(self, "@loop"))); + return R_NIL; +} + + +/* + * Ruby2D::Music#pause + */ +#if MRUBY +static R_VAL ruby2d_music_pause(mrb_state* mrb, R_VAL self) { +#else +static R_VAL ruby2d_music_pause(R_VAL self) { +#endif + S2D_PauseMusic(); + return R_NIL; +} + + +/* + * Ruby2D::Music#resume + */ +#if MRUBY +static R_VAL ruby2d_music_resume(mrb_state* mrb, R_VAL self) { +#else +static R_VAL ruby2d_music_resume(R_VAL self) { +#endif + S2D_ResumeMusic(); + return R_NIL; +} + + +/* + * Ruby2D::Music#stop + */ +#if MRUBY +static R_VAL ruby2d_music_stop(mrb_state* mrb, R_VAL self) { +#else +static R_VAL ruby2d_music_stop(R_VAL self) { +#endif + S2D_StopMusic(); + return R_NIL; +} + + +/* + * Ruby2D::Music#fadeout + */ +#if MRUBY +static R_VAL ruby2d_music_fadeout(mrb_state* mrb, R_VAL self) { + mrb_value ms; + mrb_get_args(mrb, "o", &ms); +#else +static R_VAL ruby2d_music_fadeout(R_VAL self, R_VAL ms) { +#endif + S2D_FadeOutMusic(NUM2INT(ms)); + return R_NIL; +} + + +/* + * Free sound structure attached to Ruby 2D `Sound` class + */ +#if MRUBY +static void free_music(mrb_state *mrb, void *p_) { + S2D_Music *mus = (S2D_Music *)p_; +#else +static void free_music(S2D_Music *mus) { +#endif + sprintf(S2D_msg, "Free music"); + S2D_Log(S2D_msg, S2D_INFO); + S2D_FreeMusic(mus); +} + + +/* * Simple 2D `on_key` input callback function */ -void on_key(const char *key) { - rb_funcall(self, rb_intern("key_callback"), 1, rb_str_new2(key)); +static void on_key(S2D_Event e, const char *key) { + switch (e) { + case S2D_KEYDOWN: + r_funcall(ruby2d_window, "key_down_callback", 1, r_str_new(key)); + break; + + case S2D_KEY: + r_funcall(ruby2d_window, "key_callback", 1, r_str_new(key)); + break; + + case S2D_KEYUP: + r_funcall(ruby2d_window, "key_up_callback", 1, r_str_new(key)); + break; + } } /* - * Simple 2D `on_key_down` input callback function + * Simple 2D `on_mouse` input callback function */ -void on_key_down(const char *key) { - rb_funcall(self, rb_intern("key_down_callback"), 1, rb_str_new2(key)); +void on_mouse(int x, int y) { + r_funcall(ruby2d_window, "mouse_callback", 3, r_str_new("any"), INT2NUM(x), INT2NUM(y)); } /* * Simple 2D `on_controller` input callback function */ -void on_controller(bool is_axis, int axis, int val, bool is_btn, int btn) { - rb_funcall(self, rb_intern("controller_callback"), 5, - is_axis ? Qtrue : Qfalse, INT2NUM(axis), INT2NUM(val), - is_btn ? Qtrue : Qfalse, INT2NUM(btn) +static void on_controller(int which, bool is_axis, int axis, int val, bool is_btn, int btn, bool pressed) { + r_funcall(ruby2d_window, "controller_callback", 7, + INT2NUM(which), + is_axis ? R_TRUE : R_FALSE, INT2NUM(axis), INT2NUM(val), + is_btn ? R_TRUE : R_FALSE, INT2NUM(btn), pressed ? R_TRUE : R_FALSE ); } /* * Simple 2D `update` callback function */ -void update() { +static void update() { // Set the cursor - rb_iv_set(self, "@mouse_x", INT2NUM(window->mouse.x)); - rb_iv_set(self, "@mouse_y", INT2NUM(window->mouse.y)); + r_iv_set(ruby2d_window, "@mouse_x", INT2NUM(window->mouse.x)); + r_iv_set(ruby2d_window, "@mouse_y", INT2NUM(window->mouse.y)); + // Store frames + r_iv_set(ruby2d_window, "@frames", DBL2NUM(window->frames)); + // Store frame rate - rb_iv_set(self, "@fps", INT2NUM(window->fps)); + r_iv_set(ruby2d_window, "@fps", DBL2NUM(window->fps)); // Call update proc, `window.update` - rb_funcall(self, rb_intern("update_callback"), 0); + r_funcall(ruby2d_window, "update_callback", 0); } /* * Simple 2D `render` callback function */ -void render() { +static void render() { + // Set background color + R_VAL bc = r_iv_get(ruby2d_window, "@background"); + window->background.r = NUM2DBL(r_iv_get(bc, "@r")); + window->background.g = NUM2DBL(r_iv_get(bc, "@g")); + window->background.b = NUM2DBL(r_iv_get(bc, "@b")); + window->background.a = NUM2DBL(r_iv_get(bc, "@a")); + // Read window objects - VALUE objects = rb_iv_get(self, "@objects"); - int num_objects = NUM2INT(rb_funcall(objects, rb_intern("count"), 0)); + R_VAL objects = r_iv_get(ruby2d_window, "@objects"); + int num_objects = NUM2INT(r_funcall(objects, "length", 0)); // Switch on each object type for (int i = 0; i < num_objects; ++i) { - VALUE el = rb_ary_entry(objects, i); - int type_id = NUM2INT(rb_iv_get(el, "@type_id")); + R_VAL el = r_ary_entry(objects, i); + int type_id = NUM2INT(r_iv_get(el, "@type_id")); // Switch on the object's type_id switch(type_id) { - case TRIANGLE: { - VALUE c1 = rb_iv_get(el, "@c1"); - VALUE c2 = rb_iv_get(el, "@c2"); - VALUE c3 = rb_iv_get(el, "@c3"); + case R2D_TRIANGLE: { + R_VAL c1 = r_iv_get(el, "@c1"); + R_VAL c2 = r_iv_get(el, "@c2"); + R_VAL c3 = r_iv_get(el, "@c3"); S2D_DrawTriangle( - NUM2DBL(rb_iv_get(el, "@x1")), - NUM2DBL(rb_iv_get(el, "@y1")), - NUM2DBL(rb_iv_get(c1, "@r")), - NUM2DBL(rb_iv_get(c1, "@g")), - NUM2DBL(rb_iv_get(c1, "@b")), - NUM2DBL(rb_iv_get(c1, "@a")), + NUM2DBL(r_iv_get(el, "@x1")), + NUM2DBL(r_iv_get(el, "@y1")), + NUM2DBL(r_iv_get(c1, "@r")), + NUM2DBL(r_iv_get(c1, "@g")), + NUM2DBL(r_iv_get(c1, "@b")), + NUM2DBL(r_iv_get(c1, "@a")), - NUM2DBL(rb_iv_get(el, "@x2")), - NUM2DBL(rb_iv_get(el, "@y2")), - NUM2DBL(rb_iv_get(c2, "@r")), - NUM2DBL(rb_iv_get(c2, "@g")), - NUM2DBL(rb_iv_get(c2, "@b")), - NUM2DBL(rb_iv_get(c2, "@a")), + NUM2DBL(r_iv_get(el, "@x2")), + NUM2DBL(r_iv_get(el, "@y2")), + NUM2DBL(r_iv_get(c2, "@r")), + NUM2DBL(r_iv_get(c2, "@g")), + NUM2DBL(r_iv_get(c2, "@b")), + NUM2DBL(r_iv_get(c2, "@a")), - NUM2DBL(rb_iv_get(el, "@x3")), - NUM2DBL(rb_iv_get(el, "@y3")), - NUM2DBL(rb_iv_get(c3, "@r")), - NUM2DBL(rb_iv_get(c3, "@g")), - NUM2DBL(rb_iv_get(c3, "@b")), - NUM2DBL(rb_iv_get(c3, "@a")) + NUM2DBL(r_iv_get(el, "@x3")), + NUM2DBL(r_iv_get(el, "@y3")), + NUM2DBL(r_iv_get(c3, "@r")), + NUM2DBL(r_iv_get(c3, "@g")), + NUM2DBL(r_iv_get(c3, "@b")), + NUM2DBL(r_iv_get(c3, "@a")) ); } break; - case QUAD: { - VALUE c1 = rb_iv_get(el, "@c1"); - VALUE c2 = rb_iv_get(el, "@c2"); - VALUE c3 = rb_iv_get(el, "@c3"); - VALUE c4 = rb_iv_get(el, "@c4"); + case R2D_QUAD: { + R_VAL c1 = r_iv_get(el, "@c1"); + R_VAL c2 = r_iv_get(el, "@c2"); + R_VAL c3 = r_iv_get(el, "@c3"); + R_VAL c4 = r_iv_get(el, "@c4"); S2D_DrawQuad( - NUM2DBL(rb_iv_get(el, "@x1")), - NUM2DBL(rb_iv_get(el, "@y1")), - NUM2DBL(rb_iv_get(c1, "@r")), - NUM2DBL(rb_iv_get(c1, "@g")), - NUM2DBL(rb_iv_get(c1, "@b")), - NUM2DBL(rb_iv_get(c1, "@a")), - - NUM2DBL(rb_iv_get(el, "@x2")), - NUM2DBL(rb_iv_get(el, "@y2")), - NUM2DBL(rb_iv_get(c2, "@r")), - NUM2DBL(rb_iv_get(c2, "@g")), - NUM2DBL(rb_iv_get(c2, "@b")), - NUM2DBL(rb_iv_get(c2, "@a")), - - NUM2DBL(rb_iv_get(el, "@x3")), - NUM2DBL(rb_iv_get(el, "@y3")), - NUM2DBL(rb_iv_get(c3, "@r")), - NUM2DBL(rb_iv_get(c3, "@g")), - NUM2DBL(rb_iv_get(c3, "@b")), - NUM2DBL(rb_iv_get(c3, "@a")), - - NUM2DBL(rb_iv_get(el, "@x4")), - NUM2DBL(rb_iv_get(el, "@y4")), - NUM2DBL(rb_iv_get(c4, "@r")), - NUM2DBL(rb_iv_get(c4, "@g")), - NUM2DBL(rb_iv_get(c4, "@b")), - NUM2DBL(rb_iv_get(c4, "@a")) + NUM2DBL(r_iv_get(el, "@x1")), + NUM2DBL(r_iv_get(el, "@y1")), + NUM2DBL(r_iv_get(c1, "@r")), + NUM2DBL(r_iv_get(c1, "@g")), + NUM2DBL(r_iv_get(c1, "@b")), + NUM2DBL(r_iv_get(c1, "@a")), + + NUM2DBL(r_iv_get(el, "@x2")), + NUM2DBL(r_iv_get(el, "@y2")), + NUM2DBL(r_iv_get(c2, "@r")), + NUM2DBL(r_iv_get(c2, "@g")), + NUM2DBL(r_iv_get(c2, "@b")), + NUM2DBL(r_iv_get(c2, "@a")), + + NUM2DBL(r_iv_get(el, "@x3")), + NUM2DBL(r_iv_get(el, "@y3")), + NUM2DBL(r_iv_get(c3, "@r")), + NUM2DBL(r_iv_get(c3, "@g")), + NUM2DBL(r_iv_get(c3, "@b")), + NUM2DBL(r_iv_get(c3, "@a")), + + NUM2DBL(r_iv_get(el, "@x4")), + NUM2DBL(r_iv_get(el, "@y4")), + NUM2DBL(r_iv_get(c4, "@r")), + NUM2DBL(r_iv_get(c4, "@g")), + NUM2DBL(r_iv_get(c4, "@b")), + NUM2DBL(r_iv_get(c4, "@a")) ); } break; - case IMAGE: { - if (rb_iv_get(el, "@data") == Qnil) { - VALUE data = init_image(RSTRING_PTR(rb_iv_get(el, "@path"))); - rb_iv_set(el, "@data", data); - } + case R2D_IMAGE: { + S2D_Image *img; + r_data_get_struct(el, "@data", &image_data_type, S2D_Image, img); - struct image_data *data; - Data_Get_Struct(rb_iv_get(el, "@data"), struct image_data, data); + img->x = NUM2DBL(r_iv_get(el, "@x")); + img->y = NUM2DBL(r_iv_get(el, "@y")); - data->img->x = NUM2DBL(rb_iv_get(el, "@x")); - data->img->y = NUM2DBL(rb_iv_get(el, "@y")); - S2D_DrawImage(data->img); + R_VAL w = r_iv_get(el, "@width"); + R_VAL h = r_iv_get(el, "@height"); + if (r_test(w)) img->width = NUM2INT(w); + if (r_test(h)) img->height = NUM2INT(h); + + R_VAL c = r_iv_get(el, "@color"); + img->color.r = NUM2DBL(r_iv_get(c, "@r")); + img->color.g = NUM2DBL(r_iv_get(c, "@g")); + img->color.b = NUM2DBL(r_iv_get(c, "@b")); + img->color.a = NUM2DBL(r_iv_get(c, "@a")); + + S2D_DrawImage(img); } break; - case TEXT: { - if (rb_iv_get(el, "@data") == Qnil) { - VALUE data = init_text( - RSTRING_PTR(rb_iv_get(el, "@font")), - RSTRING_PTR(rb_iv_get(el, "@text")), - NUM2DBL(rb_iv_get(el, "@size")) - ); - rb_iv_set(el, "@data", data); - } + case R2D_SPRITE: { + S2D_Sprite *spr; + r_data_get_struct(el, "@data", &sprite_data_type, S2D_Sprite, spr); - struct text_data *data; - Data_Get_Struct(rb_iv_get(el, "@data"), struct text_data, data); + spr->x = NUM2DBL(r_iv_get(el, "@x")); + spr->y = NUM2DBL(r_iv_get(el, "@y")); - data->txt->x = NUM2DBL(rb_iv_get(el, "@x")); - data->txt->y = NUM2DBL(rb_iv_get(el, "@y")); - S2D_DrawText(data->txt); + S2D_ClipSprite( + spr, + NUM2INT(r_iv_get(el, "@clip_x")), + NUM2INT(r_iv_get(el, "@clip_y")), + NUM2INT(r_iv_get(el, "@clip_w")), + NUM2INT(r_iv_get(el, "@clip_h")) + ); + + S2D_DrawSprite(spr); } break; + + case R2D_TEXT: { + S2D_Text *txt; + r_data_get_struct(el, "@data", &text_data_type, S2D_Text, txt); + + txt->x = NUM2DBL(r_iv_get(el, "@x")); + txt->y = NUM2DBL(r_iv_get(el, "@y")); + + R_VAL c = r_iv_get(el, "@color"); + txt->color.r = NUM2DBL(r_iv_get(c, "@r")); + txt->color.g = NUM2DBL(r_iv_get(c, "@g")); + txt->color.b = NUM2DBL(r_iv_get(c, "@b")); + txt->color.a = NUM2DBL(r_iv_get(c, "@a")); + + S2D_DrawText(txt); + } + break; } } } /* * Ruby2D::Window#show */ -static VALUE ruby2d_show(VALUE s) { - self = s; +#if MRUBY +static R_VAL ruby2d_show(mrb_state* mrb, R_VAL self) { +#else +static R_VAL ruby2d_show(R_VAL self) { +#endif + ruby2d_window = self; - char *title = RSTRING_PTR(rb_iv_get(self, "@title")); - int width = NUM2INT(rb_iv_get(self, "@width")); - int height = NUM2INT(rb_iv_get(self, "@height")); + if (r_test(r_iv_get(self, "@diagnostics"))) { + S2D_Diagnostics(true); + } + + // Get window attributes + char *title = RSTRING_PTR(r_iv_get(self, "@title")); + int width = NUM2INT(r_iv_get(self, "@width")); + int height = NUM2INT(r_iv_get(self, "@height")); int flags = 0; + // Get window flags + if (r_test(r_iv_get(self, "@resizable"))) { + flags = flags | S2D_RESIZABLE; + } + if (r_test(r_iv_get(self, "@borderless"))) { + flags = flags | S2D_BORDERLESS; + } + if (r_test(r_iv_get(self, "@fullscreen"))) { + flags = flags | S2D_FULLSCREEN; + } + if (r_test(r_iv_get(self, "@highdpi"))) { + flags = flags | S2D_HIGHDPI; + } + + // Check viewport size and set + + R_VAL vp_w = r_iv_get(self, "@viewport_width"); + int viewport_width = r_test(vp_w) ? NUM2INT(vp_w) : width; + + R_VAL vp_h = r_iv_get(self, "@viewport_height"); + int viewport_height = r_test(vp_h) ? NUM2INT(vp_h) : height; + + // Create and show window + window = S2D_CreateWindow( title, width, height, update, render, flags ); - window->on_key = on_key; - window->on_key_down = on_key_down; - window->on_controller = on_controller; + window->viewport.width = viewport_width; + window->viewport.height = viewport_height; + window->on_key = on_key; + window->on_mouse = on_mouse; + window->on_controller = on_controller; S2D_Show(window); - atexit(close_window); - return Qnil; + atexit(free_window); + return R_NIL; } /* + * Ruby2D::Window#close + */ +static R_VAL ruby2d_close() { + S2D_Close(window); + return R_NIL; +} + + +#if MRUBY +/* + * MRuby entry point + */ +int main(void) { + // Open the MRuby environment + mrb = mrb_open(); + if (!mrb) { /* handle error */ } + + // Load the Ruby 2D library + mrb_load_irep(mrb, ruby2d_lib); +#else +/* * Ruby C extension init */ void Init_ruby2d() { +#endif // Ruby2D - ruby2d_module = rb_define_module("Ruby2D"); + R_CLASS ruby2d_module = r_define_module("Ruby2D"); + // Ruby2D::Image + R_CLASS ruby2d_image_class = r_define_class(ruby2d_module, "Image"); + + // Ruby2D::Image#init + r_define_method(ruby2d_image_class, "init", ruby2d_image_init, r_args_req(1)); + + // Ruby2D::Sprite + R_CLASS ruby2d_sprite_class = r_define_class(ruby2d_module, "Sprite"); + + // Ruby2D::Sprite#init + r_define_method(ruby2d_sprite_class, "init", ruby2d_sprite_init, r_args_req(1)); + + // Ruby2D::Text + R_CLASS ruby2d_text_class = r_define_class(ruby2d_module, "Text"); + + // Ruby2D::Text#init + r_define_method(ruby2d_text_class, "init", ruby2d_text_init, r_args_none); + + // Ruby2D::Text#text= + r_define_method(ruby2d_text_class, "text=", ruby2d_text_equals, r_args_req(1)); + + // Ruby2D::Sound + R_CLASS ruby2d_sound_class = r_define_class(ruby2d_module, "Sound"); + + // Ruby2D::Sound#init + r_define_method(ruby2d_sound_class, "init", ruby2d_sound_init, r_args_req(1)); + + // Ruby2D::Sound#play + r_define_method(ruby2d_sound_class, "play", ruby2d_sound_play, r_args_none); + + // Ruby2D::Music + R_CLASS ruby2d_music_class = r_define_class(ruby2d_module, "Music"); + + // Ruby2D::Music#init + r_define_method(ruby2d_music_class, "init", ruby2d_music_init, r_args_req(1)); + + // Ruby2D::Music#play + r_define_method(ruby2d_music_class, "play", ruby2d_music_play, r_args_none); + + // Ruby2D::Music#pause + r_define_method(ruby2d_music_class, "pause", ruby2d_music_pause, r_args_none); + + // Ruby2D::Music#resume + r_define_method(ruby2d_music_class, "resume", ruby2d_music_resume, r_args_none); + + // Ruby2D::Music#stop + r_define_method(ruby2d_music_class, "stop", ruby2d_music_stop, r_args_none); + + // Ruby2D::Music#fadeout + r_define_method(ruby2d_music_class, "fadeout", ruby2d_music_fadeout, r_args_req(1)); + // Ruby2D::Window - ruby2d_window_klass = rb_define_class_under(ruby2d_module, "Window", rb_cObject); + R_CLASS ruby2d_window_class = r_define_class(ruby2d_module, "Window"); // Ruby2D::Window#show - rb_define_method(ruby2d_window_klass, "show", ruby2d_show, 0); + r_define_method(ruby2d_window_class, "show", ruby2d_show, r_args_none); - // Ruby2D::CData - c_data_klass = rb_define_class_under(ruby2d_module, "CData", rb_cObject); + // Ruby2D::Window#close + r_define_method(ruby2d_window_class, "close", ruby2d_close, r_args_none); + +#if MRUBY + // Load the Ruby 2D app + mrb_load_irep(mrb, ruby2d_app); + + // Close the MRuby environment + mrb_close(mrb); +#endif }