From f04c7fa161077f5c043ed57fa6ffd014e20188df Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Thu, 16 May 2013 17:02:21 +1000 Subject: [PATCH] Removed singletons --- client/in_game.cpp | 21 ++++++++++----------- client/in_game.hpp | 22 +++++++++++++--------- client/main_menu.cpp | 10 ++++------ client/main_menu.hpp | 3 +-- client/scene_manager.cpp | 16 +++++++--------- client/scene_manager.hpp | 5 +++-- client/singleton.hpp | 31 ------------------------------- client/splash.cpp | 15 ++++++++++----- client/splash.hpp | 3 +-- client/test_systems.cpp | 4 +++- client/test_systems.hpp | 15 +++++++++++---- 11 files changed, 63 insertions(+), 82 deletions(-) delete mode 100644 client/singleton.hpp diff --git a/client/in_game.cpp b/client/in_game.cpp index dd45999..4a98072 100644 --- a/client/in_game.cpp +++ b/client/in_game.cpp @@ -8,24 +8,23 @@ using namespace std; //Public access members //------------------------- -InGame::InGame() { +InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr) { #ifdef DEBUG cout << "entering InGame" << endl; #endif - surfaceMgr.Load("player", "rsc/graphics/sprites/elliot2.bmp"); - surfaceMgr.Load("flower", "rsc/graphics/sprites/aniflower.bmp"); + configUtil = cUtil; + surfaceMgr = sMgr; playerCounter = currentPlayer = 0; - playerMgr.New(playerCounter++, surfaceMgr["player"]); - playerMgr.New(playerCounter++, surfaceMgr["player"]); - playerMgr.New(playerCounter++, surfaceMgr["player"]); - playerMgr.New(playerCounter++, surfaceMgr["player"]); + playerMgr.New(playerCounter++, surfaceMgr->Get("elliot")); + playerMgr.New(playerCounter++, surfaceMgr->Get("elliot")); + playerMgr.New(playerCounter++, surfaceMgr->Get("coa")); + playerMgr.New(playerCounter++, surfaceMgr->Get("coa")); } InGame::~InGame() { playerMgr.DeleteAll(); - surfaceMgr.FreeAll(); #ifdef DEBUG cout << "leaving InGame" << endl; #endif @@ -71,7 +70,7 @@ void InGame::MouseButtonUp(SDL_MouseButtonEvent const& button) { void InGame::KeyDown(SDL_KeyboardEvent const& key) { switch(key.keysym.sym) { case SDLK_ESCAPE: - QuitEvent(); + SetNextScene(SceneList::MAINMENU); break; case SDLK_w: @@ -124,7 +123,7 @@ void InGame::KeyUp(SDL_KeyboardEvent const& key) { //------------------------- void InGame::NewPlayer(int index, std::string avatarName, int x, int y) { - Player* p = playerMgr.New(index, surfaceMgr[avatarName]); + Player* p = playerMgr.New(index, surfaceMgr->Get(avatarName)); p->SetPosition(Vector2(x, y)); } @@ -133,7 +132,7 @@ void InGame::SwitchToPlayer(int index) { playerMgr[currentPlayer]->SetMotion(Vector2(0,0)); currentPlayer = index; - Uint8* key = SDL_GetKeyState(NULL); + Uint8* key = SDL_GetKeyState(nullptr); if (key[SDLK_w]) { playerMgr[currentPlayer]->WalkInDirection(Direction::NORTH); diff --git a/client/in_game.hpp b/client/in_game.hpp index 40a06e1..e36f511 100644 --- a/client/in_game.hpp +++ b/client/in_game.hpp @@ -3,9 +3,11 @@ #include "base_scene.hpp" +#include "player_manager.hpp" + #include "delta.hpp" #include "frame_rate.hpp" -#include "player_manager.hpp" +#include "config_utility.hpp" #include "surface_manager.hpp" #include @@ -13,7 +15,7 @@ class InGame : public BaseScene { public: //Public access members - InGame(); + InGame(ConfigUtility*, SurfaceManager*); virtual ~InGame(); protected: @@ -24,21 +26,23 @@ protected: virtual void Render(SDL_Surface* const); //Event handlers - virtual void MouseMotion (SDL_MouseMotionEvent const&); - virtual void MouseButtonDown (SDL_MouseButtonEvent const&); - virtual void MouseButtonUp (SDL_MouseButtonEvent const&); - virtual void KeyDown (SDL_KeyboardEvent const&); - virtual void KeyUp (SDL_KeyboardEvent const&); + virtual void MouseMotion(SDL_MouseMotionEvent const&); + virtual void MouseButtonDown(SDL_MouseButtonEvent const&); + virtual void MouseButtonUp(SDL_MouseButtonEvent const&); + virtual void KeyDown(SDL_KeyboardEvent const&); + virtual void KeyUp(SDL_KeyboardEvent const&); //utilities void NewPlayer(int index, std::string avatarName, int x, int y); void SwitchToPlayer(int index); //members + PlayerManager playerMgr; + Delta delta; FrameRate frameRate; - SurfaceManager surfaceMgr; - PlayerManager playerMgr; + ConfigUtility* configUtil; + SurfaceManager* surfaceMgr; int playerCounter; int currentPlayer; diff --git a/client/main_menu.cpp b/client/main_menu.cpp index d0fa948..e682165 100644 --- a/client/main_menu.cpp +++ b/client/main_menu.cpp @@ -8,14 +8,12 @@ using namespace std; //Public access members //------------------------- -MainMenu::MainMenu() { +MainMenu::MainMenu(ConfigUtility* cUtil, SurfaceManager* sMgr) { #ifdef DEBUG cout << "entering MainMenu" << endl; #endif - configUtil = GetSingletonPtr(); - surfaceMgr = GetSingletonPtr(); - - surfaceMgr->Load("button", configUtil->String("interface") + "/button.bmp"); + configUtil = cUtil; + surfaceMgr = sMgr; buttonMap["start"] = new Button(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "start"); buttonMap["options"] = new Button(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "options"); @@ -81,7 +79,7 @@ void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { } if (buttonMap["quit"]->MouseButtonUp(button) == Button::State::HOVER) { //TODO - SetNextScene(SceneList::QUIT); + QuitEvent(); cout << "quit" << endl; } } diff --git a/client/main_menu.hpp b/client/main_menu.hpp index 00d9914..7d082ce 100644 --- a/client/main_menu.hpp +++ b/client/main_menu.hpp @@ -3,7 +3,6 @@ #include "base_scene.hpp" -#include "singleton.hpp" #include "config_utility.hpp" #include "surface_manager.hpp" @@ -15,7 +14,7 @@ class MainMenu : public BaseScene { public: //Public access members - MainMenu(); + MainMenu(ConfigUtility*, SurfaceManager*); virtual ~MainMenu(); protected: diff --git a/client/scene_manager.cpp b/client/scene_manager.cpp index 1ef4626..192b3a5 100644 --- a/client/scene_manager.cpp +++ b/client/scene_manager.cpp @@ -36,16 +36,14 @@ void SceneManager::Init() { if (SDL_Init(SDL_INIT_VIDEO)) throw(std::runtime_error("Failed to initialize SDL")); - configUtil = GetSingletonPtr(); - - configUtil->Load("rsc/config.cfg"); + configUtil.Load("rsc/config.cfg"); //set the screen from the config file int flags = SDL_HWSURFACE|SDL_DOUBLEBUF; - if (configUtil->Boolean("screen.f")) { + if (configUtil.Boolean("screen.f")) { flags |= SDL_FULLSCREEN; } - BaseScene::SetScreen(configUtil->Integer("screen.w"),configUtil->Integer("screen.h"),0,flags); + BaseScene::SetScreen(configUtil.Integer("screen.w"),configUtil.Integer("screen.h"),0,flags); } void SceneManager::Proc() { @@ -82,19 +80,19 @@ void SceneManager::LoadScene(SceneList sceneIndex) { //add scene creation calls here #ifdef DEBUG case SceneList::TESTSYSTEMS: - activeScene = new TestSystems(); + activeScene = new TestSystems(&configUtil, &surfaceMgr); break; #endif case SceneList::FIRST: case SceneList::SPLASH: - activeScene = new Splash(); + activeScene = new Splash(&configUtil, &surfaceMgr); break; case SceneList::MAINMENU: - activeScene = new MainMenu(); + activeScene = new MainMenu(&configUtil, &surfaceMgr); break; case SceneList::INGAME: - activeScene = new InGame(); + activeScene = new InGame(&configUtil, &surfaceMgr); break; #ifdef DEBUG diff --git a/client/scene_manager.hpp b/client/scene_manager.hpp index edf1da0..4488d4d 100644 --- a/client/scene_manager.hpp +++ b/client/scene_manager.hpp @@ -4,8 +4,8 @@ #include "scene_list.hpp" #include "base_scene.hpp" -#include "singleton.hpp" #include "config_utility.hpp" +#include "surface_manager.hpp" #include "SDL/SDL.h" @@ -26,7 +26,8 @@ private: BaseScene* activeScene; - ConfigUtility* configUtil; + ConfigUtility configUtil; + SurfaceManager surfaceMgr; }; #endif diff --git a/client/singleton.hpp b/client/singleton.hpp deleted file mode 100644 index e741a33..0000000 --- a/client/singleton.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef SINGLETON_HPP_ -#define SINGLETON_HPP_ - -/* -template -class Singleton { -public: - static T* GetSingletonPtr() { - return &singleton; - } - static T& GetSingletonRef() { - return singleton; - } -private: - Singleton(); - ~Singleton(); - Singleton(const Singleton&); - Singleton& operator=(const Singleton&); - Singleton(Singleton&&); - Singleton& operator=(Singleton&&); - static T singleton; -}; -*/ - -template -T* GetSingletonPtr() { - static T t; - return &t; -} - -#endif diff --git a/client/splash.cpp b/client/splash.cpp index 44593b2..5678cbb 100644 --- a/client/splash.cpp +++ b/client/splash.cpp @@ -8,21 +8,21 @@ using namespace std; //Public access members //------------------------- -Splash::Splash() { +Splash::Splash(ConfigUtility* cUtil, SurfaceManager* sMgr) { #ifdef DEBUG cout << "entering Splash" << endl; #endif loaded = false; start = clock(); - configUtil = GetSingletonPtr(); - surfaceMgr = GetSingletonPtr(); + configUtil = cUtil; + surfaceMgr = sMgr; - logo = new Image(surfaceMgr->Load("logo", configUtil->String("logos") + "/krstudios.bmp")); + logo = new Image(surfaceMgr->Load("splash-logo", configUtil->String("logos") + "/krstudios.bmp")); } Splash::~Splash() { delete logo; - surfaceMgr->Free("logo"); + surfaceMgr->Free("splash-logo"); #ifdef DEBUG cout << "leaving Splash" << endl; #endif @@ -53,5 +53,10 @@ void Splash::RunFrame() { void Splash::LoadResources() { //load the global resources here surfaceMgr->Load("font", configUtil->String("fonts") + "/pokemon_dark_font.bmp"); + surfaceMgr->Load("button", configUtil->String("interface") + "/button.bmp"); + surfaceMgr->Load("elliot", configUtil->String("sprites") + "/elliot2.bmp"); + surfaceMgr->Load("coa", configUtil->String("sprites") + "/coa2.bmp"); + surfaceMgr->Load("flower", configUtil->String("sprites") + "/aniflower.bmp"); + surfaceMgr->Load("terrain", configUtil->String("tilesets") + "/terrain.bmp"); //TODO } \ No newline at end of file diff --git a/client/splash.hpp b/client/splash.hpp index 6288ecd..5630ede 100644 --- a/client/splash.hpp +++ b/client/splash.hpp @@ -3,7 +3,6 @@ #include "base_scene.hpp" -#include "singleton.hpp" #include "config_utility.hpp" #include "surface_manager.hpp" #include "image.hpp" @@ -12,7 +11,7 @@ class Splash : public BaseScene { public: - Splash(); + Splash(ConfigUtility*, SurfaceManager*); virtual ~Splash(); protected: diff --git a/client/test_systems.cpp b/client/test_systems.cpp index d276055..1e60ea3 100644 --- a/client/test_systems.cpp +++ b/client/test_systems.cpp @@ -8,10 +8,12 @@ using namespace std; //Public access members //------------------------- -TestSystems::TestSystems() { +TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr) { #ifdef DEBUG cout << "entering TestSystems" << endl; #endif + configUtil = cUtil; + surfaceMgr = sMgr; } TestSystems::~TestSystems() { diff --git a/client/test_systems.hpp b/client/test_systems.hpp index 9193478..94d7e82 100644 --- a/client/test_systems.hpp +++ b/client/test_systems.hpp @@ -3,25 +3,32 @@ #include "base_scene.hpp" +#include "config_utility.hpp" +#include "surface_manager.hpp" + class TestSystems : public BaseScene { public: - /* Public access members */ - TestSystems(); + //Public access members + TestSystems(ConfigUtility*, SurfaceManager*); virtual ~TestSystems(); protected: - /* Frame loop */ + //Frame loop virtual void FrameStart(); virtual void FrameEnd(); virtual void Update(); virtual void Render(SDL_Surface* const); - /* Event handlers */ + //Event handlers virtual void MouseMotion(SDL_MouseMotionEvent const&); virtual void MouseButtonDown(SDL_MouseButtonEvent const&); virtual void MouseButtonUp(SDL_MouseButtonEvent const&); virtual void KeyDown(SDL_KeyboardEvent const&); virtual void KeyUp(SDL_KeyboardEvent const&); + + //members + ConfigUtility* configUtil; + SurfaceManager* surfaceMgr; }; #endif