diff options
| author | realtradam <[email protected]> | 2022-02-07 05:11:26 -0500 |
|---|---|---|
| committer | realtradam <[email protected]> | 2022-02-07 05:11:26 -0500 |
| commit | 187cab9f77a6274ba4e5bb9c012fca5549c020fb (patch) | |
| tree | aa8c02849eb23524b1e785fb812dc0c14db0efc5 /src | |
| parent | 2a795c7b474025b71155f8d330bfebdc52d18451 (diff) | |
| download | mruby-raylib-187cab9f77a6274ba4e5bb9c012fca5549c020fb.tar.gz mruby-raylib-187cab9f77a6274ba4e5bb9c012fca5549c020fb.zip | |
reworked some more functions
Diffstat (limited to 'src')
| -rw-r--r-- | src/core.c | 83 | ||||
| -rw-r--r-- | src/raylib.c | 50 |
2 files changed, 93 insertions, 40 deletions
@@ -4,6 +4,9 @@ const struct mrb_data_type Color_type = { "Color", mrb_free }; +const struct mrb_data_type Rectangle_type = { + "Rectangle", mrb_free +}; /* * @overload init_window(width: 800, height: 600, title: "Hello World from Raylib!") @@ -72,17 +75,93 @@ mrb_clear_background(mrb_state* mrb, mrb_value self) { const mrb_kwargs kwargs = { kw_num, 0, kw_names, kw_values, NULL }; mrb_get_args(mrb, "|o:", &color_obj, &kwargs); + if (mrb_undef_p(kw_values[0])) { + kw_values[0] = color_obj; + } - Color *color_data = DATA_GET_PTR(mrb, color_obj, &Color_type, Color); + Color *color_data = DATA_GET_PTR(mrb, kw_values[0], &Color_type, Color); ClearBackground(*color_data); return mrb_nil_value(); } +/* + * Setup canvas (framebuffer) to start drawing + * @return [Nil] + */ +static mrb_value +mrb_begin_drawing(mrb_state* mrb, mrb_value self) { + BeginDrawing(); + return mrb_nil_value(); +} + +/* + * End canvas drawing and swap buffers (double buffering) + * @return [Nil] + */ +static mrb_value +mrb_end_drawing(mrb_state* mrb, mrb_value self) { + EndDrawing(); + return mrb_nil_value(); +} +/* + * Begin scissor mode (define screen area for following drawing) + * @overload begin_scissor_mode(x: 0, y: 0, width: 10, height: 10) + * @param x [Integer] + * @param y [Integer] + * @param width [Integer] + * @param height [Integer] + * @return [Nil] + */ +static mrb_value +mrb_begin_scissor_mode(mrb_state* mrb, mrb_value self) { + mrb_int x = 0; + mrb_int y = 0; + mrb_int width = 10; + mrb_int height = 10; + //mrb_get_args(mrb, "iiii", &x, &y, &width, &height); + + + uint32_t kw_num = 4; + const mrb_sym kw_names[] = { + mrb_intern_lit(mrb, "x"), + mrb_intern_lit(mrb, "y"), + mrb_intern_lit(mrb, "width"), + mrb_intern_lit(mrb, "height"), + }; + mrb_value kw_values[kw_num]; + const mrb_kwargs kwargs = { kw_num, 0, kw_names, kw_values, NULL }; + mrb_get_args(mrb, "|iiii:", &x, &y, &width, &height, &kwargs); + + if (mrb_undef_p(kw_values[0]) && mrb_undef_p(kw_values[1]) && mrb_undef_p(kw_values[2]) && mrb_undef_p(kw_values[3])) { + // pass params to kwargs + kw_values[0] = mrb_fixnum_value(x); + kw_values[1] = mrb_fixnum_value(y); + kw_values[2] = mrb_fixnum_value(width); + kw_values[3] = mrb_fixnum_value(height); + } + BeginScissorMode(mrb_fixnum(kw_values[0]), mrb_fixnum(kw_values[1]), mrb_fixnum(kw_values[2]), mrb_fixnum(kw_values[3])); + return mrb_nil_value(); +} + +/* + * End scissor mode + * @overload end_scissor_mode + * @return [Nil] + */ +static mrb_value +mrb_end_scissor_mode(mrb_state* mrb, mrb_value self) { + EndScissorMode(); + return mrb_nil_value(); +} void mrb_init_raylib_core(mrb_state* mrb) { struct RClass *raylib = mrb_define_module(mrb, "Raylib"); mrb_define_module_function(mrb, raylib, "init_window", mrb_init_window, MRB_ARGS_OPT(3)); mrb_define_module_function(mrb, raylib, "window_should_close?", mrb_window_should_close, MRB_ARGS_NONE()); - mrb_define_module_function(mrb, raylib, "clear_background", mrb_clear_background, MRB_ARGS_REQ(1)); + mrb_define_module_function(mrb, raylib, "clear_background", mrb_clear_background, MRB_ARGS_OPT(1)); + mrb_define_module_function(mrb, raylib, "begin_drawing", mrb_begin_drawing, MRB_ARGS_NONE()); + mrb_define_module_function(mrb, raylib, "end_drawing", mrb_end_drawing, MRB_ARGS_NONE()); + mrb_define_module_function(mrb, raylib, "begin_scissor_mode", mrb_begin_scissor_mode, MRB_ARGS_REQ(4)); + mrb_define_module_function(mrb, raylib, "end_scissor_mode", mrb_end_scissor_mode, MRB_ARGS_NONE()); } diff --git a/src/raylib.c b/src/raylib.c index 2da256b..dd69f96 100644 --- a/src/raylib.c +++ b/src/raylib.c @@ -47,9 +47,6 @@ static const struct mrb_data_type Vector2_type = { "Vector2", mrb_free }; -static const struct mrb_data_type Rectangle_type = { - "Rectangle", mrb_free -}; static const struct mrb_data_type NPatchInfo_type = { "NPatchInfo", mrb_free @@ -567,24 +564,6 @@ mrb_draw_texture_npatch(mrb_state* mrb, mrb_value self) { } static mrb_value -mrb_begin_scissor_mode(mrb_state* mrb, mrb_value self) { - mrb_int x; - mrb_int y; - mrb_int width; - mrb_int height; - mrb_get_args(mrb, "iiii", &x, &y, &width, &height); - - BeginScissorMode(x, y, width, height); - return mrb_nil_value(); -} - -static mrb_value -mrb_end_scissor_mode(mrb_state* mrb, mrb_value self) { - EndScissorMode(); - return mrb_nil_value(); -} - -static mrb_value mrb_begin_blend_mode(mrb_state* mrb, mrb_value self) { mrb_int mode; mrb_get_args(mrb, "i", &mode); @@ -608,6 +587,7 @@ mrb_Color_initialize(mrb_state* mrb, mrb_value self) { mrb_int a = 255; mrb_get_args(mrb, "|iiii", &r, &g, &b, &a); + Color *color; WRAPSTRUCT(Color, Color_type, self, color); color->r = r; @@ -621,12 +601,14 @@ mrb_Color_initialize(mrb_state* mrb, mrb_value self) { static mrb_value mrb_Color_get_red(mrb_state* mrb, mrb_value self) { + Color *color; UNWRAPSTRUCT(Color, Color_type, self, color); return mrb_fixnum_value(color->r); } static mrb_value mrb_Color_set_red(mrb_state* mrb, mrb_value self) { + Color *color; UNWRAPSTRUCT(Color, Color_type, self, color); mrb_int r; mrb_get_args(mrb, "i", &r); @@ -637,12 +619,14 @@ mrb_Color_set_red(mrb_state* mrb, mrb_value self) { static mrb_value mrb_Color_get_green(mrb_state* mrb, mrb_value self) { + Color *color; UNWRAPSTRUCT(Color, Color_type, self, color); return mrb_fixnum_value(color->g); } static mrb_value mrb_Color_set_green(mrb_state* mrb, mrb_value self) { + Color *color; UNWRAPSTRUCT(Color, Color_type, self, color); mrb_int g; mrb_get_args(mrb, "i", &g); @@ -653,6 +637,7 @@ mrb_Color_set_green(mrb_state* mrb, mrb_value self) { static mrb_value mrb_Color_get_blue(mrb_state* mrb, mrb_value self) { + Color *color; UNWRAPSTRUCT(Color, Color_type, self, color); return mrb_fixnum_value(color->b); @@ -660,6 +645,7 @@ mrb_Color_get_blue(mrb_state* mrb, mrb_value self) { static mrb_value mrb_Color_set_blue(mrb_state* mrb, mrb_value self) { + Color *color; UNWRAPSTRUCT(Color, Color_type, self, color); mrb_int b; mrb_get_args(mrb, "i", &b); @@ -670,12 +656,14 @@ mrb_Color_set_blue(mrb_state* mrb, mrb_value self) { static mrb_value mrb_Color_get_alpha(mrb_state* mrb, mrb_value self) { + Color *color; UNWRAPSTRUCT(Color, Color_type, self, color); return mrb_fixnum_value(color->a); } static mrb_value mrb_Color_set_alpha(mrb_state* mrb, mrb_value self) { + Color *color; UNWRAPSTRUCT(Color, Color_type, self, color); mrb_int a; mrb_get_args(mrb, "i", &a); @@ -832,24 +820,12 @@ mrb_draw_text(mrb_state* mrb, mrb_value self) { mrb_get_args(mrb, "|ziiio", &text, &x, &y, &fontSize, &color_obj); + Color *color; UNWRAPSTRUCT(Color, Color_type, color_obj, color); DrawText(text, x, y, fontSize, *color); return mrb_nil_value(); } -static mrb_value -mrb_begin_drawing(mrb_state* mrb, mrb_value self) { - BeginDrawing(); - return mrb_nil_value(); -} - -static mrb_value -mrb_end_drawing(mrb_state* mrb, mrb_value self) { - EndDrawing(); - return mrb_nil_value(); -} - - static mrb_value mrb_call_main_loop(mrb_state* mrb, mrb_value self) { struct RClass *c = mrb_module_get(mrb, "Raylib"); @@ -976,6 +952,7 @@ mrb_Rectangle_draw_rectangle_rec(mrb_state* mrb, mrb_value self) { mrb_value color_obj; mrb_get_args(mrb, "o", &color_obj); + Color *color; UNWRAPSTRUCT(Color, Color_type, color_obj, color); Rectangle *rec_self = DATA_GET_PTR(mrb, self, &Rectangle_type, Rectangle); DrawRectangleRec(*rec_self, *color); @@ -989,6 +966,7 @@ mrb_Rectangle_draw_rectangle_lines_ex(mrb_state* mrb, mrb_value self) { mrb_float line_thick; mrb_get_args(mrb, "fo", &line_thick, &color_obj); + Color *color; UNWRAPSTRUCT(Color, Color_type, color_obj, color); Rectangle *rec_self = DATA_GET_PTR(mrb, self, &Rectangle_type, Rectangle); DrawRectangleLinesEx(*rec_self, line_thick, *color); @@ -1003,8 +981,6 @@ mrb_mruby_raylib_gem_init(mrb_state* mrb) { struct RClass *raylib = mrb_define_module(mrb, "Raylib"); mrb_define_module_function(mrb, raylib, "platform", mrb_platform, MRB_ARGS_NONE()); mrb_define_module_function(mrb, raylib, "_draw_text", mrb_draw_text, MRB_ARGS_OPT(5)); - mrb_define_module_function(mrb, raylib, "begin_drawing", mrb_begin_drawing, MRB_ARGS_NONE()); - mrb_define_module_function(mrb, raylib, "end_drawing", mrb_end_drawing, MRB_ARGS_NONE()); mrb_define_module_function(mrb, raylib, "call_main_loop", mrb_call_main_loop, MRB_ARGS_NONE()); mrb_define_module_function(mrb, raylib, "target_fps=", mrb_target_fps, MRB_ARGS_REQ(1)); mrb_define_module_function(mrb, raylib, "fps", mrb_fps, MRB_ARGS_NONE()); @@ -1026,8 +1002,6 @@ mrb_mruby_raylib_gem_init(mrb_state* mrb) { mrb_define_module_function(mrb, raylib, "mouse_y", mrb_get_mouse_y, MRB_ARGS_NONE()); mrb_define_module_function(mrb, raylib, "mouse_position", mrb_get_mouse_position, MRB_ARGS_NONE()); mrb_define_module_function(mrb, raylib, "mouse_wheel", mrb_get_mouse_wheel_move, MRB_ARGS_NONE()); - mrb_define_module_function(mrb, raylib, "begin_scissor_mode", mrb_begin_scissor_mode, MRB_ARGS_REQ(4)); - mrb_define_module_function(mrb, raylib, "end_scissor_mode", mrb_end_scissor_mode, MRB_ARGS_NONE()); mrb_define_module_function(mrb, raylib, "begin_blend_mode", mrb_begin_blend_mode, MRB_ARGS_REQ(1)); mrb_define_module_function(mrb, raylib, "end_blend_mode", mrb_end_blend_mode, MRB_ARGS_NONE()); |
