diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 2 | ||||
-rw-r--r-- | src/sprite.cpp | 24 | ||||
-rw-r--r-- | src/vector_4d.cpp | 190 | ||||
-rw-r--r-- | src/vector_4d.hpp | 27 |
4 files changed, 231 insertions, 12 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 */ |