1 /***************************************************************************
2  *      Mechanized Assault and Exploration Reloaded Projectfile            *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 
20 #ifndef ui_graphical_game_animations_animationtimerH
21 #define ui_graphical_game_animations_animationtimerH
22 
23 #include <SDL_timer.h>
24 #include <atomic>
25 
26 #include "utility/signal/signal.h"
27 #include "utility/runnable.h"
28 
29 /**
30  * Central class to get animation timers from.
31  *
32  * This class implements the runnable interface so that the execution
33  * of animation callbacks can be put into the main game loop.
34  *
35  * Hence the callbacks will be synchronized with the drawing thread which
36  * makes extra synchronization unnecessary.
37  */
38 class cAnimationTimer : public cRunnable
39 {
40 public:
41 	cAnimationTimer();
42 	~cAnimationTimer();
43 
44 	/**
45 	 * Increases the internal timer.
46 	 *
47 	 * For internal use only!
48 	 */
49 	void increaseTimer();
50 
51 	/**
52 	 * Returns the animation time since the animation timer has been started in 100ms steps.
53 	 */
54 	unsigned long long getAnimationTime() const;
55 
56 	/**
57 	 * Checks the time since the last call and emits the
58 	 * signals according to this interval.
59 	 */
60 	virtual void run() MAXR_OVERRIDE_FUNCTION;
61 
62 	/*
63 	 * The following signals get called during the run method.
64 	 *
65 	 * Lets take the 10ms signal as example.
66 	 * It gets triggered exactly once when the time since the last
67 	 * exceeds 10ms.
68 	 * To make it absolutely clear: only one call even if 95ms
69 	 * have been passed since the last call. But not one call if only 5ms
70 	 * have passed.
71 	 */
72 	cSignal<void ()> triggered10ms;
73 	cSignal<void ()> triggered50ms;
74 	cSignal<void ()> triggered100ms;
75 	cSignal<void ()> triggered400ms;
76 
77 	/*
78 	 * The following signals get called during the run method.
79 	 *
80 	 * These signals do catch up if the FPS drop below the time
81 	 * of the signal.
82 	 *
83 	 * Lets take the 10ms signal as example.
84 	 * It gets triggered for every period of 10ms between the last
85 	 * and the current calls.
86 	 * To make it absolutely clear: if 95ms passed since the last call
87 	 * the signal will be called 9 times in a row. But not one call if only 5ms
88 	 * have passed.
89 	 */
90 	cSignal<void ()> triggered10msCatchUp;
91 	cSignal<void ()> triggered50msCatchUp;
92 	cSignal<void ()> triggered100msCatchUp;
93 	cSignal<void ()> triggered400msCatchUp;
94 private:
95 	const Uint32 sdlTimerInterval;
96 	SDL_TimerID timerId;
97 
98 	std::atomic<unsigned long long> timerTime;
99 
100 	unsigned long long nextTrigger10msTime;
101 	unsigned long long nextTrigger50msTime;
102 	unsigned long long nextTrigger100msTime;
103 	unsigned long long nextTrigger400msTime;
104 };
105 
106 #endif // ui_graphical_game_animations_animationtimerH
107