ext/ruby2d/ruby2d.c in ruby2d-0.8.0 vs ext/ruby2d/ruby2d.c in ruby2d-0.8.1

- old
+ new

@@ -165,25 +165,10 @@ return r_str_new(SDL_GetBasePath()); } /* - * Ruby2D#self.ext_screenshot - */ -#if MRUBY -static R_VAL ruby2d_ext_screenshot(mrb_state* mrb, R_VAL self) { - mrb_value path; - mrb_get_args(mrb, "o", &path); -#else -static R_VAL ruby2d_ext_screenshot(R_VAL self, R_VAL path) { -#endif - S2D_Screenshot(window, RSTRING_PTR(path)); - return R_NIL; -} - - -/* * Ruby2D::Triangle#ext_render */ #if MRUBY static R_VAL ruby2d_triangle_ext_render(mrb_state* mrb, R_VAL self) { #else @@ -346,22 +331,22 @@ mrb_value path; mrb_get_args(mrb, "o", &path); #else static R_VAL ruby2d_image_ext_init(R_VAL self, R_VAL path) { #endif - S2D_Log(S2D_INFO, "Init image: %s", RSTRING_PTR(path)); S2D_Image *img = S2D_CreateImage(RSTRING_PTR(path)); + if (!img) return R_FALSE; // Get width and height from Ruby class. If set, use it, else choose the // native dimensions of the image. R_VAL w = r_iv_get(self, "@width"); R_VAL h = r_iv_get(self, "@height"); r_iv_set(self, "@width" , r_test(w) ? w : INT2NUM(img->width)); r_iv_set(self, "@height", r_test(h) ? h : INT2NUM(img->height)); - r_iv_set(self, "@data", r_data_wrap_struct(image, img)); - return R_NIL; + + return R_TRUE; } /* * Ruby2D::Image#ext_render @@ -403,11 +388,10 @@ static void free_image(mrb_state *mrb, void *p_) { S2D_Image *img = (S2D_Image *)p_; #else static void free_image(S2D_Image *img) { #endif - S2D_Log(S2D_INFO, "Free image `%s` at %i, %i", img->path, img->x, img->y); S2D_FreeImage(img); } /* @@ -419,18 +403,18 @@ mrb_value path; mrb_get_args(mrb, "o", &path); #else static R_VAL ruby2d_sprite_ext_init(R_VAL self, R_VAL path) { #endif - S2D_Log(S2D_INFO, "Init sprite: %s", RSTRING_PTR(path)); S2D_Sprite *spr = S2D_CreateSprite(RSTRING_PTR(path)); + if (!spr) return R_FALSE; r_iv_set(self, "@img_width" , INT2NUM(spr->width)); r_iv_set(self, "@img_height", INT2NUM(spr->height)); r_iv_set(self, "@data", r_data_wrap_struct(sprite, spr)); - return R_NIL; + return R_TRUE; } /* * Ruby2D::Sprite#ext_render @@ -483,11 +467,10 @@ static void free_sprite(mrb_state *mrb, void *p_) { S2D_Sprite *spr = (S2D_Sprite *)p_; #else static void free_sprite(S2D_Sprite *spr) { #endif - S2D_Log(S2D_INFO, "Free sprite `%s` at %i, %i", spr->path, spr->x, spr->y); S2D_FreeSprite(spr); } /* @@ -497,12 +480,10 @@ #if MRUBY static R_VAL ruby2d_text_ext_init(mrb_state* mrb, R_VAL self) { #else static R_VAL ruby2d_text_ext_init(R_VAL self) { #endif - S2D_Log(S2D_INFO, "Init text: %s", RSTRING_PTR(r_iv_get(self, "@text"))); - // Trim the font file string to its actual length on MRuby #if MRUBY mrb_value s = r_iv_get(self, "@font"); mrb_str_resize(mrb, s, RSTRING_LEN(s)); #endif @@ -510,16 +491,17 @@ 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")) ); + if (!txt) return R_FALSE; r_iv_set(self, "@width", INT2NUM(txt->width)); r_iv_set(self, "@height", INT2NUM(txt->height)); - r_iv_set(self, "@data", r_data_wrap_struct(text, txt)); - return R_NIL; + + return R_TRUE; } /* * Ruby2D::Text#ext_set @@ -578,11 +560,10 @@ static void free_text(mrb_state *mrb, void *p_) { S2D_Text *txt = (S2D_Text *)p_; #else static void free_text(S2D_Text *txt) { #endif - S2D_Log(S2D_INFO, "Free text \"%s\" with font `%s`", txt->msg, txt->font); S2D_FreeText(txt); } /* @@ -594,14 +575,14 @@ mrb_value path; mrb_get_args(mrb, "o", &path); #else static R_VAL ruby2d_sound_ext_init(R_VAL self, R_VAL path) { #endif - S2D_Log(S2D_INFO, "Init sound: %s", RSTRING_PTR(path)); S2D_Sound *snd = S2D_CreateSound(RSTRING_PTR(path)); + if (!snd) return R_FALSE; r_iv_set(self, "@data", r_data_wrap_struct(sound, snd)); - return R_NIL; + return R_TRUE; } /* * Ruby2D::Sound#ext_play @@ -625,11 +606,10 @@ static void free_sound(mrb_state *mrb, void *p_) { S2D_Sound *snd = (S2D_Sound *)p_; #else static void free_sound(S2D_Sound *snd) { #endif - S2D_Log(S2D_INFO, "Free sound `%s`", snd->path); S2D_FreeSound(snd); } /* @@ -641,14 +621,14 @@ mrb_value path; mrb_get_args(mrb, "o", &path); #else static R_VAL ruby2d_music_ext_init(R_VAL self, R_VAL path) { #endif - S2D_Log(S2D_INFO, "Init music: %s", RSTRING_PTR(path)); S2D_Music *mus = S2D_CreateMusic(RSTRING_PTR(path)); + if (!mus) return R_FALSE; r_iv_set(self, "@data", r_data_wrap_struct(music, mus)); - return R_NIL; + return R_TRUE; } /* * Ruby2D::Music#ext_play @@ -753,11 +733,10 @@ static void free_music(mrb_state *mrb, void *p_) { S2D_Music *mus = (S2D_Music *)p_; #else static void free_music(S2D_Music *mus) { #endif - S2D_Log(S2D_INFO, "Free music `%s`", mus->path); S2D_FreeMusic(mus); } /* @@ -977,10 +956,26 @@ } } /* + * Ruby2D::Window#ext_diagnostics + */ +#if MRUBY +static R_VAL ruby2d_ext_diagnostics(mrb_state* mrb, R_VAL self) { + mrb_value enable; + mrb_get_args(mrb, "o", &enable); +#else +static R_VAL ruby2d_ext_diagnostics(R_VAL self, R_VAL enable) { +#endif + // Set Simple 2D diagnostics + S2D_Diagnostics(r_test(enable)); + return R_TRUE; +} + + +/* * Ruby2D::Window#ext_get_display_dimensions */ #if MRUBY static R_VAL ruby2d_window_ext_get_display_dimensions(mrb_state* mrb, R_VAL self) { #else @@ -1018,15 +1013,10 @@ #else static R_VAL ruby2d_window_ext_show(R_VAL self) { #endif ruby2d_window = self; - // Set Simple 2D diagnostics - if (r_test(r_iv_get(self, "@diagnostics"))) { - S2D_Diagnostics(true); - } - // Add controller mappings from file r_funcall(self, "add_controller_mappings", 0); // Get window attributes char *title = RSTRING_PTR(r_iv_get(self, "@title")); @@ -1082,10 +1072,30 @@ return R_NIL; } /* + * Ruby2D::Window#ext_screenshot + */ +#if MRUBY +static R_VAL ruby2d_ext_screenshot(mrb_state* mrb, R_VAL self) { + mrb_value path; + mrb_get_args(mrb, "o", &path); +#else +static R_VAL ruby2d_ext_screenshot(R_VAL self, R_VAL path) { +#endif + if (window) { + S2D_Screenshot(window, RSTRING_PTR(path)); + return path; + } else { + S2D_Log(S2D_WARN, "Cannot take screenshot before window is shown"); + return R_FALSE; + } +} + + +/* * Ruby2D::Window#ext_close */ static R_VAL ruby2d_window_ext_close() { S2D_Close(window); return R_NIL; @@ -1207,9 +1217,12 @@ // Ruby2D::Music#ext_fadeout r_define_method(ruby2d_music_class, "ext_fadeout", ruby2d_music_ext_fadeout, r_args_req(1)); // Ruby2D::Window R_CLASS ruby2d_window_class = r_define_class(ruby2d_module, "Window"); + + // Ruby2D::Window#ext_diagnostics + r_define_method(ruby2d_window_class, "ext_diagnostics", ruby2d_ext_diagnostics, r_args_req(1)); // Ruby2D::Window#ext_get_display_dimensions r_define_method(ruby2d_window_class, "ext_get_display_dimensions", ruby2d_window_ext_get_display_dimensions, r_args_none); // Ruby2D::Window#ext_add_controller_mappings