Skip to content

Commit

Permalink
Make render type switchable in game and fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalkah committed Aug 12, 2024
1 parent a1ae2bf commit 3de664d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/Core/include/Core/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace wolfenstein {

enum class RenderType { TEXTURE, LINE };

struct GeneralConfig
{
int screen_width;
Expand Down Expand Up @@ -58,6 +60,7 @@ class Game

GeneralConfig config_;
bool is_running_;
RenderType render_type_;
};

} // namespace wolfenstein
Expand Down
25 changes: 23 additions & 2 deletions src/Core/src/game.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "Core/game.h"
#include "Graphics/renderer.h"
#include "Math/vector.h"
#include <SDL2/SDL_video.h>
#include <functional>

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);
Expand Down Expand Up @@ -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;
}
}
}
}

Expand All @@ -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;
}
}
}

Expand Down
37 changes: 24 additions & 13 deletions src/Graphics/src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(hit_point * texture_height);
const auto texture_width =
TextureManager::GetInstance().GetTexture(ray.wall_id).width;
int texture_point = static_cast<int>(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};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down

0 comments on commit 3de664d

Please sign in to comment.