From 418f45f78bbf743021c53030da2ad2158fa355d2 Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Tue, 29 Mar 2022 11:51:11 -0300 Subject: Initial commit --- src/rect.c | 209 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 src/rect.c (limited to 'src/rect.c') diff --git a/src/rect.c b/src/rect.c new file mode 100644 index 0000000..206ab45 --- /dev/null +++ b/src/rect.c @@ -0,0 +1,209 @@ +/* + * Copyright 2022 Frederico de Oliveira Linhares + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "rect.h" + +void +cg_free_rect(mrb_state *mrb, void* obj) +{ + struct cg_rect *ptr = obj; + + mrb_free(mrb, ptr); +} + +const struct mrb_data_type cg_rect_type = { + "CG_Rect", cg_free_rect }; + +static mrb_value +cg_cRect_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_int x, y, width, height; + struct cg_rect *ptr; + + mrb_get_args(mrb, "iiii", &x, &y, &width, &height); + ptr = (struct cg_rect *)DATA_PTR(self); + if(ptr) mrb_free(mrb, ptr); + ptr = (struct cg_rect *)mrb_malloc(mrb, sizeof(struct cg_rect)); + + ptr->rect.x = x; + ptr->rect.y = y; + ptr->rect.w = width; + ptr->rect.h = height; + + mrb_data_init(self, ptr, &cg_rect_type); + return self; +} + +static mrb_value +cg_cRect_get_x(mrb_state *mrb, mrb_value self) +{ + struct cg_rect *ptr; + + ptr = (struct cg_rect *)DATA_PTR(self); + + return mrb_int_value(mrb, ptr->rect.x); +} + +static mrb_value +cg_cRect_set_x(mrb_state *mrb, mrb_value self) +{ + mrb_int x; + struct cg_rect *ptr; + + mrb_get_args(mrb, "i", &x); + ptr = (struct cg_rect *)DATA_PTR(self); + + ptr->rect.x = x; + + return mrb_int_value(mrb, ptr->rect.x); +} + +static mrb_value +cg_cRect_get_y(mrb_state *mrb, mrb_value self) +{ + struct cg_rect *ptr; + + ptr = (struct cg_rect *)DATA_PTR(self); + + return mrb_int_value(mrb, ptr->rect.y); +} + +static mrb_value +cg_cRect_set_y(mrb_state *mrb, mrb_value self) +{ + mrb_int y; + struct cg_rect *ptr; + + mrb_get_args(mrb, "i", &y); + ptr = (struct cg_rect *)DATA_PTR(self); + + ptr->rect.y = y; + + return mrb_int_value(mrb, ptr->rect.y); +} + +static mrb_value +cg_cRect_get_width(mrb_state *mrb, mrb_value self) +{ + struct cg_rect *ptr; + + ptr = (struct cg_rect *)DATA_PTR(self); + + return mrb_int_value(mrb, ptr->rect.w); +} + +static mrb_value +cg_cRect_set_width(mrb_state *mrb, mrb_value self) +{ + mrb_int width; + struct cg_rect *ptr; + + mrb_get_args(mrb, "i", &width); + ptr = (struct cg_rect *)DATA_PTR(self); + + ptr->rect.w = width; + + return mrb_int_value(mrb, ptr->rect.w); +} + +static mrb_value +cg_cRect_get_height(mrb_state *mrb, mrb_value self) +{ + struct cg_rect *ptr; + + ptr = (struct cg_rect *)DATA_PTR(self); + + return mrb_int_value(mrb, ptr->rect.h); +} + +static mrb_value +cg_cRect_set_height(mrb_state *mrb, mrb_value self) +{ + mrb_int height; + struct cg_rect *ptr; + + mrb_get_args(mrb, "i", &height); + ptr = (struct cg_rect *)DATA_PTR(self); + + ptr->rect.h = height; + + return mrb_int_value(mrb, ptr->rect.h); +} + +static mrb_value +cg_cRect_draw_lines(mrb_state *mrb, mrb_value self) +{ + struct cg_rect *ptr; + ptr = (struct cg_rect *)DATA_PTR(self); + SDL_RenderDrawRect(cg_core.renderer, &ptr->rect); + + return self; +} + +static mrb_value +cg_cRect_draw_fill(mrb_state *mrb, mrb_value self) +{ + struct cg_rect *ptr; + ptr = (struct cg_rect *)DATA_PTR(self); + SDL_RenderFillRect(cg_core.renderer, &ptr->rect); + + return self; +} + +static mrb_value +cg_cRect_collide(mrb_state *mrb, mrb_value self) +{ + struct cg_rect *ptr, *that; + + mrb_get_args(mrb, "d", &that, &cg_rect_type); + ptr = (struct cg_rect *)DATA_PTR(self); + + return mrb_bool_value( + ptr->rect.x <= that->rect.x + that->rect.w && + ptr->rect.x + ptr->rect.w >= that->rect.x && + ptr->rect.y <= that->rect.y + that->rect.h && + ptr->rect.y + ptr->rect.h >= that->rect.y); +} + +void +cg_rect_init(mrb_state *mrb) +{ + struct RClass *cg_m, *cg_cRect; + + cg_m = mrb_module_get(mrb, "CandyGear"); + cg_cRect = mrb_define_class_under(mrb, cg_m, "Rect", mrb->object_class); + MRB_SET_INSTANCE_TT(cg_cRect, MRB_TT_DATA); + mrb_define_method( + mrb, cg_cRect, "initialize", cg_cRect_initialize, MRB_ARGS_REQ(4)); + mrb_define_method(mrb, cg_cRect, "x", cg_cRect_get_x, MRB_ARGS_NONE()); + mrb_define_method(mrb, cg_cRect, "x=", cg_cRect_set_x, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, cg_cRect, "y", cg_cRect_get_y, MRB_ARGS_NONE()); + mrb_define_method(mrb, cg_cRect, "y=", cg_cRect_set_y, MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cRect, "width", cg_cRect_get_width, MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cRect, "width=", cg_cRect_set_width, MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cRect, "height", cg_cRect_get_height, MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cRect, "height=", cg_cRect_set_height, MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cRect, "draw_lines", cg_cRect_draw_lines, MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cRect, "draw_fill", cg_cRect_draw_fill, MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cRect, "collide?", cg_cRect_collide, MRB_ARGS_REQ(1)); +} -- cgit v1.2.3