diff --git a/.hgignore b/.hgignore index 37e9599..36a72c0 100644 --- a/.hgignore +++ b/.hgignore @@ -9,7 +9,7 @@ Makefile start runtests -hasteroids +run_asteroids ./doc/html/* diff --git a/asteroids/ControllerCommands.cc b/asteroids/ControllerCommands.cc index bcb42c3..520e58a 100644 --- a/asteroids/ControllerCommands.cc +++ b/asteroids/ControllerCommands.cc @@ -3,7 +3,7 @@ #include "Controller.h" #include "Model.h" #include "EntityBase.h" -#include "EventsBase.h" +#include "EventBase.h" #include "Controller.h" #include "ShipEntity.h" diff --git a/asteroids/Model.cc b/asteroids/Model.cc index d37e314..e6d9ed4 100644 --- a/asteroids/Model.cc +++ b/asteroids/Model.cc @@ -57,17 +57,33 @@ int Model::OnInit (int argc, char* argv[]) { } // initialize event handlers and register them - mLevelCompleteEventHandler = new LevelCompleteEventHandler (this); - Engine::RegisterListener (mLevelCompleteEventHandler, EventLevelComplete); - - mGameOverEventHandler = new GameOverEventHandler (this); - Engine::RegisterListener (mGameOverEventHandler, EventGameOver); + Engine::RegisterListener (this, EventLevelComplete); + Engine::RegisterListener (this, EventGameOver); mPlayerName = "Player"; return result; } +bool Model::OnReceiveEvent (const Engine::EventBasePtr &event) { + switch (event->mEventType) { + case EventLevelComplete: + return OnLevelComplete(); + break; + case EventGameOver: + return OnGameOver(); + break; + + default: Engine::LogWarning ("Received Event with type %d but don't know what to do with it!", event->mEventType); + break; + } + + return false; +} + +/* + * Module specific functions + */ void Model::Process () { if (mLastGameState == mGameState) { if (mGameState == GameStateRunning) { diff --git a/asteroids/Model.h b/asteroids/Model.h index 85c7ed8..1762332 100644 --- a/asteroids/Model.h +++ b/asteroids/Model.h @@ -50,8 +50,6 @@ class Model : public Engine::ModelBase { mAsteroids.clear(); mLevelList.clear(); SaveHighscoreList(); - delete mLevelCompleteEventHandler; - delete mGameOverEventHandler; }; virtual void OnRegisterCommands (); @@ -70,32 +68,7 @@ class Model : public Engine::ModelBase { std::string mPlayerName; std::vector mLevelList; - /* event handler class definitions */ - class LevelCompleteEventHandler : public Engine::EventListenerBase { - public: - explicit LevelCompleteEventHandler (Model *view) : mModel (view) {}; - virtual bool HandleEvent (const Engine::EventBasePtr &event) const { - return mModel->OnLevelComplete(); - } - private: - LevelCompleteEventHandler() {}; - Model *mModel; - }; - - class GameOverEventHandler : public Engine::EventListenerBase { - public: - explicit GameOverEventHandler (Model *view) : mModel (view) {}; - virtual bool HandleEvent (const Engine::EventBasePtr &event) const { - return mModel->OnGameOver(); - } - private: - GameOverEventHandler() {}; - Model *mModel; - }; - - /* event handler member variables */ - LevelCompleteEventHandler *mLevelCompleteEventHandler; - GameOverEventHandler *mGameOverEventHandler; + virtual bool OnReceiveEvent (const Engine::EventBasePtr &event); friend class View; }; diff --git a/asteroids/View.cc b/asteroids/View.cc index 983b1e5..137a366 100644 --- a/asteroids/View.cc +++ b/asteroids/View.cc @@ -6,7 +6,7 @@ #include "Engine.h" #include "Physics.h" #include "Model.h" -#include "EventsBase.h" +#include "EventBase.h" #include "ShipEntity.h" #include "AsteroidEntity.h" @@ -78,61 +78,64 @@ int View::OnInit (int argc, char* argv[]) { mShipPartsSprite.LoadFromPNG ("./data/textures/ship_parts.png"); mShipPartsSprite.SetSubSpriteCount (10); - mAccelerateEventHandler = new AccelerateEventHandler (this); - Engine::RegisterListener (mAccelerateEventHandler, EventAccelerateStart); - Engine::RegisterListener (mAccelerateEventHandler, EventAccelerateStop); - - mShipExplodeEventHandler = new ShipExplodeEventHandler (this); - Engine::RegisterListener (mShipExplodeEventHandler, EventShipExplode); + Engine::RegisterListener (this, EventAccelerateStart); + Engine::RegisterListener (this, EventAccelerateStop); + Engine::RegisterListener (this, EventShipExplode); return 0; } void View::OnDestroy() { - delete mAccelerateEventHandler; - delete mShipExplodeEventHandler; - mBackgroundStars.clear(); mShipPartsEntityIds.clear(); Engine::ViewBase::OnDestroy(); } -/* - * Event Handlers - */ -bool View::AccelerateEventHandler::HandleEvent (const Engine::EventBasePtr &event) const { - if (event->mEventType == EventAccelerateStart) - mView->mShipThrustSprite.ResetAnimation(); +bool View::OnReceiveEvent (const Engine::EventBasePtr &event) { + switch (event->mEventType) { + case EventAccelerateStart: + case EventAccelerateStop: + if (event->mEventType == EventAccelerateStart) + mShipThrustSprite.ResetAnimation(); - Engine::LogDebug ("Received Acceleration Event: %d", event->mEventType); - return true; -} + Engine::LogDebug ("Received Acceleration Event: %d", event->mEventType); + return true; -bool View::ShipExplodeEventHandler::HandleEvent (const Engine::EventBasePtr &event) const { - if (event->mEventType == EventShipExplode) { - Engine::EntityBase *ship_entity = Engine::GetEntity (event->mEventUnsignedInt); - vector3d position = ship_entity->mPhysicState->mPosition; - vector3d orientation = ship_entity->mPhysicState->mOrientation; - vector3d velocity = ship_entity->mPhysicState->mVelocity; + break; + case EventShipExplode: + if (event->mEventType == EventShipExplode) { + Engine::EntityBase *ship_entity = Engine::GetEntity (event->mEventUnsignedInt); + vector3d position = ship_entity->mPhysicState->mPosition; + vector3d orientation = ship_entity->mPhysicState->mOrientation; + vector3d velocity = ship_entity->mPhysicState->mVelocity; - unsigned int i; - mView->mShipPartsEntityIds.clear(); + unsigned int i; + mShipPartsEntityIds.clear(); - for (i = 0; i < mView->mShipPartsSprite.GetSubSpriteCount(); i++) { - Engine::EntityBase* part_sprite_particle = Engine::CreateEntity (GameEntityTypeShipPart); - part_sprite_particle->mPhysicState->mPosition = position; - part_sprite_particle->mPhysicState->mOrientation = orientation; - part_sprite_particle->mPhysicState->mVelocity = velocity; - part_sprite_particle->mPhysicState->mVelocity = vector3d (velocity[0] * (rand()/float(RAND_MAX)) * 1.7, 0., velocity[2] * (rand()/float(RAND_MAX)) * 1.5); - part_sprite_particle->mPhysicState->mAngleVelocity = (rand()/float(RAND_MAX) - 0.5 ) * 100.; + for (i = 0; i < mShipPartsSprite.GetSubSpriteCount(); i++) { + Engine::EntityBase* part_sprite_particle = Engine::CreateEntity (GameEntityTypeShipPart); + part_sprite_particle->mPhysicState->mPosition = position; + part_sprite_particle->mPhysicState->mOrientation = orientation; + part_sprite_particle->mPhysicState->mVelocity = velocity; + part_sprite_particle->mPhysicState->mVelocity = vector3d (velocity[0] * (rand()/float(RAND_MAX)) * 1.7, 0., velocity[2] * (rand()/float(RAND_MAX)) * 1.5); + part_sprite_particle->mPhysicState->mAngleVelocity = (rand()/float(RAND_MAX) - 0.5 ) * 100.; - mView->mShipPartsEntityIds.push_back(part_sprite_particle->mId); - } + mShipPartsEntityIds.push_back(part_sprite_particle->mId); + } + } + + Engine::LogDebug ("Received Ship Explode Event: %d", event->mEventType); + return true; + + + break; + + default: Engine::LogWarning ("Received Event with type %d but don't know what to do with it!", event->mEventType); + break; } - Engine::LogDebug ("Received Ship Explode Event: %d", event->mEventType); - return true; + return false; } /* diff --git a/asteroids/View.h b/asteroids/View.h index 8e6fbd6..c49238a 100644 --- a/asteroids/View.h +++ b/asteroids/View.h @@ -54,26 +54,7 @@ class View : public Engine::ViewBase { Engine::Sprite mShipThrustSprite; Engine::Sprite mShipPartsSprite; - class AccelerateEventHandler : public Engine::EventListenerBase { - public: - explicit AccelerateEventHandler (View *view) : mView (view) {}; - virtual bool HandleEvent (const Engine::EventBasePtr &event) const; - private: - View *mView; - }; - - class ShipExplodeEventHandler : public Engine::EventListenerBase { - public: - explicit ShipExplodeEventHandler (View *view) : mView (view) {}; - virtual bool HandleEvent (const Engine::EventBasePtr &event) const; - private: - View *mView; - }; - - AccelerateEventHandler *mAccelerateEventHandler; - ShipExplodeEventHandler *mShipExplodeEventHandler; - - friend class AccelerateEventHandler; + virtual bool OnReceiveEvent (const Engine::EventBasePtr &event); }; } diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index f8acc54..ccddc3e 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -22,7 +22,7 @@ SET ( ENGINE_SRCS PhysicsBase.cc PhysicsEntityBase.cc ViewBase.cc - EventsBase.cc + EventBase.cc OverlayBase.cc Commands.cc diff --git a/engine/Engine.cc b/engine/Engine.cc index a061fc3..2718269 100644 --- a/engine/Engine.cc +++ b/engine/Engine.cc @@ -11,7 +11,7 @@ #include "ControllerBase.h" #include "EntityFactoryBase.h" -#include "EventsBase.h" +#include "EventManager.h" #include #include diff --git a/engine/Engine.h b/engine/Engine.h index a063cc5..3d3149e 100644 --- a/engine/Engine.h +++ b/engine/Engine.h @@ -154,6 +154,6 @@ ControllerBase* EngineGetController (); #include "CommandsGlobal.h" #include "ModelBaseGlobal.h" #include "ViewBaseGlobal.h" -#include "EventsBaseGlobal.h" +#include "EventBaseGlobal.h" #endif // _ENGINE_H diff --git a/engine/EventsBase.cc b/engine/EventBase.cc similarity index 72% rename from engine/EventsBase.cc rename to engine/EventBase.cc index 221055b..5344b34 100644 --- a/engine/EventsBase.cc +++ b/engine/EventBase.cc @@ -1,4 +1,5 @@ -#include "EventsBase.h" +#include "EventBase.h" +#include "EventManager.h" #include "Logging.h" namespace Engine { @@ -15,10 +16,10 @@ void EventManager::OnDestroy() { EventManagerInstance = NULL; } -bool EventManager::RegisterListener (const EventListenerBase *listener, const int event_type) { - LogDebug ("Registering Event listener %x for event type %d", listener, event_type); +bool EventManager::RegisterListener (Module *listener_module, const int event_type) { + LogDebug ("Registering Event listener %x for event type %d", listener_module, event_type); - mEventTypeListeners[event_type].push_back(listener); + mEventTypeListeners[event_type].push_back(listener_module); return true; } @@ -42,9 +43,9 @@ bool EventManager::TriggerEvent (const EventBasePtr &event) { if (!HasEventTypeListener (event->mEventType)) return false; - std::vector::iterator listener_iter = mEventTypeListeners[event->mEventType].begin(); + std::vector::iterator listener_iter = mEventTypeListeners[event->mEventType].begin(); for ( ; listener_iter != mEventTypeListeners[event->mEventType].end(); listener_iter++) { - if ((*listener_iter)->HandleEvent (event)) + if ((*listener_iter)->SendEvent (event)) return true; } @@ -55,13 +56,13 @@ bool EventManager::TriggerEvent (const EventBasePtr &event) { * Global Functions */ /** \brief Registers a listener to a given event type */ -bool RegisterListener (const EventListenerBase *listener, const int event_type) { +bool RegisterListener (Module *listener_module, const int event_type) { if (!EventManagerInstance) { - LogError ("Could not register EventListenerBase: EventManager not initialized!"); + LogError ("Could not register Module: EventManager not initialized!"); return false; } - return EventManagerInstance->RegisterListener (listener, event_type); + return EventManagerInstance->RegisterListener (listener_module, event_type); } /** \brief Calls all event listeners to handle the events */ diff --git a/engine/EventBase.h b/engine/EventBase.h new file mode 100644 index 0000000..dc2916f --- /dev/null +++ b/engine/EventBase.h @@ -0,0 +1,32 @@ +#ifndef EVENTSBASE_H +#define EVENTSBASE_H + +// #include "Module.h" + +#include + +#include +#include +#include +#include + +namespace Engine { + +class Module; + +/** \brief Contains all information relevant to the Event */ +struct EventBase { + int mEventType; + + /** \brief This might later be used for de-/serializing of the event data */ + std::string mEventData; + float mEventFloat; + int mEventInt; + unsigned int mEventUnsignedInt; +}; + +typedef boost::shared_ptr EventBasePtr; + +} + +#endif /* EVENTSBASE_H */ diff --git a/engine/EventsBaseGlobal.h b/engine/EventBaseGlobal.h similarity index 78% rename from engine/EventsBaseGlobal.h rename to engine/EventBaseGlobal.h index f83a8b8..d5becb5 100644 --- a/engine/EventsBaseGlobal.h +++ b/engine/EventBaseGlobal.h @@ -1,12 +1,12 @@ #ifndef EVENTSBASEGLOBAL_H #define EVENTSBASEGLOBAL_H -#include "EventsBase.h" +#include "EventBase.h" namespace Engine { /** \brief Registers a listener to a given event type */ -bool RegisterListener (const EventListenerBase *listener, const int event_type); +bool RegisterListener (Module *listener_module, const int event_type); /** \brief Calls all event listeners to handle the events */ bool QueueEvent (const EventBasePtr &event); /** \brief Calls the listener handlers immediately */ diff --git a/engine/EventsBase.h b/engine/EventManager.h similarity index 54% rename from engine/EventsBase.h rename to engine/EventManager.h index fa3be18..7627836 100644 --- a/engine/EventsBase.h +++ b/engine/EventManager.h @@ -1,7 +1,7 @@ -#ifndef EVENTSBASE_H -#define EVENTSBASE_H +#ifndef EVENTSMANAGER_H +#define EVENTSMANAGER_H -#include "Module.h" +// #include "Module.h" #include @@ -10,47 +10,20 @@ #include #include +#include "EventBase.h" +#include "Module.h" + namespace Engine { -/** \brief Contains all information relevant to the Event */ -struct EventBase { - int mEventType; - - /** \brief This might later be used for de-/serializing of the event data */ - std::string mEventData; - float mEventFloat; - int mEventInt; - unsigned int mEventUnsignedInt; -}; - -typedef boost::shared_ptr EventBasePtr; - -/** \brief Takes care of the handling of the Event +/** \brief Keeps track of all the Modules that registered for a given event type * - * Processing of the Event is done in the EventListenerBase::HandleEvent() - * function. - */ -class EventListenerBase { - public: - virtual ~EventListenerBase() {}; - /** \brief Handles the Event */ - virtual bool HandleEvent (const EventBasePtr &event) const = 0; - void just_for_the_vtable() {}; - - /** \brief For debugging */ - std::string mName; -}; - -typedef boost::shared_ptr EventListenerPtr; - -/** \brief Keeps track of all the EventListenerBase objects and receives Event notifications. */ class EventManager : public Module { public: virtual ~EventManager() {}; /** \brief Registers a listener to a given event type */ - bool RegisterListener (const EventListenerBase *listener, const int event_type); + bool RegisterListener (Module *listener_module, const int event_type); /** \brief Calls all event listeners to handle the events */ void Process (); /** \brief Queues the until Process() gets called */ @@ -79,10 +52,10 @@ class EventManager : public Module { private: /** \brief Contains for each event type the list of listeners */ - std::map > mEventTypeListeners; + std::map > mEventTypeListeners; std::queue mQueuedEvents; }; } -#endif /* EVENTSBASE_H */ +#endif /* EVENTSMANAGER_H */ diff --git a/engine/Module.h b/engine/Module.h index d7ac06a..ef00d19 100644 --- a/engine/Module.h +++ b/engine/Module.h @@ -1,6 +1,8 @@ #ifndef MODULE_H #define MODULE_H +#include "EventBase.h" + namespace Engine { /** \brief Base class for the separate modules @@ -36,6 +38,7 @@ class Module { /** \brief Calls the function that registers its commands to the * Commandsystem */ virtual void RegisterCommands () { OnRegisterCommands (); } + virtual bool SendEvent (const EventBasePtr &event) { return OnReceiveEvent (event); } protected: /** \brief The actual function being called when Init () is called */ @@ -44,6 +47,8 @@ class Module { virtual void OnDestroy () { }; /** \brief Registers the commands of the Module */ virtual void OnRegisterCommands () { }; + /** \brief Reacts on a event that was sent to the module */ + virtual bool OnReceiveEvent (const EventBasePtr &event) { return false; }; }; } diff --git a/engine/libraries/coll2d/CMakeLists.txt b/engine/libraries/coll2d/CMakeLists.txt index f4d35d7..548c17f 100644 --- a/engine/libraries/coll2d/CMakeLists.txt +++ b/engine/libraries/coll2d/CMakeLists.txt @@ -17,5 +17,5 @@ SET_TARGET_PROPERTIES ( ${PROJECT_EXECUTABLES} PROPERTIES SUBDIRS (tests) -ADD_LIBRARY ( coll2d ${COLL2D_SRCS} ) +ADD_LIBRARY ( coll2d SHARED ${COLL2D_SRCS} ) diff --git a/engine/libraries/mathlib/CMakeLists.txt b/engine/libraries/mathlib/CMakeLists.txt index 96ae294..724a06b 100644 --- a/engine/libraries/mathlib/CMakeLists.txt +++ b/engine/libraries/mathlib/CMakeLists.txt @@ -6,5 +6,4 @@ SET ( SRCS mathlib.cc main.cc ) - -ADD_LIBRARY ( mathlib mathlib.cc mathlib.h) +ADD_LIBRARY ( mathlib SHARED mathlib.cc mathlib.h) diff --git a/engine/libraries/oglft/liboglft/CMakeLists.txt b/engine/libraries/oglft/liboglft/CMakeLists.txt index fb6d0ad..ef0805c 100644 --- a/engine/libraries/oglft/liboglft/CMakeLists.txt +++ b/engine/libraries/oglft/liboglft/CMakeLists.txt @@ -23,7 +23,7 @@ IF( WIN32 ) ADD_DEFINITIONS( -DOGLFT_BUILD ) ENDIF( WIN32 ) -ADD_LIBRARY( oglft STATIC ${sources} ) +ADD_LIBRARY( oglft SHARED ${sources} ) TARGET_LINK_LIBRARIES( oglft ${FREETYPE2_LIBRARIES} diff --git a/engine/tests/CMakeLists.txt b/engine/tests/CMakeLists.txt index 54f96ba..5797f41 100644 --- a/engine/tests/CMakeLists.txt +++ b/engine/tests/CMakeLists.txt @@ -9,7 +9,7 @@ SET ( TESTS_SRCS main.cc CommandsTests.cc EntityTests.cc - EventsBaseTests.cc + EventBaseTests.cc PhysicsTests.cc ControllerBaseTests.cc ) diff --git a/engine/tests/EventsBaseTests.cc b/engine/tests/EventBaseTests.cc similarity index 80% rename from engine/tests/EventsBaseTests.cc rename to engine/tests/EventBaseTests.cc index 8228668..42dbf6d 100644 --- a/engine/tests/EventsBaseTests.cc +++ b/engine/tests/EventBaseTests.cc @@ -1,7 +1,8 @@ #include #include "Logging.h" -#include "EventsBase.h" +#include "Module.h" +#include "EventManager.h" using namespace std; using namespace Engine; @@ -9,18 +10,21 @@ using namespace Engine; int global_event_type = -1; string global_event_string = ""; -class TestEventListener : public EventListenerBase { - public: - TestEventListener() { - mName = "TestEventListener"; - } +struct EventsFixture; - virtual bool HandleEvent (const EventBasePtr &event) const { +class TestEventModule : public Module { + private: + virtual int OnInit (int argc, char* argv[]) { + return 0; + }; + virtual bool OnReceiveEvent (const EventBasePtr &event) { global_event_type = event->mEventType; global_event_string = event->mEventData; return true; - } + } + + friend class EventsFixture; }; struct EventsFixture { @@ -43,17 +47,17 @@ struct EventsFixture { Logging LoggingModule; - TestEventListener Listener; + TestEventModule TestModule; EventManager *TestEventManager; }; -TEST_FIXTURE ( EventsFixture, TestEventListenerHandleEvent ) { +TEST_FIXTURE ( EventsFixture, TestEventModuleSendEvent ) { EventBasePtr event (new EventBase); event->mEventType = 1; event->mEventData = "test"; - Listener.HandleEvent (event); + TestModule.SendEvent (event); CHECK_EQUAL (1, global_event_type); CHECK_EQUAL ("test", global_event_string); @@ -65,17 +69,17 @@ TEST_FIXTURE ( EventsFixture, TestTestEventManagerZeroListeners ) { } TEST_FIXTURE ( EventsFixture, TestTestEventManagerAddListener ) { - TestEventManager->RegisterListener (&Listener, 1); + TestEventManager->RegisterListener (&TestModule, 1); CHECK_EQUAL (true, TestEventManager->HasEventTypeListener(1)); CHECK_EQUAL (false, TestEventManager->HasEventTypeListener(2)); - TestEventManager->RegisterListener (&Listener, 1); + TestEventManager->RegisterListener (&TestModule, 1); CHECK_EQUAL (2, TestEventManager->GetEventTypeListenerCount(1)); } TEST_FIXTURE ( EventsFixture, TestTestEventManagerTriggerEvent ) { - TestEventManager->RegisterListener (&Listener, 1); + TestEventManager->RegisterListener (&TestModule, 1); CHECK_EQUAL (true, TestEventManager->HasEventTypeListener(1)); @@ -94,7 +98,7 @@ TEST_FIXTURE ( EventsFixture, TestTestEventManagerTriggerEvent ) { } TEST_FIXTURE ( EventsFixture, TestTestEventManagerQueueEvent ) { - TestEventManager->RegisterListener (&Listener, 1); + TestEventManager->RegisterListener (&TestModule, 1); EventBasePtr event (new EventBase); @@ -111,7 +115,7 @@ TEST_FIXTURE ( EventsFixture, TestTestEventManagerQueueEvent ) { } TEST_FIXTURE ( EventsFixture, TestTestEventManagerProcess ) { - TestEventManager->RegisterListener (&Listener, 1); + TestEventManager->RegisterListener (&TestModule, 1); EventBasePtr event (new EventBase);