diff --git a/src/Core/include/Core/game.h b/src/Core/include/Core/game.h index 67f5229..4210bf4 100644 --- a/src/Core/include/Core/game.h +++ b/src/Core/include/Core/game.h @@ -24,6 +24,8 @@ namespace wolfenstein { +enum class RenderType { TEXTURE, LINE }; + struct GeneralConfig { int screen_width; @@ -58,6 +60,7 @@ class Game GeneralConfig config_; bool is_running_; + RenderType render_type_; }; } // namespace wolfenstein diff --git a/src/Core/src/game.cpp b/src/Core/src/game.cpp index c4a9b3d..bd9904a 100644 --- a/src/Core/src/game.cpp +++ b/src/Core/src/game.cpp @@ -1,11 +1,13 @@ #include "Core/game.h" #include "Graphics/renderer.h" #include "Math/vector.h" +#include #include namespace wolfenstein { -Game::Game(GeneralConfig& config) : config_(config) { +Game::Game(GeneralConfig& config) + : config_(config), is_running_(false), render_type_(RenderType::TEXTURE) { Init(); // Hide cursor SDL_ShowCursor(SDL_DISABLE); @@ -79,6 +81,18 @@ void Game::CheckEvent() { SDL_WarpMouseInWindow(nullptr, 400, 300); } } + + // When user press a key + if (event.type == SDL_KEYDOWN) { + if (event.key.keysym.sym == SDLK_p && + render_type_ == RenderType::TEXTURE) { + render_type_ = RenderType::LINE; + } + else if (event.key.keysym.sym == SDLK_p && + render_type_ == RenderType::LINE) { + render_type_ = RenderType::TEXTURE; + } + } } } @@ -87,7 +101,14 @@ void Game::Run() { CheckEvent(); time_manager_->CalculateDeltaTime(); scene_->Update(time_manager_->GetDeltaTime()); - renderer_->RenderScene(scene_, camera_); + switch (render_type_) { + case RenderType::TEXTURE: + renderer_->RenderScene(scene_, camera_); + break; + case RenderType::LINE: + renderer_->RenderScene2D(scene_, camera_); + break; + } } } diff --git a/src/Graphics/src/renderer.cpp b/src/Graphics/src/renderer.cpp index 78e03f0..92014ac 100644 --- a/src/Graphics/src/renderer.cpp +++ b/src/Graphics/src/renderer.cpp @@ -118,7 +118,9 @@ void Renderer::RenderIfRayHit(const int horizontal_slice, const Ray& ray, hit_point = std::fmod(hit_point, 1.0); const auto texture_height = TextureManager::GetInstance().GetTexture(ray.wall_id).height; - int texture_point = static_cast(hit_point * texture_height); + const auto texture_width = + TextureManager::GetInstance().GetTexture(ray.wall_id).width; + int texture_point = static_cast(hit_point * texture_width); SDL_Rect src_rect = {texture_point, 0, 2, texture_height}; SDL_Rect dest_rect = {horizontal_slice, draw_start, 2, line_height}; @@ -234,18 +236,18 @@ void Renderer::RenderObjects( const auto position = player->GetPosition(); const auto crosshair_ray = camera_ptr->GetCrosshairRay(); - SetDrawColor({0xFF, 0xA5, 0, 255}); - const auto rays = camera_ptr->GetRays(); - const auto first_ray = rays->front(); - const auto start = ToVector2i(position.pose * config_.scale); - const auto end = ToVector2i((first_ray.hit_point) * config_.scale); - DrawLine(start, end); - - const auto last_ray = rays->back(); - const auto start_last = ToVector2i(position.pose * config_.scale); - const auto end_last = - ToVector2i((last_ray.hit_point) * config_.scale); - DrawLine(start_last, end_last); + SetDrawColor({00, 0xA5, 0, 1}); + const auto rays = *camera_ptr->GetRays(); + for (int i = 0; i < rays.size(); i++) { + if (!rays[i].is_hit || i % 3 != 0) { + continue; + } + const auto start = ToVector2i(position.pose * config_.scale); + const auto end = + ToVector2i((rays[i].hit_point) * config_.scale); + DrawLine(start, end); + } + SetDrawColor({255, 0, 0, 255}); const auto circle_points = GenerateCirclePoints( ToVector2i(position.pose * config_.scale), 10, 20); @@ -305,10 +307,19 @@ void Renderer::RenderObjects( ToVector2i(right_vertex * config_.scale)); const auto left_point = GenerateCirclePoints( ToVector2i(left_vertex * config_.scale), 5, 20); + SetDrawColor({0xFF, 0, 0, 255}); for (unsigned int i = 0; i < left_point.size(); i++) { SDL_RenderDrawPoint(renderer_, left_point[i].x, left_point[i].y); } + + const auto right_point = GenerateCirclePoints( + ToVector2i(right_vertex * config_.scale), 5, 20); + SetDrawColor({0, 0xFF, 0, 255}); + for (unsigned int i = 0; i < right_point.size(); i++) { + SDL_RenderDrawPoint(renderer_, right_point[i].x, + right_point[i].y); + } } } }