You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.0 KiB

14 years ago
#ifndef Magnum_Timeline_h
#define Magnum_Timeline_h
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
14 years ago
*/
#include <chrono>
#include "Types.h"
14 years ago
#include "magnumVisibility.h"
/** @file
* @brief Class Magnum::Timeline
*/
namespace Magnum {
/**
@brief %Timeline
Keeps track of time delta between frames and allows FPS limiting. Can be used
as source for animation speed computations.
@section Timeline-usage Basic usage
Construct the timeline on initialization so the instance is available for
whole lifetime of the application. Call start() before first draw event is
performed, after everything is properly initialized.
@note When timeline is started, it immediately starts measuring frame time.
Be prepared that time of first frame will be much longer than time of
following frames. It mainly depends on where you called start() in your
initialization routine, but can be also affected by driver- and
GPU-specific lazy texture binding, shader recompilations etc.
In your draw event implementation don't forget to call nextFrame() after
buffer swap. You can use previousFrameDuration() to compute animation speed.
13 years ago
Example usage:
@code
13 years ago
MyApplication::MyApplication(const Parameters& parameters): Platform::Application(parameters) {
// Initialization ...
timeline.setMinimalFrameTime(1/120.0f) // 120 FPS at max
.start();
}
void MyApplication::drawEvent() {
// Distance of object travelling at speed of 15 units per second
Float distance = 15.0f*timeline.previousFrameDuration();
// Move object, draw ...
swapBuffers();
redraw();
timeline.nextFrame();
}
@endcode
13 years ago
@todo FPS should be governed by Application (imagine more than one simultaenous
timeline and the harm it could do, also vsync etc. can't be handled in
platform-independent way here)
*/
14 years ago
class MAGNUM_EXPORT Timeline {
public:
/**
* @brief Constructor
*
* Creates stopped timeline.
14 years ago
* @see start()
*/
explicit Timeline(): _minimalFrameTime(0), _previousFrameDuration(0), running(false) {}
14 years ago
/** @brief Minimal frame time (in seconds) */
Float minimalFrameTime() const { return _minimalFrameTime; }
14 years ago
/**
* @brief Set minimal frame time
* @return Reference to self (for method chaining)
14 years ago
*
* Default value is 0.
14 years ago
* @see nextFrame()
*/
Timeline& setMinimalFrameTime(Float seconds) {
14 years ago
_minimalFrameTime = seconds;
return *this;
14 years ago
}
/**
* @brief Start timeline
*
* Sets previous frame time and duration to `0`.
14 years ago
* @see stop(), previousFrameDuration()
*/
void start();
/**
* @brief Stop timeline
*
* @see start(), nextFrame()
14 years ago
*/
void stop();
/**
* @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.
* @see setMinimalFrameTime(), stop()
14 years ago
*/
void nextFrame();
/**
* @brief Time at previous frame (in seconds)
*
* Returns time elapsed since start() was called. If the timeline is
* stopped, the function returns `0.0f`.
*/
Float previousFrameTime() const;
/**
* @brief Duration of previous frame (in seconds)
*
* If the timeline is stopped, the function returns `0.0f`.
*/
Float previousFrameDuration() const { return _previousFrameDuration; }
14 years ago
private:
std::chrono::high_resolution_clock::time_point _startTime;
std::chrono::high_resolution_clock::time_point _previousFrameTime;
Float _minimalFrameTime;
Float _previousFrameDuration;
14 years ago
bool running;
};
}
#endif