From fd320767c5b3c5061a71e46675c373a3001380d5 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 3 Aug 2014 23:20:39 +1000 Subject: [PATCH] Implemented the changes to ConfigUtility in the client and server The server's changes were easy. The clients means that the constructors for each scene have one less argument, and each scene has one less member. The exception to this is LobbyMenu, where the config is used in multiple places, so it was easier to have the config's reference as a member. To replace the config's usage, I added this line in most cases: ConfigUtility& config = ConfigUtility::GetSingleton(); The only requirement is that ConfigUtility::Create() and ConfigUtility::Delete() are called from the main() function. --- client/client_application.cpp | 16 +++++++++------- client/client_application.hpp | 2 -- client/main.cpp | 12 +++++++++++- client/scenes/clean_up.cpp | 5 +++-- client/scenes/clean_up.hpp | 3 --- client/scenes/in_combat.cpp | 3 +-- client/scenes/in_combat.hpp | 3 --- client/scenes/in_world.cpp | 7 ++++--- client/scenes/in_world.hpp | 3 --- client/scenes/lobby_menu.cpp | 2 -- client/scenes/lobby_menu.hpp | 3 +-- client/scenes/main_menu.cpp | 8 +++++--- client/scenes/main_menu.hpp | 6 +----- client/scenes/options_menu.cpp | 8 +++++--- client/scenes/options_menu.hpp | 6 +----- client/scenes/splash_screen.cpp | 8 ++++---- client/scenes/splash_screen.hpp | 6 +----- rsc/config.cfg | 2 ++ server/main.cpp | 10 ++++++++++ server/server_application.hpp | 2 +- 20 files changed, 59 insertions(+), 56 deletions(-) diff --git a/client/client_application.cpp b/client/client_application.cpp index 991cfd6..d50dcd2 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -22,6 +22,7 @@ #include "client_application.hpp" #include "serial.hpp" +#include "config_utility.hpp" #include #include @@ -48,6 +49,7 @@ void ClientApplication::Init(int argc, char** argv) { std::cout << "Beginning " << argv[0] << std::endl; //load the prerequisites + ConfigUtility& config = ConfigUtility::GetSingleton(); config.Load("rsc\\config.cfg"); //------------------------- @@ -165,25 +167,25 @@ void ClientApplication::LoadScene(SceneList sceneIndex) { //add scene creation calls here case SceneList::FIRST: case SceneList::SPLASHSCREEN: - activeScene = new SplashScreen(&config); + activeScene = new SplashScreen(); break; case SceneList::MAINMENU: - activeScene = new MainMenu(&config); + activeScene = new MainMenu(); break; case SceneList::OPTIONSMENU: - activeScene = new OptionsMenu(&config); + activeScene = new OptionsMenu(); break; case SceneList::LOBBYMENU: - activeScene = new LobbyMenu(&config, &network, &clientIndex, &accountIndex); + activeScene = new LobbyMenu(&network, &clientIndex, &accountIndex); break; case SceneList::INWORLD: - activeScene = new InWorld(&config, &network, &clientIndex, &accountIndex, &characterIndex, &characterMap); + activeScene = new InWorld(&network, &clientIndex, &accountIndex, &characterIndex, &characterMap); break; case SceneList::INCOMBAT: - activeScene = new InCombat(&config, &network, &clientIndex, &accountIndex, &characterIndex, &characterMap); + activeScene = new InCombat(&network, &clientIndex, &accountIndex, &characterIndex, &characterMap); break; case SceneList::CLEANUP: - activeScene = new CleanUp(&config, &network, &clientIndex, &accountIndex, &characterIndex, &characterMap); + activeScene = new CleanUp(&network, &clientIndex, &accountIndex, &characterIndex, &characterMap); break; default: throw(std::logic_error("Failed to recognize the scene index")); diff --git a/client/client_application.hpp b/client/client_application.hpp index 2efb742..e0e67c7 100644 --- a/client/client_application.hpp +++ b/client/client_application.hpp @@ -25,7 +25,6 @@ #include "scene_list.hpp" #include "base_scene.hpp" -#include "config_utility.hpp" #include "udp_network_utility.hpp" #include "character.hpp" @@ -48,7 +47,6 @@ private: BaseScene* activeScene = nullptr; //shared parameters - ConfigUtility config; UDPNetworkUtility network; int clientIndex = -1; int accountIndex = -1; diff --git a/client/main.cpp b/client/main.cpp index 156adff..cc3fc08 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -21,6 +21,9 @@ */ #include "client_application.hpp" +//singletons +#include "config_utility.hpp" + #include #include @@ -28,14 +31,21 @@ using namespace std; int main(int argc, char** argv) { try { + //create the singletons + ConfigUtility::Create(); + + //call the server's routines ClientApplication app; app.Init(argc, argv); app.Proc(); app.Quit(); + + //delete the singletons + ConfigUtility::Delete(); } catch(exception& e) { cerr << "Fatal exception thrown: " << e.what() << endl; return 1; } return 0; -} +} \ No newline at end of file diff --git a/client/scenes/clean_up.cpp b/client/scenes/clean_up.cpp index 42ab69c..87846d5 100644 --- a/client/scenes/clean_up.cpp +++ b/client/scenes/clean_up.cpp @@ -22,6 +22,7 @@ #include "clean_up.hpp" #include "channels.hpp" +#include "config_utility.hpp" #include @@ -30,20 +31,20 @@ //------------------------- CleanUp::CleanUp( - ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, CharacterMap* argCharacterMap ): - config(*argConfig), network(*argNetwork), clientIndex(*argClientIndex), accountIndex(*argAccountIndex), characterIndex(*argCharacterIndex), characterMap(*argCharacterMap) { + ConfigUtility& config = ConfigUtility::GetSingleton(); + //setup the utility objects image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); image.SetClipH(image.GetClipH()/3); diff --git a/client/scenes/clean_up.hpp b/client/scenes/clean_up.hpp index 5586f5c..3ba76ce 100644 --- a/client/scenes/clean_up.hpp +++ b/client/scenes/clean_up.hpp @@ -31,7 +31,6 @@ #include "button.hpp" //common -#include "config_utility.hpp" #include "frame_rate.hpp" #include "character.hpp" @@ -46,7 +45,6 @@ class CleanUp : public BaseScene { public: //Public access members CleanUp( - ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, @@ -70,7 +68,6 @@ protected: void KeyUp(SDL_KeyboardEvent const&); //shared parameters - ConfigUtility& config; UDPNetworkUtility& network; int& clientIndex; int& accountIndex; diff --git a/client/scenes/in_combat.cpp b/client/scenes/in_combat.cpp index 68c1bec..358bd08 100644 --- a/client/scenes/in_combat.cpp +++ b/client/scenes/in_combat.cpp @@ -23,6 +23,7 @@ #include "channels.hpp" #include "utility.hpp" +#include "config_utility.hpp" #include @@ -31,14 +32,12 @@ //------------------------- InCombat::InCombat( - ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, CharacterMap* argCharacterMap ): - config(*argConfig), network(*argNetwork), clientIndex(*argClientIndex), accountIndex(*argAccountIndex), diff --git a/client/scenes/in_combat.hpp b/client/scenes/in_combat.hpp index 6a84851..2f5cd20 100644 --- a/client/scenes/in_combat.hpp +++ b/client/scenes/in_combat.hpp @@ -31,7 +31,6 @@ #include "button.hpp" //common -#include "config_utility.hpp" #include "frame_rate.hpp" #include "character.hpp" @@ -43,7 +42,6 @@ class InCombat : public BaseScene { public: //Public access members InCombat( - ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, @@ -79,7 +77,6 @@ protected: void RequestShutdown(); //shared parameters - ConfigUtility& config; UDPNetworkUtility& network; int& clientIndex; int& accountIndex; diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 59f43ba..dac3bce 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -23,6 +23,7 @@ #include "channels.hpp" #include "utility.hpp" +#include "config_utility.hpp" #include #include @@ -34,20 +35,20 @@ //------------------------- InWorld::InWorld( - ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, CharacterMap* argCharacterMap ): - config(*argConfig), network(*argNetwork), clientIndex(*argClientIndex), accountIndex(*argAccountIndex), characterIndex(*argCharacterIndex), characterMap(*argCharacterMap) { + ConfigUtility& config = ConfigUtility::GetSingleton(); + //setup the utility objects buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp"); buttonImage.SetClipH(buttonImage.GetClipH()/3); @@ -302,7 +303,7 @@ void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) { newCharacter.SetHandle(argPacket->handle); newCharacter.SetAvatar(argPacket->avatar); - newCharacter.GetSprite()->LoadSurface(config["dir.sprites"] + newCharacter.GetAvatar(), 4, 4); + newCharacter.GetSprite()->LoadSurface(ConfigUtility::GetSingleton()["dir.sprites"] + newCharacter.GetAvatar(), 4, 4); newCharacter.SetOrigin(argPacket->origin); newCharacter.SetMotion(argPacket->motion); diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 34ce942..8dddfdf 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -35,7 +35,6 @@ #include "tile_sheet.hpp" //common -#include "config_utility.hpp" #include "frame_rate.hpp" #include "character.hpp" @@ -50,7 +49,6 @@ class InWorld : public BaseScene { public: //Public access members InWorld( - ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, @@ -94,7 +92,6 @@ protected: void UpdateMap(); //shared parameters - ConfigUtility& config; UDPNetworkUtility& network; int& clientIndex; int& accountIndex; diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index f4b5e32..2e71a89 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -31,12 +31,10 @@ //------------------------- LobbyMenu::LobbyMenu( - ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex ): - config(*argConfig), network(*argNetwork), clientIndex(*argClientIndex), accountIndex(*argAccountIndex) diff --git a/client/scenes/lobby_menu.hpp b/client/scenes/lobby_menu.hpp index a27c1a9..96afaa0 100644 --- a/client/scenes/lobby_menu.hpp +++ b/client/scenes/lobby_menu.hpp @@ -41,7 +41,6 @@ class LobbyMenu : public BaseScene { public: //Public access members LobbyMenu( - ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex @@ -68,7 +67,7 @@ protected: void HandleJoinResponse(ClientPacket* const); //shared parameters - ConfigUtility& config; + ConfigUtility& config = ConfigUtility::GetSingleton(); UDPNetworkUtility& network; int& clientIndex; int& accountIndex; diff --git a/client/scenes/main_menu.cpp b/client/scenes/main_menu.cpp index d722141..ca82ef8 100644 --- a/client/scenes/main_menu.cpp +++ b/client/scenes/main_menu.cpp @@ -21,13 +21,15 @@ */ #include "main_menu.hpp" +#include "config_utility.hpp" + //------------------------- //Public access members //------------------------- -MainMenu::MainMenu(ConfigUtility* const argConfig): - config(*argConfig) -{ +MainMenu::MainMenu() { + ConfigUtility& config = ConfigUtility::GetSingleton(); + //setup the utility objects image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); image.SetClipH(image.GetClipH()/3); diff --git a/client/scenes/main_menu.hpp b/client/scenes/main_menu.hpp index 3816482..2dec3ae 100644 --- a/client/scenes/main_menu.hpp +++ b/client/scenes/main_menu.hpp @@ -24,7 +24,6 @@ #include "base_scene.hpp" -#include "config_utility.hpp" #include "image.hpp" #include "raster_font.hpp" #include "button.hpp" @@ -32,7 +31,7 @@ class MainMenu : public BaseScene { public: //Public access members - MainMenu(ConfigUtility* const); + MainMenu(); ~MainMenu(); protected: @@ -49,9 +48,6 @@ protected: void KeyDown(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&); - //shared parameters - ConfigUtility& config; - //members Image image; RasterFont font; diff --git a/client/scenes/options_menu.cpp b/client/scenes/options_menu.cpp index 19b3b9e..4a991f7 100644 --- a/client/scenes/options_menu.cpp +++ b/client/scenes/options_menu.cpp @@ -21,13 +21,15 @@ */ #include "options_menu.hpp" +#include "config_utility.hpp" + //------------------------- //Public access members //------------------------- -OptionsMenu::OptionsMenu(ConfigUtility* const argConfig): - config(*argConfig) -{ +OptionsMenu::OptionsMenu() { + ConfigUtility& config = ConfigUtility::GetSingleton(); + //setup the utility objects image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); image.SetClipH(image.GetClipH()/3); diff --git a/client/scenes/options_menu.hpp b/client/scenes/options_menu.hpp index 37e4e64..8570644 100644 --- a/client/scenes/options_menu.hpp +++ b/client/scenes/options_menu.hpp @@ -24,7 +24,6 @@ #include "base_scene.hpp" -#include "config_utility.hpp" #include "image.hpp" #include "raster_font.hpp" #include "button.hpp" @@ -33,7 +32,7 @@ class OptionsMenu : public BaseScene { public: //Public access members - OptionsMenu(ConfigUtility* const); + OptionsMenu(); ~OptionsMenu(); protected: @@ -50,9 +49,6 @@ protected: void KeyDown(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&); - //shared parameters - ConfigUtility& config; - //members Image image; RasterFont font; diff --git a/client/scenes/splash_screen.cpp b/client/scenes/splash_screen.cpp index a363a2c..2c3b233 100644 --- a/client/scenes/splash_screen.cpp +++ b/client/scenes/splash_screen.cpp @@ -21,14 +21,14 @@ */ #include "splash_screen.hpp" +#include "config_utility.hpp" + //------------------------- //Public access members //------------------------- -SplashScreen::SplashScreen(ConfigUtility* const argConfig): - config(*argConfig) -{ - logo.LoadSurface(config["dir.logos"] + "krstudios.bmp"); +SplashScreen::SplashScreen() { + logo.LoadSurface(ConfigUtility::GetSingleton()["dir.logos"] + "krstudios.bmp"); startTick = std::chrono::steady_clock::now(); } diff --git a/client/scenes/splash_screen.hpp b/client/scenes/splash_screen.hpp index 2b2000b..9d55c0c 100644 --- a/client/scenes/splash_screen.hpp +++ b/client/scenes/splash_screen.hpp @@ -24,7 +24,6 @@ #include "base_scene.hpp" -#include "config_utility.hpp" #include "image.hpp" #include @@ -32,7 +31,7 @@ class SplashScreen : public BaseScene { public: //Public access members - SplashScreen(ConfigUtility* const); + SplashScreen(); ~SplashScreen(); protected: @@ -40,9 +39,6 @@ protected: void Update(double delta); void Render(SDL_Surface* const); - //shared parameters - ConfigUtility& config; - //members std::chrono::steady_clock::time_point startTick; Image logo; diff --git a/rsc/config.cfg b/rsc/config.cfg index b2f34d9..bbe6611 100644 --- a/rsc/config.cfg +++ b/rsc/config.cfg @@ -8,6 +8,8 @@ server.name = local server.dbname = database.db #client specific settings +#client.screen.w = 800 +#client.screen.h = 600 client.screen.f = false client.username = Kayne Ruse diff --git a/server/main.cpp b/server/main.cpp index 5c01971..bfa7f7c 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -21,6 +21,9 @@ */ #include "server_application.hpp" +//singletons +#include "config_utility.hpp" + #include #include @@ -28,10 +31,17 @@ using namespace std; int main(int argc, char** argv) { try { + //create the singletons + ConfigUtility::Create(); + + //call the server's routines ServerApplication app; app.Init(argc, argv); app.Proc(); app.Quit(); + + //delete the singletons + ConfigUtility::Delete(); } catch(exception& e) { cerr << "Fatal exception thrown: " << e.what() << endl; diff --git a/server/server_application.hpp b/server/server_application.hpp index c5d0c1d..74b8777 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -83,7 +83,7 @@ private: sqlite3* database = nullptr; lua_State* luaState = nullptr; UDPNetworkUtility network; - ConfigUtility config; + ConfigUtility& config = ConfigUtility::GetSingleton(); //simple tables std::map clientMap;