Skip to content

Commit

Permalink
Separate renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalkah committed Nov 30, 2024
1 parent 033494c commit d56e3f4
Show file tree
Hide file tree
Showing 14 changed files with 697 additions and 537 deletions.
5 changes: 3 additions & 2 deletions src/Camera/include/Camera/raycaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class RayCaster
void Update(const std::shared_ptr<Map>& map_ptr, const Position2D& position,
const std::shared_ptr<RayVector>& rays);

Ray Cast(const std::shared_ptr<Map>& map_ptr, const Position2D& position,
const double ray_theta);
Ray Cast(const std::vector<std::vector<uint16_t>>& map_,
const uint16_t row_size, const uint16_t col_size,
const Position2D& position, const double ray_theta);
double GetDeltaTheta() const;

private:
Expand Down
11 changes: 6 additions & 5 deletions src/Camera/src/raycaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ void RayCaster::Update(const std::shared_ptr<Map>& map_ptr,
const Position2D& position,
const std::shared_ptr<RayVector>& rays) {

const auto map_ = map_ptr->GetMap();
const auto& row_size = map_ptr->GetSizeX();
const auto& col_size = map_ptr->GetSizeY();
double ray_theta = position.theta - (fov_ / 2);
for (auto& ray : *rays) {
ray = Cast(map_ptr, position, ray_theta);
ray = Cast(map_, row_size, col_size, position, ray_theta);
ray_theta += delta_theta_;
}
}

