Browse Source

Finally deprecated Timeline::setMinimalFrameTime().

Replaced with combination of setSwapInterval() and
setMinimalLoopPeriod() on Platform::Sdl2Application.
pull/107/head
Vladimír Vondruš 11 years ago
parent
commit
3a968b035f
  1. 2
      src/Magnum/Timeline.cpp
  2. 47
      src/Magnum/Timeline.h

2
src/Magnum/Timeline.cpp

@ -55,11 +55,13 @@ void Timeline::nextFrame() {
auto duration = UnsignedInt(duration_cast<microseconds>(now-_previousFrameTime).count()); auto duration = UnsignedInt(duration_cast<microseconds>(now-_previousFrameTime).count());
_previousFrameDuration = duration/1e6f; _previousFrameDuration = duration/1e6f;
#ifdef MAGNUM_BUILD_DEPRECATED
if(_previousFrameDuration < _minimalFrameTime) { if(_previousFrameDuration < _minimalFrameTime) {
Utility::sleep(std::size_t(_minimalFrameTime*1000) - duration/1000); Utility::sleep(std::size_t(_minimalFrameTime*1000) - duration/1000);
now = high_resolution_clock::now(); now = high_resolution_clock::now();
_previousFrameDuration = duration_cast<microseconds>(now-_previousFrameTime).count()/1e6f; _previousFrameDuration = duration_cast<microseconds>(now-_previousFrameTime).count()/1e6f;
} }
#endif
_previousFrameTime = now; _previousFrameTime = now;
} }

47
src/Magnum/Timeline.h

@ -34,13 +34,17 @@
#include "Magnum/Types.h" #include "Magnum/Types.h"
#include "Magnum/visibility.h" #include "Magnum/visibility.h"
#ifdef MAGNUM_BUILD_DEPRECATED
#include <Corrade/Utility/Macros.h>
#endif
namespace Magnum { namespace Magnum {
/** /**
@brief Timeline @brief Timeline
Keeps track of time delta between frames and allows FPS limiting. Can be used Keeps track of time delta between frames. Can be used as source for animation
as source for animation speed computations. speed computations.
## Basic usage ## Basic usage
@ -56,15 +60,21 @@ performed, after everything is properly initialized.
In your draw event implementation don't forget to call @ref nextFrame() after In your draw event implementation don't forget to call @ref nextFrame() after
buffer swap. You can use @ref previousFrameDuration() to compute animation buffer swap. You can use @ref previousFrameDuration() to compute animation
speed. speed. To limit application framerate you can use
@ref Platform::Sdl2Application::setSwapInterval() "Platform::*Application::setSwapInterval()" or
@ref Platform::Sdl2Application::setMinimalLoopPeriod() "Platform::*Application::setMinimalLoopPeriod()".
Note that on @ref CORRADE_TARGET_NACL "NaCl" and @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten"
the framerate is governed by browser and you can't do anything about it.
Example usage: Example usage:
@code @code
MyApplication::MyApplication(const Parameters& parameters): Platform::Application(parameters) { MyApplication::MyApplication(const Parameters& parameters): Platform::Application(parameters) {
// Initialization ... // Initialization ...
timeline.setMinimalFrameTime(1/120.0f) // 120 FPS at max // Enable VSync or set minimal loop period for the application, if
.start(); // needed/applicable ...
timeline.start();
} }
void MyApplication::drawEvent() { void MyApplication::drawEvent() {
@ -78,9 +88,6 @@ void MyApplication::drawEvent() {
timeline.nextFrame(); timeline.nextFrame();
} }
@endcode @endcode
@todo FPS should be governed by Application (imagine more than one simultaneous
timeline and the harm it could do, also vsync etc. can't be handled in
platform-independent way here)
*/ */
class MAGNUM_EXPORT Timeline { class MAGNUM_EXPORT Timeline {
public: public:
@ -90,26 +97,32 @@ class MAGNUM_EXPORT Timeline {
* Creates stopped timeline. * Creates stopped timeline.
* @see @ref start() * @see @ref start()
*/ */
explicit Timeline(): _minimalFrameTime(0), _previousFrameDuration(0), running(false) {} explicit Timeline():
#ifdef MAGNUM_BUILD_DEPRECATED
_minimalFrameTime(0),
#endif
_previousFrameDuration(0), running(false) {}
/** @brief Minimal frame time (in seconds) */ /** @brief Minimal frame time (in seconds) */
Float minimalFrameTime() const { return _minimalFrameTime; } Float minimalFrameTime() const { return _minimalFrameTime; }
#ifdef MAGNUM_BUILD_DEPRECATED
/** /**
* @brief Set minimal frame time * @brief Set minimal frame time
* @return Reference to self (for method chaining) * @return Reference to self (for method chaining)
* *
* Default value is `0.0f`. Cannot be used on some platforms where * Default value is `0.0f`. Cannot be used on some platforms where
* blocking the main loop is not allowed (such as * blocking the main loop is not allowed (such as
* @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten"). Prefer to use VSync * @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten").
* where possible. * @deprecated Use @ref Platform::Sdl2Application::setSwapInterval() "Platform::*Application::setSwapInterval()" or
* @see @ref nextFrame(), * @ref Platform::Sdl2Application::setMinimalLoopPeriod() "Platform::*Application::setMinimalLoopPeriod()"
* @ref Platform::Sdl2Application::setSwapInterval() "Platform::*Application::setSwapInterval()" * instead.
*/ */
Timeline& setMinimalFrameTime(Float seconds) { CORRADE_DEPRECATED("use Platfomr::*Application::setMinimalLoopPeriod() instead") Timeline& setMinimalFrameTime(Float seconds) {
_minimalFrameTime = seconds; _minimalFrameTime = seconds;
return *this; return *this;
} }
#endif
/** /**
* @brief Start timeline * @brief Start timeline
@ -129,10 +142,8 @@ class MAGNUM_EXPORT Timeline {
/** /**
* @brief Advance to next frame * @brief Advance to next frame
* *
* If current frame time is smaller than minimal frame time, pauses
* the execution for remaining time.
* @note This function does nothing if the timeline is stopped. * @note This function does nothing if the timeline is stopped.
* @see @ref setMinimalFrameTime(), @ref stop() * @see @ref stop()
*/ */
void nextFrame(); void nextFrame();
@ -154,7 +165,9 @@ class MAGNUM_EXPORT Timeline {
private: private:
std::chrono::high_resolution_clock::time_point _startTime; std::chrono::high_resolution_clock::time_point _startTime;
std::chrono::high_resolution_clock::time_point _previousFrameTime; std::chrono::high_resolution_clock::time_point _previousFrameTime;
#ifdef MAGNUM_BUILD_DEPRECATED
Float _minimalFrameTime; Float _minimalFrameTime;
#endif
Float _previousFrameDuration; Float _previousFrameDuration;
bool running; bool running;

Loading…
Cancel
Save