Skip to content

Instantly share code, notes, and snippets.

@dahnielson
Created July 9, 2010 14:15
Show Gist options
  • Save dahnielson/469501 to your computer and use it in GitHub Desktop.
Save dahnielson/469501 to your computer and use it in GitHub Desktop.
Time accumulator
import pygame
class Time(object):
def __init__(self, uiTimeStep):
"Takes the time step in milliseconds as argument."
self.uiTimeStep = uiTimeStep
self.uiNewTime = 0
self.uiCurrentTime = 0
self.uiTimeAccumulator = 0
self.iDeltaTime = 0
def NewFrame(self):
self.uiNewTime = pygame.time.get_ticks()
self.iDeltaTime = self.uiNewTime - self.uiCurrentTime
self.uiCurrentTime = self.uiNewTime
self.uiTimeAccumulator += min(self.iDeltaTime, 250)
def StepTime(self):
if self.uiTimeAccumulator >= self.uiTimeStep:
self.uiTimeAccumulator -= self.uiTimeStep
return True
return False
#include <sys/time.h>
#include <algorithm>
class time
{
public:
time(int64_t step_usec) :
m_time_step(step_usec),
m_delta_time(0),
m_maximum_lag(250000),
m_time_accumulator(0)
{
gettimeofday(&m_now, NULL);
m_current_time = (int64_t)m_now.tv_sec * 1000000 + m_now.tv_usec;
}
void new_frame()
{
gettimeofday(&m_now, NULL);
m_delta_time = ((int64_t)m_now.tv_sec * 1000000 + m_now.tv_usec) - m_current_time;
m_current_time = (int64_t)m_now.tv_sec * 1000000 + m_now.tv_usec;
m_time_accumulator += std::min(m_delta_time, m_maximum_lag);
}
bool step_time()
{
if (m_time_accumulator >= m_time_step)
{
m_time_accumulator -= m_time_step;
return true;
}
return false;
}
private:
int64_t m_time_step;
int64_t m_current_time;
int64_t m_delta_time;
int64_t m_maximum_lag;
int64_t m_time_accumulator;
struct timeval m_now;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment