summaryrefslogtreecommitdiff
path: root/src/rectangle.cpp
diff options
context:
space:
mode:
authorFrederico Linhares <fred@linhares.blue>2022-10-27 13:56:08 -0300
committerFrederico Linhares <fred@linhares.blue>2022-10-27 13:56:08 -0300
commite5340023e3bc51bc699327dbca0d539b6ee3e188 (patch)
tree1dbe6684eda4f25fe83ccb2d2616d749e48413ea /src/rectangle.cpp
parente14be07f0fab02d8c7742fc0232d96b56bc32361 (diff)
feat Create rectangles
Diffstat (limited to 'src/rectangle.cpp')
-rw-r--r--src/rectangle.cpp224
1 files changed, 224 insertions, 0 deletions
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));
+}