mirror of
https://github.com/love2d/love.git
synced 2026-08-14 10:13:46 +02:00
Moved most of the love.timer code out of the sdl implementation folder.
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2015 LOVE Development Team
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
// LOVE
|
||||
#include "common/config.h"
|
||||
#include "common/int.h"
|
||||
#include "Timer.h"
|
||||
|
||||
#if defined(LOVE_WINDOWS)
|
||||
#include <windows.h>
|
||||
#elif defined(LOVE_MACOSX) || defined(LOVE_IOS)
|
||||
#include <mach/mach_time.h>
|
||||
#include <sys/time.h>
|
||||
#elif defined(LOVE_LINUX)
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#if defined(LOVE_LINUX)
|
||||
static inline double getTimeOfDay()
|
||||
{
|
||||
timeval t;
|
||||
gettimeofday(&t, NULL);
|
||||
return (double) t.tv_sec + (double) t.tv_usec / 1000000.0;
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace timer
|
||||
{
|
||||
|
||||
Timer::Timer()
|
||||
: currTime(0)
|
||||
, prevFpsUpdate(0)
|
||||
, fps(0)
|
||||
, averageDelta(0)
|
||||
, fpsUpdateFrequency(1)
|
||||
, frames(0)
|
||||
, dt(0)
|
||||
, timerPeriod(getTimerPeriod())
|
||||
{
|
||||
prevFpsUpdate = currTime = getTime();
|
||||
}
|
||||
|
||||
void Timer::step()
|
||||
{
|
||||
// Frames rendered
|
||||
frames++;
|
||||
|
||||
// "Current" time is previous time by now.
|
||||
prevTime = currTime;
|
||||
|
||||
// Get time from system.
|
||||
currTime = getTime();
|
||||
|
||||
// Convert to number of seconds.
|
||||
dt = currTime - prevTime;
|
||||
|
||||
double timeSinceLast = currTime - prevFpsUpdate;
|
||||
// Update FPS?
|
||||
if (timeSinceLast > fpsUpdateFrequency)
|
||||
{
|
||||
fps = int((frames/timeSinceLast) + 0.5);
|
||||
averageDelta = timeSinceLast/frames;
|
||||
prevFpsUpdate = currTime;
|
||||
frames = 0;
|
||||
}
|
||||
}
|
||||
|
||||
double Timer::getDelta() const
|
||||
{
|
||||
return dt;
|
||||
}
|
||||
|
||||
int Timer::getFPS() const
|
||||
{
|
||||
return fps;
|
||||
}
|
||||
|
||||
double Timer::getAverageDelta() const
|
||||
{
|
||||
return averageDelta;
|
||||
}
|
||||
|
||||
double Timer::getTimerPeriod()
|
||||
{
|
||||
#if defined(LOVE_MACOSX) || defined(LOVE_IOS)
|
||||
mach_timebase_info_data_t info;
|
||||
mach_timebase_info(&info);
|
||||
return (double) info.numer / (double) info.denom / 1000000000.0;
|
||||
#elif defined(LOVE_WINDOWS)
|
||||
LARGE_INTEGER temp;
|
||||
if (QueryPerformanceFrequency(&temp) != 0 && temp.QuadPart != 0)
|
||||
return 1.0 / (double) temp.QuadPart;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
double Timer::getTime() const
|
||||
{
|
||||
#if defined(LOVE_LINUX)
|
||||
double mt;
|
||||
// Check for POSIX timers and monotonic clocks. If not supported, use the gettimeofday fallback.
|
||||
#if _POSIX_TIMERS > 0 && defined(_POSIX_MONOTONIC_CLOCK) \
|
||||
&& (defined(CLOCK_MONOTONIC_RAW) || defined(CLOCK_MONOTONIC))
|
||||
timespec t;
|
||||
#ifdef CLOCK_MONOTONIC_RAW
|
||||
clockid_t clk_id = CLOCK_MONOTONIC_RAW;
|
||||
#else
|
||||
clockid_t clk_id = CLOCK_MONOTONIC;
|
||||
#endif
|
||||
if (clock_gettime(clk_id, &t) == 0)
|
||||
mt = (double) t.tv_sec + (double) t.tv_nsec / 1000000000.0;
|
||||
else
|
||||
#endif
|
||||
mt = getTimeOfDay();
|
||||
return mt;
|
||||
#elif defined(LOVE_MACOSX) || defined(LOVE_IOS)
|
||||
return (double) mach_absolute_time() * timerPeriod;
|
||||
#elif defined(LOVE_WINDOWS)
|
||||
LARGE_INTEGER microTime;
|
||||
QueryPerformanceCounter(µTime);
|
||||
return (double) microTime.QuadPart * timerPeriod;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // timer
|
||||
} // love
|
||||
@@ -33,6 +33,7 @@ class Timer : public Module
|
||||
{
|
||||
public:
|
||||
|
||||
Timer();
|
||||
virtual ~Timer() {}
|
||||
|
||||
// Implements Module.
|
||||
@@ -42,7 +43,7 @@ public:
|
||||
* Measures the time between this call and the previous call,
|
||||
* and updates internal values accordinly.
|
||||
**/
|
||||
virtual void step() = 0;
|
||||
virtual void step();
|
||||
|
||||
/**
|
||||
* Tries to sleep for the specified amount of time. The precision is
|
||||
@@ -55,19 +56,19 @@ public:
|
||||
* Gets the time between the last two frames, assuming step is called
|
||||
* each frame.
|
||||
**/
|
||||
virtual double getDelta() const = 0;
|
||||
virtual double getDelta() const;
|
||||
|
||||
/**
|
||||
* Gets the average FPS over the last second. Beucase the value is only updated
|
||||
* once per second, it does not look erratic when displayed on screen.
|
||||
* @return The "current" FPS.
|
||||
**/
|
||||
virtual int getFPS() const = 0;
|
||||
virtual int getFPS() const;
|
||||
|
||||
/**
|
||||
* Gets the average delta time (seconds per frame) over the last second.
|
||||
**/
|
||||
virtual double getAverageDelta() const = 0;
|
||||
virtual double getAverageDelta() const;
|
||||
|
||||
/**
|
||||
* Gets the amount of time passed since an unspecified time. Useful for
|
||||
@@ -75,7 +76,33 @@ public:
|
||||
* and increases monotonically.
|
||||
* @return The time (in seconds)
|
||||
**/
|
||||
virtual double getTime() const = 0;
|
||||
virtual double getTime() const;
|
||||
|
||||
private:
|
||||
|
||||
// Frame delta vars.
|
||||
double currTime;
|
||||
double prevTime;
|
||||
double prevFpsUpdate;
|
||||
|
||||
// Updated with a certain frequency.
|
||||
int fps;
|
||||
double averageDelta;
|
||||
|
||||
// The frequency by which to update the FPS.
|
||||
double fpsUpdateFrequency;
|
||||
|
||||
// Frames since last FPS update.
|
||||
int frames;
|
||||
|
||||
// The current timestep.
|
||||
double dt;
|
||||
|
||||
// The timer period (reciprocal of the frequency.)
|
||||
const double timerPeriod;
|
||||
|
||||
// Returns the timer period on some platforms.
|
||||
static double getTimerPeriod();
|
||||
|
||||
}; // Timer
|
||||
|
||||
|
||||
@@ -19,38 +19,12 @@
|
||||
**/
|
||||
|
||||
// LOVE
|
||||
#include "common/config.h"
|
||||
#include "Timer.h"
|
||||
|
||||
#include "common/delay.h"
|
||||
#include "common/int.h"
|
||||
|
||||
// SDL
|
||||
#include <SDL.h>
|
||||
|
||||
#if defined(LOVE_WINDOWS)
|
||||
#include <windows.h>
|
||||
#elif defined(LOVE_MACOSX) || defined(LOVE_IOS)
|
||||
#include <mach/mach_time.h>
|
||||
#include <sys/time.h>
|
||||
#elif defined(LOVE_LINUX)
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
#if defined(LOVE_LINUX)
|
||||
inline double getTimeOfDay()
|
||||
{
|
||||
timeval t;
|
||||
gettimeofday(&t, NULL);
|
||||
return (double) t.tv_sec + (double) t.tv_usec / 1000000.0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace timer
|
||||
@@ -59,20 +33,10 @@ namespace sdl
|
||||
{
|
||||
|
||||
Timer::Timer()
|
||||
: currTime(0)
|
||||
, prevFpsUpdate(0)
|
||||
, fps(0)
|
||||
, averageDelta(0)
|
||||
, fpsUpdateFrequency(1)
|
||||
, frames(0)
|
||||
, dt(0)
|
||||
, timerPeriod(getTimerPeriod())
|
||||
{
|
||||
// Init the SDL timer system (needed for SDL_Delay.)
|
||||
if (SDL_InitSubSystem(SDL_INIT_TIMER) < 0)
|
||||
throw love::Exception("%s", SDL_GetError());
|
||||
|
||||
prevFpsUpdate = currTime = getTime();
|
||||
}
|
||||
|
||||
Timer::~Timer()
|
||||
@@ -86,94 +50,12 @@ const char *Timer::getName() const
|
||||
return "love.timer.sdl";
|
||||
}
|
||||
|
||||
void Timer::step()
|
||||
{
|
||||
// Frames rendered
|
||||
frames++;
|
||||
|
||||
// "Current" time is previous time by now.
|
||||
prevTime = currTime;
|
||||
|
||||
// Get time from system.
|
||||
currTime = getTime();
|
||||
|
||||
// Convert to number of seconds.
|
||||
dt = currTime - prevTime;
|
||||
|
||||
double timeSinceLast = currTime - prevFpsUpdate;
|
||||
// Update FPS?
|
||||
if (timeSinceLast > fpsUpdateFrequency)
|
||||
{
|
||||
fps = int((frames/timeSinceLast) + 0.5);
|
||||
averageDelta = timeSinceLast/frames;
|
||||
prevFpsUpdate = currTime;
|
||||
frames = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Timer::sleep(double seconds) const
|
||||
{
|
||||
if (seconds > 0)
|
||||
delay((unsigned int)(seconds*1000));
|
||||
}
|
||||
|
||||
double Timer::getDelta() const
|
||||
{
|
||||
return dt;
|
||||
}
|
||||
|
||||
int Timer::getFPS() const
|
||||
{
|
||||
return fps;
|
||||
}
|
||||
|
||||
double Timer::getAverageDelta() const
|
||||
{
|
||||
return averageDelta;
|
||||
}
|
||||
|
||||
double Timer::getTimerPeriod()
|
||||
{
|
||||
#if defined(LOVE_MACOSX) || defined(LOVE_IOS)
|
||||
mach_timebase_info_data_t info;
|
||||
mach_timebase_info(&info);
|
||||
return (double) info.numer / (double) info.denom / 1000000000.0;
|
||||
#elif defined(LOVE_WINDOWS)
|
||||
LARGE_INTEGER temp;
|
||||
if (QueryPerformanceFrequency(&temp) != 0 && temp.QuadPart != 0)
|
||||
return 1.0 / (double) temp.QuadPart;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
double Timer::getTime() const
|
||||
{
|
||||
#if defined(LOVE_LINUX)
|
||||
double mt;
|
||||
// Check for POSIX timers and monotonic clocks. If not supported, use the gettimeofday fallback.
|
||||
#if _POSIX_TIMERS > 0 && defined(_POSIX_MONOTONIC_CLOCK) \
|
||||
&& (defined(CLOCK_MONOTONIC_RAW) || defined(CLOCK_MONOTONIC))
|
||||
timespec t;
|
||||
#ifdef CLOCK_MONOTONIC_RAW
|
||||
clockid_t clk_id = CLOCK_MONOTONIC_RAW;
|
||||
#else
|
||||
clockid_t clk_id = CLOCK_MONOTONIC;
|
||||
#endif
|
||||
if (clock_gettime(clk_id, &t) == 0)
|
||||
mt = (double) t.tv_sec + (double) t.tv_nsec / 1000000000.0;
|
||||
else
|
||||
#endif
|
||||
mt = getTimeOfDay();
|
||||
return mt;
|
||||
#elif defined(LOVE_MACOSX) || defined(LOVE_IOS)
|
||||
return (double) mach_absolute_time() * timerPeriod;
|
||||
#elif defined(LOVE_WINDOWS)
|
||||
LARGE_INTEGER microTime;
|
||||
QueryPerformanceCounter(µTime);
|
||||
return (double) microTime.QuadPart * timerPeriod;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // sdl
|
||||
} // timer
|
||||
} // love
|
||||
|
||||
@@ -31,57 +31,16 @@ namespace timer
|
||||
namespace sdl
|
||||
{
|
||||
|
||||
/**
|
||||
* An SDL timer module. Can keep track of time between certain function
|
||||
* calls, and provides access to a FPS metric which updates once each second.
|
||||
**/
|
||||
class Timer : public love::timer::Timer
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor. Initializes the SDL/timer subsystem.
|
||||
**/
|
||||
Timer();
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
**/
|
||||
virtual ~Timer();
|
||||
|
||||
const char *getName() const;
|
||||
void step();
|
||||
void sleep(double seconds) const;
|
||||
double getDelta() const;
|
||||
int getFPS() const;
|
||||
double getAverageDelta() const;
|
||||
double getTime() const;
|
||||
const char *getName() const override;
|
||||
|
||||
private:
|
||||
|
||||
// Frame delta vars.
|
||||
double currTime;
|
||||
double prevTime;
|
||||
double prevFpsUpdate;
|
||||
|
||||
// Updated with a certain frequency.
|
||||
int fps;
|
||||
double averageDelta;
|
||||
|
||||
// The frequency by which to update the FPS.
|
||||
double fpsUpdateFrequency;
|
||||
|
||||
// Frames since last FPS update.
|
||||
int frames;
|
||||
|
||||
// The current timestep.
|
||||
double dt;
|
||||
|
||||
// The timer period (reciprocal of the frequency.)
|
||||
const double timerPeriod;
|
||||
|
||||
// Returns the timer period on some platforms.
|
||||
static double getTimerPeriod();
|
||||
void sleep(double seconds) const override;
|
||||
|
||||
}; // Timer
|
||||
|
||||
|
||||
Reference in New Issue
Block a user