The getTicks() method is updated to work similar to gettimeofday, for

systems where gettimeofday doesn't exist.  Basically, the functionality
for OSystem::getTicks() has changed to always output an increasing
value each time it's called, even between program runs (needed for
timestamps for continuous snapshots, but also for similar things in
the future).  It does this by refering to some set time in the past,
which is currently the UNIX epoch.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@2056 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2010-06-16 15:40:57 +00:00
parent 9b6ed83d7e
commit 99e7f6c5d0
2 changed files with 21 additions and 9 deletions

View File

@ -22,8 +22,8 @@
#include <fstream>
#include <zlib.h>
#include <ctime>
#ifdef HAVE_GETTIMEOFDAY
#include <time.h>
#include <sys/time.h>
#endif
@ -95,6 +95,9 @@ OSystem::OSystem()
myFont(NULL),
myConsoleFont(NULL)
{
// Calculate startup time
myMillisAtStart = (uInt32)(time(NULL) * 1000);
// Get built-in features
#ifdef DISPLAY_OPENGL
myFeatures += "OpenGL ";
@ -918,12 +921,17 @@ void OSystem::stateChanged(EventHandler::State state)
uInt64 OSystem::getTicks() const
{
#ifdef HAVE_GETTIMEOFDAY
// Gettimeofday natively refers to the UNIX epoch (a set time in the past)
timeval now;
gettimeofday(&now, 0);
return uInt64(now.tv_sec) * 1000000 + now.tv_usec;
#else
return uInt64(SDL_GetTicks()) * 1000;
// We use SDL_GetTicks, but add in the time when the application was
// initialized. This is necessary, since SDL_GetTicks only measures how
// long SDL has been running, which can be the same between multiple runs
// of the application.
return uInt64(SDL_GetTicks() + myMillisAtStart) * 1000;
#endif
}

View File

@ -377,20 +377,21 @@ class OSystem
public:
//////////////////////////////////////////////////////////////////////
// The following methods are system-specific and must be implemented
// in derived classes.
// The following methods are system-specific and can be overrided in
// derived classes. Otherwise, the base methods will be used.
//////////////////////////////////////////////////////////////////////
/**
This method returns number of ticks in microseconds.
This method returns number of ticks in microseconds since some
pre-defined time in the past. *NOTE*: it is necessary that this
pre-defined time exists between runs of the application, and must
be (relatively) unique. For example, the time since the system
started running is not a good choice, since it can be duplicated.
The current implementation uses time since the UNIX epoch.
@return Current time in microseconds.
*/
virtual uInt64 getTicks() const;
//////////////////////////////////////////////////////////////////////
// The following methods are system-specific and can be overrided in
// derived classes. Otherwise, the base methods will be used.
//////////////////////////////////////////////////////////////////////
/**
This method runs the main loop. Since different platforms
may use different timing methods and/or algorithms, this method can
@ -503,6 +504,9 @@ class OSystem
// Time per frame for a video update, based on the current framerate
uInt32 myTimePerFrame;
// The time (in milliseconds) from the UNIX epoch when the application starts
uInt32 myMillisAtStart;
// Indicates whether to stop the main loop
bool myQuitLoop;