Make game class singleton

This commit is contained in:
JackCarterSmith 2024-09-30 13:31:50 +02:00
parent 1946f16385
commit 969ba7e94a
Signed by: JackCarterSmith
GPG Key ID: 832E52F4E23F8F24
4 changed files with 45 additions and 11 deletions

View File

@ -20,9 +20,12 @@ set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation direc
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
# define project # define project
project(ProtoTank VERSION 0.1.0 DESCRIPTION "Arcade 80s-style game with tanks" LANGUAGES C;CXX) 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) #configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/config.h @ONLY)
# Compilation option
option(DISABLE_CPU_OPTI "Disable CPU optimizations" OFF)
include(FindPkgConfig) include(FindPkgConfig)
include(CheckIncludeFile) include(CheckIncludeFile)
include(CheckCSourceCompiles) include(CheckCSourceCompiles)
@ -34,13 +37,35 @@ include_directories(sfml::sfml)
# define src/headers files groups # define src/headers files groups
include(srcs.list) include(srcs.list)
set(BUILDNAME_SUFFIX _v${PROJECT_VERSION})
if(DISABLE_CPU_OPTI)
set(BUILDNAME_SUFFIX ${BUILDNAME_SUFFIX}_noSIMD)
endif()
# targets declarations # targets declarations
add_executable(${PROJECT_NAME} ${MAIN_SCRS} ${UTILS_SCRS} ${MISC_SCRS} ${GAME_SCRS} ${GRAPHS_SCRS} ${SOUNDS_SCRS}) 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) target_link_libraries(${PROJECT_NAME} sfml::sfml)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) set_target_properties(${PROJECT_NAME} PROPERTIES
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME}-v${PROJECT_VERSION}) OUTPUT_NAME ${PROJECT_NAME}${BUILDNAME_SUFFIX}
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
)
# targets build options
if(MSVC) if(MSVC)
# msvc does not append 'lib' - do it here to have consistent name # msvc does not append 'lib' - do it here to have consistent name
set_target_properties(AST PROPERTIES IMPORT_PREFIX "lib") set_target_properties(${PROJECT_NAME} PROPERTIES IMPORT_PREFIX "lib")
endif() endif()
target_link_libraries(${PROJECT_NAME} sfml::sfml) if (DISABLE_CPU_OPTI)
target_compile_definitions(${PROJECT_NAME} PUBLIC DISABLE_INTRINSICS)
endif()
# GPG signature custom command
#add_custom_command(
# OUTPUT ""
# COMMAND gpg --batch --detach-sign
# -o ${PROJECT_NAME}_${CI_SYS_TARGET}.gpg
# ${PROJECT_NAME}
# DEPENDS ${PROJECT_NAME}
# VERBATIM
#)

View File

@ -18,8 +18,9 @@ using std::make_unique;
std::unique_ptr<SysTimer> mPerfsTimer = nullptr; std::unique_ptr<SysTimer> mPerfsTimer = nullptr;
Game* Game::smInstance = nullptr;
Game::Game(bool dbg) : mbDbgModeEnabled(dbg) { Game::Game(bool dbgFlag) : mbDbgModeEnabled(dbgFlag) {
InitWindows(); InitWindows();
mCockpitUI = make_unique<CockpitUI>(); mCockpitUI = make_unique<CockpitUI>();

View File

@ -17,8 +17,13 @@ typedef enum eGameStatus {
class Game final { class Game final {
public: public:
Game(bool dbg) noexcept(false); static Game& getInstance(bool dbgFlag) {
~Game() {} if (smInstance == nullptr)
smInstance = new Game(dbgFlag);
return *smInstance;
}
~Game() = default;
Game(Game&&) = default; Game(Game&&) = default;
Game& operator= (Game&&) = default; Game& operator= (Game&&) = default;
@ -28,6 +33,9 @@ public:
GAME_STATUS Tick(SysTimer& time); GAME_STATUS Tick(SysTimer& time);
private: private:
Game(bool dbgFlag) noexcept(false);
static Game* smInstance;
void InitWindows(); void InitWindows();
void Update(); void Update();
void Render(); void Render();

View File

@ -14,10 +14,10 @@ int main(int argc, char** argv) {
GAME_STATUS status = GAME_INIT; GAME_STATUS status = GAME_INIT;
std::cout << "[" << mSysTimer.GetElapsedTimeS() << "] Init game instance" << std::endl; std::cout << "[" << mSysTimer.GetElapsedTimeS() << "] Init game instance" << std::endl;
auto arcadeGame = std::make_unique<Game>(true); Game& arcadeGame = Game::getInstance(true);
for ( ;; ) { for ( ;; ) {
status = arcadeGame->Tick(mSysTimer); status = arcadeGame.Tick(mSysTimer);
if (status == GAME_QUIT) if (status == GAME_QUIT)
break; break;