Ray RayCaster::Cast(const std::shared_ptr<Map>& map_ptr,
Ray RayCaster::Cast(const std::vector<std::vector<uint16_t>>& map_,
const uint16_t row_size, const uint16_t col_size,
const Position2D& position, const double ray_theta) {
const auto map_ = map_ptr->GetMap();
const auto& row_size = map_ptr->GetSizeX();
const auto& col_size = map_ptr->GetSizeY();
Ray ray;
vector2d ray_unit_step, ray_length_1d;
vector2i step, map_check;
Expand Down
4 changes: 3 additions & 1 deletion src/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ target_link_libraries(
game_object
map
vector
renderer
time_manager
collision_manager
texture_manager
Expand All @@ -43,4 +42,7 @@ target_link_libraries(
SDL2_ttf
enemy_state
single_raycaster
renderer_interface
renderer_2d
renderer_3d
)
8 changes: 6 additions & 2 deletions src/Core/include/Core/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
#include "Characters/player.h"
#include "CollisionManager/collision_manager.h"
#include "Core/scene.h"
#include "Graphics/renderer.h"
#include "Graphics/renderer_2d.h"
#include "Graphics/renderer_3d.h"
#include "Graphics/renderer_interface.h"
#include "Map/map.h"
#include "Math/vector.h"
#include "TextureManager/texture_manager.h"
#include "TimeManager/time_manager.h"
#include <memory>

namespace wolfenstein {

Expand Down Expand Up @@ -66,7 +69,8 @@ class Game
void PrepareDynamicObjects();
void PrepareStaticObjects();

std::shared_ptr<Renderer> renderer_;
std::shared_ptr<IRenderer> renderer_;
std::shared_ptr<RendererContext> renderer_context_;
std::shared_ptr<Scene> scene_;
std::shared_ptr<Player> player_;
std::shared_ptr<Map> map_;
Expand Down
24 changes: 13 additions & 11 deletions src/Core/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "Characters/enemy.h"
#include "GameObjects/dynamic_object.h"
#include "GameObjects/static_object.h"
#include "Graphics/renderer.h"
#include "Math/vector.h"
#include "NavigationManager/navigation_manager.h"
#include "ShootingManager/shooting_manager.h"
Expand Down Expand Up @@ -44,8 +43,11 @@ void Game::Init() {
config_.padding, config_.scale,
config_.fps, config_.view_distance,
config_.fov, config_.fullscreen};
renderer_ =
std::make_shared<Renderer>("Wolfenstein", render_config, camera_);

renderer_context_ = std::make_shared<RendererContext>(
"Wolfenstein", render_config, camera_);

renderer_ = std::make_shared<Renderer3D>(renderer_context_);

CharacterConfig player_config = {Position2D({3, 1.5}, 1.50), 2.0, 0.4, 0.4,
1.0};
Expand Down Expand Up @@ -107,10 +109,12 @@ void Game::CheckEvent() {
if (event.key.keysym.sym == SDLK_p &&
render_type_ == RenderType::TEXTURE) {
render_type_ = RenderType::LINE;
renderer_ = std::make_shared<Renderer2D>(renderer_context_);
}
else if (event.key.keysym.sym == SDLK_p &&
render_type_ == RenderType::LINE) {
render_type_ = RenderType::TEXTURE;
renderer_ = std::make_shared<Renderer3D>(renderer_context_);
}
}
}
Expand All @@ -121,14 +125,7 @@ void Game::Run() {
CheckEvent();
TimeManager::GetInstance().CalculateDeltaTime();
scene_->Update(TimeManager::GetInstance().GetDeltaTime());
switch (render_type_) {
case RenderType::TEXTURE:
renderer_->RenderScene(scene_);
break;
case RenderType::LINE:
renderer_->RenderScene2D(scene_);
break;
}
renderer_->RenderScene(scene_);
}
}

Expand All @@ -149,10 +146,15 @@ void Game::PrepareEnemies() {
"cyber_demon",
CharacterConfig(Position2D({23.0, 4}, 1.50), 0.8, 0.4, 0.5, 1.0));

auto cyber_demon_2 = EnemyFactory::CreateEnemy(
"cyber_demon",
CharacterConfig(Position2D({20.0, 4}, 1.50), 0.8, 0.4, 0.5, 1.0));

scene_->AddObject(caco_demon);
scene_->AddObject(soldier);
scene_->AddObject(soldier_2);
scene_->AddObject(cyber_demon);
scene_->AddObject(cyber_demon_2);
}

void Game::PrepareDynamicObjects() {
Expand Down
40 changes: 35 additions & 5 deletions src/Graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
add_library(
renderer
renderer_interface
STATIC
src/renderer.cpp
src/renderer_interface.cpp
)
target_include_directories(renderer PUBLIC
target_include_directories(renderer_interface PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(
renderer
renderer_interface
PUBLIC
camera
scene
texture_manager
navigation_manager
SDL2
SDL2_image
SDL2_ttf
)

add_library(
renderer_2d
STATIC
src/renderer_2d.cpp
)
target_include_directories(renderer_2d PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(
renderer_2d
PUBLIC
renderer_interface
)

add_library(
renderer_3d
STATIC
src/renderer_3d.cpp
)
target_include_directories(renderer_3d PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(
renderer_3d
PUBLIC
renderer_interface
)
124 changes: 0 additions & 124 deletions src/Graphics/include/Graphics/renderer.h

This file was deleted.

42 changes: 42 additions & 0 deletions src/Graphics/include/Graphics/renderer_2d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @file renderer_2d.h
* @author Bilal Kahraman ([email protected])
* @brief
* @version 0.1
* @date 2024-12-01
*
* @copyright Copyright (c) 2024
*
*/

#ifndef GRAPHICS_INCLUDE_GRAPHICS_RENDERER_2D_H_
#define GRAPHICS_INCLUDE_GRAPHICS_RENDERER_2D_H_

#include "Graphics/renderer_interface.h"

namespace wolfenstein {

class Renderer2D : public IRenderer
{
public:
using IRenderer::IRenderer;
~Renderer2D() = default;
void RenderScene(const std::shared_ptr<Scene>& scene_ptr) override;

private:
void RenderMap(const std::shared_ptr<Map> map_ptr);
void RenderPlayer(const std::shared_ptr<Player> player_ptr);
void RenderObjects(
const std::vector<std::shared_ptr<IGameObject>>& objects);
void RenderPaths(const std::vector<std::shared_ptr<Enemy>>& enemies);
void RenderCrosshairs(const std::vector<std::shared_ptr<Enemy>>& enemies);

void SetDrawColor(SDL_Color color);
void DrawFilledRectangle(vector2i start, vector2i end);
void DrawLine(vector2i start, vector2i end);

}; // class Renderer2D

} // namespace wolfenstein

#endif // GRAPHICS_INCLUDE_GRAPHICS_RENDERER_2D_H_
Loading

0 comments on commit d56e3f4

Please sign in to comment.