Removed singletons

This commit is contained in:
Kayne Ruse
2013-05-16 17:02:21 +10:00
parent 30d163ec80
commit f04c7fa161
11 changed files with 63 additions and 82 deletions
+10 -11
View File
@@ -8,24 +8,23 @@ using namespace std;
//Public access members //Public access members
//------------------------- //-------------------------
InGame::InGame() { InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr) {
#ifdef DEBUG #ifdef DEBUG
cout << "entering InGame" << endl; cout << "entering InGame" << endl;
#endif #endif
surfaceMgr.Load("player", "rsc/graphics/sprites/elliot2.bmp"); configUtil = cUtil;
surfaceMgr.Load("flower", "rsc/graphics/sprites/aniflower.bmp"); surfaceMgr = sMgr;
playerCounter = currentPlayer = 0; playerCounter = currentPlayer = 0;
playerMgr.New(playerCounter++, surfaceMgr["player"]); playerMgr.New(playerCounter++, surfaceMgr->Get("elliot"));
playerMgr.New(playerCounter++, surfaceMgr["player"]); playerMgr.New(playerCounter++, surfaceMgr->Get("elliot"));
playerMgr.New(playerCounter++, surfaceMgr["player"]); playerMgr.New(playerCounter++, surfaceMgr->Get("coa"));
playerMgr.New(playerCounter++, surfaceMgr["player"]); playerMgr.New(playerCounter++, surfaceMgr->Get("coa"));
} }
InGame::~InGame() { InGame::~InGame() {
playerMgr.DeleteAll(); playerMgr.DeleteAll();
surfaceMgr.FreeAll();
#ifdef DEBUG #ifdef DEBUG
cout << "leaving InGame" << endl; cout << "leaving InGame" << endl;
#endif #endif
@@ -71,7 +70,7 @@ void InGame::MouseButtonUp(SDL_MouseButtonEvent const& button) {
void InGame::KeyDown(SDL_KeyboardEvent const& key) { void InGame::KeyDown(SDL_KeyboardEvent const& key) {
switch(key.keysym.sym) { switch(key.keysym.sym) {
case SDLK_ESCAPE: case SDLK_ESCAPE:
QuitEvent(); SetNextScene(SceneList::MAINMENU);
break; break;
case SDLK_w: 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) { 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)); p->SetPosition(Vector2(x, y));
} }
@@ -133,7 +132,7 @@ void InGame::SwitchToPlayer(int index) {
playerMgr[currentPlayer]->SetMotion(Vector2(0,0)); playerMgr[currentPlayer]->SetMotion(Vector2(0,0));
currentPlayer = index; currentPlayer = index;
Uint8* key = SDL_GetKeyState(NULL); Uint8* key = SDL_GetKeyState(nullptr);
if (key[SDLK_w]) { if (key[SDLK_w]) {
playerMgr[currentPlayer]->WalkInDirection(Direction::NORTH); playerMgr[currentPlayer]->WalkInDirection(Direction::NORTH);
+8 -4
View File
@@ -3,9 +3,11 @@
#include "base_scene.hpp" #include "base_scene.hpp"
#include "player_manager.hpp"
#include "delta.hpp" #include "delta.hpp"
#include "frame_rate.hpp" #include "frame_rate.hpp"
#include "player_manager.hpp" #include "config_utility.hpp"
#include "surface_manager.hpp" #include "surface_manager.hpp"
#include <string> #include <string>
@@ -13,7 +15,7 @@
class InGame : public BaseScene { class InGame : public BaseScene {
public: public:
//Public access members //Public access members
InGame(); InGame(ConfigUtility*, SurfaceManager*);
virtual ~InGame(); virtual ~InGame();
protected: protected:
@@ -35,10 +37,12 @@ protected:
void SwitchToPlayer(int index); void SwitchToPlayer(int index);
//members //members
PlayerManager playerMgr;
Delta delta; Delta delta;
FrameRate frameRate; FrameRate frameRate;
SurfaceManager surfaceMgr; ConfigUtility* configUtil;
PlayerManager playerMgr; SurfaceManager* surfaceMgr;
int playerCounter; int playerCounter;
int currentPlayer; int currentPlayer;
+4 -6
View File
@@ -8,14 +8,12 @@ using namespace std;
//Public access members //Public access members
//------------------------- //-------------------------
MainMenu::MainMenu() { MainMenu::MainMenu(ConfigUtility* cUtil, SurfaceManager* sMgr) {
#ifdef DEBUG #ifdef DEBUG
cout << "entering MainMenu" << endl; cout << "entering MainMenu" << endl;
#endif #endif
configUtil = GetSingletonPtr<ConfigUtility>(); configUtil = cUtil;
surfaceMgr = GetSingletonPtr<SurfaceManager>(); surfaceMgr = sMgr;
surfaceMgr->Load("button", configUtil->String("interface") + "/button.bmp");
buttonMap["start"] = new Button(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "start"); 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"); 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) { if (buttonMap["quit"]->MouseButtonUp(button) == Button::State::HOVER) {
//TODO //TODO
SetNextScene(SceneList::QUIT); QuitEvent();
cout << "quit" << endl; cout << "quit" << endl;
} }
} }
+1 -2
View File
@@ -3,7 +3,6 @@
#include "base_scene.hpp" #include "base_scene.hpp"
#include "singleton.hpp"
#include "config_utility.hpp" #include "config_utility.hpp"
#include "surface_manager.hpp" #include "surface_manager.hpp"
@@ -15,7 +14,7 @@
class MainMenu : public BaseScene { class MainMenu : public BaseScene {
public: public:
//Public access members //Public access members
MainMenu(); MainMenu(ConfigUtility*, SurfaceManager*);
virtual ~MainMenu(); virtual ~MainMenu();
protected: protected:
+7 -9
View File
@@ -36,16 +36,14 @@ void SceneManager::Init() {
if (SDL_Init(SDL_INIT_VIDEO)) if (SDL_Init(SDL_INIT_VIDEO))
throw(std::runtime_error("Failed to initialize SDL")); throw(std::runtime_error("Failed to initialize SDL"));
configUtil = GetSingletonPtr<ConfigUtility>(); configUtil.Load("rsc/config.cfg");
configUtil->Load("rsc/config.cfg");
//set the screen from the config file //set the screen from the config file
int flags = SDL_HWSURFACE|SDL_DOUBLEBUF; int flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
if (configUtil->Boolean("screen.f")) { if (configUtil.Boolean("screen.f")) {
flags |= SDL_FULLSCREEN; 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() { void SceneManager::Proc() {
@@ -82,19 +80,19 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
//add scene creation calls here //add scene creation calls here
#ifdef DEBUG #ifdef DEBUG
case SceneList::TESTSYSTEMS: case SceneList::TESTSYSTEMS:
activeScene = new TestSystems(); activeScene = new TestSystems(&configUtil, &surfaceMgr);
break; break;
#endif #endif
case SceneList::FIRST: case SceneList::FIRST:
case SceneList::SPLASH: case SceneList::SPLASH:
activeScene = new Splash(); activeScene = new Splash(&configUtil, &surfaceMgr);
break; break;
case SceneList::MAINMENU: case SceneList::MAINMENU:
activeScene = new MainMenu(); activeScene = new MainMenu(&configUtil, &surfaceMgr);
break; break;
case SceneList::INGAME: case SceneList::INGAME:
activeScene = new InGame(); activeScene = new InGame(&configUtil, &surfaceMgr);
break; break;
#ifdef DEBUG #ifdef DEBUG
+3 -2
View File
@@ -4,8 +4,8 @@
#include "scene_list.hpp" #include "scene_list.hpp"
#include "base_scene.hpp" #include "base_scene.hpp"
#include "singleton.hpp"
#include "config_utility.hpp" #include "config_utility.hpp"
#include "surface_manager.hpp"
#include "SDL/SDL.h" #include "SDL/SDL.h"
@@ -26,7 +26,8 @@ private:
BaseScene* activeScene; BaseScene* activeScene;
ConfigUtility* configUtil; ConfigUtility configUtil;
SurfaceManager surfaceMgr;
}; };
#endif #endif
-31
View File
@@ -1,31 +0,0 @@
#ifndef SINGLETON_HPP_
#define SINGLETON_HPP_
/*
template<typename T>
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<typename T>
T* GetSingletonPtr() {
static T t;
return &t;
}
#endif
+10 -5
View File
@@ -8,21 +8,21 @@ using namespace std;
//Public access members //Public access members
//------------------------- //-------------------------
Splash::Splash() { Splash::Splash(ConfigUtility* cUtil, SurfaceManager* sMgr) {
#ifdef DEBUG #ifdef DEBUG
cout << "entering Splash" << endl; cout << "entering Splash" << endl;
#endif #endif
loaded = false; loaded = false;
start = clock(); start = clock();
configUtil = GetSingletonPtr<ConfigUtility>(); configUtil = cUtil;
surfaceMgr = GetSingletonPtr<SurfaceManager>(); 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() { Splash::~Splash() {
delete logo; delete logo;
surfaceMgr->Free("logo"); surfaceMgr->Free("splash-logo");
#ifdef DEBUG #ifdef DEBUG
cout << "leaving Splash" << endl; cout << "leaving Splash" << endl;
#endif #endif
@@ -53,5 +53,10 @@ void Splash::RunFrame() {
void Splash::LoadResources() { void Splash::LoadResources() {
//load the global resources here //load the global resources here
surfaceMgr->Load("font", configUtil->String("fonts") + "/pokemon_dark_font.bmp"); 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 //TODO
} }
+1 -2
View File
@@ -3,7 +3,6 @@
#include "base_scene.hpp" #include "base_scene.hpp"
#include "singleton.hpp"
#include "config_utility.hpp" #include "config_utility.hpp"
#include "surface_manager.hpp" #include "surface_manager.hpp"
#include "image.hpp" #include "image.hpp"
@@ -12,7 +11,7 @@
class Splash : public BaseScene { class Splash : public BaseScene {
public: public:
Splash(); Splash(ConfigUtility*, SurfaceManager*);
virtual ~Splash(); virtual ~Splash();
protected: protected:
+3 -1
View File
@@ -8,10 +8,12 @@ using namespace std;
//Public access members //Public access members
//------------------------- //-------------------------
TestSystems::TestSystems() { TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr) {
#ifdef DEBUG #ifdef DEBUG
cout << "entering TestSystems" << endl; cout << "entering TestSystems" << endl;
#endif #endif
configUtil = cUtil;
surfaceMgr = sMgr;
} }
TestSystems::~TestSystems() { TestSystems::~TestSystems() {
+11 -4
View File
@@ -3,25 +3,32 @@
#include "base_scene.hpp" #include "base_scene.hpp"
#include "config_utility.hpp"
#include "surface_manager.hpp"
class TestSystems : public BaseScene { class TestSystems : public BaseScene {
public: public:
/* Public access members */ //Public access members
TestSystems(); TestSystems(ConfigUtility*, SurfaceManager*);
virtual ~TestSystems(); virtual ~TestSystems();
protected: protected:
/* Frame loop */ //Frame loop
virtual void FrameStart(); virtual void FrameStart();
virtual void FrameEnd(); virtual void FrameEnd();
virtual void Update(); virtual void Update();
virtual void Render(SDL_Surface* const); virtual void Render(SDL_Surface* const);
/* Event handlers */ //Event handlers
virtual void MouseMotion(SDL_MouseMotionEvent const&); virtual void MouseMotion(SDL_MouseMotionEvent const&);
virtual void MouseButtonDown(SDL_MouseButtonEvent const&); virtual void MouseButtonDown(SDL_MouseButtonEvent const&);
virtual void MouseButtonUp(SDL_MouseButtonEvent const&); virtual void MouseButtonUp(SDL_MouseButtonEvent const&);
virtual void KeyDown(SDL_KeyboardEvent const&); virtual void KeyDown(SDL_KeyboardEvent const&);
virtual void KeyUp(SDL_KeyboardEvent const&); virtual void KeyUp(SDL_KeyboardEvent const&);
//members
ConfigUtility* configUtil;
SurfaceManager* surfaceMgr;
}; };
#endif #endif