From 99e7f6c5d0891032ea5e5c2664d5ac245ace3218 Mon Sep 17 00:00:00 2001 From: stephena Date: Wed, 16 Jun 2010 15:40:57 +0000 Subject: [PATCH] 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 --- src/emucore/OSystem.cxx | 12 ++++++++++-- src/emucore/OSystem.hxx | 18 +++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/emucore/OSystem.cxx b/src/emucore/OSystem.cxx index 0dff979b4..2e34e4ded 100644 --- a/src/emucore/OSystem.cxx +++ b/src/emucore/OSystem.cxx @@ -22,8 +22,8 @@ #include #include +#include #ifdef HAVE_GETTIMEOFDAY - #include #include #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 } diff --git a/src/emucore/OSystem.hxx b/src/emucore/OSystem.hxx index f8d3e23ea..f242daea5 100644 --- a/src/emucore/OSystem.hxx +++ b/src/emucore/OSystem.hxx @@ -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;