Skip to content

Commit

Permalink
Update game
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalkah committed Dec 1, 2024
1 parent 2a647ba commit f833023
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ target_link_libraries(
renderer_interface
renderer_2d
renderer_3d
renderer_menu
renderer_result
)
9 changes: 8 additions & 1 deletion src/Core/include/Core/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "Graphics/renderer_2d.h"
#include "Graphics/renderer_3d.h"
#include "Graphics/renderer_interface.h"
#include "Graphics/renderer_menu.h"
#include "Graphics/renderer_result.h"
#include "Map/map.h"
#include "Math/vector.h"
#include "TextureManager/texture_manager.h"
Expand Down Expand Up @@ -62,21 +64,26 @@ class Game

private:
void Init();
void CheckEvent();
void CheckGameEvent();
void CheckMenuEvent();
void Update();
void Render();
void PrepareEnemies();
void PrepareDynamicObjects();
void PrepareStaticObjects();

std::shared_ptr<IRenderer> renderer_;
std::shared_ptr<Menu> menu_;
std::shared_ptr<RendererContext> renderer_context_;
std::shared_ptr<RendererResult> renderer_result_;
std::shared_ptr<Scene> scene_;
std::shared_ptr<Player> player_;
std::shared_ptr<Map> map_;

GeneralConfig config_;
bool is_running_;
bool is_menu_;
bool is_result_;
RenderType render_type_;
};

Expand Down
82 changes: 76 additions & 6 deletions src/Core/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "State/enemy_state.h"
#include "TextureManager/texture_manager.h"
#include "TimeManager/time_manager.h"
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_video.h>
#include <functional>
#include <memory>
Expand Down Expand Up @@ -43,11 +44,14 @@ void Game::Init() {
config_.padding, config_.scale,
config_.fps, config_.view_distance,
config_.fov, config_.fullscreen};

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

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

menu_ = std::make_shared<Menu>(renderer_context_);

CharacterConfig player_config = {Position2D({3, 1.5}, 1.50), 2.0, 0.4, 0.4,
1.0};
Expand Down Expand Up @@ -78,19 +82,54 @@ void Game::Init() {
ShootingManager::GetInstance().InitManager(map_, player_,
scene_->GetEnemies());
NavigationManager::GetInstance().InitManager(map_, scene_->GetEnemies());

is_running_ = true;
TimeManager::GetInstance().InitClock();
is_running_ = false;
is_result_ = false;
is_menu_ = true;
}

void Game::CheckMenuEvent() {

SDL_Event event;
while (SDL_PollEvent(&event)) {
// When user close the window
if (event.type == SDL_QUIT ||
(event.type == SDL_EventType::SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_ESCAPE)) {
is_running_ = false;
is_menu_ = false;
}

// When user press a key
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_SPACE) {
player_->SetWeapon(menu_->GetSelectedWeapon());
is_menu_ = false;
is_running_ = true;
}
// left arrow
if (event.key.keysym.sym == SDLK_LEFT) {
menu_->ChangeSelection(-1);
}
// right arrow
if (event.key.keysym.sym == SDLK_RIGHT) {
menu_->ChangeSelection(1);
}
}
}
}

void Game::CheckEvent() {
void Game::CheckGameEvent() {

SDL_Event event;
while (SDL_PollEvent(&event)) {

// When user close the window
if (event.type == SDL_QUIT ||
(event.type == SDL_EventType::SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_ESCAPE)) {
is_running_ = false;
is_menu_ = false;
}

// When user alt tab the window
Expand All @@ -110,22 +149,53 @@ void Game::CheckEvent() {
render_type_ == RenderType::TEXTURE) {
render_type_ = RenderType::LINE;
renderer_ = std::make_shared<Renderer2D>(renderer_context_);
renderer_->SetScene(scene_);
}
else if (event.key.keysym.sym == SDLK_p &&
render_type_ == RenderType::LINE) {
render_type_ = RenderType::TEXTURE;
renderer_ = std::make_shared<Renderer3D>(renderer_context_);
renderer_->SetScene(scene_);
}
}
}
}

void Game::Run() {

while (is_menu_) {
CheckMenuEvent();
TimeManager::GetInstance().CalculateDeltaTime();
menu_->Render();
SDL_WarpMouseInWindow(nullptr, 400, 300);
}

while (is_running_) {
CheckEvent();
CheckGameEvent();
TimeManager::GetInstance().CalculateDeltaTime();
scene_->Update(TimeManager::GetInstance().GetDeltaTime());
renderer_->RenderScene(scene_);
renderer_->RenderScene();
// Check if player is dead
if (!player_->IsAlive()) {
is_running_ = false;
is_result_ = true;
renderer_result_ = std::make_shared<RendererResult>(
renderer_context_,
TextureManager::GetInstance().GetTexture(10));
}
}

while (is_result_) {
SDL_Event event;
SDL_PollEvent(&event);
if (event.type == SDL_QUIT ||
(event.type == SDL_EventType::SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_ESCAPE)) {
is_result_ = false;
}

TimeManager::GetInstance().CalculateDeltaTime();
renderer_result_->Render();
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/Graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,33 @@ target_link_libraries(
PUBLIC
renderer_interface
)

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

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

0 comments on commit f833023

Please sign in to comment.