diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core.cpp | 2 | ||||
-rw-r--r-- | src/rectangle.cpp | 224 | ||||
-rw-r--r-- | src/rectangle.hpp | 32 |
3 files changed, 258 insertions, 0 deletions
diff --git a/src/core.cpp b/src/core.cpp index 3a78af2..f3f0e14 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -21,6 +21,7 @@ #include "key.hpp" #include "mesh.hpp" #include "model.hpp" +#include "rectangle.hpp" #include "rotation_3d.hpp" #include "sound.hpp" #include "sprite.hpp" @@ -632,6 +633,7 @@ load_mruby_interface(void *obj) cg_key_init(cg_core.mrb); cg_mesh_init(cg_core.mrb); cg_model_init(cg_core.mrb); + cg_rectangle_init(cg_core.mrb); cg_rotation_3d_init(cg_core.mrb); cg_sound_init(cg_core.mrb); cg_sprite_init(cg_core.mrb); diff --git a/src/rectangle.cpp b/src/rectangle.cpp new file mode 100644 index 0000000..3bc0a84 --- /dev/null +++ b/src/rectangle.cpp @@ -0,0 +1,224 @@ +/* + * 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 "rectangle.hpp" + +namespace +{ + +constexpr bool +align_vertically(float a_x, float a_width, float b_x, float b_width) +{ + return a_x <= b_x + b_width && a_x + a_width >= b_x; +} + +constexpr bool +align_horizontally(float a_y, float a_height, float b_y, float b_height) +{ + return a_y <= b_y + b_height && a_y + a_height >= b_y; +} + +} + +void +cg_free_rectangle(mrb_state *mrb, void* obj) +{ + auto ptr = static_cast<cg_rectangle*>(obj); + + mrb_free(mrb, ptr); +} + +const struct mrb_data_type cg_rectangle_type = { + "CG_Rectangle", cg_free_rectangle }; + +static mrb_value +cg_cRectangle_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_float x, y, width, height; + struct cg_rectangle *ptr; + + mrb_get_args(mrb, "ffff", &x, &y, &width, &height); + ptr = (cg_rectangle *)DATA_PTR(self); + if(ptr) mrb_free(mrb, ptr); + ptr = (cg_rectangle *)mrb_malloc(mrb, sizeof(cg_rectangle)); + + ptr->x = x; + ptr->y = y; + ptr->width = width; + ptr->height = height; + + mrb_data_init(self, ptr, &cg_rectangle_type); + return self; +} + +static mrb_value +cg_cRectangle_get_x(mrb_state *mrb, mrb_value self) +{ + auto ptr = (cg_rectangle *)DATA_PTR(self); + + return mrb_float_value(mrb, ptr->x); +} + +static mrb_value +cg_cRectangle_set_x(mrb_state *mrb, mrb_value self) +{ + mrb_float x; + auto ptr = (cg_rectangle *)DATA_PTR(self); + + mrb_get_args(mrb, "f", &x); + + ptr->x = x; + + return mrb_float_value(mrb, ptr->x); +} + +static mrb_value +cg_cRectangle_get_y(mrb_state *mrb, mrb_value self) +{ + auto ptr = (cg_rectangle *)DATA_PTR(self); + + return mrb_float_value(mrb, ptr->y); +} + +static mrb_value +cg_cRectangle_set_y(mrb_state *mrb, mrb_value self) +{ + mrb_float y; + auto *ptr = (cg_rectangle *)DATA_PTR(self); + + mrb_get_args(mrb, "f", &y); + + ptr->y = y; + + return mrb_float_value(mrb, ptr->y); +} + +static mrb_value +cg_cRectangle_get_width(mrb_state *mrb, mrb_value self) +{ + auto *ptr = (cg_rectangle *)DATA_PTR(self); + + return mrb_float_value(mrb, ptr->width); +} + +static mrb_value +cg_cRectangle_set_width(mrb_state *mrb, mrb_value self) +{ + mrb_float width; + auto *ptr = (struct cg_rectangle *)DATA_PTR(self); + + mrb_get_args(mrb, "f", &width); + + ptr->width = width; + + return mrb_float_value(mrb, ptr->width); +} + +static mrb_value +cg_cRectangle_get_height(mrb_state *mrb, mrb_value self) +{ + auto *ptr = (struct cg_rectangle *)DATA_PTR(self); + + return mrb_float_value(mrb, ptr->height); +} + +static mrb_value +cg_cRectangle_set_height(mrb_state *mrb, mrb_value self) +{ + mrb_float height; + auto *ptr = (cg_rectangle *)DATA_PTR(self); + + mrb_get_args(mrb, "f", &height); + + ptr->height = height; + + return mrb_float_value(mrb, ptr->height); +} + +static mrb_value +cg_cRectangle_align_vertically(mrb_state *mrb, mrb_value self) +{ + cg_rectangle *ptr, *that; + + mrb_get_args(mrb, "d", &that, &cg_rectangle_type); + ptr = (cg_rectangle *)DATA_PTR(self); + + return mrb_bool_value( + align_vertically(ptr->x, ptr->width, that->x, that->width)); +} + +static mrb_value +cg_cRectangle_align_horizontally(mrb_state *mrb, mrb_value self) +{ + cg_rectangle *ptr, *that; + + mrb_get_args(mrb, "d", &that, &cg_rectangle_type); + ptr = (cg_rectangle *)DATA_PTR(self); + + return mrb_bool_value( + align_horizontally(ptr->y, ptr->height, that->y, that->height)); +} + +static mrb_value +cg_cRectangle_collide(mrb_state *mrb, mrb_value self) +{ + cg_rectangle *ptr, *that; + + mrb_get_args(mrb, "d", &that, &cg_rectangle_type); + ptr = (cg_rectangle *)DATA_PTR(self); + + return mrb_bool_value( + align_vertically(ptr->x, ptr->width, that->x, that->width) && + align_horizontally(ptr->y, ptr->height, that->y, that->height)); +} + +void +cg_rectangle_init(mrb_state *mrb) +{ + RClass *cg_m, *cg_cRectangle; + + cg_m = mrb_module_get(mrb, "CandyGear"); + cg_cRectangle = mrb_define_class_under( + mrb, cg_m, "Rectangle", mrb->object_class); + MRB_SET_INSTANCE_TT(cg_cRectangle, MRB_TT_DATA); + mrb_define_method( + mrb, cg_cRectangle, "initialize", cg_cRectangle_initialize, + MRB_ARGS_REQ(4)); + mrb_define_method( + mrb, cg_cRectangle, "x", cg_cRectangle_get_x, MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cRectangle, "x=", cg_cRectangle_set_x, MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cRectangle, "y", cg_cRectangle_get_y, MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cRectangle, "y=", cg_cRectangle_set_y, MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cRectangle, "width", cg_cRectangle_get_width, MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cRectangle, "width=", cg_cRectangle_set_width, MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cRectangle, "height", cg_cRectangle_get_height, MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cRectangle, "height=", cg_cRectangle_set_height, MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cRectangle, "align_vertically?", cg_cRectangle_align_vertically, + MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cRectangle, "align_horizontally?", + cg_cRectangle_align_horizontally, MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cRectangle, "collide?", cg_cRectangle_collide, MRB_ARGS_REQ(1)); +} diff --git a/src/rectangle.hpp b/src/rectangle.hpp new file mode 100644 index 0000000..e89b6ba --- /dev/null +++ b/src/rectangle.hpp @@ -0,0 +1,32 @@ +/* + * 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. + */ + +#ifndef CANDY_GEAR_RECTANGLE_H +#define CANDY_GEAR_RECTANGLE_H 1 + +#include "core.hpp" + +struct cg_rectangle +{ + float x, y, width, height; +}; + +extern const struct mrb_data_type cg_rectangle_type; + +void +cg_rectangle_init(mrb_state *mrb); + +#endif /* CANDY_GEAR_RECTANGLE_H */ |