Fix #2051 by keeping a copy of the timer in the SktTimer class.

This commit is contained in:
hiker 2015-03-28 01:30:42 +11:00
parent 80de98f40a
commit 17039141e4
3 changed files with 30 additions and 1 deletions

View File

@ -1085,6 +1085,7 @@ void initRest()
stk_config->load(file_manager->getAsset("stk_config.xml"));
irr_driver = new IrrDriver();
StkTime::init(); // grabs the timer object from the irrlicht device
// Now create the actual non-null device in the irrlicht driver
irr_driver->initDevice();

View File

@ -22,13 +22,31 @@
#include <ctime>
irr::ITimer *StkTime::m_timer = NULL;
/** Init function for the timer. It grabs a copy of the timer of the
* current irrlicht device (which is the NULL device). This way the
* irrlicht time routine can be used even if no device exists. This
* situation can happen when the window resolution is changed - if the
* sfx manager (in a separate thread) would access the timer while the
* device does not exist, stk crashes.
*/
void StkTime::init()
{
assert(!m_timer);
m_timer = irr_driver->getDevice()->getTimer();
m_timer->grab();
} // init
// ----------------------------------------------------------------------------
/** Returns a time based on an arbitrary 'epoch' (e.g. could be start
* time of the application, 1.1.1970, ...).
* The value is a double precision floating point value in seconds.
*/
double StkTime::getRealTime(long startAt)
{
return irr_driver->getRealTime()/1000.0;
assert(m_timer);
return m_timer->getRealTime()/1000.0;
} // getTimeSinceEpoch
// ----------------------------------------------------------------------------

View File

@ -18,6 +18,9 @@
#ifndef HEADER_TIME_HPP
#define HEADER_TIME_HPP
#include "ITimer.h"
#include <stdexcept>
#ifdef WIN32
@ -36,9 +39,16 @@
class StkTime
{
private:
/** This objects keeps a copy of irrlicht's null-device timer. This is
* important otherwise we can't get the time when resolution is switched
* (and the sfx threads needs real time at that time). */
static irr::ITimer *m_timer;
public:
typedef time_t TimeType;
static void init();
static void getDate(int *day=NULL, int *month=NULL, int *year=NULL);
/** Converts the time in this object to a human readable string. */