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;