diff options
| author | realtradam <[email protected]> | 2022-02-22 13:26:34 -0500 |
|---|---|---|
| committer | realtradam <[email protected]> | 2022-02-22 13:26:34 -0500 |
| commit | 3a559960526ee7a9c59bfdef48699cf4880e1e7d (patch) | |
| tree | ac1d2021b54b822a52b4f3ef516e47ae9b9be22f /src | |
| parent | e06bb3f7ee54e67a5e2e54bdb0eeb886e52afe3c (diff) | |
| download | mruby-raylib-3a559960526ee7a9c59bfdef48699cf4880e1e7d.tar.gz mruby-raylib-3a559960526ee7a9c59bfdef48699cf4880e1e7d.zip | |
method for drawing polygonsdevelopment
Diffstat (limited to 'src')
| -rw-r--r-- | src/raylib.c | 117 | ||||
| -rw-r--r-- | src/text.c | 57 | ||||
| -rw-r--r-- | src/types.c | 10 |
3 files changed, 182 insertions, 2 deletions
diff --git a/src/raylib.c b/src/raylib.c index c293a16..2ab20d9 100644 --- a/src/raylib.c +++ b/src/raylib.c @@ -782,6 +782,121 @@ mrb_Rectangle_get_collision_rec(mrb_state* mrb, mrb_value self) { } static mrb_value +mrb_draw_poly(mrb_state* mrb, mrb_value self) { + // void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color color); // Draw a regular polygon (Vector version) + Vector2 center = {0}; + int sides = 3; + float radius = 100; + float rotation = 0; + Color color = WHITE; + + uint32_t kw_num = 5; + const mrb_sym kw_names[] = { + mrb_intern_lit(mrb, "center"), + mrb_intern_lit(mrb, "sides"), + mrb_intern_lit(mrb, "radius"), + mrb_intern_lit(mrb, "rotation"), + mrb_intern_lit(mrb, "color"), + }; + mrb_value kw_values[kw_num]; + const mrb_kwargs kwargs = { kw_num, 0, kw_names, kw_values, NULL }; + mrb_get_args(mrb, "|:", &kwargs); + + //center + if (!(mrb_undef_p(kw_values[0]))) { + Vector2 *vector2_data; + UNWRAPSTRUCT(Vector2, Vector2_type, kw_values[0], vector2_data); + center = *vector2_data; + } + + // sides + if (!(mrb_undef_p(kw_values[1]))) { + sides = mrb_as_int(mrb, kw_values[1]); + } + + // radius + if (!(mrb_undef_p(kw_values[2]))) { + radius = mrb_as_float(mrb, kw_values[2]); + } + + // rotation + if (!mrb_undef_p(kw_values[3])) { + rotation = mrb_as_float(mrb, kw_values[3]) / 0.017453; + } + + // color + if (!mrb_undef_p(kw_values[4])) { + Color *color_data; + UNWRAPSTRUCT(Color, Color_type, kw_values[4], color_data); + color = *color_data; + } + + DrawPoly(center, sides, radius, rotation, color); + return mrb_nil_value(); +} + +static mrb_value +mrb_draw_poly_lines(mrb_state* mrb, mrb_value self) { + // void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color color); // Draw a regular polygon (Vector version) + Vector2 center = {0}; + int sides = 3; + float radius = 100; + float rotation = 0; + float line_thickness = 1; + Color color = WHITE; + + uint32_t kw_num = 6; + const mrb_sym kw_names[] = { + mrb_intern_lit(mrb, "center"), + mrb_intern_lit(mrb, "sides"), + mrb_intern_lit(mrb, "radius"), + mrb_intern_lit(mrb, "rotation"), + mrb_intern_lit(mrb, "line_thickness"), + mrb_intern_lit(mrb, "color"), + }; + mrb_value kw_values[kw_num]; + const mrb_kwargs kwargs = { kw_num, 0, kw_names, kw_values, NULL }; + mrb_get_args(mrb, "|:", &kwargs); + + //center + if (!(mrb_undef_p(kw_values[0]))) { + Vector2 *vector2_data; + UNWRAPSTRUCT(Vector2, Vector2_type, kw_values[0], vector2_data); + center = *vector2_data; + } + + // sides + if (!(mrb_undef_p(kw_values[1]))) { + sides = mrb_as_int(mrb, kw_values[1]); + } + + // radius + if (!(mrb_undef_p(kw_values[2]))) { + radius = mrb_as_float(mrb, kw_values[2]); + } + + // rotation + if (!mrb_undef_p(kw_values[3])) { + rotation = mrb_as_float(mrb, kw_values[3]) / 0.017453; + } + + // line_thickness + if (!mrb_undef_p(kw_values[4])) { + line_thickness = mrb_as_float(mrb, kw_values[4]); + } + + // color + if (!mrb_undef_p(kw_values[5])) { + Color *color_data; + UNWRAPSTRUCT(Color, Color_type, kw_values[5], color_data); + color = *color_data; + } + + DrawPolyLinesEx(center, sides, radius, rotation, line_thickness, color); + return mrb_nil_value(); +} + +static mrb_value mrb_Rectangle_draw_rectangle_rec(mrb_state* mrb, mrb_value self) { mrb_value color_obj; mrb_get_args(mrb, "o", &color_obj); @@ -909,6 +1024,8 @@ mrb_mruby_raylib_gem_init(mrb_state* mrb) { mrb_define_method(mrb, rectangle_class, "collide_with_point?", mrb_Rectangle_collide_with_point, MRB_ARGS_REQ(1)); mrb_define_method(mrb, rectangle_class, "_draw", mrb_Rectangle_draw_rectangle_rec, MRB_ARGS_REQ(1)); mrb_define_method(mrb, rectangle_class, "_draw_lines", mrb_Rectangle_draw_rectangle_lines_ex, MRB_ARGS_REQ(2)); + mrb_define_module_function(mrb, raylib, "draw_poly", mrb_draw_poly, MRB_ARGS_OPT(1)); + mrb_define_module_function(mrb, raylib, "draw_poly_lines", mrb_draw_poly_lines, MRB_ARGS_OPT(1)); struct RClass *circle_class = mrb_define_class_under(mrb, raylib, "Circle", mrb->object_class); mrb_define_method(mrb, circle_class, "collide_with_rec?", mrb_Circle_collide_with_rec, MRB_ARGS_REQ(1)); @@ -7,8 +7,51 @@ #include "mruby-raylib/text.h" #include "mruby-raylib/types.h" #include <raylib.h> +#include <mruby/class.h> +/* Create a new texture. + * @overload initialize(path:) + * @param path [String] File path to the texture to be loaded + * @return [Texture] + */ +static mrb_value +mrb_Font_initialize(mrb_state* mrb, mrb_value self) { + char* path; + + uint32_t kw_num = 2; + const mrb_sym kw_names[] = { + mrb_intern_lit(mrb, "path"), + mrb_intern_lit(mrb, "font_size"), + }; + mrb_value kw_values[kw_num]; + const mrb_kwargs kwargs = { kw_num, 0, kw_names, kw_values, NULL }; + mrb_get_args(mrb, "|:", &kwargs); + + printf("wrapping struct"); + fflush(stdout); + Font *font; + WRAPSTRUCT(Font, Font_type, self, font); + + if(mrb_undef_p(kw_values[0])) { + *font = GetFontDefault(); + } else { + path = mrb_str_to_cstr(mrb, kw_values[0]); + if(mrb_undef_p(kw_values[1])) { + printf("default size"); + fflush(stdout); + *font = LoadFont(path); + } else { + printf("custom size"); + fflush(stdout); + *font = LoadFontEx(path, mrb_as_int(mrb, kw_values[1]), NULL, 95); + } + } + + mrb_data_init(self, font, &Font_type); + return self; +} + /* * Draw the string as text on the screen. * @overload draw(x: 0, y: 0, origin: Rl::Vector2.default, rotation: 0, font_size: 20, spacing: font_size/10, font: Rl::Font.default) @@ -45,6 +88,14 @@ mrb_String_draw_text(mrb_state* mrb, mrb_value self) { const mrb_kwargs kwargs = { kw_num, 0, kw_names, kw_values, NULL }; mrb_get_args(mrb, "|:", &kwargs); + if (!mrb_undef_p(kw_values[0])){ + + Font *tmp_font; + UNWRAPSTRUCT(Font, Font_type, kw_values[0], tmp_font); + + default_font = *tmp_font; + } + // x if (!mrb_undef_p(kw_values[1])) { x = mrb_as_int(mrb, kw_values[1]); @@ -128,7 +179,7 @@ mrb_Raylib_draw_fps(mrb_state* mrb, mrb_value self) { } DrawFPS(x, y); - mrb_nil_value(); + return mrb_nil_value(); } @@ -137,7 +188,9 @@ mrb_init_raylib_text(mrb_state* mrb) { struct RClass *raylib = mrb_define_module(mrb, "Raylib"); struct RClass *string_extension = mrb_define_module_under(mrb, raylib, "StringExtension"); struct RClass *font_class = mrb_define_class_under(mrb, raylib, "Font", mrb->object_class); + MRB_SET_INSTANCE_TT(font_class, MRB_TT_DATA); + mrb_define_method(mrb, font_class, "initialize", mrb_Font_initialize, MRB_ARGS_OPT(1)); mrb_define_method(mrb, string_extension, "draw", mrb_String_draw_text, MRB_ARGS_OPT(1)); - mrb_define_module_function(mrb, raylib, "draw_fps", mrb_Raylib_draw_fps, MRB_ARGS_NONE()); + mrb_define_module_function(mrb, raylib, "draw_fps", mrb_Raylib_draw_fps, MRB_ARGS_OPT(1)); } diff --git a/src/types.c b/src/types.c index c1b3e34..8889f8a 100644 --- a/src/types.c +++ b/src/types.c @@ -50,3 +50,13 @@ const struct mrb_data_type NPatchInfo_type = { "NPatchInfo", mrb_free }; +const struct mrb_data_type Font_type = { + "Font", helper_font_free +}; + +void +helper_font_free(mrb_state* mrb, void*ptr) { + Font *font = (Font*)ptr; + UnloadFont(*font); + mrb_free(mrb, ptr); +} |
