| 1 | /** |
| 2 | * Copyright (c) 2006-2023 LOVE Development Team |
| 3 | * |
| 4 | * This software is provided 'as-is', without any express or implied |
| 5 | * warranty. In no event will the authors be held liable for any damages |
| 6 | * arising from the use of this software. |
| 7 | * |
| 8 | * Permission is granted to anyone to use this software for any purpose, |
| 9 | * including commercial applications, and to alter it and redistribute it |
| 10 | * freely, subject to the following restrictions: |
| 11 | * |
| 12 | * 1. The origin of this software must not be misrepresented; you must not |
| 13 | * claim that you wrote the original software. If you use this software |
| 14 | * in a product, an acknowledgment in the product documentation would be |
| 15 | * appreciated but is not required. |
| 16 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 17 | * misrepresented as being the original software. |
| 18 | * 3. This notice may not be removed or altered from any source distribution. |
| 19 | **/ |
| 20 | |
| 21 | #ifndef LOVE_TIMER_TIMER_H |
| 22 | #define LOVE_TIMER_TIMER_H |
| 23 | |
| 24 | // LOVE |
| 25 | #include "common/Module.h" |
| 26 | |
| 27 | namespace love |
| 28 | { |
| 29 | namespace timer |
| 30 | { |
| 31 | |
| 32 | class Timer : public Module |
| 33 | { |
| 34 | public: |
| 35 | |
| 36 | Timer(); |
| 37 | virtual ~Timer() {} |
| 38 | |
| 39 | // Implements Module. |
| 40 | ModuleType getModuleType() const override { return M_TIMER; } |
| 41 | const char *getName() const override { return "love.timer" ; } |
| 42 | |
| 43 | /** |
| 44 | * Measures the time between this call and the previous call, |
| 45 | * and updates internal values accordingly. |
| 46 | **/ |
| 47 | double step(); |
| 48 | |
| 49 | /** |
| 50 | * Tries to sleep for the specified amount of time. The precision is |
| 51 | * usually 1ms. |
| 52 | * @param seconds The number of seconds to sleep for. |
| 53 | **/ |
| 54 | void sleep(double seconds) const; |
| 55 | |
| 56 | /** |
| 57 | * Gets the time between the last two frames, assuming step is called |
| 58 | * each frame. |
| 59 | **/ |
| 60 | double getDelta() const; |
| 61 | |
| 62 | /** |
| 63 | * Gets the average FPS over the last second. Beucase the value is only updated |
| 64 | * once per second, it does not look erratic when displayed on screen. |
| 65 | * @return The "current" FPS. |
| 66 | **/ |
| 67 | int getFPS() const; |
| 68 | |
| 69 | /** |
| 70 | * Gets the average delta time (seconds per frame) over the last second. |
| 71 | **/ |
| 72 | double getAverageDelta() const; |
| 73 | |
| 74 | /** |
| 75 | * Gets the amount of time in seconds passed since its first invocation |
| 76 | * (which happens as part of the Timer constructor, |
| 77 | * which is called when the module is first opened). |
| 78 | * Useful for profiling code or measuring intervals. |
| 79 | * The time is microsecond-precise, and increases monotonically. |
| 80 | * @return The time (in seconds) |
| 81 | **/ |
| 82 | static double getTime(); |
| 83 | |
| 84 | private: |
| 85 | |
| 86 | // Frame delta vars. |
| 87 | double currTime; |
| 88 | double prevTime; |
| 89 | double prevFpsUpdate; |
| 90 | |
| 91 | // Updated with a certain frequency. |
| 92 | int fps; |
| 93 | double averageDelta; |
| 94 | |
| 95 | // The frequency by which to update the FPS. |
| 96 | double fpsUpdateFrequency; |
| 97 | |
| 98 | // Frames since last FPS update. |
| 99 | int frames; |
| 100 | |
| 101 | // The current timestep. |
| 102 | double dt; |
| 103 | |
| 104 | }; // Timer |
| 105 | |
| 106 | } // timer |
| 107 | } // love |
| 108 | |
| 109 | #endif // LOVE_TIMER_TIMER_H |
| 110 | |