Skip to content

Commit

Permalink
Remove ServiceLocator and Component bases for most services.
Browse files Browse the repository at this point in the history
EventDispatcher and Mainloop still need component base classes because they
expose events to Lua using jd.connect().

Games will need small adoptions: The services are now global objects in the
jd.svc table instead of being returned from a function in jd.svc with the
same name.

Additionally, isKeyPressed, isMouseButtonPressed and mousePosition are now
exported as member functions of EventDispatcher instead of as free functions.
  • Loading branch information
Oberon00 committed Jul 26, 2013
1 parent f691b34 commit 15ff616
Show file tree
Hide file tree
Showing 23 changed files with 107 additions and 331 deletions.
2 changes: 0 additions & 2 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <luabind/operator.hpp>
and is good style anyway.
Expand Down
3 changes: 0 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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})
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/compsys/MetaComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "Entity.hpp"
#include "Logfile.hpp"
#include "luaUtils.hpp"
#include "svc/ServiceLocator.hpp"
#include "svc/LuaVm.hpp"

#include <boost/format.hpp>
Expand Down
1 change: 0 additions & 1 deletion src/luaexport/Collisions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "LuaFunction.hpp"
#include "SfBaseTypes.hpp"
#include "sfUtil.hpp"
#include "svc/ServiceLocator.hpp"
#include "Tilemap.hpp"

#include <luabind/out_value_policy.hpp>
Expand Down
13 changes: 5 additions & 8 deletions src/luaexport/DrawServiceMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -21,20 +17,21 @@ 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
class_<LHCURCLASS>("DrawLayer")
.def_readonly(LHMEMARGS(group))
.LHPROPRW(view),
# undef LHCURCLASS
class_<DrawService, Component>("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
];
}
34 changes: 3 additions & 31 deletions src/luaexport/EventDispatcherMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<sf::Keyboard::Key>(k));
}

static bool EventDispatcher_isMouseButtonPressed(unsigned b)
{
if (!dsp)
dsp = &ServiceLocator::eventDispatcher();
return dsp->isMouseButtonPressed(static_cast<sf::Mouse::Button>(b));
}

static LuaVec2 EventDispatcher_mousePosition()
{
if (!dsp)
dsp = &ServiceLocator::eventDispatcher();
return static_cast<LuaVec2>(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_<LHCURCLASS, Component>(BOOST_STRINGIZE(LHCURCLASS))
.LHPROPG(isWindowFocused)
.LHMEMFN(isKeyPressed)
.LHMEMFN(isMouseButtonPressed)
.LHMEMFN(mousePosition)
];
}
43 changes: 0 additions & 43 deletions src/luaexport/ServiceLocator.cpp

This file was deleted.

8 changes: 1 addition & 7 deletions src/luaexport/SoundManagerMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -35,7 +29,7 @@ static void init(LuaVm& vm)
LHMODULE [
cSoundBuffer,
# define LHCURCLASS SoundManager
class_<LHCURCLASS, Component>("SoundManager")
LHCLASS
.def("playSound",
(void(LHCURCLASS::*)(std::string const&))
&SoundManager::playSound)
Expand Down
5 changes: 1 addition & 4 deletions src/luaexport/StateManagerMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -20,7 +19,7 @@ static void init(LuaVm& vm)
vm.initLib("State");
LHMODULE [
# define LHCURCLASS StateManager
class_<StateManager, bases<Component>>("StateManager")
class_<StateManager>("StateManager")
.def("push", (void(StateManager::*)(State&))(&StateManager::push))
.def("push", (void(StateManager::*)(std::string const&))(&StateManager::push))
.def("pushAdditional", (void(StateManager::*)(State&))(&StateManager::pushAdditional))
Expand All @@ -37,5 +36,3 @@ static void init(LuaVm& vm)
.def("state", &StateManager::stateForId)
];
}

JD_BASIC_COMPONENT_IMPL(StateManager)
7 changes: 1 addition & 6 deletions src/luaexport/TimerMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -33,7 +28,7 @@ static void init(LuaVm& vm)
LHMODULE [

# define LHCURCLASS Timer
class_<LHCURCLASS, Component>("Timer")
LHCLASS
.def("callEvery", &Timer_callEvery)
.def("callAfter", &Timer_callAfter)
.LHPROPG(frameDuration)
Expand Down
Loading

0 comments on commit 15ff616

Please sign in to comment.