mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Moved most of the love.timer code out of the sdl implementation folder.
This commit is contained in:
@@ -729,6 +729,7 @@ source_group("modules\\thread\\sdl" FILES ${LOVE_SRC_MODULE_THREAD_SDL})
|
||||
#
|
||||
|
||||
set(LOVE_SRC_MODULE_TIMER_ROOT
|
||||
src/modules/timer/Timer.cpp
|
||||
src/modules/timer/Timer.h
|
||||
src/modules/timer/wrap_Timer.cpp
|
||||
src/modules/timer/wrap_Timer.h
|
||||
|
||||
@@ -826,6 +826,8 @@
|
||||
FA0B7EF21A959D2C000E1D17 /* iOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = FA0B7EF11A959D2C000E1D17 /* iOS.mm */; };
|
||||
FA317EBA18F28B6D00B0BCD7 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = FA317EB918F28B6D00B0BCD7 /* libz.dylib */; };
|
||||
FA48E43918F10D6C007CF0BD /* jpeg-turbo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA48E43818F10D6C007CF0BD /* jpeg-turbo.framework */; };
|
||||
FA4B66C91ABBCF1900558F15 /* Timer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA4B66C81ABBCF1900558F15 /* Timer.cpp */; };
|
||||
FA4B66CA1ABBCF1900558F15 /* Timer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA4B66C81ABBCF1900558F15 /* Timer.cpp */; };
|
||||
FA577AB016C7507900860150 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA577A7916C71A1700860150 /* Cocoa.framework */; };
|
||||
FA577AC216C7512D00860150 /* FreeType.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA577A6716C719D900860150 /* FreeType.framework */; };
|
||||
FA577AC516C7513400860150 /* libmodplug.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA577A8216C71A5300860150 /* libmodplug.framework */; };
|
||||
@@ -1462,6 +1464,7 @@
|
||||
FA0B7EF11A959D2C000E1D17 /* iOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = iOS.mm; sourceTree = "<group>"; };
|
||||
FA317EB918F28B6D00B0BCD7 /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = usr/lib/libz.dylib; sourceTree = SDKROOT; };
|
||||
FA48E43818F10D6C007CF0BD /* jpeg-turbo.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = "jpeg-turbo.framework"; path = "/Library/Frameworks/jpeg-turbo.framework"; sourceTree = "<absolute>"; };
|
||||
FA4B66C81ABBCF1900558F15 /* Timer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Timer.cpp; sourceTree = "<group>"; };
|
||||
FA577A6716C719D900860150 /* FreeType.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = FreeType.framework; path = /Library/Frameworks/FreeType.framework; sourceTree = "<absolute>"; };
|
||||
FA577A6D16C719EA00860150 /* Lua.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Lua.framework; path = /Library/Frameworks/Lua.framework; sourceTree = "<absolute>"; };
|
||||
FA577A6F16C719F000860150 /* mpg123.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = mpg123.framework; path = /Library/Frameworks/mpg123.framework; sourceTree = "<absolute>"; };
|
||||
@@ -2602,6 +2605,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
FA0B7CB81A95902C000E1D17 /* sdl */,
|
||||
FA4B66C81ABBCF1900558F15 /* Timer.cpp */,
|
||||
FA0B7CBB1A95902C000E1D17 /* Timer.h */,
|
||||
FA0B7CBC1A95902C000E1D17 /* wrap_Timer.cpp */,
|
||||
FA0B7CBD1A95902C000E1D17 /* wrap_Timer.h */,
|
||||
@@ -3202,6 +3206,7 @@
|
||||
FA0B7EBF1A95902C000E1D17 /* Thread.cpp in Sources */,
|
||||
FA0B7EB91A95902C000E1D17 /* Channel.cpp in Sources */,
|
||||
FA0B7AFB1A958EA3000E1D17 /* mime.c in Sources */,
|
||||
FA4B66CA1ABBCF1900558F15 /* Timer.cpp in Sources */,
|
||||
FA0B7A3F1A958EA3000E1D17 /* b2ChainShape.cpp in Sources */,
|
||||
FA0B7E921A95902C000E1D17 /* ModPlugDecoder.cpp in Sources */,
|
||||
FA0B7E521A95902C000E1D17 /* wrap_FrictionJoint.cpp in Sources */,
|
||||
@@ -3482,6 +3487,7 @@
|
||||
FA0B7EBE1A95902C000E1D17 /* Thread.cpp in Sources */,
|
||||
FA0B7EB81A95902C000E1D17 /* Channel.cpp in Sources */,
|
||||
FA0B7AB11A958EA3000E1D17 /* b2Rope.cpp in Sources */,
|
||||
FA4B66C91ABBCF1900558F15 /* Timer.cpp in Sources */,
|
||||
FA0B7A351A958EA3000E1D17 /* b2Distance.cpp in Sources */,
|
||||
FA0B7E911A95902C000E1D17 /* ModPlugDecoder.cpp in Sources */,
|
||||
FA0B7E511A95902C000E1D17 /* wrap_FrictionJoint.cpp in Sources */,
|
||||
|
||||
@@ -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