From 8bedf8a366cb6c1179bc89678c863517b9356d48 Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Sat, 21 Jun 2025 16:51:22 -0300 Subject: refa Remove View2D View2D is almost useless and add too much complexity for the engine, so I am removing it. --- src/candy_gear/candy_gear.cpp | 41 +++++--------- src/candy_gear/core.cpp | 6 +- src/candy_gear/sprite.cpp | 11 +--- src/candy_gear/vector_4d.cpp | 12 ++-- src/candy_gear/view.cpp | 127 ++++++++++++++++++++++++++++++++++++++++++ src/candy_gear/view.hpp | 27 +++++++++ src/candy_gear/view_2d.cpp | 90 ------------------------------ src/candy_gear/view_2d.hpp | 33 ----------- src/candy_gear/view_3d.cpp | 127 ------------------------------------------ src/candy_gear/view_3d.hpp | 27 --------- 10 files changed, 177 insertions(+), 324 deletions(-) create mode 100644 src/candy_gear/view.cpp create mode 100644 src/candy_gear/view.hpp delete mode 100644 src/candy_gear/view_2d.cpp delete mode 100644 src/candy_gear/view_2d.hpp delete mode 100644 src/candy_gear/view_3d.cpp delete mode 100644 src/candy_gear/view_3d.hpp (limited to 'src/candy_gear') diff --git a/src/candy_gear/candy_gear.cpp b/src/candy_gear/candy_gear.cpp index b1758a4..7b5a9d9 100644 --- a/src/candy_gear/candy_gear.cpp +++ b/src/candy_gear/candy_gear.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -21,8 +21,7 @@ #include #include "core.hpp" -#include "view_2d.hpp" -#include "view_3d.hpp" +#include "view.hpp" static mrb_value cg_mCandyGear_set_game_name(mrb_state *mrb, mrb_value self) @@ -36,40 +35,28 @@ cg_mCandyGear_set_game_name(mrb_state *mrb, mrb_value self) } static mrb_value -cg_mCandyGear_set_views(mrb_state *mrb, mrb_value self) +cg_mCandyGear_change_views(mrb_state *mrb, mrb_value self) { - struct RClass *cg_m, *cg_cView2D, *cg_cView3D; + struct RClass *cg_m, *cg_cView; mrb_value *array; mrb_int array_len; + mrb_float width, height; - std::vector> views_2d; - std::vector> views_3d; + std::vector> views; cg_m = mrb_module_get(mrb, "CandyGear"); - cg_cView2D = mrb_class_get_under(mrb, cg_m, "View2D"); - cg_cView3D = mrb_class_get_under(mrb, cg_m, "View3D"); + cg_cView = mrb_class_get_under(mrb, cg_m, "View"); - mrb_get_args(mrb, "a", &array, &array_len); + mrb_get_args(mrb, "aff", &array, &array_len, &width, &height); for(mrb_int i{0}; i < array_len; i++) { - if(mrb_obj_is_kind_of(mrb, array[i], cg_cView2D)) - { - auto v = (std::shared_ptr*)DATA_PTR(array[i]); - views_2d.push_back(*v); - } - else if(mrb_obj_is_kind_of(mrb, array[i], cg_cView3D)) - { - auto v = (std::shared_ptr*)DATA_PTR(array[i]); - views_3d.push_back(*v); - } + auto v = (std::shared_ptr*)DATA_PTR(array[i]); + views.push_back(*v); } - // A Renderer need at least one view to work. - if(views_2d.size() > 0 || views_3d.size() > 0) - { - delete BluCat::INT::core.vk_renderer; - BluCat::INT::core.vk_renderer = new BluCat::GRA::Renderer({views_2d, views_3d}); - } + delete BluCat::INT::core.vk_renderer; + BluCat::INT::core.vk_renderer = new BluCat::GRA::Renderer( + {views, static_cast(width), static_cast(height)}); return self; } @@ -138,7 +125,7 @@ cg_candy_gear_init(mrb_state *mrb) cg_m = mrb_module_get(mrb, "CandyGear"); mrb_define_class_method( - mrb, cg_m, "views=", cg_mCandyGear_set_views, MRB_ARGS_REQ(1)); + mrb, cg_m, "change_views", cg_mCandyGear_change_views, MRB_ARGS_REQ(3)); mrb_define_class_method( mrb, cg_m, "log", cg_mCandyGear_log, MRB_ARGS_REQ(2)); mrb_define_class_method( diff --git a/src/candy_gear/core.cpp b/src/candy_gear/core.cpp index 6897921..0f5330c 100644 --- a/src/candy_gear/core.cpp +++ b/src/candy_gear/core.cpp @@ -31,8 +31,7 @@ #include "texture.hpp" #include "vector_3d.hpp" #include "vector_4d.hpp" -#include "view_2d.hpp" -#include "view_3d.hpp" +#include "view.hpp" #ifdef DEBUG #include @@ -117,8 +116,7 @@ load_mruby_interface(void *obj) cg_texture_init(cg_core.mrb); cg_vector_3d_init(cg_core.mrb); cg_vector_4d_init(cg_core.mrb); - cg_view_2d_init(cg_core.mrb); - cg_view_3d_init(cg_core.mrb); + cg_view_init(cg_core.mrb); } } diff --git a/src/candy_gear/sprite.cpp b/src/candy_gear/sprite.cpp index daf96a8..2847d76 100644 --- a/src/candy_gear/sprite.cpp +++ b/src/candy_gear/sprite.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -18,7 +18,6 @@ #include "texture.hpp" #include "vector_4d.hpp" -#include "view_2d.hpp" #include "../blu_cat/gra/sprite.hpp" void @@ -61,17 +60,13 @@ cg_cSprite_initialize(mrb_state *mrb, mrb_value self) static mrb_value cg_cSprite_draw(mrb_state *mrb, mrb_value self) { - mrb_value view_value; - BluCat::GRA::View2D *view_2d; mrb_float x, y, w, h, z_index{0.0}; auto ptr = (std::shared_ptr*)DATA_PTR(self); - mrb_get_args(mrb, "offff|f", &view_value, &x, &y, &w, &h, &z_index); - - view_2d = cg_cView_to_view_2d(mrb, view_value); + mrb_get_args(mrb, "ffff|f", &x, &y, &w, &h, &z_index); glm::vec4 rect(x, y, x + w, y + h); - auto &sprites_to_draw = view_2d->sprites_to_draw[ + auto &sprites_to_draw = BluCat::INT::core.vk_renderer->sprites_to_draw[ BluCat::INT::core.vk_swapchain->current_frame]; sprites_to_draw.emplace_back(*ptr, rect, z_index); diff --git a/src/candy_gear/vector_4d.cpp b/src/candy_gear/vector_4d.cpp index b44b612..5ee674d 100644 --- a/src/candy_gear/vector_4d.cpp +++ b/src/candy_gear/vector_4d.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 Frederico de Oliveira Linhares + * Copyright 2022-2025 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. @@ -22,7 +22,7 @@ #include #include "vector_3d.hpp" -#include "view_2d.hpp" +#include "view.hpp" namespace { @@ -347,19 +347,15 @@ cg_cVector4D_collide(mrb_state *mrb, mrb_value self) static mrb_value cg_cVector4D_draw_rectangle(mrb_state *mrb, mrb_value self) { - mrb_value view_value; - BluCat::GRA::View2D *view_2d; std::shared_ptr *color; auto ptr = (std::shared_ptr*)DATA_PTR(self); - mrb_get_args(mrb, "od", &view_value, &color, &cg_vector_3d_type); - - view_2d = cg_cView_to_view_2d(mrb, view_value); + mrb_get_args(mrb, "d", &color, &cg_vector_3d_type); glm::vec4 position( (*ptr)->x, (*ptr)->y, (*ptr)->x + (*ptr)->z, (*ptr)->y + (*ptr)->w); BluCat::GRA::Rectangle rect{position, (**color)}; - auto &rectangles = view_2d->rectangles_to_draw[ + auto &rectangles = BluCat::INT::core.vk_renderer->rectangles_to_draw[ BluCat::INT::core.vk_swapchain->current_frame]; rectangles.push_back(rect); diff --git a/src/candy_gear/view.cpp b/src/candy_gear/view.cpp new file mode 100644 index 0000000..40a03a8 --- /dev/null +++ b/src/candy_gear/view.cpp @@ -0,0 +1,127 @@ +/* + * Copyright 2022-2025 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 "view.hpp" + +#include "sprite.hpp" +#include "orientation_3d.hpp" +#include "vector_3d.hpp" +#include "vector_4d.hpp" +#include "../blu_cat/gra/sprite.hpp" +#include "../blu_cat/gra/view.hpp" + +void +cg_free_view(mrb_state *mrb, void* obj) +{ + auto ptr = static_cast*>(obj); + + ptr->~shared_ptr(); + mrb_free(mrb, ptr); +} + +const struct mrb_data_type cg_view_type = { "CG_View", cg_free_view }; + +static mrb_value +cg_cView_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, "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(), projection_width, projection_height)); + + mrb_data_init(self, ptr, &cg_view_type); + return self; +} + +static mrb_value +cg_cView_set_camera_position(mrb_state *mrb, mrb_value self) +{ + std::shared_ptr *camera_position; + auto ptr = (std::shared_ptr*)DATA_PTR(self); + + mrb_get_args(mrb, "d", &camera_position, &cg_vector_3d_type); + (*ptr)->camera_position = (*camera_position); + + return self; +} + +static mrb_value +cg_cView_set_camera_orientation(mrb_state *mrb, mrb_value self) +{ + std::shared_ptr *camera_orientation; + auto ptr = (std::shared_ptr*)DATA_PTR(self); + + mrb_get_args(mrb, "d", &camera_orientation, &cg_orientation_3d_type); + (*ptr)->camera_orientation = (*camera_orientation); + + return self; +} + +static mrb_value +cg_cView_get_field_of_view(mrb_state *mrb, mrb_value self) +{ + auto ptr = (std::shared_ptr*)DATA_PTR(self); + + return mrb_float_value(mrb, (*ptr)->field_of_view); + + return self; +} + +static mrb_value +cg_cView_set_field_of_view(mrb_state *mrb, mrb_value self) +{ + mrb_float fov; + auto ptr = (std::shared_ptr*)DATA_PTR(self); + + mrb_get_args(mrb, "f", &fov); + (*ptr)->field_of_view = fov; + + return self; +} + +void +cg_view_init(mrb_state *mrb) +{ + struct RClass *cg_m, *cg_cView; + + cg_m = mrb_module_get(mrb, "CandyGear"); + cg_cView = mrb_define_class_under(mrb, cg_m, "View", mrb->object_class); + MRB_SET_INSTANCE_TT(cg_cView, MRB_TT_DATA); + mrb_define_method( + mrb, cg_cView, "initialize", cg_cView_initialize, MRB_ARGS_REQ(3)); + mrb_define_method( + mrb, cg_cView, "camera_position=", cg_cView_set_camera_position, + MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cView, "camera_orientation=", cg_cView_set_camera_orientation, + MRB_ARGS_REQ(1)); + mrb_define_method( + mrb, cg_cView, "field_of_view", cg_cView_get_field_of_view, + MRB_ARGS_NONE()); + mrb_define_method( + mrb, cg_cView, "field_of_view=", cg_cView_set_field_of_view, + MRB_ARGS_REQ(1)); +} diff --git a/src/candy_gear/view.hpp b/src/candy_gear/view.hpp new file mode 100644 index 0000000..ce1b556 --- /dev/null +++ b/src/candy_gear/view.hpp @@ -0,0 +1,27 @@ +/* + * Copyright 2022-2025 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_VIEW_H +#define CANDY_GEAR_VIEW_H 1 + +#include "core.hpp" + +extern const struct mrb_data_type cg_view_type; + +void +cg_view_init(mrb_state *mrb); + +#endif /* CANDY_GEAR_VIEW_H */ diff --git a/src/candy_gear/view_2d.cpp b/src/candy_gear/view_2d.cpp deleted file mode 100644 index e0dc8a1..0000000 --- a/src/candy_gear/view_2d.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2022-2024 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 "view_2d.hpp" - -#include "sprite.hpp" -#include "vector_4d.hpp" -#include "view_3d.hpp" -#include "../blu_cat/gra/sprite.hpp" -#include "../blu_cat/gra/view_2d.hpp" - -void -cg_free_view_2d(mrb_state *mrb, void* obj) -{ - auto ptr = static_cast*>(obj); - - ptr->~shared_ptr(); - mrb_free(mrb, ptr); -} - -const struct mrb_data_type cg_view_2d_type = { "CG_View2D", cg_free_view_2d }; - -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, "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(), projection_width, projection_height)); - - mrb_data_init(self, ptr, &cg_view_2d_type); - return self; -} - -BluCat::GRA::View2D* -cg_cView_to_view_2d(mrb_state *mrb, mrb_value view_value) -{ - BluCat::GRA::View2D* view_2d; - const mrb_data_type *type = DATA_TYPE(view_value); - - if(type == &cg_view_2d_type) - view_2d = static_cast*>( - DATA_PTR(view_value))->get(); - else if (type == &cg_view_3d_type) - view_2d = static_cast( - static_cast*>( - DATA_PTR(view_value))->get()); - else - mrb_raisef( - mrb, E_TYPE_ERROR, "wrong argument type %s (expected %s or %s)", - type->struct_name, cg_view_2d_type.struct_name, - cg_view_3d_type.struct_name); - - return view_2d; -} - -void -cg_view_2d_init(mrb_state *mrb) -{ - struct RClass *cg_m, *cg_cView2D; - - cg_m = mrb_module_get(mrb, "CandyGear"); - 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(3)); -} diff --git a/src/candy_gear/view_2d.hpp b/src/candy_gear/view_2d.hpp deleted file mode 100644 index 9b742a0..0000000 --- a/src/candy_gear/view_2d.hpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2022-2024 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_VIEW_2D_H -#define CANDY_GEAR_VIEW_2D_H 1 - -#include "core.hpp" - -extern const struct mrb_data_type cg_view_2d_type; - -// Receives a mrb_value that points to a View2D or a View3D and returns a -// pointer to View2D. If the mrb_value points to a View3D, the view will be -// cast to a BluCat::GRA::View2D. -BluCat::GRA::View2D* -cg_cView_to_view_2d(mrb_state *mrb, mrb_value view_value); - -void -cg_view_2d_init(mrb_state *mrb); - -#endif /* CANDY_GEAR_VIEW_2D_H */ diff --git a/src/candy_gear/view_3d.cpp b/src/candy_gear/view_3d.cpp deleted file mode 100644 index b829cca..0000000 --- a/src/candy_gear/view_3d.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright 2022-2024 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 "view_3d.hpp" - -#include "sprite.hpp" -#include "orientation_3d.hpp" -#include "vector_3d.hpp" -#include "vector_4d.hpp" -#include "../blu_cat/gra/sprite.hpp" -#include "../blu_cat/gra/view_3d.hpp" - -void -cg_free_view_3d(mrb_state *mrb, void* obj) -{ - auto ptr = static_cast*>(obj); - - ptr->~shared_ptr(); - mrb_free(mrb, ptr); -} - -const struct mrb_data_type cg_view_3d_type = { "CG_View3D", cg_free_view_3d }; - -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, "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(), projection_width, projection_height)); - - mrb_data_init(self, ptr, &cg_view_3d_type); - return self; -} - -static mrb_value -cg_cView3D_set_camera_position(mrb_state *mrb, mrb_value self) -{ - std::shared_ptr *camera_position; - auto ptr = (std::shared_ptr*)DATA_PTR(self); - - mrb_get_args(mrb, "d", &camera_position, &cg_vector_3d_type); - (*ptr)->camera_position = (*camera_position); - - return self; -} - -static mrb_value -cg_cView3D_set_camera_orientation(mrb_state *mrb, mrb_value self) -{ - std::shared_ptr *camera_orientation; - auto ptr = (std::shared_ptr*)DATA_PTR(self); - - mrb_get_args(mrb, "d", &camera_orientation, &cg_orientation_3d_type); - (*ptr)->camera_orientation = (*camera_orientation); - - return self; -} - -static mrb_value -cg_cView3D_get_field_of_view(mrb_state *mrb, mrb_value self) -{ - auto ptr = (std::shared_ptr*)DATA_PTR(self); - - return mrb_float_value(mrb, (*ptr)->field_of_view); - - return self; -} - -static mrb_value -cg_cView3D_set_field_of_view(mrb_state *mrb, mrb_value self) -{ - mrb_float fov; - auto ptr = (std::shared_ptr*)DATA_PTR(self); - - mrb_get_args(mrb, "f", &fov); - (*ptr)->field_of_view = fov; - - return self; -} - -void -cg_view_3d_init(mrb_state *mrb) -{ - struct RClass *cg_m, *cg_cView3D; - - cg_m = mrb_module_get(mrb, "CandyGear"); - 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(3)); - mrb_define_method( - mrb, cg_cView3D, "camera_position=", cg_cView3D_set_camera_position, - MRB_ARGS_REQ(1)); - mrb_define_method( - mrb, cg_cView3D, "camera_orientation=", cg_cView3D_set_camera_orientation, - MRB_ARGS_REQ(1)); - mrb_define_method( - mrb, cg_cView3D, "field_of_view", cg_cView3D_get_field_of_view, - MRB_ARGS_NONE()); - mrb_define_method( - mrb, cg_cView3D, "field_of_view=", cg_cView3D_set_field_of_view, - MRB_ARGS_REQ(1)); -} diff --git a/src/candy_gear/view_3d.hpp b/src/candy_gear/view_3d.hpp deleted file mode 100644 index 22aa084..0000000 --- a/src/candy_gear/view_3d.hpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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_VIEW_3D_H -#define CANDY_GEAR_VIEW_3D_H 1 - -#include "core.hpp" - -extern const struct mrb_data_type cg_view_3d_type; - -void -cg_view_3d_init(mrb_state *mrb); - -#endif /* CANDY_GEAR_VIEW_3D_H */ -- cgit v1.2.3