summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.cpp2
-rw-r--r--src/sprite.cpp24
-rw-r--r--src/vector_4d.cpp190
-rw-r--r--src/vector_4d.hpp27
-rw-r--r--test/src/main.rb8
5 files changed, 236 insertions, 15 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 120a7ed..590ea89 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -26,6 +26,7 @@
#include "sprite.hpp"
#include "texture.hpp"
#include "vector_3d.hpp"
+#include "vector_4d.hpp"
cg_sCore cg_core;
@@ -67,6 +68,7 @@ int main(int argc, char *argv[])
cg_sprite_init(cg_core.mrb);
cg_texture_init(cg_core.mrb);
cg_vector_3d_init(cg_core.mrb);
+ cg_vector_4d_init(cg_core.mrb);
main_obj = mrb_obj_iv_inspect(cg_core.mrb, cg_core.mrb->top_self);
sym_init = mrb_intern_cstr(cg_core.mrb, "init");
diff --git a/src/sprite.cpp b/src/sprite.cpp
index 2aad6d6..7c1f109 100644
--- a/src/sprite.cpp
+++ b/src/sprite.cpp
@@ -16,8 +16,9 @@
#include "sprite.hpp"
-#include "vk/sprite.hpp"
#include "texture.hpp"
+#include "vk/sprite.hpp"
+#include "vector_4d.hpp"
void
cg_free_sprite(mrb_state *mrb, void* obj)
@@ -33,20 +34,19 @@ const struct mrb_data_type cg_sprite_type = { "CG_Sprite", cg_free_sprite };
static mrb_value
cg_cSprite_initialize(mrb_state *mrb, mrb_value self)
{
- mrb_float x, y, w, h;
-
+ std::shared_ptr<glm::vec4> *vector_4d;
std::shared_ptr<VK::Texture> *texture;
std::shared_ptr<VK::Sprite> *ptr;
- mrb_get_args(mrb, "dffff", &texture, &cg_texture_type, &x, &y, &w, &h);
+ mrb_get_args(
+ mrb, "dd", &texture, &cg_texture_type, &vector_4d, &cg_vector_4d_type);
ptr = (std::shared_ptr<VK::Sprite>*)DATA_PTR(self);
if(ptr) mrb_free(mrb, ptr);
ptr = (std::shared_ptr<VK::Sprite>*)mrb_malloc(
mrb, sizeof(std::shared_ptr<VK::Sprite>));
- glm::vec4 rect{x, y, w, h};
new(ptr)std::shared_ptr<VK::Sprite>(
- std::make_shared<VK::Sprite>(*texture, rect));
+ std::make_shared<VK::Sprite>(*texture, *vector_4d->get()));
mrb_data_init(self, ptr, &cg_sprite_type);
return self;
@@ -56,13 +56,13 @@ static mrb_value
cg_cSprite_draw(mrb_state *mrb, mrb_value self)
{
auto *ptr = (std::shared_ptr<VK::Sprite>*)DATA_PTR(self);
- mrb_float x, y, w, h;
- mrb_get_args(mrb, "ffff", &x, &y, &w, &h);
+ std::shared_ptr<glm::vec4> *position;
+
+ mrb_get_args(mrb, "d", &position, &cg_vector_4d_type);
auto &positions = cg_core.vk_graphics_pipeline_2d->sprites_to_draw[
cg_core.vk_renderer->current_frame][*ptr];
- glm::vec4 position{x, y, w, h};
- positions.push_back(position);
+ positions.push_back(*position->get());
return self;
}
@@ -76,6 +76,6 @@ cg_sprite_init(mrb_state *mrb)
cg_cSprite = mrb_define_class_under(mrb, cg_m, "Sprite", mrb->object_class);
MRB_SET_INSTANCE_TT(cg_cSprite, MRB_TT_DATA);
mrb_define_method(
- mrb, cg_cSprite, "initialize", cg_cSprite_initialize, MRB_ARGS_REQ(5));
- mrb_define_method(mrb, cg_cSprite, "draw", cg_cSprite_draw, MRB_ARGS_REQ(4));
+ mrb, cg_cSprite, "initialize", cg_cSprite_initialize, MRB_ARGS_REQ(2));
+ mrb_define_method(mrb, cg_cSprite, "draw", cg_cSprite_draw, MRB_ARGS_REQ(1));
}
diff --git a/src/vector_4d.cpp b/src/vector_4d.cpp
new file mode 100644
index 0000000..5271823
--- /dev/null
+++ b/src/vector_4d.cpp
@@ -0,0 +1,190 @@
+/*
+ * 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 "vector_4d.hpp"
+
+#include <memory>
+
+#include <glm/vec4.hpp>
+
+void
+cg_free_vector_4d(mrb_state *mrb, void* obj)
+{
+ auto ptr = static_cast<std::shared_ptr<glm::vec4>*>(obj);
+
+ ptr->~shared_ptr();
+ mrb_free(mrb, ptr);
+}
+
+const struct mrb_data_type cg_vector_4d_type = {
+ "CG_Vector4D", cg_free_vector_4d};
+
+static mrb_value
+cg_cVector4D_initialize(mrb_state *mrb, mrb_value self)
+{
+ mrb_float x = 0.0f;
+ mrb_float y = 0.0f;
+ mrb_float w = 0.0f;
+ mrb_float h = 0.0f;
+ std::shared_ptr<glm::vec4> *ptr;
+
+ mrb_get_args(mrb, "|ffff", &x, &y, &w, &h);
+ ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ if(ptr) mrb_free(mrb, ptr);
+ ptr = (std::shared_ptr<glm::vec4>*)mrb_malloc(
+ mrb, sizeof(std::shared_ptr<glm::vec4>));
+
+ new(ptr)std::shared_ptr<glm::vec4>(std::make_shared<glm::vec4>(x, y, w, h));
+
+ mrb_data_init(self, ptr, &cg_vector_4d_type);
+ return self;
+}
+
+mrb_value
+cg_cVector4D_get_x(mrb_state *mrb, mrb_value self)
+{
+ std::shared_ptr<glm::vec4> *ptr =
+ (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+
+ return mrb_float_value(mrb, (*ptr)->x);
+}
+
+mrb_value
+cg_cVector4D_get_y(mrb_state *mrb, mrb_value self)
+{
+ std::shared_ptr<glm::vec4> *ptr =
+ (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+
+ return mrb_float_value(mrb, (*ptr)->y);
+}
+
+mrb_value
+cg_cVector4D_get_w(mrb_state *mrb, mrb_value self)
+{
+ std::shared_ptr<glm::vec4> *ptr =
+ (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+
+ return mrb_float_value(mrb, (*ptr)->z);
+}
+
+mrb_value
+cg_cVector4D_get_h(mrb_state *mrb, mrb_value self)
+{
+ std::shared_ptr<glm::vec4> *ptr =
+ (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+
+ return mrb_float_value(mrb, (*ptr)->w);
+}
+
+static mrb_value
+cg_cVector4D_set_x(mrb_state *mrb, mrb_value self)
+{
+ mrb_float x;
+ std::shared_ptr<glm::vec4> *ptr =
+ (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+
+ mrb_get_args(mrb, "f", &x);
+ (*ptr)->x = x;
+
+ return self;
+}
+
+static mrb_value
+cg_cVector4D_set_y(mrb_state *mrb, mrb_value self)
+{
+ mrb_float y;
+ std::shared_ptr<glm::vec4> *ptr =
+ (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+
+ mrb_get_args(mrb, "f", &y);
+ (*ptr)->y = y;
+
+ return self;
+}
+
+static mrb_value
+cg_cVector4D_set_w(mrb_state *mrb, mrb_value self)
+{
+ mrb_float w;
+ std::shared_ptr<glm::vec4> *ptr =
+ (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+
+ mrb_get_args(mrb, "f", &w);
+ (*ptr)->z = w;
+
+ return self;
+}
+
+static mrb_value
+cg_cVector4D_set_h(mrb_state *mrb, mrb_value self)
+{
+ mrb_float h;
+ std::shared_ptr<glm::vec4> *ptr =
+ (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+
+ mrb_get_args(mrb, "f", &h);
+ (*ptr)->w = h;
+
+ return self;
+}
+
+void
+cg_vector_4d_init(mrb_state *mrb)
+{
+ struct RClass *cg_m, *cg_cVector4D;
+
+ cg_m = mrb_module_get(mrb, "CandyGear");
+ cg_cVector4D = mrb_define_class_under(
+ mrb, cg_m, "Vector4D", mrb->object_class);
+ MRB_SET_INSTANCE_TT(cg_cVector4D, MRB_TT_DATA);
+ mrb_define_method(
+ mrb, cg_cVector4D, "initialize", cg_cVector4D_initialize,
+ MRB_ARGS_NONE()|MRB_ARGS_OPT(4));
+
+ mrb_define_method(
+ mrb, cg_cVector4D, "x", cg_cVector4D_get_x, MRB_ARGS_NONE());
+ mrb_define_method(
+ mrb, cg_cVector4D, "y", cg_cVector4D_get_y, MRB_ARGS_NONE());
+ mrb_define_method(
+ mrb, cg_cVector4D, "w", cg_cVector4D_get_w, MRB_ARGS_NONE());
+ mrb_define_method(
+ mrb, cg_cVector4D, "h", cg_cVector4D_get_h, MRB_ARGS_NONE());
+ mrb_define_method(
+ mrb, cg_cVector4D, "r", cg_cVector4D_get_x, MRB_ARGS_NONE());
+ mrb_define_method(
+ mrb, cg_cVector4D, "g", cg_cVector4D_get_y, MRB_ARGS_NONE());
+ mrb_define_method(
+ mrb, cg_cVector4D, "b", cg_cVector4D_get_w, MRB_ARGS_NONE());
+ mrb_define_method(
+ mrb, cg_cVector4D, "a", cg_cVector4D_get_h, MRB_ARGS_NONE());
+
+ mrb_define_method(
+ mrb, cg_cVector4D, "x=", cg_cVector4D_set_x, MRB_ARGS_REQ(1));
+ mrb_define_method(
+ mrb, cg_cVector4D, "y=", cg_cVector4D_set_y, MRB_ARGS_REQ(1));
+ mrb_define_method(
+ mrb, cg_cVector4D, "w=", cg_cVector4D_set_w, MRB_ARGS_REQ(1));
+ mrb_define_method(
+ mrb, cg_cVector4D, "h=", cg_cVector4D_set_h, MRB_ARGS_REQ(1));
+ mrb_define_method(
+ mrb, cg_cVector4D, "r=", cg_cVector4D_set_x, MRB_ARGS_REQ(1));
+ mrb_define_method(
+ mrb, cg_cVector4D, "g=", cg_cVector4D_set_y, MRB_ARGS_REQ(1));
+ mrb_define_method(
+ mrb, cg_cVector4D, "b=", cg_cVector4D_set_w, MRB_ARGS_REQ(1));
+ mrb_define_method(
+ mrb, cg_cVector4D, "a=", cg_cVector4D_set_h, MRB_ARGS_REQ(1));
+}
diff --git a/src/vector_4d.hpp b/src/vector_4d.hpp
new file mode 100644
index 0000000..2797d53
--- /dev/null
+++ b/src/vector_4d.hpp
@@ -0,0 +1,27 @@
+/*
+ * 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_VECTOR_4D_H
+#define CANDY_GEAR_VECTOR_4D_H 1
+
+#include "core.hpp"
+
+extern const struct mrb_data_type cg_vector_4d_type;
+
+void
+cg_vector_4d_init(mrb_state *mrb);
+
+#endif /* CANDY_GEAR_VECTOR_4D_H */
diff --git a/test/src/main.rb b/test/src/main.rb
index b153a50..f5c0676 100644
--- a/test/src/main.rb
+++ b/test/src/main.rb
@@ -18,8 +18,10 @@ TRANSLATION_SPEED = 0.5;
def init()
$texture = CandyGear::Texture.from_image("textures/color_texture.png");
- $sprite = CandyGear::Sprite.new($texture, 0, 0, 1.0, 1.0);
$model = CandyGear::Model.new("models/cube.cgmodel", $texture);
+ $sprite = CandyGear::Sprite.new(
+ $texture, CandyGear::Vector4D.new(0, 0, 1.0, 1.0));
+ $sprite_position = CandyGear::Vector4D.new(1.0, 1.0, 101.0, 101.0);
$instances = [
CandyGear::Vector3D.new(5.0, 0.0, 0.0),
@@ -33,7 +35,7 @@ def init()
$camera_position = CandyGear::Vector3D.new(0.0, 0.0, 0.0);
$camera_rotation = CandyGear::Rotation3D.new(0.0, 0.0, 0.0);
- CandyGear.camera_position = $camera_position
+ CandyGear.camera_position = $camera_position;
CandyGear.camera_rotation = $camera_rotation;
end
@@ -68,7 +70,7 @@ end
def quit() = CandyGear.quit();
def tick()
- $sprite.draw(1.0, 1.0, 101.0, 101.0);
+ $sprite.draw($sprite_position);
$instances_rotation.rotate(0.0, BOX_ROTATION_SPEED);
$instances.each do |i|
$model.draw(i, $instances_rotation);