diff --git a/TODO.txt b/TODO.txt index 8df05c5..dad6c5d 100644 --- a/TODO.txt +++ b/TODO.txt @@ -4,8 +4,6 @@ TODO Refactoring ----------- -* Replace ServiceLocator with Dependency Injection or be at least so honest to - use ordinary globals instead (no need to derive from Component). * Put (at least) core components in namespace jd to allow ADL: avoids ugly forward declarations of operator<< before #include and is good style anyway. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2facff8..7384f06 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,7 +10,6 @@ set(SVC_HEADERS svc/EventDispatcher.hpp svc/DrawService.hpp svc/Configuration.hpp - svc/ServiceLocator.hpp svc/Timer.hpp svc/SoundManager.hpp) @@ -22,7 +21,6 @@ set(SVC_SOURCES svc/EventDispatcher.cpp svc/DrawService.cpp svc/Configuration.cpp - svc/ServiceLocator.cpp svc/Timer.cpp svc/SoundManager.cpp) source_group("Services" FILES ${SVC_SOURCES} ${SVC_HEADERS}) @@ -75,7 +73,6 @@ set(LUAEXPORT_SOURCES luaexport/SfGraphics.cpp luaexport/Logfile.cpp luaexport/EntitySystem.cpp - luaexport/ServiceLocator.cpp luaexport/Collisions.cpp luaexport/Geometry.cpp luaexport/DrawServiceMeta.cpp diff --git a/src/compsys/MetaComponent.cpp b/src/compsys/MetaComponent.cpp index f4847c7..ed657aa 100644 --- a/src/compsys/MetaComponent.cpp +++ b/src/compsys/MetaComponent.cpp @@ -9,7 +9,6 @@ #include "Entity.hpp" #include "Logfile.hpp" #include "luaUtils.hpp" -#include "svc/ServiceLocator.hpp" #include "svc/LuaVm.hpp" #include diff --git a/src/luaexport/Collisions.cpp b/src/luaexport/Collisions.cpp index 4869e95..8629de7 100644 --- a/src/luaexport/Collisions.cpp +++ b/src/luaexport/Collisions.cpp @@ -13,7 +13,6 @@ #include "LuaFunction.hpp" #include "SfBaseTypes.hpp" #include "sfUtil.hpp" -#include "svc/ServiceLocator.hpp" #include "Tilemap.hpp" #include diff --git a/src/luaexport/DrawServiceMeta.cpp b/src/luaexport/DrawServiceMeta.cpp index ff5ca99..7a0334c 100644 --- a/src/luaexport/DrawServiceMeta.cpp +++ b/src/luaexport/DrawServiceMeta.cpp @@ -4,14 +4,10 @@ #include "svc/DrawService.hpp" -#include "compsys/BasicMetaComponent.hpp" - static char const libname[] = "DrawService"; #include "ExportThis.hpp" -JD_BASIC_COMPONENT_IMPL(DrawService) - static DrawService::Layer& getLayer(DrawService& ds, std::size_t n) { if (--n >= ds.layerCount()) @@ -21,7 +17,6 @@ static DrawService::Layer& getLayer(DrawService& ds, std::size_t n) static void init(LuaVm& vm) { - vm.initLib("ComponentSystem"); vm.initLib("SfGraphics"); LHMODULE [ # define LHCURCLASS DrawService::Layer @@ -29,12 +24,14 @@ static void init(LuaVm& vm) .def_readonly(LHMEMARGS(group)) .LHPROPRW(view), # undef LHCURCLASS - class_("DrawService") - .def("resetLayerViews", &DrawService::resetLayerViews) +# define LHCURCLASS DrawService + LHCLASS + .LHMEMFN(resetLayerViews) .def("layer", &getLayer) - .property("layerCount", &DrawService::layerCount) + .LHPROPG(layerCount) .property("backgroundColor", &DrawService::backgroundColor, &DrawService::setBackgroundColor) +# undef LHCURCLASS ]; } diff --git a/src/luaexport/EventDispatcherMeta.cpp b/src/luaexport/EventDispatcherMeta.cpp index 76e49f5..fb8a5e8 100644 --- a/src/luaexport/EventDispatcherMeta.cpp +++ b/src/luaexport/EventDispatcherMeta.cpp @@ -7,7 +7,6 @@ #include "compsys/BasicMetaComponent.hpp" #include "LuaEventHelpers.hpp" #include "SfBaseTypes.hpp" -#include "svc/ServiceLocator.hpp" static char const libname[] = "EventDispatcher"; #include "ExportThis.hpp" @@ -37,42 +36,15 @@ JD_EVENT_TABLE_BEGIN(EventDispatcher) #undef E1 JD_EVENT_TABLE_END -static EventDispatcher* dsp = nullptr; - -static bool EventDispatcher_isKeyPressed(unsigned k) -{ - if (!dsp) - dsp = &ServiceLocator::eventDispatcher(); - return dsp->isKeyPressed(static_cast(k)); -} - -static bool EventDispatcher_isMouseButtonPressed(unsigned b) -{ - if (!dsp) - dsp = &ServiceLocator::eventDispatcher(); - return dsp->isMouseButtonPressed(static_cast(b)); -} - -static LuaVec2 EventDispatcher_mousePosition() -{ - if (!dsp) - dsp = &ServiceLocator::eventDispatcher(); - return static_cast(dsp->mousePosition()); -} - static void init(LuaVm& vm) { vm.initLib("ComponentSystem"); LHMODULE [ - namespace_("kb") [ - def("isKeyPressed", &EventDispatcher_isKeyPressed) - ], - namespace_("mouse") [ - def("position", &EventDispatcher_mousePosition), - def("isButtonPressed", &EventDispatcher_isMouseButtonPressed) - ], # define LHCURCLASS EventDispatcher class_(BOOST_STRINGIZE(LHCURCLASS)) .LHPROPG(isWindowFocused) + .LHMEMFN(isKeyPressed) + .LHMEMFN(isMouseButtonPressed) + .LHMEMFN(mousePosition) ]; } diff --git a/src/luaexport/ServiceLocator.cpp b/src/luaexport/ServiceLocator.cpp deleted file mode 100644 index c15ea99..0000000 --- a/src/luaexport/ServiceLocator.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// Part of the Jade Engine -- Copyright (c) Christian Neumüller 2012--2013 -// This file is subject to the terms of the BSD 2-Clause License. -// See LICENSE.txt or http://opensource.org/licenses/BSD-2-Clause - -#include "compsys/Component.hpp" -#include "compsys/ComponentRegistry.hpp" -#include "compsys/MetaComponent.hpp" -#include "svc/Configuration.hpp" -#include "svc/DrawService.hpp" -#include "svc/EventDispatcher.hpp" -#include "svc/Mainloop.hpp" -#include "svc/ServiceLocator.hpp" -#include "svc/SoundManager.hpp" -#include "svc/StateManager.hpp" -#include "svc/Timer.hpp" - -static char const libname[] = "ServiceLocator"; -#include "ExportThis.hpp" - - -static luabind::object getService(lua_State* L, std::string const& name) -{ - MetaComponent const& mc = ComponentRegistry::metaComponent(name); - mc.castDown(L, &ServiceLocator::get(mc)); - luabind::object result(luabind::from_stack(L, -1)); - lua_pop(L, 1); - return result; -} - -static void init(LuaVm& vm) -{ - LHMODULE [ namespace_("svc") [ -#define LHCURCLASS ServiceLocator - LHMEMFN(stateManager), - LHMEMFN(mainloop), - LHMEMFN(drawService), - LHMEMFN(eventDispatcher), - LHMEMFN(timer), - LHMEMFN(soundManager), - LHMEMFN(configuration), - def("get", &getService) - ] ]; -} diff --git a/src/luaexport/SoundManagerMeta.cpp b/src/luaexport/SoundManagerMeta.cpp index 837e872..e3a3df3 100644 --- a/src/luaexport/SoundManagerMeta.cpp +++ b/src/luaexport/SoundManagerMeta.cpp @@ -4,20 +4,14 @@ #include "svc/SoundManager.hpp" -#include "compsys/BasicMetaComponent.hpp" #include "resources.hpp" #include "sharedPtrConverter.hpp" static char const libname[] = "SoundManager"; #include "ExportThis.hpp" - -JD_BASIC_COMPONENT_IMPL(SoundManager) - static void init(LuaVm& vm) { - vm.initLib("ComponentSystem"); - using namespace sf; using namespace luabind; @@ -35,7 +29,7 @@ static void init(LuaVm& vm) LHMODULE [ cSoundBuffer, # define LHCURCLASS SoundManager - class_("SoundManager") + LHCLASS .def("playSound", (void(LHCURCLASS::*)(std::string const&)) &SoundManager::playSound) diff --git a/src/luaexport/StateManagerMeta.cpp b/src/luaexport/StateManagerMeta.cpp index 5013fc9..2685735 100644 --- a/src/luaexport/StateManagerMeta.cpp +++ b/src/luaexport/StateManagerMeta.cpp @@ -4,7 +4,6 @@ #include "svc/StateManager.hpp" -#include "compsys/BasicMetaComponent.hpp" #include "jdConfig.hpp" #include "State.hpp" // State class must be defined to make binding compile @@ -20,7 +19,7 @@ static void init(LuaVm& vm) vm.initLib("State"); LHMODULE [ # define LHCURCLASS StateManager - class_>("StateManager") + class_("StateManager") .def("push", (void(StateManager::*)(State&))(&StateManager::push)) .def("push", (void(StateManager::*)(std::string const&))(&StateManager::push)) .def("pushAdditional", (void(StateManager::*)(State&))(&StateManager::pushAdditional)) @@ -37,5 +36,3 @@ static void init(LuaVm& vm) .def("state", &StateManager::stateForId) ]; } - -JD_BASIC_COMPONENT_IMPL(StateManager) diff --git a/src/luaexport/TimerMeta.cpp b/src/luaexport/TimerMeta.cpp index b3253e4..5559b0a 100644 --- a/src/luaexport/TimerMeta.cpp +++ b/src/luaexport/TimerMeta.cpp @@ -4,14 +4,9 @@ #include "svc/Timer.hpp" -#include "compsys/BasicMetaComponent.hpp" - static char const libname[] = "Timer"; #include "ExportThis.hpp" - -JD_BASIC_COMPONENT_IMPL(Timer) - static Timer::CallOrder Timer_callEvery( Timer& timer, sf::Time every, luabind::object o) { @@ -33,7 +28,7 @@ static void init(LuaVm& vm) LHMODULE [ # define LHCURCLASS Timer - class_("Timer") + LHCLASS .def("callEvery", &Timer_callEvery) .def("callAfter", &Timer_callAfter) .LHPROPG(frameDuration) diff --git a/src/main.cpp b/src/main.cpp index aade274..9125a51 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,6 @@ #include "State.hpp" #include "svc/LuaVm.hpp" #include "svc/FileSystem.hpp" -#include "svc/ServiceLocator.hpp" #include "svc/Mainloop.hpp" #include "svc/DrawService.hpp" #include "svc/EventDispatcher.hpp" @@ -28,6 +27,7 @@ #include #include #include // call_function (used @ loadStateFromLua) +#include #include #include #include @@ -58,52 +58,49 @@ static std::vector cmdLine; static int argc_ = 0; static const char* const* argv_ = nullptr; -template -struct ServiceEntry: public T -{ - ServiceEntry() +class LoadStateFromLua { +public: + LoadStateFromLua(lua_State* L): m_L(L) {} + + State* operator() (std::string const& name) { - ServiceLocator::registerService(*this); - } -}; - -static State* loadStateFromLua(std::string const& name) -{ - std::string const filename = "lua/states/" + name + ".lua"; - if (!PHYSFS_exists(filename.c_str())) - return nullptr; + std::string const filename = "lua/states/" + name + ".lua"; + if (!PHYSFS_exists(filename.c_str())) + return nullptr; - LOG_D("Loading state \"" + name + "\"..."); - State* s = nullptr; - try { - lua_State* L = ServiceLocator::luaVm().L(); - luaU::load(ServiceLocator::luaVm().L(), filename); - luabind::object chunk(luabind::from_stack(L, -1)); - lua_pop(L, 1); - - // The state must stay alive as long as the StateManager. - // However, if we simply use luabind::adopt here, we cause a memory - // leak. So, instead we bind the lifetime of the state to the one - // from the lua_State (== LuaVm in this case). As long as the LuaVm is - // destroyed (shortly) *after* the StateManager it just works. - // Otherwise, we would have a real problem: how could you possibly keep - // a Lua object alive, longer than it's lua_State? - luabind::object sobj = chunk(); - s = luabind::object_cast(sobj); - sobj.push(L); - luaL_ref(L, LUA_REGISTRYINDEX); - } catch (luabind::cast_failed const& e) { - LOG_E("failed casting lua value to " + std::string(e.info().name())); - } catch (luabind::error const& e) { - LOG_E(luaU::Error(e, "failed loading state \"" + name + '\"').what()); - } catch (std::exception const& e) { - LOG_EX(e); + LOG_D("Loading state \"" + name + "\"..."); + State* s = nullptr; + try { + luaU::load(m_L, filename); + luabind::object chunk(luabind::from_stack(m_L, -1)); + lua_pop(m_L, 1); + + // The state must stay alive as long as the StateManager. + // However, if we simply use luabind::adopt here, we cause a memory + // leak. So, instead we bind the lifetime of the state to the one + // from the lua_State (== LuaVm in this case). As long as the LuaVm is + // destroyed (shortly) *after* the StateManager it just works. + // Otherwise, we would have a real problem: how could you possibly keep + // a Lua object alive, longer than it's lua_State? + luabind::object sobj = chunk(); + s = luabind::object_cast(sobj); + sobj.push(m_L); + luaL_ref(m_L, LUA_REGISTRYINDEX); + } catch (luabind::cast_failed const& e) { + LOG_E("failed casting lua value to " + std::string(e.info().name())); + } catch (luabind::error const& e) { + LOG_E(luaU::Error(e, "failed loading state \"" + name + '\"').what()); + } catch (std::exception const& e) { + LOG_EX(e); + } + log().write( + "Loading State " + name + (s ? " finished." : " failed."), + s ? loglevel::debug : loglevel::error, LOGFILE_LOCATION); + return s; } - log().write( - "Loading State " + name + (s ? " finished." : " failed."), - s ? loglevel::debug : loglevel::error, LOGFILE_LOCATION); - return s; -} +private: + lua_State* m_L; +}; #ifdef _WIN32 static int openOfsStdHandle(DWORD nStdHandle, int flags = _O_TEXT) @@ -277,7 +274,6 @@ int main(int argc, char* argv[]) initializeStdStreams(basepath); // Construct and register services // - auto const regSvc = ServiceLocator::registerService; LOG_D("Initializing virtual filesystem..."); vfs::Init fsinit; @@ -309,16 +305,30 @@ int main(int argc, char* argv[]) initDefaultResourceLoaders(); LOG_D("Initializing Lua..."); - ServiceEntry luaVm; + LuaVm luaVm; try { luaVm.initLibs(); LOG_D("Finished initializing Lua."); - - ServiceEntry mainloop; - ServiceEntry conf; - ServiceEntry timer; - ServiceEntry sound; - ServiceEntry stateManager; + + luabind::object svctable = luabind::newtable(luaVm.L()); + luabind::object jdtable = luabind::rawget( + luabind::globals(luaVm.L()), "jd"); + luabind::rawset(jdtable, "svc", svctable); + + Mainloop mainloop; + luabind::rawset(svctable, "mainloop", &mainloop); + + StateManager stateManager; + luabind::rawset(svctable, "stateManager", &stateManager); + + Configuration conf(luaVm.L()); + luabind::rawset(svctable, "configuration", &conf); + + SoundManager sound; + luabind::rawset(svctable, "soundManager", &sound); + + Timer timer; + luabind::rawset(svctable, "timer", &timer); LOG_D("Loading configuration..."); conf.load(); @@ -340,22 +350,24 @@ int main(int argc, char* argv[]) LOG_D("Finished creating Window and preparing SFML."); EventDispatcher eventDispatcher(*window); - regSvc(eventDispatcher); + luabind::rawset(svctable, "eventDispatcher", &eventDispatcher); using boost::bind; mainloop.connect_processInput( bind(&EventDispatcher::dispatch, &eventDispatcher)); // Various other initializations // - ServiceLocator::stateManager().setStateNotFoundCallback( - &loadStateFromLua); + stateManager.setStateNotFoundCallback(LoadStateFromLua(luaVm.L())); DrawService drawService( *window, conf.get("misc.layerCount", 1UL)); - regSvc(drawService); + luabind::rawset(svctable, "drawService", &drawService); + mainloop.connect_preFrame(bind(&Timer::beginFrame, &timer)); mainloop.connect_update(bind(&Timer::processCallbacks, &timer)); - mainloop.connect_update(bind(&SoundManager::fade, &sound)); + mainloop.connect_update([&timer, &sound]() { + sound.fade(timer.frameDuration()); + }); mainloop.connect_preDraw(bind(&DrawService::clear, &drawService)); mainloop.connect_draw(bind(&DrawService::draw, &drawService)); mainloop.connect_postDraw(bind(&DrawService::display, &drawService)); @@ -380,7 +392,7 @@ int main(int argc, char* argv[]) // Run mainloop // LOG_D("Mainloop..."); - r = ServiceLocator::mainloop().exec(); + r = mainloop.exec(); LOG_D("Mainloop finished with exit code " + boost::lexical_cast(r) + "."); diff --git a/src/svc/Configuration.cpp b/src/svc/Configuration.cpp index 37cf772..e2b20d1 100644 --- a/src/svc/Configuration.cpp +++ b/src/svc/Configuration.cpp @@ -4,13 +4,11 @@ #include "Configuration.hpp" -#include "compsys/BasicMetaComponent.hpp" #include "Logfile.hpp" extern "C" { # include "lua.h" } #include "svc/FileSystem.hpp" -#include "svc/ServiceLocator.hpp" #include #include @@ -18,7 +16,9 @@ extern "C" { static char const libname[] = "Configuration"; #include "luaexport/ExportThis.hpp" -JD_BASIC_COMPONENT_IMPL(Configuration) +Configuration::Configuration(lua_State* L): + m_conf(luabind::newtable(L)) +{ } void Configuration::load( std::string const& configurationFilename, @@ -27,12 +27,12 @@ void Configuration::load( LOG_D("Configuration filename is \"" + configurationFilename + "\"."); m_confPath = configurationFilename; reload(); - luabind::globals(ServiceLocator::luaVm().L())[jd::moduleName][globalName] = m_conf; + luabind::globals(m_conf.interpreter())[jd::moduleName][globalName] = m_conf; } void Configuration::reload() { - lua_State* L = ServiceLocator::luaVm().L(); + lua_State* L = m_conf.interpreter(); if (m_conf) { LUAU_BALANCED_STACK(L); m_conf.push(L); @@ -55,7 +55,7 @@ void Configuration::reload() void Configuration::save() { - lua_State* L = ServiceLocator::luaVm().L(); + lua_State* L = m_conf.interpreter(); m_conf.push(L); std::string s = "return "; s += luaU::serialize(L, -1); @@ -104,7 +104,7 @@ static void init(LuaVm& vm) { LHMODULE [ # define LHCURCLASS Configuration - class_(BOOST_STRINGIZE(LHCURCLASS)) + LHCLASS .LHMEMFN(reload) .LHMEMFN(save) ]; diff --git a/src/svc/Configuration.hpp b/src/svc/Configuration.hpp index 8b1531b..293c368 100644 --- a/src/svc/Configuration.hpp +++ b/src/svc/Configuration.hpp @@ -5,8 +5,6 @@ #ifndef CONFIGURATION_HPP_INCLUDED #define CONFIGURATION_HPP_INCLUDED CONFIGURATION_HPP_INCLUDED -#include "compsys/Component.hpp" -#include "jdConfig.hpp" #include "luaUtils.hpp" #include "LuaVm.hpp" @@ -15,10 +13,10 @@ #include -class Configuration: public Component { - JD_COMPONENT +class Configuration { public: - + Configuration(lua_State* L); + class Error: public std::runtime_error { public: Error(std::string const& msg): @@ -55,7 +53,6 @@ class Configuration: public Component { } } - luabind::object getObject(std::string const& p); private: diff --git a/src/svc/DrawService.hpp b/src/svc/DrawService.hpp index 3c18564..1406f69 100644 --- a/src/svc/DrawService.hpp +++ b/src/svc/DrawService.hpp @@ -5,7 +5,6 @@ #ifndef DRAW_SERVICE_HPP_INCLUDED #define DRAW_SERVICE_HPP_INCLUDED DRAW_SERVICE_HPP_INCLUDED -#include "compsys/Component.hpp" #include "TransformGroup.hpp" #include @@ -17,8 +16,7 @@ namespace sf { class RenderWindow; class RenderTarget; } -class DrawService: public Component { - JD_COMPONENT +class DrawService { public: struct Layer { Layer() { } diff --git a/src/svc/LuaVm.cpp b/src/svc/LuaVm.cpp index 7729ece..a006d47 100644 --- a/src/svc/LuaVm.cpp +++ b/src/svc/LuaVm.cpp @@ -4,13 +4,13 @@ #include "LuaVm.hpp" -#include "compsys/BasicMetaComponent.hpp" #include "FileSystem.hpp" #include "jdConfig.hpp" #include "Logfile.hpp" #include "luaUtils.hpp" #include +#include extern "C" { # include } @@ -228,6 +228,3 @@ void LuaVm::deinit() collectgarbage(m_L); } - - -JD_BASIC_COMPONENT_IMPL(LuaVm) diff --git a/src/svc/LuaVm.hpp b/src/svc/LuaVm.hpp index e31d410..fab67f0 100644 --- a/src/svc/LuaVm.hpp +++ b/src/svc/LuaVm.hpp @@ -5,8 +5,6 @@ #ifndef LUA_VM_HPP_INCLUDED #define LUA_VM_HPP_INCLUDED LUA_VM_HPP_INCLUDED -#include "compsys/Component.hpp" - #include #include @@ -18,8 +16,7 @@ class MetaComponent; struct lua_State; -class LuaVm: public Component { - JD_COMPONENT +class LuaVm { public: explicit LuaVm(std::string const& libConfigFilename = "luainit.lua"); diff --git a/src/svc/ServiceLocator.cpp b/src/svc/ServiceLocator.cpp deleted file mode 100644 index 15cc711..0000000 --- a/src/svc/ServiceLocator.cpp +++ /dev/null @@ -1,65 +0,0 @@ -// Part of the Jade Engine -- Copyright (c) Christian Neumüller 2012--2013 -// This file is subject to the terms of the BSD 2-Clause License. -// See LICENSE.txt or http://opensource.org/licenses/BSD-2-Clause - -#include "svc/ServiceLocator.hpp" - -#include "compsys/Component.hpp" -#include "compsys/MetaComponent.hpp" -#include "svc/Configuration.hpp" -#include "svc/DrawService.hpp" -#include "svc/EventDispatcher.hpp" -#include "svc/LuaVm.hpp" -#include "svc/Mainloop.hpp" -#include "svc/SoundManager.hpp" -#include "svc/StateManager.hpp" -#include "svc/Timer.hpp" - - -/* static */ std::unordered_map ServiceLocator::registry; - -#define DEFINE_SVC_GETTER(cn, vn) \ - static cn* vn##_ = nullptr; \ - /* static */ cn& ServiceLocator::vn() \ - { \ - if (!vn##_) \ - throw Error("ServiceLocator: no " #cn " registered"); \ - return *vn##_; \ - } - -DEFINE_SVC_GETTER(StateManager, stateManager) -DEFINE_SVC_GETTER(LuaVm, luaVm) -DEFINE_SVC_GETTER(Mainloop, mainloop) -DEFINE_SVC_GETTER(EventDispatcher, eventDispatcher) -DEFINE_SVC_GETTER(DrawService, drawService) -DEFINE_SVC_GETTER(Timer, timer) -DEFINE_SVC_GETTER(SoundManager, soundManager) -DEFINE_SVC_GETTER(Configuration, configuration) - -/* static */ void ServiceLocator::registerService(Service& s) -{ - registry.insert(std::make_pair(&s.metaComponent(), &s)); - -# define ENTRY(cn, vn) \ - if (cn* ss = component_cast(&s)) \ - vn##_ = ss; - - ENTRY(StateManager, stateManager) - else ENTRY(LuaVm, luaVm) - else ENTRY(Mainloop, mainloop) - else ENTRY(EventDispatcher, eventDispatcher) - else ENTRY(DrawService, drawService) - else ENTRY(Timer, timer) - else ENTRY(SoundManager, soundManager) - else ENTRY(Configuration, configuration) - -# undef ENTRY -} - -/* static */ Component& ServiceLocator::get(MetaComponent const& m) -{ - auto const it = registry.find(&m); - if (it == registry.end()) - throw Error("ServiceLocator: no Service of type \"" + m.name() + "\" registered"); - return *it->second; -} diff --git a/src/svc/ServiceLocator.hpp b/src/svc/ServiceLocator.hpp deleted file mode 100644 index a4d8b27..0000000 --- a/src/svc/ServiceLocator.hpp +++ /dev/null @@ -1,56 +0,0 @@ -// Part of the Jade Engine -- Copyright (c) Christian Neumüller 2012--2013 -// This file is subject to the terms of the BSD 2-Clause License. -// See LICENSE.txt or http://opensource.org/licenses/BSD-2-Clause - -#ifndef SERVICE_LOCATOR_HPP_INCLUDED -#define SERVICE_LOCATOR_HPP_INCLUDED SERVICE_LOCATOR_HPP_INCLUDED - -#include -#include - - -class Configuration; -class StateManager; -class LuaVm; -class Mainloop; -class DrawService; -class EventDispatcher; -class Timer; -class SoundManager; - -class Component; -class MetaComponent; - -typedef Component Service; - -class ServiceLocator { -public: - class Error: public std::runtime_error { - public: - Error(std::string const& msg): std::runtime_error(msg) { } - }; - - template - static T& get() - { - return ServiceLocator::get(T::staticMetaComponent).template as(); - } - - static Service& get (MetaComponent const& m); - - static void registerService(Service& s); - - static StateManager& stateManager(); - static LuaVm& luaVm(); - static Mainloop& mainloop(); - static DrawService& drawService(); - static EventDispatcher& eventDispatcher(); - static Timer& timer(); - static SoundManager& soundManager(); - static Configuration& configuration(); - -private: - static std::unordered_map registry; -}; - -#endif diff --git a/src/svc/SoundManager.cpp b/src/svc/SoundManager.cpp index 4f41fc8..6bc4233 100644 --- a/src/svc/SoundManager.cpp +++ b/src/svc/SoundManager.cpp @@ -3,8 +3,6 @@ // See LICENSE.txt or http://opensource.org/licenses/BSD-2-Clause #include "SoundManager.hpp" -#include "Timer.hpp" -#include "ServiceLocator.hpp" #include "ressys/ResourceManager.hpp" @@ -42,9 +40,9 @@ SoundManager::FadedMusic& SoundManager::FadedMusic::operator= (FadedMusic&& rhs) return *this; } -bool SoundManager::FadedMusic::fade() +bool SoundManager::FadedMusic::fade(sf::Time elapsedTime) { - float frameSeconds = ServiceLocator::timer().frameDuration().asSeconds(); + float frameSeconds = elapsedTime.asSeconds(); float const newVolume = music->getVolume() + increment * frameSeconds; music->setVolume(newVolume); if (increment < 0 ? newVolume <= target : newVolume >= target) { @@ -96,7 +94,7 @@ void SoundManager::tidy() m_playingSounds.end()); } -void SoundManager::fade() +void SoundManager::fade(sf::Time elapsedTime) { if ( !m_currentMusic.music || @@ -104,8 +102,9 @@ void SoundManager::fade() ) { return; } + if (m_previousMusic.music) - m_previousMusic.fade(); - if (m_currentMusic.fade()) + m_previousMusic.fade(elapsedTime); + if (m_currentMusic.fade(elapsedTime)) m_previousMusic.music.reset(); } diff --git a/src/svc/SoundManager.hpp b/src/svc/SoundManager.hpp index 1037b1e..b7ed63a 100644 --- a/src/svc/SoundManager.hpp +++ b/src/svc/SoundManager.hpp @@ -5,7 +5,6 @@ #ifndef SOUNDMANAGER_HPP_INCLUDED #define SOUNDMANAGER_HPP_INCLUDED SOUNDMANAGER_HPP_INCLUDED -#include "compsys/Component.hpp" #include "ressys/AutoResource.hpp" #include "ressys/AutoSoundBuffer.hpp" #include "ressys/VFileMusic.hpp" @@ -18,9 +17,7 @@ typedef AutoResource AutoSound; -class SoundManager: public Component { - JD_COMPONENT - +class SoundManager { public: SoundManager(); ~SoundManager(); @@ -32,7 +29,7 @@ class SoundManager: public Component { void setBackgroundMusic(std::string const& name, sf::Time fadeDuration = sf::Time::Zero); void setBackgroundMusic(VFileMusic& music, sf::Time fadeDuration = sf::Time::Zero); - void fade(); + void fade(sf::Time elapsedTime); void tidy(); private: @@ -41,7 +38,7 @@ class SoundManager: public Component { FadedMusic(VFileMusic& music, float target, sf::Time fadeDuration); FadedMusic& operator= (FadedMusic&& rhs); - bool fade(); + bool fade(sf::Time elapsedTime); float target; std::unique_ptr music; diff --git a/src/svc/StateManager.hpp b/src/svc/StateManager.hpp index a4c7dce..05f1d21 100644 --- a/src/svc/StateManager.hpp +++ b/src/svc/StateManager.hpp @@ -5,8 +5,6 @@ #ifndef STATE_MANAGER_HPP_INCLUDED #define STATE_MANAGER_HPP_INCLUDED STATE_MANAGER_HPP_INCLUDED -#include "compsys/Component.hpp" - #include #include @@ -16,8 +14,7 @@ class State; -class StateManager: public Component { - JD_COMPONENT +class StateManager { public: StateManager(): m_modifying(false) { } diff --git a/src/svc/Timer.cpp b/src/svc/Timer.cpp index b0e8787..19ba7ec 100644 --- a/src/svc/Timer.cpp +++ b/src/svc/Timer.cpp @@ -120,7 +120,7 @@ void Timer::CallOrder::disconnect() if (!m_timer.valid()) throw std::logic_error("attempt to cancel a CallOrder twice"); m_timer->cancelOrder(m_id); - m_timer = static_cast(nullptr); + m_timer = static_cast(nullptr); } bool Timer::CallOrder::isConnected() const diff --git a/src/svc/Timer.hpp b/src/svc/Timer.hpp index 9f95428..ebeea9b 100644 --- a/src/svc/Timer.hpp +++ b/src/svc/Timer.hpp @@ -5,7 +5,7 @@ #ifndef TIMER_HPP_INCLUDED #define TIMER_HPP_INCLUDED TIMER_HPP_INCLUDED -#include "compsys/Component.hpp" +#include "WeakRef.hpp" #include #include @@ -16,9 +16,7 @@ #include -class Timer: public Component { - JD_COMPONENT - +class Timer: public EnableWeakRefFromThis { public: typedef boost::function Callback;