2011-09-13 17:50:18 +03:00
|
|
|
#ifndef _framerate__hpp__included__
|
|
|
|
#define _framerate__hpp__included__
|
|
|
|
|
|
|
|
#include <cstdint>
|
2014-05-23 19:43:03 +03:00
|
|
|
#include "library/threads.hpp"
|
2011-09-13 17:50:18 +03:00
|
|
|
|
2014-05-23 19:43:03 +03:00
|
|
|
#define FRAMERATE_HISTORY_FRAMES 10
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Framerate regulator.
|
|
|
|
*/
|
|
|
|
class framerate_regulator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
framerate_regulator();
|
2013-02-09 16:24:31 +02:00
|
|
|
/**
|
|
|
|
* Set the target speed multiplier.
|
|
|
|
*
|
|
|
|
* Parameter multiplier: The multiplier target. May be INFINITE.
|
|
|
|
*/
|
2014-05-23 19:43:03 +03:00
|
|
|
void set_speed_multiplier(double multiplier) throw();
|
2013-02-09 16:24:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the target speed multiplier.
|
|
|
|
*
|
|
|
|
* Returns: The multiplier. May be INFINITE.
|
|
|
|
*/
|
2014-05-23 19:43:03 +03:00
|
|
|
double get_speed_multiplier() throw();
|
2011-09-13 17:50:18 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the nominal frame rate. Framerate limiting tries to maintain the nominal framerate when there is no other
|
|
|
|
* explict framerate to maintain.
|
|
|
|
*/
|
2014-05-23 19:43:03 +03:00
|
|
|
void set_nominal_framerate(double fps) throw();
|
2011-09-13 17:50:18 +03:00
|
|
|
|
|
|
|
/**
|
2013-02-09 16:24:31 +02:00
|
|
|
* Returns the current realized framerate multiplier.
|
2011-09-15 22:56:33 +03:00
|
|
|
*
|
2013-02-09 16:24:31 +02:00
|
|
|
* returns: The framerate multiplier the system is currently archiving.
|
2011-09-13 17:50:18 +03:00
|
|
|
*/
|
2014-05-23 19:43:03 +03:00
|
|
|
double get_realized_multiplier() throw();
|
2011-09-13 17:50:18 +03:00
|
|
|
|
|
|
|
/**
|
2012-02-20 13:39:17 +02:00
|
|
|
* Freeze time.
|
|
|
|
*
|
|
|
|
* Parameter usec: Current time in microseconds.
|
|
|
|
*/
|
2014-05-23 19:43:03 +03:00
|
|
|
void freeze_time(uint64_t usec);
|
2012-02-20 13:39:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unfreeze time.
|
|
|
|
*
|
|
|
|
* Parameter usec: Current time in microseconds.
|
|
|
|
*/
|
2014-05-23 19:43:03 +03:00
|
|
|
void unfreeze_time(uint64_t usec);
|
2012-02-20 13:39:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Acknowledge frame start for timing purposes. If time is frozen, it is automatically unfrozen.
|
2011-09-15 22:56:33 +03:00
|
|
|
*
|
2011-10-02 19:05:45 +03:00
|
|
|
* parameter usec: Current time (relative to some unknown epoch) in microseconds.
|
2011-09-13 17:50:18 +03:00
|
|
|
*/
|
2014-05-23 19:43:03 +03:00
|
|
|
void ack_frame_tick(uint64_t usec) throw();
|
2011-09-13 17:50:18 +03:00
|
|
|
|
|
|
|
/**
|
2011-10-02 19:05:45 +03:00
|
|
|
* Computes the number of microseconds to wait for next frame.
|
2011-09-15 22:56:33 +03:00
|
|
|
*
|
2011-10-02 19:05:45 +03:00
|
|
|
* parameter usec: Current time (relative to some unknown epoch) in microseconds.
|
|
|
|
* returns: Number of more microseconds to wait.
|
2011-09-13 17:50:18 +03:00
|
|
|
*/
|
2014-05-23 19:43:03 +03:00
|
|
|
uint64_t to_wait_frame(uint64_t usec) throw();
|
2011-10-02 19:05:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return microsecond-resolution time since unix epoch.
|
|
|
|
*/
|
2014-05-23 19:43:03 +03:00
|
|
|
static uint64_t get_utime();
|
2011-10-02 19:05:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait specified number of microseconds.
|
|
|
|
*/
|
2014-05-23 19:43:03 +03:00
|
|
|
void wait_usec(uint64_t usec);
|
|
|
|
/**
|
|
|
|
* Turbo flag.
|
|
|
|
*/
|
|
|
|
bool turboed;
|
|
|
|
private:
|
|
|
|
uint64_t get_time(uint64_t curtime, bool update);
|
|
|
|
double get_realized_fps();
|
|
|
|
void add_frame(uint64_t linear_time);
|
|
|
|
std::pair<bool, double> read_fps();
|
|
|
|
uint64_t last_time_update;
|
|
|
|
uint64_t time_at_last_update;
|
|
|
|
bool time_frozen;
|
|
|
|
uint64_t frame_number;
|
|
|
|
uint64_t frame_start_times[FRAMERATE_HISTORY_FRAMES];
|
|
|
|
//Framerate.
|
|
|
|
double nominal_framerate;
|
|
|
|
double multiplier_framerate;
|
|
|
|
threads::lock framerate_lock;
|
|
|
|
|
|
|
|
};
|
2011-09-13 17:50:18 +03:00
|
|
|
|
|
|
|
#endif
|