diff options
author | Frederico Linhares <fred@linhares.blue> | 2023-05-29 10:54:59 -0300 |
---|---|---|
committer | Frederico Linhares <fred@linhares.blue> | 2023-05-29 16:04:16 -0300 |
commit | 7f7515f22517509fb93ee1615c72006dca6abb03 (patch) | |
tree | 2799565116c209ff057809494e4b9eb0e90ec279 | |
parent | b8614162cdbb3817a7a58ceadf8b6b0d05fae952 (diff) |
refa Sprite receives floats instead of Vector4D
* lib/menu.rb: Uses the new interface for Sprite.
* src/sprite.cpp (Sprite#new, Sprite#draw): By requiring a Vector4D as
an argument, sometimes the code calling a Sprite method needs to
instantiate a Vector4D and use the stance once. The functions now
require four float variables to eliminate this unnecessary
instantiation.
-rw-r--r-- | lib/menu.rb | 89 | ||||
-rw-r--r-- | src/sprite.cpp | 23 | ||||
-rw-r--r-- | src/vk/sprite.cpp | 11 | ||||
-rw-r--r-- | src/vk/sprite.hpp | 2 | ||||
-rw-r--r-- | test/src/mode/demo.rb | 19 |
5 files changed, 76 insertions, 68 deletions
diff --git a/lib/menu.rb b/lib/menu.rb index 9f4c55c..3921147 100644 --- a/lib/menu.rb +++ b/lib/menu.rb @@ -41,7 +41,7 @@ module CandyGear @sprites = { arrow_select: Sprite.new( - @texture, Vector4D.new(1.0/3.0, 1.0/3.0, 2.0/3.0, 2.0/3.0)) + @texture, 1.0/3.0, 1.0/3.0, 2.0/3.0, 2.0/3.0) } end @@ -64,23 +64,23 @@ module CandyGear @sprites = { arrow_select: Sprite.new( - @texture, Vector4D.new(1.0/3.0, 1.0/3.0, 2.0/3.0, 2.0/3.0)), + @texture, 1.0/3.0, 1.0/3.0, 2.0/3.0, 2.0/3.0), box_top_left: Sprite.new( - @texture, Vector4D.new(0.0, 0.0, 1.0/3.0, 1.0/3.0)), + @texture, 0.0, 0.0, 1.0/3.0, 1.0/3.0), box_top: Sprite.new( - @texture, Vector4D.new(1.1/3.0, 0.0, 1.9/3.0, 1.0/3.0)), + @texture, 1.1/3.0, 0.0, 1.9/3.0, 1.0/3.0), box_top_right: Sprite.new( - @texture, Vector4D.new(2.0/3.0, 0.0, 1.0, 1.0/3.0)), + @texture, 2.0/3.0, 0.0, 1.0, 1.0/3.0), box_left: Sprite.new( - @texture, Vector4D.new(0.0, 1.1/3.0, 1.0/3.0, 1.9/3.0)), + @texture, 0.0, 1.1/3.0, 1.0/3.0, 1.9/3.0), box_right: Sprite.new( - @texture, Vector4D.new(2.0/3.0, 1.1/3.0, 1.0, 1.9/3.0)), + @texture, 2.0/3.0, 1.1/3.0, 1.0, 1.9/3.0), box_bottom_left: Sprite.new( - @texture, Vector4D.new(0.0, 2.0/3.0, 1.0/3.0, 1.0)), + @texture, 0.0, 2.0/3.0, 1.0/3.0, 1.0), box_bottom: Sprite.new( - @texture, Vector4D.new(1.1/3.0, 2.0/3.0, 1.9/3.0, 1.0)), + @texture, 1.1/3.0, 2.0/3.0, 1.9/3.0, 1.0), box_bottom_right: Sprite.new( - @texture, Vector4D.new(2.0/3.0, 2.0/3.0, 1.0, 1.0)) + @texture, 2.0/3.0, 2.0/3.0, 1.0, 1.0) } end @@ -91,45 +91,44 @@ module CandyGear num_vertical_sprites += 1 if height % @border_height > 0; # Draw the corners. - @sprites[:box_top_left].draw( - view, Vector4D.new(x, y, border_width, border_height)); + @sprites[:box_top_left].draw(view, x, y, border_width, border_height); @sprites[:box_top_right].draw( - view, Vector4D.new( - @border_width * (num_horizontal_sprites + 1) + x, y, - border_width, border_height)); + view, + @border_width * (num_horizontal_sprites + 1) + x, y, + border_width, border_height); @sprites[:box_bottom_left].draw( - view, Vector4D.new( - x, @border_height * (num_vertical_sprites + 1) + y, - border_width, border_height)); + view, + x, @border_height * (num_vertical_sprites + 1) + y, + border_width, border_height); @sprites[:box_bottom_right].draw( - view, Vector4D.new( - @border_width * (num_horizontal_sprites + 1) + x, - @border_height * (num_vertical_sprites + 1) + y, - border_width, border_height)); + view, + @border_width * (num_horizontal_sprites + 1) + x, + @border_height * (num_vertical_sprites + 1) + y, + border_width, border_height); # Draw the edges. num_horizontal_sprites.times do |i| # Top @sprites[:box_top].draw( - view, Vector4D.new(@border_width * (i + 1) + x, y, - border_width, border_height)); + view, @border_width * (i + 1) + x, y, + border_width, border_height); # Bottom @sprites[:box_bottom].draw( - view, Vector4D.new(@border_width * (i + 1) + x, - @border_height * (num_vertical_sprites + 1) + y, - border_width, border_height)); + view, @border_width * (i + 1) + x, + @border_height * (num_vertical_sprites + 1) + y, + border_width, border_height); end num_vertical_sprites.times do |i| # Left @sprites[:box_left].draw( - view, Vector4D.new( - x, @border_height * (i + 1) + y, border_width, border_height)); + view, + x, @border_height * (i + 1) + y, border_width, border_height); # Right @sprites[:box_right].draw( - view, Vector4D.new( - @border_width * (num_horizontal_sprites + 1) + x, - @border_height * (i + 1) + y, - border_width, border_height)); + view, + @border_width * (num_horizontal_sprites + 1) + x, + @border_height * (i + 1) + y, + border_width, border_height); end end end @@ -157,7 +156,7 @@ module CandyGear @options = options.map do |opt| texture = Texture.from_text(menu_view.font, opt[:text]) Option.new( - Sprite.new(texture, CandyGear::Vector4D.new(0, 0, 1.0, 1.0)), + Sprite.new(texture, 0, 0, 1.0, 1.0), opt[:action], texture.width, texture.height); @@ -201,20 +200,20 @@ module CandyGear @options.each_with_index do |opt, i| opt.text.draw( - @view, Vector4D.new( - @pos_x + @menu_view.border_width + - @menu_view.border_width, - @pos_y + @menu_view.border_height + - @option_max_height * i, - opt.width, opt.height)); + @view, + @pos_x + @menu_view.border_width + + @menu_view.border_width, + @pos_y + @menu_view.border_height + + @option_max_height * i, + opt.width, opt.height); end @menu_view.sprites[:arrow_select].draw( - @view, Vector4D.new( - @pos_x + @menu_view.border_width, - @pos_y + @menu_view.border_height + - @option_max_height * @current_option, - @menu_view.border_width, @menu_view.border_height)); + @view, + @pos_x + @menu_view.border_width, + @pos_y + @menu_view.border_height + + @option_max_height * @current_option, + @menu_view.border_width, @menu_view.border_height); end end end diff --git a/src/sprite.cpp b/src/sprite.cpp index c72f7c5..8f25f03 100644 --- a/src/sprite.cpp +++ b/src/sprite.cpp @@ -35,19 +35,24 @@ 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) { - std::shared_ptr<glm::vec4> *vector_4d; + mrb_float x, y, w, h; + glm::vec4 vector_4d; std::shared_ptr<VK::Texture> *texture; std::shared_ptr<VK::Sprite> *ptr; mrb_get_args( - mrb, "dd", &texture, &cg_texture_type, &vector_4d, &cg_vector_4d_type); + mrb, "dffff", &texture, &cg_texture_type, &x, &y, &w, &h); 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>)); + vector_4d.x = x; + vector_4d.y = y; + vector_4d.z = w; + vector_4d.w = h; new(ptr)std::shared_ptr<VK::Sprite>( - std::make_shared<VK::Sprite>(*texture, *vector_4d->get())); + std::make_shared<VK::Sprite>(*texture, vector_4d)); mrb_data_init(self, ptr, &cg_sprite_type); return self; @@ -58,16 +63,14 @@ cg_cSprite_draw(mrb_state *mrb, mrb_value self) { mrb_value view_value; VK::View2D *view_2d; - std::shared_ptr<glm::vec4> *position; + mrb_float x, y, w, h; auto ptr = (std::shared_ptr<VK::Sprite>*)DATA_PTR(self); - mrb_get_args(mrb, "od", &view_value, &position, &cg_vector_4d_type); + mrb_get_args(mrb, "offff", &view_value, &x, &y, &w, &h); view_2d = cg_cView_to_view_2d(mrb, view_value); - glm::vec4 rect( - (*position)->x, (*position)->y, - (*position)->x + (*position)->z, (*position)->y + (*position)->w); + glm::vec4 rect(x, y, x + w, y + h); auto &positions = view_2d->sprites_to_draw[ cg_core.vk_swapchain->current_frame][*ptr]; positions.push_back(rect); @@ -84,6 +87,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(2)); - mrb_define_method(mrb, cg_cSprite, "draw", cg_cSprite_draw, MRB_ARGS_REQ(2)); + mrb, cg_cSprite, "initialize", cg_cSprite_initialize, MRB_ARGS_REQ(5)); + mrb_define_method(mrb, cg_cSprite, "draw", cg_cSprite_draw, MRB_ARGS_REQ(5)); } diff --git a/src/vk/sprite.cpp b/src/vk/sprite.cpp index bc9cb57..069bf6a 100644 --- a/src/vk/sprite.cpp +++ b/src/vk/sprite.cpp @@ -28,12 +28,12 @@ namespace struct SpriteBuilder { VK::Sprite *sprite; - glm::vec4 rect; + glm::vec4 ▭ - SpriteBuilder(VK::Sprite *sprite, glm::vec4 rect); + SpriteBuilder(VK::Sprite *sprite, glm::vec4 &rect); }; -SpriteBuilder::SpriteBuilder(VK::Sprite *sprite, glm::vec4 rect): +SpriteBuilder::SpriteBuilder(VK::Sprite *sprite, glm::vec4 &rect): sprite{sprite}, rect{rect} { @@ -172,7 +172,7 @@ static const CommandChain loader{ namespace VK { -Sprite::Sprite(std::shared_ptr<Texture> texture, glm::vec4 rect): +Sprite::Sprite(std::shared_ptr<Texture> texture, glm::vec4 &rect): texture{texture} { SpriteBuilder sprite_builder(this, rect); @@ -181,7 +181,8 @@ Sprite::Sprite(std::shared_ptr<Texture> texture, glm::vec4 rect): Sprite::~Sprite() { - SpriteBuilder sprite_builder(this, glm::vec4()); + glm::vec4 vector_4d{}; + SpriteBuilder sprite_builder(this, vector_4d); loader.revert(&sprite_builder); } diff --git a/src/vk/sprite.hpp b/src/vk/sprite.hpp index e053bba..5934501 100644 --- a/src/vk/sprite.hpp +++ b/src/vk/sprite.hpp @@ -44,7 +44,7 @@ struct Sprite std::shared_ptr<Texture> texture; - Sprite(std::shared_ptr<Texture> texture, glm::vec4 rect); + Sprite(std::shared_ptr<Texture> texture, glm::vec4 &rect); ~Sprite(); }; diff --git a/test/src/mode/demo.rb b/test/src/mode/demo.rb index b3be78d..5f28454 100644 --- a/test/src/mode/demo.rb +++ b/test/src/mode/demo.rb @@ -29,16 +29,15 @@ module Mode @color = CandyGear::Vector3D.new(0.8, 0.2, 0.2); @model = CandyGear::Model.new(mesh, texture); - @sprite = CandyGear::Sprite.new( - texture, CandyGear::Vector4D.new(0, 0, 1.0, 1.0)); + @sprite = CandyGear::Sprite.new(texture, 0, 0, 1.0, 1.0); @rectangle = CandyGear::Vector4D.new(103.0, 1.0, 100.0, 100.0); @sprite_position = CandyGear::Vector4D.new(1.0, 1.0, 100.0, 100.0); @japanese_text_sprite = CandyGear::Sprite.new( - japanese_text, CandyGear::Vector4D.new(0, 0, 1.0, 1.0)); + japanese_text, 0, 0, 1.0, 1.0); @japanese_text_position = CandyGear::Vector4D.new( 204.0, 1.0, japanese_text.width, japanese_text.height); @english_text_sprite = CandyGear::Sprite.new( - english_text, CandyGear::Vector4D.new(0, 0, 1.0, 1.0)); + english_text, 0, 0, 1.0, 1.0); @english_text_position = CandyGear::Vector4D.new( 204.0, japanese_text.height + 2.0, english_text.width, english_text.height); @@ -100,9 +99,15 @@ module Mode end def tick() - @sprite.draw(@view1, @sprite_position); - @japanese_text_sprite.draw(@view1, @japanese_text_position); - @english_text_sprite.draw(@view1, @english_text_position); + @sprite.draw( + @view1, @sprite_position.x, @sprite_position.y, + @sprite_position.w, @sprite_position.h); + @japanese_text_sprite.draw( + @view1, @japanese_text_position.x, @japanese_text_position.y, + @japanese_text_position.w, @japanese_text_position.h); + @english_text_sprite.draw( + @view1, @english_text_position.x, @english_text_position.y, + @english_text_position.w, @english_text_position.h); @instances_rotation.rotate(0.0, BOX_ROTATION_SPEED); @rectangle.draw_rectangle(@view1, @color); @instances.each do |i| |