From aaed2a48f4565862aed1694e68105f1a06ebd3c8 Mon Sep 17 00:00:00 2001 From: realtradam Date: Wed, 9 Feb 2022 06:48:37 -0500 Subject: rewrite of texture drawing methods --- src/textures.c | 155 +++++++++++++++++++-------------------------------------- 1 file changed, 51 insertions(+), 104 deletions(-) (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c index 3742257..4126c21 100644 --- a/src/textures.c +++ b/src/textures.c @@ -52,6 +52,7 @@ mrb_Texture_get_width(mrb_state* mrb, mrb_value self) { * @param width [Integer] The new width * @overload w=(width) * @param w [Integer] The new width + * @return [Integer] */ static mrb_value mrb_Texture_set_width(mrb_state* mrb, mrb_value self) { @@ -82,6 +83,7 @@ mrb_Texture_get_height(mrb_state* mrb, mrb_value self) { * @param height [Integer] The new height * @overload h=(height) * @param h [Integer] The new height + * @return [Integer] */ static mrb_value mrb_Texture_set_height(mrb_state* mrb, mrb_value self) { @@ -132,136 +134,83 @@ mrb_Texture_get_id(mrb_state* mrb, mrb_value self) { /* * Draw the texture - * @overload draw(x: 0, y: 0, tint: Rl::Color.white) - * @param x [Integer] x position of the drawn texture. - * @param y [Integer] y position of the drawn texture. - * @param tint [Integer] The color the drawn texture is tinted(white is no tint). - * @overload draw(source: Rl::Rectangle.new(0, 0, texture.width, texture.height), x: 0, y: 0, origin: Rl::Vector2.new(0,0), rotation: 0, tint: Rl::Color.white) + * @overload draw(source: Rl::Rectangle.new(0,0,texture.width,texture.height), dest: Rl::Rectangle.new(0,0,texture.width,texture.height), origin: Rl::Vector2.default, rotation: 0, tint: Rl::Color.white) + * @param tint [Color] The color the drawn texture is tinted(white is 'no tint'). * @param source [Rectangle] Which section of the texture is to be drawn, the default is the entire texture. - * @param origin [Vector2] Offset of the drawn texture. + * @param origin [Vector2] Offset of the drawn texture. Default is no offset. * @param rotation [Float] How much the texture is rotated when drawn(In radians). - * @overload draw(source: Rl::Rectangle.new(0,0,texture.width,texture.height), dest: Rl::Rectangle.new(0,0,texture.width,texture.height), origin: Rl::Vector2.new(0,0), rotation: 0, tint: Rl::Color.white) - * @param dest [Rectangle] Where the texture is to be drawn on the screen(This can scale the texture) + * @param dest [Rectangle] Where the texture is to be drawn on the screen(This can scale the texture). Default is the size of the texture. * @return [Nil] */ static mrb_value -mrb_draw_texture(mrb_state* mrb, mrb_value self) { - mrb_int x = 0; - mrb_int y = 0; - struct RClass *raylib = mrb_module_get(mrb, "Raylib"); - struct RClass *color = mrb_class_get_under(mrb, raylib, Color_type.struct_name); - mrb_value tint_obj = mrb_funcall(mrb, mrb_obj_value(color), "white", 0); +mrb_Texture_draw_texture(mrb_state* mrb, mrb_value self) { + struct RClass *raylib;// = mrb_module_get(mrb, "Raylib"); + Rectangle source = {0}; + Rectangle dest = {0}; + float rotation = 0.0; + Texture *texture_data; + UNWRAPSTRUCT(Texture, Texture_type, self, texture_data); + //mrb_value tint_obj = mrb_funcall(mrb, mrb_obj_value(color), "white", 0); //Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint uint32_t kw_num = 7; - const mrb_sym kw_names[] = { + const mrb_sym kw_names[] = { mrb_intern_lit(mrb, "source"), - mrb_intern_lit(mrb, "x"), - mrb_intern_lit(mrb, "y"), mrb_intern_lit(mrb, "dest"), mrb_intern_lit(mrb, "origin"), mrb_intern_lit(mrb, "rotation"), mrb_intern_lit(mrb, "tint"), - }; mrb_value kw_values[kw_num]; const mrb_kwargs kwargs = { kw_num, 0, kw_names, kw_values, NULL }; - mrb_get_args(mrb, "|iio:", &x, &y, &tint_obj, &kwargs); + mrb_get_args(mrb, "|:", &kwargs); - // if dest exists - if (!(mrb_undef_p(kw_values[3]))) { - //x = mrb_fixnum(mrb_ensure_int_type(mrb, kw_values[3])); - } - // use it - // else check if x/y exist - if (!(mrb_undef_p(kw_values[1]) && mrb_undef_p(kw_values[2]))) { - //x = mrb_fixnum(mrb_ensure_int_type(mrb, kw_values[1])); - } - // use them - // else - // use arg x/y - // end - // if source + // if source defined if (!(mrb_undef_p(kw_values[0]))) { - //x = mrb_fixnum(mrb_ensure_int_type(mrb, kw_values[0])); - } - // use it - // elsea - // use texture values - // end - // if tint exists - if (!(mrb_undef_p(kw_values[6]))) { - //x = mrb_fixnum(mrb_ensure_int_type(mrb, kw_values[6])); - } - // use it - // else - // use arg - // end - // if origin etc. - if (!(mrb_undef_p(kw_values[4]))) { - //x = mrb_fixnum(mrb_ensure_int_type(mrb, kw_values[0])); - } - // if rotation etc. - if (!(mrb_undef_p(kw_values[5]))) { - //x = mrb_fixnum(mrb_ensure_int_type(mrb, kw_values[0])); + Rectangle *temp_rec; + UNWRAPSTRUCT(Rectangle, Rectangle_type, kw_values[0], temp_rec); + source = *temp_rec; + } else { + source = (struct Rectangle){ 0, 0, texture_data->width, texture_data->height }; } - if (!(mrb_undef_p(kw_values[0]))) { - x = mrb_fixnum(mrb_ensure_int_type(mrb, kw_values[0])); - } + // if dest defined if (!(mrb_undef_p(kw_values[1]))) { - y = mrb_fixnum(mrb_ensure_int_type(mrb, kw_values[1])); - } - if (!(mrb_undef_p(kw_values[2]))) { - tint_obj = kw_values[2]; + Rectangle *temp_rec; + UNWRAPSTRUCT(Rectangle, Rectangle_type, kw_values[1], temp_rec); + dest = *temp_rec; + } else { + dest = (struct Rectangle){ 0, 0, texture_data->width, texture_data->height }; } + // if origin undefined + if ((mrb_undef_p(kw_values[2]))) { + raylib = mrb_module_get(mrb, "Raylib"); + struct RClass *vector2 = mrb_class_get_under(mrb, raylib, Vector2_type.struct_name); + kw_values[2] = mrb_funcall(mrb, mrb_obj_value(vector2), "default", 0); + } - Texture *texture_data; - UNWRAPSTRUCT(Texture, Texture_type, self, texture_data); - - Color *tint_data; - UNWRAPSTRUCT(Color, Color_type, tint_obj, tint_data); - - DrawTexture(*texture_data, x, y, *tint_data); - - return mrb_nil_value(); -} - -static mrb_value -mrb_draw_texture_ex(mrb_state* mrb, mrb_value self) { - mrb_value texture_obj; - mrb_value pos_obj; - mrb_float rotation; - mrb_float scale; - mrb_value tint_obj; - mrb_get_args(mrb, "ooffo", &texture_obj, &pos_obj, &rotation, &scale, &tint_obj); + // if rotation defined + if (!(mrb_undef_p(kw_values[3]))) { + rotation = mrb_as_float(mrb, kw_values[3]) / 0.017453; + } - Texture *texture_data = DATA_GET_PTR(mrb, texture_obj, &Texture_type, Texture); - Vector2 *pos_data = DATA_GET_PTR(mrb, pos_obj, &Vector2_type, Vector2); - Color *tint_data = DATA_GET_PTR(mrb, tint_obj, &Color_type, Color); + // if color undefined + if ((mrb_undef_p(kw_values[4]))) { + raylib = mrb_module_get(mrb, "Raylib"); // needs to be called again or else segfault + struct RClass *color = mrb_class_get_under(mrb, raylib, Color_type.struct_name); + kw_values[4] = mrb_funcall(mrb, mrb_obj_value(color), "white", 0); + } - DrawTextureEx(*texture_data, *pos_data, rotation, scale, *tint_data); - return mrb_nil_value(); -} -static mrb_value -mrb_draw_texture_pro(mrb_state* mrb, mrb_value self) { - mrb_value texture_obj; - mrb_value pos_obj; - mrb_value source_rec_obj; - mrb_value dest_rec_obj; - mrb_float rotation; - mrb_value tint_obj; - mrb_get_args(mrb, "oooofo", &texture_obj, &source_rec_obj, &dest_rec_obj, &pos_obj, &rotation, &tint_obj); + Color *tint_data; + UNWRAPSTRUCT(Color, Color_type, kw_values[4], tint_data); - Texture *texture_data = DATA_GET_PTR(mrb, texture_obj, &Texture_type, Texture); - Vector2 *pos_data = DATA_GET_PTR(mrb, pos_obj, &Vector2_type, Vector2); - Rectangle *source_rec_data = DATA_GET_PTR(mrb, source_rec_obj, &Rectangle_type, Rectangle); - Rectangle *dest_rec_data = DATA_GET_PTR(mrb, dest_rec_obj, &Rectangle_type, Rectangle); - Color *tint_data = DATA_GET_PTR(mrb, tint_obj, &Color_type, Color); + Vector2 *vector2_data; + UNWRAPSTRUCT(Vector2, Vector2_type, kw_values[2], vector2_data); - DrawTexturePro(*texture_data, *source_rec_data, *dest_rec_data, *pos_data, rotation, *tint_data); + //Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint + DrawTexturePro(*texture_data, source, dest, *vector2_data, rotation, *tint_data); return mrb_nil_value(); } @@ -284,8 +233,6 @@ mrb_init_raylib_textures(mrb_state* mrb) { mrb_define_method(mrb, texture_class, "id", mrb_Texture_get_id, MRB_ARGS_NONE()); //mrb_define_method(mrb, texture_class, "mipmaps", mrb_Texture_get_mipmaps, MRB_ARGS_NONE()); //mrb_define_method(mrb, texture_class, "format", mrb_Texture_get_format, MRB_ARGS_NONE()); - mrb_define_module_function(mrb, texture_class, "draw", mrb_draw_texture, MRB_ARGS_OPT(3)); - mrb_define_module_function(mrb, raylib, "_draw_texture_ex", mrb_draw_texture_ex, MRB_ARGS_OPT(4)); - mrb_define_module_function(mrb, raylib, "_draw_texture_pro", mrb_draw_texture_pro, MRB_ARGS_OPT(5)); + mrb_define_method(mrb, texture_class, "draw", mrb_Texture_draw_texture, MRB_ARGS_OPT(3)); } -- cgit v1.2.3