some optimizations

This commit is contained in:
Eduardo Bart
2012-06-02 11:43:27 -03:00
parent 4c143f4a33
commit 1c5b906b5b
24 changed files with 120 additions and 105 deletions

View File

@@ -21,6 +21,7 @@
*/
#include "adaptativeframecounter.h"
#include "clock.h"
AdaptativeFrameCounter::AdaptativeFrameCounter()
{
@@ -35,8 +36,8 @@ AdaptativeFrameCounter::AdaptativeFrameCounter()
m_maxFps = 0;
m_sleepMicros = 0;
m_mediumFrameDelay = 0;
m_lastFpsUpdate = stdext::micros();
m_lastPartialFpsUpdate = stdext::micros();
m_lastFpsUpdate = g_clock.micros();
m_lastPartialFpsUpdate = g_clock.micros();
}
bool AdaptativeFrameCounter::shouldProcessNextFrame()
@@ -44,7 +45,7 @@ bool AdaptativeFrameCounter::shouldProcessNextFrame()
if(m_maxFps == 0)
return true;
ticks_t now = stdext::micros();
ticks_t now = g_clock.micros();
if(now - m_lastFrame < m_bestFrameDelay)
return false;
return true;
@@ -52,7 +53,7 @@ bool AdaptativeFrameCounter::shouldProcessNextFrame()
void AdaptativeFrameCounter::processNextFrame()
{
ticks_t now = stdext::micros();
ticks_t now = g_clock.micros();
m_frames++;
m_partialFrames++;
m_frameDelaySum += now - m_lastFrame;
@@ -61,7 +62,7 @@ void AdaptativeFrameCounter::processNextFrame()
void AdaptativeFrameCounter::update()
{
ticks_t now = stdext::micros();
ticks_t now = g_clock.micros();
ticks_t delta = now - m_lastPartialFpsUpdate;
if(delta > 41000 && m_partialFrames > 0) {
m_partialFps = m_partialFrames / (delta / 1000000.0f);
@@ -101,7 +102,7 @@ int AdaptativeFrameCounter::getMaximumSleepMicros()
{
if(m_maxFps == 0)
return 0;
return m_lastFrame + m_bestFrameDelay - stdext::micros();
return m_lastFrame + m_bestFrameDelay - g_clock.micros();
}
float AdaptativeFrameCounter::getFrameDelayHit()

View File

@@ -22,30 +22,18 @@
#include "clock.h"
// for usleep
#include <unistd.h>
Clock g_clock;
Clock::Clock()
{
m_startupTime = std::chrono::high_resolution_clock::now();
m_currentTicks = 0;
}
ticks_t Clock::updateTicks()
{
m_currentTicks = asyncTicks();
m_currentTime = m_currentTicks/1000.0f;
return m_currentTicks;
m_currentMicros = 0;
m_currentMillis = 0;
m_currentSeconds = 0;
}
ticks_t Clock::asyncTicks()
void Clock::update()
{
auto timeNow = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(timeNow - m_startupTime).count();
}
void Clock::sleep(int ms)
{
usleep(ms * 1000);
m_currentMicros = stdext::micros();
m_currentMillis = m_currentMicros / 1000;
m_currentSeconds = m_currentMicros / 1000000.0f;
}

View File

@@ -30,23 +30,16 @@ class Clock
public:
Clock();
ticks_t updateTicks();
void sleep(int ms);
void update();
ticks_t asyncTicks();
ticks_t ticks() { return m_currentTicks; }
ticks_t ticksElapsed(ticks_t prevTicks) { return m_currentTicks - prevTicks; }
ticks_t ticksFor(int delay) { return m_currentTicks + delay; }
float asyncTime() { return asyncTicks()/1000.0f; }
float time() { return m_currentTime; }
float timeElapsed(float prevTime) { return m_currentTime - prevTime; }
float timeFor(float delay) { return m_currentTime + delay; }
ticks_t micros() { return m_currentMicros; }
ticks_t millis() { return m_currentMillis; }
float seconds() { return m_currentSeconds; }
private:
ticks_t m_currentTicks;
float m_currentTime;
std::chrono::system_clock::time_point m_startupTime;
ticks_t m_currentMicros;
ticks_t m_currentMillis;
float m_currentSeconds;
};
extern Clock g_clock;

View File

@@ -53,11 +53,11 @@ class ScheduledEvent : public Event
{
public:
ScheduledEvent(const std::function<void()>& callback, int delay) : Event(callback) {
m_ticks = g_clock.ticksFor(delay);
m_ticks = g_clock.millis() + delay;
}
int ticks() const { return m_ticks; }
int reamaningTicks() const { return m_ticks - g_clock.ticks(); }
int reamaningTicks() const { return m_ticks - g_clock.millis(); }
private:
ticks_t m_ticks;

View File

@@ -26,11 +26,11 @@
void Timer::restart()
{
m_startTicks = g_clock.ticks();
m_startTicks = g_clock.millis();
m_stopped = false;
}
ticks_t Timer::ticksElapsed()
{
return g_clock.ticks() - m_startTicks;
return g_clock.millis() - m_startTicks;
}