From 947910fc3c541ffdab3b042cfa3a3926b64f0ba3 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 6 Apr 2022 21:28:00 +0200 Subject: Streamlined instance management for Game and derived classes --- StarsEx/Game.cpp | 42 ++++++++++++++++++------------------------ StarsEx/Game.h | 3 +++ StarsEx/GameWinDX9.cpp | 8 ++++---- StarsEx/StarServer.cpp | 11 +++++++---- StarsEx/StarServer.h | 4 +++- StarsEx/Starshatter.cpp | 8 +++++--- StarsEx/Starshatter.h | 4 +++- 7 files changed, 43 insertions(+), 37 deletions(-) (limited to 'StarsEx') diff --git a/StarsEx/Game.cpp b/StarsEx/Game.cpp index 272dd17..3d3315e 100644 --- a/StarsEx/Game.cpp +++ b/StarsEx/Game.cpp @@ -23,40 +23,34 @@ #include "WndProc.h" #include "SoundCard.h" -// +--------------------------------------------------------------------+ -Game* game = 0; +Game* Game::instance {nullptr}; -// +--------------------------------------------------------------------+ Game::Game() : - world(0), - status(Game::OK), - exit_code(0), - game_mode(MENU_MODE) + world {nullptr}, + status {OK}, + exit_code {0}, + game_mode {MENU_MODE}, + active {false}, + paused {false}, + server {false}, + show_mouse {false}, + frame_number {0} { + if (instance != nullptr) + throw "Game may have only one instance"; + instance = this; Clock::Init(); - if (!game) { - game = this; - - active = false; - paused = false; - server = false; - show_mouse = false; - frame_number = 0; - } - else - status = TOO_MANY; } + Game::~Game() { - if (game == this) - game = 0; - Clock::Close(); - - delete world; + if (world) delete world; + if (instance == this) + instance = nullptr; } // +--------------------------------------------------------------------+ @@ -184,7 +178,7 @@ Game::UpdateScreen() Game* Game::GetInstance() { - return game; + return instance; } // +--------------------------------------------------------------------+ diff --git a/StarsEx/Game.h b/StarsEx/Game.h index 2aa1df7..48629c6 100644 --- a/StarsEx/Game.h +++ b/StarsEx/Game.h @@ -85,6 +85,9 @@ protected: bool server; bool show_mouse; DWORD frame_number; + +private: + static Game* instance; }; // +--------------------------------------------------------------------+ diff --git a/StarsEx/GameWinDX9.cpp b/StarsEx/GameWinDX9.cpp index 4b5b2d8..7099b90 100644 --- a/StarsEx/GameWinDX9.cpp +++ b/StarsEx/GameWinDX9.cpp @@ -24,7 +24,7 @@ #include "VideoSettings.h" -GameWinDX9* GameWinDX9::instance = nullptr; +GameWinDX9* GameWinDX9::instance {nullptr}; GameWinDX9* @@ -56,7 +56,7 @@ GameWinDX9::GameWinDX9() : is_device_restored {false} { if (instance != nullptr) - Panic::Panic("Multiple instances of GameWinDX9"); + throw "GameWinDX9 may have only one instance"; instance = this; video_settings = new VideoSettings; } @@ -64,13 +64,13 @@ GameWinDX9::GameWinDX9() : GameWinDX9::~GameWinDX9() { - if (instance != nullptr) - instance = nullptr; if (screen) delete screen; if (video_factory) delete video_factory; if (video) delete video; if (soundcard) delete soundcard; if (video_settings) delete video_settings; + if (instance == this) + instance = nullptr; } diff --git a/StarsEx/StarServer.cpp b/StarsEx/StarServer.cpp index fa0743d..eea6784 100644 --- a/StarsEx/StarServer.cpp +++ b/StarsEx/StarServer.cpp @@ -57,7 +57,7 @@ // +--------------------------------------------------------------------+ -StarServer* StarServer::instance = 0; +StarServer* StarServer::instance {nullptr}; static Mission* current_mission = 0; static double time_til_change = 0; @@ -69,7 +69,8 @@ StarServer::StarServer() : loader(0), time_mark(0), minutes(0), admin_server(0), lobby_server(0) { - if (!instance) + if (instance != nullptr) + throw "StarServer may have only one instance"; instance = this; server = true; @@ -96,6 +97,7 @@ admin_server(0), lobby_server(0) loader->EnableMedia(false); } + StarServer::~StarServer() { delete admin_server; @@ -124,9 +126,10 @@ StarServer::~StarServer() NetServerConfig::Close(); ModConfig::Close(); - instance = 0; - server = false; + + if (instance == this) + instance = nullptr; } // +--------------------------------------------------------------------+ diff --git a/StarsEx/StarServer.h b/StarsEx/StarServer.h index fa4828d..f1562bc 100644 --- a/StarsEx/StarServer.h +++ b/StarsEx/StarServer.h @@ -54,13 +54,15 @@ protected: virtual void UpdateWorld(); virtual void InstantiateMission(); - static StarServer* instance; NetServer* admin_server; NetLobbyServer* lobby_server; DataLoader* loader; DWORD time_mark; DWORD minutes; + +private: + static StarServer* instance; }; #endif // StarServer_h diff --git a/StarsEx/Starshatter.cpp b/StarsEx/Starshatter.cpp index 65c7d13..23ba839 100644 --- a/StarsEx/Starshatter.cpp +++ b/StarsEx/Starshatter.cpp @@ -121,7 +121,7 @@ Mission* quick_mission = 0; int Starshatter::keymap[256]; int Starshatter::keyalt[256]; -Starshatter* Starshatter::instance = 0; +Starshatter* Starshatter::instance {nullptr}; static Mission* current_mission = 0; static Mission* cutscene_mission = 0; @@ -158,7 +158,8 @@ lens_flare(true), corona(true), nebula(true), dust(0), load_step(0), load_progress(0), chat_mode(0), exit_time(1.2), cutscene(0) { - if (!instance) + if (instance != nullptr) + throw "Starshatter may have only one instance"; instance = this; app_name = "Starshatter: The Open Source Project"; @@ -311,7 +312,8 @@ Starshatter::~Starshatter() delete input; delete head_tracker; - instance = 0; + if (instance == this) + instance = nullptr; } void diff --git a/StarsEx/Starshatter.h b/StarsEx/Starshatter.h index ca30124..ecd1a62 100644 --- a/StarsEx/Starshatter.h +++ b/StarsEx/Starshatter.h @@ -132,7 +132,6 @@ protected: virtual bool ResizeVideo(); virtual void InitMouse(); - static Starshatter* instance; Window* gamewin; MenuScreen* menuscreen; LoadScreen* loadscreen; @@ -200,6 +199,9 @@ protected: NetLobby* net_lobby; int chat_mode; Text chat_text; + +private: + static Starshatter* instance; }; #endif // Starshatter_h -- cgit v1.1