From b7dd8d6df823aac00b2c15635bc575c8e585d2c3 Mon Sep 17 00:00:00 2001 From: Quest Date: Mon, 9 Jun 2025 18:48:47 +0600 Subject: [PATCH 1/2] Refactor: Use value-init over default-init --- include/raynder/raynder.h | 6 +++--- include/raynder/types.h | 6 +++--- src/map.cpp | 2 +- src/player/player.cpp | 22 ++++----------------- src/player/player_collision.cpp | 4 ++-- src/raycaster.cpp | 2 +- src/renderer/renderer.cpp | 34 ++++++++++++++++++--------------- 7 files changed, 33 insertions(+), 43 deletions(-) diff --git a/include/raynder/raynder.h b/include/raynder/raynder.h index 484bca5..44ca6a5 100644 --- a/include/raynder/raynder.h +++ b/include/raynder/raynder.h @@ -20,9 +20,9 @@ class RAYNDERCAST_EXPORT Game { std::unique_ptr map_ptr; std::unique_ptr player_ptr; - uint64_t last_tick; - uint64_t current_tick; - float delta; + uint64_t last_tick{0}; + uint64_t current_tick{0}; + float delta{0}; void compute_delta(); diff --git a/include/raynder/types.h b/include/raynder/types.h index 93caf54..636d5c2 100644 --- a/include/raynder/types.h +++ b/include/raynder/types.h @@ -26,8 +26,8 @@ namespace Raynder { }; struct RAYNDERCAST_EXPORT IdxPair { - uint8_t x; - uint8_t y; + uint8_t x{0}; + uint8_t y{0}; }; struct RAYNDERCAST_EXPORT CartesianPair { @@ -38,7 +38,7 @@ namespace Raynder { struct RAYNDERCAST_EXPORT HitData { CartesianPair coords; IdxPair hit_idx; - bool vertical; + bool vertical{false}; }; struct RAYNDERCAST_EXPORT Color { diff --git a/src/map.cpp b/src/map.cpp index dccfe91..6befa4f 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -33,7 +33,7 @@ Map::Map( std::regex row_regex(row_regex_str); std::stringstream ss(string_as_data); - std::string line; + std::string line{""}; uint8_t loc_y = 0; diff --git a/src/player/player.cpp b/src/player/player.cpp index ff2e64d..812541e 100644 --- a/src/player/player.cpp +++ b/src/player/player.cpp @@ -55,7 +55,7 @@ void Player::reset_collision_dir() { } enum MovementDirection Player::get_movement_direction(enum Axis axis) const { - enum MovementDirection dir; + enum MovementDirection dir = MovementDirection::UP; switch (axis) { case Axis::HORIZONTAL: if (!global_basis_dy) break; @@ -65,6 +65,9 @@ enum MovementDirection Player::get_movement_direction(enum Axis axis) const { if (!global_basis_dx) break; dir = (global_basis_dx > 0) ? MovementDirection::RIGHT : MovementDirection::LEFT; break; + default: + throw std::runtime_error("Unexpected code execution path."); + break; } return dir; } @@ -104,23 +107,6 @@ void Player::move_and_slide() { const float p_vel_x = this->global_basis_dx * this->config.translational_speed; const float p_vel_y = this->global_basis_dy * this->config.translational_speed; - /*const float relative_angle = this->get_basis_d_relative_rotation(); - - const float hit_dist_squared = pow(hit_data.coords.x, 2) + pow(hit_data.coords.y, 2); - - if (hit_dist_squared <= pow(this->config.collision_radius, 2)) { - - if (hit_data.vertical) { - this->vel_y = p_vel_y; - } else { - this->vel_x = p_vel_x; - } - } else { - this->vel_x = p_vel_x; - this->vel_y = p_vel_y; - }*/ - - if (collision_direction.y_colliding && collision_direction.x_colliding) { } else if (collision_direction.y_colliding) { diff --git a/src/player/player_collision.cpp b/src/player/player_collision.cpp index 6c2b01a..2e6427b 100644 --- a/src/player/player_collision.cpp +++ b/src/player/player_collision.cpp @@ -9,9 +9,9 @@ namespace Raynder { const float Player::get_basis_d_relative_rotation() const { - float result; + float result = 0.0; if (basis_dy > 0 && basis_dx == 0) { //forward - result = 0; + result = 0.0; } else if (basis_dy < 0 && basis_dx == 0) { //backward result = M_PI; } else if (basis_dy == 0 && basis_dx > 0) { //left diff --git a/src/raycaster.cpp b/src/raycaster.cpp index 78720b8..11be3f3 100644 --- a/src/raycaster.cpp +++ b/src/raycaster.cpp @@ -56,7 +56,7 @@ const bool Raycaster::cast_ray_along_axis_in_tile( }; CartesianPair diff; - bool vertical; + bool vertical{false}; switch (dir) { case MovementDirection::UP: diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index 8dd2755..05a7928 100644 --- a/src/renderer/renderer.cpp +++ b/src/renderer/renderer.cpp @@ -24,7 +24,7 @@ namespace Raynder { void Renderer::set_drawing_color(const uint8_t r, const uint8_t g, const uint8_t b) const { if (SDL_SetRenderDrawColor(this->context, r, g, b, 255)) { - throw std::runtime_error(SDL_GetError()); + throw std::runtime_error(SDL_GetError()); } } @@ -33,11 +33,11 @@ void Renderer::set_drawing_color(const Color color) const { } void Renderer::draw_quad( - const float x1, const float y1, - const float x2, const float y2, - const float x3, const float y3, - const float x4, const float y4, - const Color color + const float x1, const float y1, + const float x2, const float y2, + const float x3, const float y3, + const float x4, const float y4, + const Color color ) { const SDL_Color draw_color{color.r, color.g, color.b, 255}; @@ -163,9 +163,9 @@ void Renderer::draw_3d_floor( enum Viewport viewport ) { - float top_y; - float bottom_y; - float max_x; + float top_y{0}; + float bottom_y{0}; + float max_x{0}; float min_x = this->config.eucliview_offset_x; switch (viewport) { @@ -179,7 +179,10 @@ void Renderer::draw_3d_floor( top_y = this->eucliview_height/2.0 + this->config.eucliview_offset_y; bottom_y = this->eucliview_height + this->config.eucliview_offset_y; max_x = this->eucliview_width + min_x; - break; + break; + default: + throw std::runtime_error("Unexpected code execution path."); + break; } const Color col = this->config.floor_color; @@ -203,7 +206,7 @@ void Renderer::draw_3d_wall( ) { this->viewport_indicator = viewport; - float last_theta; + float last_theta{0}; HitData last_hit_data; bool first_theta_iteration = true; @@ -219,11 +222,9 @@ void Renderer::draw_3d_wall( const bool same_or_adjacent_blocks = Renderer::same_or_adjacent_blocks(last_hit_data.hit_idx, hit_data.hit_idx); - bool convex_outline_from_view; - if (!first_theta_iteration && same_or_adjacent_blocks) { - convex_outline_from_view = !diagonal_blocks(last_hit_data.hit_idx, hit_data.hit_idx); + const bool convex_outline_from_view = !diagonal_blocks(last_hit_data.hit_idx, hit_data.hit_idx); const bool diagonal = !convex_outline_from_view; const bool same_orientation = hit_data.vertical == last_hit_data.vertical; @@ -255,7 +256,7 @@ const float Renderer::get_renderer_distance( const float y, enum Viewport viewport ) const { - float result; + float result = 0.0f; switch (viewport) { case Viewport::MAIN: result = this->distance_func(x, y); @@ -263,6 +264,9 @@ const float Renderer::get_renderer_distance( case Viewport::EUCLI: result = sqrt(pow(x, 2) + pow(y, 2)); break; + default: + throw std::runtime_error("Unexpected code execution path."); + break; } return result; } From d26688fac2a39a64c0f6943ad0c53b81b5fd5214 Mon Sep 17 00:00:00 2001 From: EcSolticia Date: Sat, 14 Jun 2025 02:09:07 +0600 Subject: [PATCH 2/2] Types: Init `SignedColor` as black through init list if no arguments --- include/raynder/types.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/raynder/types.h b/include/raynder/types.h index 636d5c2..eaff960 100644 --- a/include/raynder/types.h +++ b/include/raynder/types.h @@ -51,11 +51,11 @@ namespace Raynder { }; struct SignedColor { - int16_t r = 0; - int16_t g = 0; - int16_t b = 0; + int16_t r; + int16_t g; + int16_t b; - SignedColor(); + SignedColor() : r{0}, g{0}, b{0} {}; SignedColor(const int16_t r, const int16_t g, const int16_t b) : r{r}, g{g}, b{b} {} SignedColor(const Color color) : r{(int16_t)color.r},