From f833023bf95274156da972488d68c14e95432f88 Mon Sep 17 00:00:00 2001 From: Bilal Kahraman Date: Sun, 1 Dec 2024 20:38:15 +0300 Subject: [PATCH] Update game --- src/Core/CMakeLists.txt | 2 + src/Core/include/Core/game.h | 9 +++- src/Core/src/game.cpp | 82 +++++++++++++++++++++++++++++++++--- src/Graphics/CMakeLists.txt | 30 +++++++++++++ 4 files changed, 116 insertions(+), 7 deletions(-) diff --git a/src/Core/CMakeLists.txt b/src/Core/CMakeLists.txt index c250b12..bc3cb8b 100644 --- a/src/Core/CMakeLists.txt +++ b/src/Core/CMakeLists.txt @@ -45,4 +45,6 @@ target_link_libraries( renderer_interface renderer_2d renderer_3d + renderer_menu + renderer_result ) diff --git a/src/Core/include/Core/game.h b/src/Core/include/Core/game.h index bf560b4..72d403a 100644 --- a/src/Core/include/Core/game.h +++ b/src/Core/include/Core/game.h @@ -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" @@ -62,7 +64,8 @@ class Game private: void Init(); - void CheckEvent(); + void CheckGameEvent(); + void CheckMenuEvent(); void Update(); void Render(); void PrepareEnemies(); @@ -70,13 +73,17 @@ class Game void PrepareStaticObjects(); std::shared_ptr renderer_; + std::shared_ptr menu_; std::shared_ptr renderer_context_; + std::shared_ptr renderer_result_; std::shared_ptr scene_; std::shared_ptr player_; std::shared_ptr map_; GeneralConfig config_; bool is_running_; + bool is_menu_; + bool is_result_; RenderType render_type_; }; diff --git a/src/Core/src/game.cpp b/src/Core/src/game.cpp index a7b28c0..0fc5ed7 100644 --- a/src/Core/src/game.cpp +++ b/src/Core/src/game.cpp @@ -10,6 +10,7 @@ #include "State/enemy_state.h" #include "TextureManager/texture_manager.h" #include "TimeManager/time_manager.h" +#include #include #include #include @@ -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( "Wolfenstein", render_config, camera_); renderer_ = std::make_shared(renderer_context_); + renderer_->SetScene(scene_); + + menu_ = std::make_shared(renderer_context_); CharacterConfig player_config = {Position2D({3, 1.5}, 1.50), 2.0, 0.4, 0.4, 1.0}; @@ -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 @@ -110,22 +149,53 @@ void Game::CheckEvent() { render_type_ == RenderType::TEXTURE) { render_type_ = RenderType::LINE; renderer_ = std::make_shared(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(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( + 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(); } } diff --git a/src/Graphics/CMakeLists.txt b/src/Graphics/CMakeLists.txt index c9566b5..acb62bf 100644 --- a/src/Graphics/CMakeLists.txt +++ b/src/Graphics/CMakeLists.txt @@ -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 +) \ No newline at end of file