summaryrefslogtreecommitdiff
path: root/src/sprite_3d.cpp
diff options
context:
space:
mode:
authorFrederico Linhares <fred@linhares.blue>2023-10-09 14:46:09 -0300
committerFrederico Linhares <fred@linhares.blue>2023-10-09 15:58:14 -0300
commitba447583c74d44c6b07c5409918207157f921efe (patch)
tree56cd1c36a8e77540136c8871eb3b94f0adf37139 /src/sprite_3d.cpp
parent8fa221cb60c19638d4ad0833965fee605593eea3 (diff)
feat Render sprite in a 3D position
Diffstat (limited to 'src/sprite_3d.cpp')
-rw-r--r--src/sprite_3d.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/sprite_3d.cpp b/src/sprite_3d.cpp
new file mode 100644
index 0000000..3e4e29d
--- /dev/null
+++ b/src/sprite_3d.cpp
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2022-2023 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 "sprite_3d.hpp"
+
+#include "sprite.hpp"
+#include "vector_3d.hpp"
+
+void
+cg_free_sprite_3d(mrb_state *mrb, void *obj)
+{
+ auto ptr = static_cast<std::shared_ptr<VK::Sprite3D>*>(obj);
+
+ ptr->~shared_ptr();
+ mrb_free(mrb, ptr);
+}
+
+const struct mrb_data_type cg_sprite_3d_type = {
+ "CG_Sprite3D", cg_free_sprite_3d };
+
+static mrb_value
+cg_cSprite3D_initialize(mrb_state *mrb, mrb_value self)
+{
+ std::shared_ptr<VK::Sprite> *sprite;
+ std::shared_ptr<glm::vec3> *position;
+ mrb_float w, h;
+ std::shared_ptr<VK::Sprite3D> *ptr;
+
+ mrb_get_args(mrb, "ddff", &sprite, &cg_sprite_type,
+ &position, &cg_vector_3d_type, &w, &h);
+ ptr = (std::shared_ptr<VK::Sprite3D>*)DATA_PTR(self);
+ if(ptr) mrb_free(mrb, ptr);
+ ptr = (std::shared_ptr<VK::Sprite3D>*)mrb_malloc(
+ mrb, sizeof(std::shared_ptr<VK::Sprite3D>));
+
+ glm::vec3 size{w, h, 0.0};
+ new(ptr)std::shared_ptr<VK::Sprite3D>(
+ std::make_shared<VK::Sprite3D>(*sprite, *position, size));
+
+ mrb_data_init(self, ptr, &cg_sprite_3d_type);
+ return self;
+}
+
+static mrb_value
+cg_cSprite3D_draw(mrb_state *mrb, mrb_value self)
+{
+ auto ptr = (std::shared_ptr<VK::Sprite3D>*)DATA_PTR(self);
+
+ auto &sprites_3d_to_draw = cg_core.vk_renderer->sprites_3d_to_draw[
+ cg_core.vk_swapchain->current_frame];
+ sprites_3d_to_draw.emplace_back(*ptr);
+
+ return self;
+}
+
+void
+cg_sprite_3d_init(mrb_state *mrb)
+{
+ struct RClass *cg_m, *cg_cSprite3D;
+
+ cg_m = mrb_module_get(mrb, "CandyGear");
+ cg_cSprite3D = mrb_define_class_under(
+ mrb, cg_m, "Sprite3D", mrb->object_class);
+ MRB_SET_INSTANCE_TT(cg_cSprite3D, MRB_TT_DATA);
+ mrb_define_method(
+ mrb, cg_cSprite3D, "initialize", cg_cSprite3D_initialize, MRB_ARGS_REQ(3));
+ mrb_define_method(
+ mrb, cg_cSprite3D, "draw", cg_cSprite3D_draw, MRB_ARGS_NONE());
+}