From ed206fb71dcbaaeb3afc76dbf90ca609874718e4 Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Sat, 14 Sep 2024 16:28:15 +0200 Subject: [PATCH] Create the main window --- .gitignore | 8 ++----- CMakeLists.txt | 14 ++++++++++++ Game.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ Game.hpp | 41 ++++++++++++++++++++++++++++++++++ ProtoTank.cpp | 33 ++++++++++++++------------- conanfile.py | 2 +- srcs.list | 30 ++++++++++++------------- 7 files changed, 149 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index b6c05cb..3a7a465 100644 --- a/.gitignore +++ b/.gitignore @@ -46,12 +46,7 @@ CTestTestfile.cmake _deps # ---> VisualStudioCode -.vscode/* -!.vscode/settings.json -!.vscode/tasks.json -!.vscode/launch.json -!.vscode/extensions.json -!.vscode/*.code-snippets +.vscode/ .cmake/ .vsconan/ @@ -61,5 +56,6 @@ _deps # Built Visual Studio Code Extensions *.vsix +bin/ build/ CMakeUserPresets.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 256c42d..1729e35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,19 @@ cmake_minimum_required(VERSION 3.23) cmake_policy(VERSION 3.23) +if(NOT DEFINED PROJECT_BINARY_DIR) + set(PROJECT_BINARY_DIR ".") +endif() + +if(NOT MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") +else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Wall") +endif() + +set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables") +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin) + # define project project(ProtoTank VERSION 0.1.0 DESCRIPTION "Arcade 80s-style game with tanks" LANGUAGES C;CXX) #configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/config.h @ONLY) @@ -24,6 +37,7 @@ include(srcs.list) # targets declarations add_executable(${PROJECT_NAME} ${MAIN_SCRS} ${UTILS_SCRS} ${MISC_SCRS} ${GAME_SCRS} ${GRAPHS_SCRS} ${SOUNDS_SCRS}) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17) +set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME}-v${PROJECT_VERSION}) if(MSVC) # msvc does not append 'lib' - do it here to have consistent name diff --git a/Game.cpp b/Game.cpp index e69de29..ac07ccc 100644 --- a/Game.cpp +++ b/Game.cpp @@ -0,0 +1,60 @@ +#include "Game.hpp" + +#include +#include + +using std::make_shared; +using std::make_unique; + + +Game::Game(bool dbg) : gbDbgModeEnabled(dbg) { + InitWindows(); + if (gbDbgModeEnabled) + gDbgUI = make_unique(); +} + +void Game::InitWindows() { + // Create default game window + sf::ContextSettings sWindowSettings; + sWindowSettings.depthBits = 0; + sWindowSettings.stencilBits = 0; + sWindowSettings.antialiasingLevel = 8; + sWindowSettings.sRgbCapable = true; + + gMainWindow = make_shared( + sf::VideoMode(1080, 720), "ProtoTank", + (sf::Style::Close | sf::Style::Titlebar), + sWindowSettings + ); + gMainWindow->setVerticalSyncEnabled(false); + gMainWindow->setFramerateLimit(60); + gMainWindow->setKeyRepeatEnabled(false); + gMainWindow->setMouseCursorVisible(true); +} + +GAME_STATUS Game::Update() { + gLoopTimer.restart(); + + sf::Event event; + while (gMainWindow->pollEvent(event)) { + if (event.type == sf::Event::Closed) + gMainWindow->close(); + } + + /* + auto ticks = static_cast(mLoopTimer.getElapsedTime().asMilliseconds()); + gTicksMonitoring.push_back(ticks); + double cpuUsage = std::accumulate(gTicksMonitoring.begin(), gTicksMonitoring.end(), 0) / gTicksMonitoring.size(); + */ + + if (gMainWindow->isOpen()) + return GAME_RUNNING; + else + return GAME_QUIT; +} + +void Game::Render() { + gMainWindow->clear(); + //gDbgUI->UpdateDebugData(gMainWindow); + gMainWindow->display(); +} \ No newline at end of file diff --git a/Game.hpp b/Game.hpp index e69de29..c3d5e7d 100644 --- a/Game.hpp +++ b/Game.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include +#include +#include +#include + +#include "Engine/Graphics/DebugUI.hpp" + + +typedef enum eGameStatus { + GAME_INIT, + GAME_RUNNING, + GAME_QUIT +} GAME_STATUS; + +class Game final{ +public: + Game(bool dbg) noexcept(false); + ~Game() {} + + Game(Game&&) = default; + Game& operator= (Game&&) = default; + Game(Game const&) = delete; + Game& operator= (Game const&) = delete; + + GAME_STATUS Update(); + void Render(); + +private: + void InitWindows(); + + bool gbDbgModeEnabled; + + std::shared_ptr gMainWindow = nullptr; + std::unique_ptr gDbgUI = nullptr; + + sf::Clock gLoopTimer; + std::vector gTicksMonitoring; + +}; \ No newline at end of file diff --git a/ProtoTank.cpp b/ProtoTank.cpp index a3c3a01..4e32c06 100644 --- a/ProtoTank.cpp +++ b/ProtoTank.cpp @@ -1,29 +1,28 @@ #include #include +//#include +#include +#include -#include +#include "Game.hpp" -#ifdef __AVX2__ -#endif +using std::cout; +using std::endl; int main(int argc, char** argv) { - sf::RenderWindow window(sf::VideoMode(480, 240), "ProtoTank"); - sf::CircleShape shape(100.f); - shape.setFillColor(sf::Color::Green); + GAME_STATUS status = GAME_INIT; - while (window.isOpen()) - { - sf::Event event; - while (window.pollEvent(event)) - { - if (event.type == sf::Event::Closed) - window.close(); - } + cout << "Init game instance" << endl; + auto arcadeGame = std::make_unique(true); - window.clear(); - window.draw(shape); - window.display(); + for ( ;; ) { + status = arcadeGame->Update(); + + if (status == GAME_QUIT) + break; + + arcadeGame->Render(); } return EXIT_SUCCESS; diff --git a/conanfile.py b/conanfile.py index aaf29aa..bc5d5d5 100644 --- a/conanfile.py +++ b/conanfile.py @@ -44,7 +44,7 @@ class ProtoTank(ConanFile): tc.generate() for dep in self.dependencies.values(): if self.settings.os == "Windows" and len(dep.cpp_info.bindirs) > 0: - copy(self, "*.dll", dep.cpp_info.bindirs[0], self.build_folder) + copy(self, "*.dll", dep.cpp_info.bindirs[0], self.build_folder + "/bin") # def package(self): # copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses")) diff --git a/srcs.list b/srcs.list index 5e17db7..3b31617 100644 --- a/srcs.list +++ b/srcs.list @@ -1,27 +1,27 @@ set(MAIN_SCRS -ProtoTank.cpp -Game.cpp -Game.hpp + ProtoTank.cpp + Game.cpp + Game.hpp ) set(UTILS_SCRS -Engine/Utils/3DMaths.cpp -Engine/Utils/3DMaths.hpp + Engine/Utils/3DMaths.cpp + Engine/Utils/3DMaths.hpp ) set(MISC_SCRS -Engine/Misc/Console.cpp -Engine/Misc/Console.hpp -Engine/Misc/Logger.cpp -Engine/Misc/Logger.hpp + Engine/Misc/Console.cpp + Engine/Misc/Console.hpp + Engine/Misc/Logger.cpp + Engine/Misc/Logger.hpp ) set(GAME_SCRS -Engine/World/Arena.cpp -Engine/World/Player.cpp -Engine/World/Tank.cpp + Engine/World/Arena.cpp + Engine/World/Player.cpp + Engine/World/Tank.cpp ) set(GRAPHS_SCRS -Engine/Graphics/3DGraphics.cpp -Engine/Graphics/Camera.cpp -Engine/Graphics/UI.cpp + Engine/Graphics/3DGraphics.cpp + Engine/Graphics/Camera.cpp + Engine/Graphics/UI.cpp ) set(SOUNDS_SCRS