From cbd388f4ed0bce247b489dbe2a562b38fe039dce Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 12 Jun 2013 06:35:43 +1000 Subject: [PATCH] Started using ServiceLocator, experimental --- client/scene_manager.cpp | 58 ++++++++++++++++++++++++++++++++++++---- client/scene_manager.hpp | 12 ++++++++- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/client/scene_manager.cpp b/client/scene_manager.cpp index a6d4117..a5267cc 100644 --- a/client/scene_manager.cpp +++ b/client/scene_manager.cpp @@ -41,18 +41,60 @@ //------------------------- SceneManager::SceneManager() { - activeScene = nullptr; + // } SceneManager::~SceneManager() { UnloadScene(); } -void SceneManager::Init() { - if (SDL_Init(SDL_INIT_VIDEO)) - throw(std::runtime_error("Failed to initialize SDL")); +/* SceneManager::Init() + * This function initializes the entire program. There are a number of things + * that could go wrong here, which is why there is such an unusual order of + * operations. + * Important things to note: + * The APIs are initiated here. + * The global objects are created here. + * The game's screen is created here, based on information loaded from the config file. + * The ConfigUtility's call to Load() also ensures that the "rsc\" folder is in the directory. It's easy to forget it. +*/ - BaseScene::SetScreen(800, 600); +void SceneManager::Init() { + //load the config file + try { + configUtil = ServiceLocator::Set(new ConfigUtility()); + configUtil->Load("rsc/config.cfg"); + } + catch(std::runtime_error& e) { + std::string s = e.what(); + s += "; Ensure that the \"rsc\" directory is present"; + throw(std::runtime_error(s)); + } + + //initialize the APIs + if (SDL_Init(SDL_INIT_VIDEO)) { + throw(std::runtime_error("Failed to initialize SDL")); + } + if (SDLNet_Init()) { + throw(std::runtime_error("Failed to initialize SDL_net")); + } + + //create the screen + Uint32 flags = SDL_HWSURFACE | SDL_DOUBLEBUF; + flags |= configUtil->Bool("screen.f") ? SDL_FULLSCREEN : 0; + + BaseScene::SetScreen( + configUtil->Int("screen.w"), + configUtil->Int("screen.h"), + SDL_GetVideoInfo()->vfmt->BitsPerPixel, + flags); + + //instanciate the remaining services + surfaceMgr = ServiceLocator::Set(new SurfaceManager()); + netUtil = ServiceLocator::Set(new UDPNetworkUtility()); + + //initiate the remaining services + netUtil->Open(0, sizeof(Packet)); } void SceneManager::Proc() { @@ -94,7 +136,13 @@ void SceneManager::Proc() { } void SceneManager::Quit() { + //clean up the services + configUtil = ServiceLocator::Set(nullptr); + surfaceMgr = ServiceLocator::Set(nullptr); + netUtil = ServiceLocator::Set(nullptr); + UnloadScene(); + SDLNet_Quit(); SDL_Quit(); } diff --git a/client/scene_manager.hpp b/client/scene_manager.hpp index 83373b4..f6e23fc 100644 --- a/client/scene_manager.hpp +++ b/client/scene_manager.hpp @@ -24,6 +24,12 @@ #include "scene_list.hpp" #include "base_scene.hpp" +#include "service_locator.hpp" +#include "packet_type.hpp" + +#include "config_utility.hpp" +#include "surface_manager.hpp" +#include "udp_network_utility.hpp" #include "SDL/SDL.h" @@ -42,7 +48,11 @@ private: void LoadScene(SceneList sceneIndex); void UnloadScene(); - BaseScene* activeScene; + BaseScene* activeScene = nullptr; + + ConfigUtility* configUtil = nullptr; + SurfaceManager* surfaceMgr = nullptr; + UDPNetworkUtility* netUtil = nullptr; }; #endif