| [496] | 1 | /*! | 
|---|
 | 2 |     @file Tickable.h | 
|---|
 | 3 |     @brief Definition of the Tickable interface. | 
|---|
 | 4 |  | 
|---|
 | 5 |     The Tickable interface provides a tick(dt) function, that gets called every frame. | 
|---|
 | 6 |     float dt is the time since the last frame. | 
|---|
 | 7 |  | 
|---|
 | 8 |     Attention: | 
|---|
 | 9 |     Classes derived from a Tickable that want to have a tick(dt) function on their part, MUST call the | 
|---|
 | 10 |     parent::tick(dt) function explicit in their implementation of tick(dt) because it's a virtual function. | 
|---|
 | 11 | */ | 
|---|
 | 12 |  | 
|---|
| [384] | 13 | #ifndef _Tickable_H__ | 
|---|
 | 14 | #define _Tickable_H__ | 
|---|
 | 15 |  | 
|---|
| [496] | 16 | #include "../core/CoreIncludes.h" | 
|---|
 | 17 | #include "OgreFrameListener.h" | 
|---|
| [384] | 18 |  | 
|---|
 | 19 | namespace orxonox | 
|---|
 | 20 | { | 
|---|
| [496] | 21 |     class TickFrameListener; // Forward declaration | 
|---|
| [384] | 22 |  | 
|---|
| [496] | 23 |     //! The Tickable interface provides a tick(dt) function, that gets called every frame. | 
|---|
| [384] | 24 |     class Tickable : virtual public OrxonoxClass | 
|---|
 | 25 |     { | 
|---|
 | 26 |         public: | 
|---|
| [496] | 27 |             /** | 
|---|
 | 28 |                 @brief Gets called every frame. | 
|---|
 | 29 |                 @param dt The time since the last frame | 
|---|
 | 30 |             */ | 
|---|
| [384] | 31 |             virtual void tick(float dt) = 0; | 
|---|
 | 32 |  | 
|---|
 | 33 |         protected: | 
|---|
| [496] | 34 |             /** | 
|---|
 | 35 |                 @brief Constructor: Registers the object in the Tickable-list | 
|---|
 | 36 |             */ | 
|---|
| [384] | 37 |             Tickable() { RegisterRootObject(Tickable); } | 
|---|
 | 38 |     }; | 
|---|
 | 39 |  | 
|---|
| [496] | 40 |     //! The TickFrameListener calls the tick(dt) function of all Tickables every frame. | 
|---|
| [384] | 41 |     class TickFrameListener : public Ogre::FrameListener | 
|---|
 | 42 |     { | 
|---|
 | 43 |         private: | 
|---|
| [496] | 44 |             /** @brief Gets called before a frame gets rendered. */ | 
|---|
| [384] | 45 |             bool frameStarted(const Ogre::FrameEvent &evt) | 
|---|
 | 46 |             { | 
|---|
| [496] | 47 |                 // Iterate through all Tickables and call their tick(dt) function | 
|---|
| [384] | 48 |                 for (Iterator<Tickable> it = ObjectList<Tickable>::start(); it; ++it) | 
|---|
 | 49 |                     it->tick(evt.timeSinceLastFrame); | 
|---|
 | 50 |  | 
|---|
 | 51 |                 return FrameListener::frameStarted(evt); | 
|---|
 | 52 |             } | 
|---|
 | 53 |     }; | 
|---|
 | 54 | } | 
|---|
 | 55 |  | 
|---|
 | 56 | #endif | 
|---|