From 8b778dda61c45f5d7a4dc416478c42ff4aa1de7f Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 23 Feb 2022 23:13:27 +0100 Subject: Moved content management out to ContentBundle --- Stars45/ContentBundle.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++--- Stars45/ContentBundle.h | 10 ++++++- Stars45/Game.cpp | 58 ++-------------------------------------- Stars45/Game.h | 9 +------ Stars45/ModConfig.cpp | 3 ++- 5 files changed, 78 insertions(+), 70 deletions(-) (limited to 'Stars45') diff --git a/Stars45/ContentBundle.cpp b/Stars45/ContentBundle.cpp index 9f09833..534f550 100644 --- a/Stars45/ContentBundle.cpp +++ b/Stars45/ContentBundle.cpp @@ -18,12 +18,23 @@ // +--------------------------------------------------------------------+ +ContentBundle* ContentBundle::GetInstance() +{ + static ContentBundle instance{}; + return &instance; +} + +// +--------------------------------------------------------------------+ + +ContentBundle::ContentBundle() : ContentBundle("content", nullptr) +{ +} + +// +--------------------------------------------------------------------+ + ContentBundle::ContentBundle(const char* bundle, Locale* locale) { - Text file = FindFile(bundle, locale); - if (file.length() > 0) { - LoadBundle(file); - } + Load(bundle, locale); } // +--------------------------------------------------------------------+ @@ -34,6 +45,55 @@ ContentBundle::~ContentBundle() // +--------------------------------------------------------------------+ +bool +ContentBundle::Init() +{ + DataLoader* loader = DataLoader::GetLoader(); + List bundles; + + loader->SetDataPath("Content/"); + loader->ListFiles("content*", bundles); + + ListIter iter = bundles; + while (++iter) { + Text* filename = iter.value(); + int n = filename->indexOf('_'); + if (n > 0) { + Locale::ParseLocale(filename->data() + n); // unused? + } + } + + loader->SetDataPath(0); + return true; +} + +// +--------------------------------------------------------------------+ + +void +ContentBundle::UseLocale(Locale* locale) +{ + DataLoader* loader = DataLoader::GetLoader(); + loader->SetDataPath("Content/"); + Load("content", locale); + loader->SetDataPath(nullptr); +} + +// +--------------------------------------------------------------------+ + +void +ContentBundle::Load(const char* bundle, Locale* locale) +{ + if (values.size() > 0) { + values.clear(); + } + Text file = FindFile(bundle, locale); + if (file.length() > 0) { + LoadBundle(file); + } +} + +// +--------------------------------------------------------------------+ + Text ContentBundle::GetText(const char* key) const { diff --git a/Stars45/ContentBundle.h b/Stars45/ContentBundle.h index 346b509..7c23c66 100644 --- a/Stars45/ContentBundle.h +++ b/Stars45/ContentBundle.h @@ -26,10 +26,18 @@ class ContentBundle public: static const char* TYPENAME() { return "ContentBundle"; } + static ContentBundle* GetInstance(); + + ContentBundle(); ContentBundle(const char* bundle, Locale* locale); virtual ~ContentBundle(); - int operator == (const ContentBundle& that) const { return this == &that; } + bool Init(); + void UseLocale(Locale* locale); + void Load(const char* bundle, Locale* locale); + + int operator == (const ContentBundle& that) = delete; + ContentBundle(const ContentBundle& that) = delete; const Text& GetName() const { return name; } Text GetText(const char* key) const; diff --git a/Stars45/Game.cpp b/Stars45/Game.cpp index 4a2e8d8..5ec6c0c 100644 --- a/Stars45/Game.cpp +++ b/Stars45/Game.cpp @@ -46,7 +46,7 @@ Game::Game() : world(0), video_factory(0), video(0), video_settings(0), soundcard(0), gamma(128), max_tex_size(2048), screen(0), totaltime(0), hInst(0), hwnd(0), frame_rate(0), frame_count(0), frame_count0(0), - frame_time(0), frame_time0(0), gui_seconds(0), content(0), + frame_time(0), frame_time0(0), gui_seconds(0), status(Game::OK), exit_code(0), window_style(0) { if (!game) { @@ -87,7 +87,6 @@ Game::~Game() if (game == this) game = 0; - delete content; delete world; delete screen; delete video_factory; @@ -127,17 +126,6 @@ bool Game::IsWindowed() // +--------------------------------------------------------------------+ -Text -Game::GetText(const char* key) -{ - if (game && game->content && game->content->IsLoaded()) - return game->content->GetText(key); - - return key; -} - -// +--------------------------------------------------------------------+ - int Game::GammaLevel() { @@ -276,7 +264,7 @@ Game::Init(HINSTANCE hi, HINSTANCE hpi, LPSTR cmdline, int nCmdShow) if (status == OK) { Print(" Initializing content...\n"); - InitContent(); + ContentBundle::GetInstance()->Init(); Print(" Initializing game...\n"); if (!InitGame()) { @@ -712,48 +700,6 @@ Game::InitGame() return true; } - -// +--------------------------------------------------------------------+ - -bool -Game::InitContent() -{ - DataLoader* loader = DataLoader::GetLoader(); - List bundles; - - loader->SetDataPath("Content/"); - loader->ListFiles("content*", bundles); - - ListIter iter = bundles; - while (++iter) { - Text* filename = iter.value(); - int n = filename->indexOf('_'); - - if (n > 0) { - Locale::ParseLocale(filename->data() + n); - } - else { - delete content; - content = new(__FILE__,__LINE__) ContentBundle("content", 0); - } - } - - loader->SetDataPath(0); - return true; -} - -void -Game::UseLocale(Locale* locale) -{ - DataLoader* loader = DataLoader::GetLoader(); - loader->SetDataPath("Content/"); - delete content; - - content = new(__FILE__,__LINE__) ContentBundle("content", locale); - - loader->SetDataPath(0); -} - // +--------------------------------------------------------------------+ bool diff --git a/Stars45/Game.h b/Stars45/Game.h index 6245b40..b86e0fc 100644 --- a/Stars45/Game.h +++ b/Stars45/Game.h @@ -26,8 +26,6 @@ void ProcessKeyMessage(); // +--------------------------------------------------------------------+ -class ContentBundle; -class Locale; class Universe; class Sound; class SoundCard; @@ -69,7 +67,7 @@ public: static Game* GetInstance(); static void Panic(const char* msg=0); - static const char* GetPanicMessage() { return panicbuf; } + static const char* GetPanicMessage() { return panicbuf; } bool DisplayModeSupported(int w, int h, int bpp); int MaxTexSize(); @@ -109,9 +107,6 @@ public: HINSTANCE GetHINST(); HWND GetHWND(); - void UseLocale(Locale* locale); - Text GetText(const char* key); - virtual bool GameLoop(); virtual void UpdateWorld(); virtual void GameState(); @@ -120,7 +115,6 @@ public: virtual bool InitApplication(HINSTANCE); virtual bool InitInstance(HINSTANCE, int); - virtual bool InitContent(); virtual bool InitGame(); virtual bool InitVideo(); virtual bool ResizeVideo(); @@ -135,7 +129,6 @@ public: protected: friend LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM uParam, LPARAM lParam); - ContentBundle* content; Universe* world; VideoFactory* video_factory; Video* video; diff --git a/Stars45/ModConfig.cpp b/Stars45/ModConfig.cpp index f91a0dd..0839e6e 100644 --- a/Stars45/ModConfig.cpp +++ b/Stars45/ModConfig.cpp @@ -21,6 +21,7 @@ #include "Starshatter.h" #include "ParseUtil.h" #include "DataLoader.h" +#include "ContentBundle.h" // +-------------------------------------------------------------------+ @@ -254,7 +255,7 @@ ModConfig::Deploy() } Print("\n"); - Game::GetInstance()->UseLocale(0); + ContentBundle::GetInstance()->UseLocale(nullptr); } void -- cgit v1.1