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
//-------------------------
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);
+13 -9
View File
@@ -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 <string>
@@ -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;
+4 -6
View File
@@ -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<ConfigUtility>();
surfaceMgr = GetSingletonPtr<SurfaceManager>();
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;
}
}
+1 -2
View File
@@ -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:
+7 -9
View File
@@ -36,16 +36,14 @@ void SceneManager::Init() {
if (SDL_Init(SDL_INIT_VIDEO))
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
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
+3 -2
View File
@@ -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
-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
//-------------------------
Splash::Splash() {
Splash::Splash(ConfigUtility* cUtil, SurfaceManager* sMgr) {
#ifdef DEBUG
cout << "entering Splash" << endl;
#endif
loaded = false;
start = clock();
configUtil = GetSingletonPtr<ConfigUtility>();
surfaceMgr = GetSingletonPtr<SurfaceManager>();
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
}
+1 -2
View File
@@ -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:
+3 -1
View File
@@ -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() {
+11 -4
View File
@@ -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