From 75337ae1c59e8b2c3bc6fbcd7ccafc7a8205d6b2 Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Fri, 23 Dec 2022 15:02:39 -0300 Subject: feat Add size to projections in 2D views --- src/core.cpp | 2 +- src/view_2d.cpp | 9 ++++++--- src/view_3d.cpp | 9 ++++++--- src/vk/view_2d.cpp | 9 ++++++--- src/vk/view_2d.hpp | 3 ++- src/vk/view_3d.cpp | 5 +++-- src/vk/view_3d.hpp | 2 +- test/src/main.rb | 6 ++++-- 8 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/core.cpp b/src/core.cpp index 0de2dbd..bd5ea75 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -649,7 +649,7 @@ load_vk_renderer(void *obj) static_cast(cg_core.display_height)); cg_core.vk_renderer = new VK::Renderer( {}, - {std::make_shared(region)}); + {std::make_shared(region, region.z, region.w)}); } catch(const CommandError &e) { diff --git a/src/view_2d.cpp b/src/view_2d.cpp index 3f4325d..4a973d6 100644 --- a/src/view_2d.cpp +++ b/src/view_2d.cpp @@ -37,16 +37,19 @@ static mrb_value cg_cView2D_initialize(mrb_state *mrb, mrb_value self) { std::shared_ptr *region; + mrb_float projection_width, projection_height; std::shared_ptr *ptr; - mrb_get_args(mrb, "d", ®ion, &cg_vector_4d_type); + mrb_get_args(mrb, "dff", ®ion, &cg_vector_4d_type, + &projection_width, &projection_height); ptr = (std::shared_ptr*)DATA_PTR(self); if(ptr) mrb_free(mrb, ptr); ptr = (std::shared_ptr*)mrb_malloc( mrb, sizeof(std::shared_ptr)); new(ptr)std::shared_ptr( - std::make_shared(*region->get())); + std::make_shared( + *region->get(), projection_width, projection_height)); mrb_data_init(self, ptr, &cg_view_2d_type); return self; @@ -83,5 +86,5 @@ cg_view_2d_init(mrb_state *mrb) cg_cView2D = mrb_define_class_under(mrb, cg_m, "View2D", mrb->object_class); MRB_SET_INSTANCE_TT(cg_cView2D, MRB_TT_DATA); mrb_define_method( - mrb, cg_cView2D, "initialize", cg_cView2D_initialize, MRB_ARGS_REQ(1)); + mrb, cg_cView2D, "initialize", cg_cView2D_initialize, MRB_ARGS_REQ(3)); } diff --git a/src/view_3d.cpp b/src/view_3d.cpp index 1b733b8..92e57c9 100644 --- a/src/view_3d.cpp +++ b/src/view_3d.cpp @@ -38,16 +38,19 @@ static mrb_value cg_cView3D_initialize(mrb_state *mrb, mrb_value self) { std::shared_ptr *region; + mrb_float projection_width, projection_height; std::shared_ptr *ptr; - mrb_get_args(mrb, "d", ®ion, &cg_vector_4d_type); + mrb_get_args(mrb, "dff", ®ion, &cg_vector_4d_type, + &projection_width, &projection_height); ptr = (std::shared_ptr*)DATA_PTR(self); if(ptr) mrb_free(mrb, ptr); ptr = (std::shared_ptr*)mrb_malloc( mrb, sizeof(std::shared_ptr)); new(ptr)std::shared_ptr( - std::make_shared(*region->get())); + std::make_shared( + *region->get(), projection_width, projection_height)); mrb_data_init(self, ptr, &cg_view_3d_type); return self; @@ -86,7 +89,7 @@ cg_view_3d_init(mrb_state *mrb) cg_cView3D = mrb_define_class_under(mrb, cg_m, "View3D", mrb->object_class); MRB_SET_INSTANCE_TT(cg_cView3D, MRB_TT_DATA); mrb_define_method( - mrb, cg_cView3D, "initialize", cg_cView3D_initialize, MRB_ARGS_REQ(1)); + mrb, cg_cView3D, "initialize", cg_cView3D_initialize, MRB_ARGS_REQ(3)); mrb_define_method( mrb, cg_cView3D, "camera_position=", cg_cView3D_set_camera_position, MRB_ARGS_REQ(1)); diff --git a/src/vk/view_2d.cpp b/src/vk/view_2d.cpp index a80cc89..b6f208c 100644 --- a/src/vk/view_2d.cpp +++ b/src/vk/view_2d.cpp @@ -101,8 +101,8 @@ load_resources_to_descriptor_sets_2d(void *obj) VK::UBOView2D ubo_view_2d; ubo_view_2d.proj = glm::ortho( - 0.0f, self->region.z, - 0.0f, self->region.w, + 0.0f, self->projection_width, + 0.0f, self->projection_height, 0.0f, 100.0f); self->ub_2d[i].copy_data(&ubo_view_2d); } @@ -122,7 +122,10 @@ const CommandChain View2D::descriptor_sets_loader{ {&load_resources_to_descriptor_sets_2d, nullptr} }; -View2D::View2D(glm::vec4 region): +View2D::View2D( + glm::vec4 region, float projection_width, float projection_height): + projection_width{projection_width}, + projection_height{projection_height}, region{region}, descriptor_pool{VK_NULL_HANDLE}, rectangles_to_draw{cg_core.vk_swapchain->images_count}, diff --git a/src/vk/view_2d.hpp b/src/vk/view_2d.hpp index f49c3be..caa3d0f 100644 --- a/src/vk/view_2d.hpp +++ b/src/vk/view_2d.hpp @@ -31,6 +31,7 @@ namespace VK struct View2D { glm::vec4 region; + float projection_width, projection_height; // FIXME: if these vectors get resized, they can cause a segmentation fault! std::vector ub_2d; @@ -43,7 +44,7 @@ struct View2D std::unordered_map, std::vector>> sprites_to_draw; - View2D(glm::vec4 region); + View2D(glm::vec4 region, float projection_width, float projection_height); virtual ~View2D(); void diff --git a/src/vk/view_3d.cpp b/src/vk/view_3d.cpp index 39d1159..d9e9844 100644 --- a/src/vk/view_3d.cpp +++ b/src/vk/view_3d.cpp @@ -122,8 +122,9 @@ const CommandChain descriptor_sets_loader{ namespace VK { -View3D::View3D(glm::vec4 region): - View2D{region}, +View3D::View3D( + glm::vec4 region, float projection_width, float projection_height): + View2D{region, projection_width, projection_height}, camera_position{std::make_shared(0.0f, 0.0f, 0.0f)}, camera_rotation{std::make_shared(0.0f, 0.0f, 0.0f)} { diff --git a/src/vk/view_3d.hpp b/src/vk/view_3d.hpp index ba08957..1d51070 100644 --- a/src/vk/view_3d.hpp +++ b/src/vk/view_3d.hpp @@ -32,7 +32,7 @@ struct View3D: public View2D std::shared_ptr camera_position; std::shared_ptr camera_rotation; - View3D(glm::vec4 region); + View3D(glm::vec4 region, float projection_width, float projection_height); ~View3D(); void diff --git a/test/src/main.rb b/test/src/main.rb index 84a47bd..53e92a0 100644 --- a/test/src/main.rb +++ b/test/src/main.rb @@ -47,8 +47,10 @@ def init() $camera_rotation = CandyGear::Rotation3D.new(0.0, 0.0, 0.0); color = CandyGear::Vector3D.new(0.12, 0.12, 0.18); - $view1 = CandyGear::View2D.new(CandyGear::Vector4D.new(0, 0, 1280, 360)); - $view2 = CandyGear::View3D.new(CandyGear::Vector4D.new(0, 360, 1280, 360)); + $view1 = CandyGear::View2D.new( + CandyGear::Vector4D.new(0, 0, 1280, 360), 640, 180); + $view2 = CandyGear::View3D.new( + CandyGear::Vector4D.new(0, 360, 1280, 360), 1280, 360); CandyGear.views = [$view1, $view2]; $view2.camera_position = $camera_position; -- cgit v1.2.3