瀏覽代碼

Get the timer to talk milliseconds instead of seconds.

legacy
Sam Hocevar sam 14 年之前
父節點
當前提交
50e4f67254
共有 23 個文件被更改,包括 84 次插入83 次删除
  1. +11
    -11
      src/debugfps.cpp
  2. +1
    -1
      src/debugfps.h
  3. +4
    -4
      src/debugrecord.cpp
  4. +2
    -2
      src/debugrecord.h
  5. +4
    -4
      src/debugsprite.cpp
  6. +2
    -2
      src/debugsprite.h
  7. +2
    -2
      src/entity.cpp
  8. +2
    -2
      src/entity.h
  9. +2
    -2
      src/font.cpp
  10. +1
    -1
      src/font.h
  11. +4
    -4
      src/game.cpp
  12. +2
    -2
      src/game.h
  13. +1
    -1
      src/gtk/editor.cpp
  14. +3
    -3
      src/profiler.cpp
  15. +2
    -2
      src/sdlinput.cpp
  16. +1
    -1
      src/sdlinput.h
  17. +1
    -1
      src/test-map.cpp
  18. +11
    -11
      src/ticker.cpp
  19. +1
    -1
      src/ticker.h
  20. +2
    -2
      src/tileset.cpp
  21. +1
    -1
      src/tileset.h
  22. +21
    -20
      src/timer.cpp
  23. +3
    -3
      src/timer.h

+ 11
- 11
src/debugfps.cpp 查看文件

@@ -43,36 +43,36 @@ Entity::Group DebugFps::GetGroup()
return GROUP_AFTER; return GROUP_AFTER;
} }


void DebugFps::TickRender(float delta_time) void DebugFps::TickRender(float deltams)
{ {
Entity::TickRender(delta_time); Entity::TickRender(deltams);


data->frame++; data->frame++;


char buf[1024]; char buf[1024];


sprintf(buf, "%2.2f fps (%i)", sprintf(buf, "%2.2f fps (%i)",
1.0f / Profiler::GetMean(Profiler::STAT_TICK_FRAME), data->frame); 1e3f / Profiler::GetMean(Profiler::STAT_TICK_FRAME), data->frame);
data->font->PrintBold(10, 10, buf); data->font->PrintBold(10, 10, buf);


sprintf(buf, "Game % 7.2f % 7.2f", sprintf(buf, "Game % 7.2f % 7.2f",
1e3f * Profiler::GetMean(Profiler::STAT_TICK_GAME), Profiler::GetMean(Profiler::STAT_TICK_GAME),
1e3f * Profiler::GetMax(Profiler::STAT_TICK_GAME)); Profiler::GetMax(Profiler::STAT_TICK_GAME));
data->font->PrintBold(10, 34, buf); data->font->PrintBold(10, 34, buf);


sprintf(buf, "Render % 7.2f % 7.2f", sprintf(buf, "Render % 7.2f % 7.2f",
1e3f * Profiler::GetMean(Profiler::STAT_TICK_RENDER), Profiler::GetMean(Profiler::STAT_TICK_RENDER),
1e3f * Profiler::GetMax(Profiler::STAT_TICK_RENDER)); Profiler::GetMax(Profiler::STAT_TICK_RENDER));
data->font->PrintBold(10, 50, buf); data->font->PrintBold(10, 50, buf);


sprintf(buf, "Blit % 7.2f % 7.2f", sprintf(buf, "Blit % 7.2f % 7.2f",
1e3f * Profiler::GetMean(Profiler::STAT_TICK_BLIT), Profiler::GetMean(Profiler::STAT_TICK_BLIT),
1e3f * Profiler::GetMax(Profiler::STAT_TICK_BLIT)); Profiler::GetMax(Profiler::STAT_TICK_BLIT));
data->font->PrintBold(10, 66, buf); data->font->PrintBold(10, 66, buf);


sprintf(buf, "Frame % 7.2f % 7.2f", sprintf(buf, "Frame % 7.2f % 7.2f",
1e3f * Profiler::GetMean(Profiler::STAT_TICK_FRAME), Profiler::GetMean(Profiler::STAT_TICK_FRAME),
1e3f * Profiler::GetMax(Profiler::STAT_TICK_FRAME)); Profiler::GetMax(Profiler::STAT_TICK_FRAME));
data->font->PrintBold(10, 82, buf); data->font->PrintBold(10, 82, buf);
} }




+ 1
- 1
src/debugfps.h 查看文件

@@ -23,7 +23,7 @@ public:


protected: protected:
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickRender(float delta_time); virtual void TickRender(float deltams);


private: private:
DebugFpsData *data; DebugFpsData *data;


+ 4
- 4
src/debugrecord.cpp 查看文件

@@ -47,14 +47,14 @@ Entity::Group DebugRecord::GetGroup()
return GROUP_RENDER_CAPTURE; return GROUP_RENDER_CAPTURE;
} }


void DebugRecord::TickGame(float delta_time) void DebugRecord::TickGame(float deltams)
{ {
Entity::TickGame(delta_time); Entity::TickGame(deltams);
} }


void DebugRecord::TickRender(float delta_time) void DebugRecord::TickRender(float deltams)
{ {
Entity::TickRender(delta_time); Entity::TickRender(deltams);


int width = Video::GetWidth(); int width = Video::GetWidth();
int height = Video::GetHeight(); int height = Video::GetHeight();


+ 2
- 2
src/debugrecord.h 查看文件

@@ -23,8 +23,8 @@ public:


protected: protected:
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickGame(float delta_time); virtual void TickGame(float deltams);
virtual void TickRender(float delta_time); virtual void TickRender(float deltams);


private: private:
DebugRecordData *data; DebugRecordData *data;


+ 4
- 4
src/debugsprite.cpp 查看文件

@@ -43,14 +43,14 @@ Entity::Group DebugSprite::GetGroup()
return GROUP_DEFAULT; return GROUP_DEFAULT;
} }


void DebugSprite::TickGame(float delta_time) void DebugSprite::TickGame(float deltams)
{ {
Entity::TickGame(delta_time); Entity::TickGame(deltams);
} }


void DebugSprite::TickRender(float delta_time) void DebugSprite::TickRender(float deltams)
{ {
Entity::TickRender(delta_time); Entity::TickRender(deltams);


data->game->GetScene()->AddTile((data->tiler << 16) | 15, 320, 240, 32, 1); data->game->GetScene()->AddTile((data->tiler << 16) | 15, 320, 240, 32, 1);
data->game->GetScene()->AddTile((data->tiler << 16) | 31, 320, 240, 0, 1); data->game->GetScene()->AddTile((data->tiler << 16) | 31, 320, 240, 0, 1);


+ 2
- 2
src/debugsprite.h 查看文件

@@ -24,8 +24,8 @@ public:


protected: protected:
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickGame(float delta_time); virtual void TickGame(float deltams);
virtual void TickRender(float delta_time); virtual void TickRender(float deltams);


private: private:
DebugSpriteData *data; DebugSpriteData *data;


+ 2
- 2
src/entity.cpp 查看文件

@@ -41,7 +41,7 @@ Entity::Group Entity::GetGroup()
return GROUP_DEFAULT; return GROUP_DEFAULT;
} }


void Entity::TickGame(float delta_time) void Entity::TickGame(float deltams)
{ {
#if !FINAL_RELEASE #if !FINAL_RELEASE
if (state != STATE_PRETICK_GAME) if (state != STATE_PRETICK_GAME)
@@ -50,7 +50,7 @@ void Entity::TickGame(float delta_time)
#endif #endif
} }


void Entity::TickRender(float delta_time) void Entity::TickRender(float deltams)
{ {
#if !FINAL_RELEASE #if !FINAL_RELEASE
if (state != STATE_PRETICK_RENDER) if (state != STATE_PRETICK_RENDER)


+ 2
- 2
src/entity.h 查看文件

@@ -42,8 +42,8 @@ protected:


virtual Group GetGroup(); virtual Group GetGroup();


virtual void TickGame(float delta_time); virtual void TickGame(float deltams);
virtual void TickRender(float delta_time); virtual void TickRender(float deltams);


Entity *next; Entity *next;
int ref, destroy; int ref, destroy;


+ 2
- 2
src/font.cpp 查看文件

@@ -81,9 +81,9 @@ Entity::Group Font::GetGroup()
return GROUP_BEFORE; return GROUP_BEFORE;
} }


void Font::TickRender(float delta_time) void Font::TickRender(float deltams)
{ {
Entity::TickRender(delta_time); Entity::TickRender(deltams);
} }


char const *Font::GetName() char const *Font::GetName()


+ 1
- 1
src/font.h 查看文件

@@ -24,7 +24,7 @@ public:
protected: protected:
/* Inherited from Entity */ /* Inherited from Entity */
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickRender(float delta_time); virtual void TickRender(float deltams);


public: public:
/* New methods */ /* New methods */


+ 4
- 4
src/game.cpp 查看文件

@@ -53,14 +53,14 @@ Entity::Group Game::GetGroup()
return Entity::GetGroup(); return Entity::GetGroup();
} }


void Game::TickGame(float delta_time) void Game::TickGame(float deltams)
{ {
Entity::TickGame(delta_time); Entity::TickGame(deltams);
} }


void Game::TickRender(float delta_time) void Game::TickRender(float deltams)
{ {
Entity::TickRender(delta_time); Entity::TickRender(deltams);


GetScene(); GetScene();




+ 2
- 2
src/game.h 查看文件

@@ -25,8 +25,8 @@ public:
protected: protected:
/* Inherited from Entity */ /* Inherited from Entity */
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickGame(float delta_time); virtual void TickGame(float deltams);
virtual void TickRender(float delta_time); virtual void TickRender(float deltams);


public: public:
/* New methods */ /* New methods */


+ 1
- 1
src/gtk/editor.cpp 查看文件

@@ -84,7 +84,7 @@ static gint draw(GtkWidget *widget, GdkEventExpose *event)
gtk_gl_area_swapbuffers(GTK_GL_AREA(widget)); gtk_gl_area_swapbuffers(GTK_GL_AREA(widget));
while (g_main_context_iteration(NULL, FALSE)) while (g_main_context_iteration(NULL, FALSE))
; ;
Ticker::ClampFps(1.0f / FPS); Ticker::ClampFps(1000.0f / FPS);
} }


return TRUE; return TRUE;


+ 3
- 3
src/profiler.cpp 查看文件

@@ -47,14 +47,14 @@ data[Profiler::STAT_COUNT];


void Profiler::Start(int id) void Profiler::Start(int id)
{ {
data[id].timer.GetSeconds(); data[id].timer.GetMs();
} }


void Profiler::Stop(int id) void Profiler::Stop(int id)
{ {
float delta_time = data[id].timer.GetSeconds(); float deltams = data[id].timer.GetMs();


data[id].history[data->frame % ProfilerData::HISTORY] = delta_time; data[id].history[data->frame % ProfilerData::HISTORY] = deltams;
data[id].frame++; data[id].frame++;
data[id].mean = 0.0f; data[id].mean = 0.0f;
data[id].max = 0.0f; data[id].max = 0.0f;


+ 2
- 2
src/sdlinput.cpp 查看文件

@@ -40,9 +40,9 @@ Entity::Group SdlInput::GetGroup()
return GROUP_BEFORE; return GROUP_BEFORE;
} }


void SdlInput::TickGame(float delta_time) void SdlInput::TickGame(float deltams)
{ {
Entity::TickGame(delta_time); Entity::TickGame(deltams);


if (data->game->Finished()) if (data->game->Finished())
destroy = 1; destroy = 1;


+ 1
- 1
src/sdlinput.h 查看文件

@@ -24,7 +24,7 @@ public:


protected: protected:
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickGame(float delta_time); virtual void TickGame(float deltams);


private: private:
SdlInputData *data; SdlInputData *data;


+ 1
- 1
src/test-map.cpp 查看文件

@@ -66,7 +66,7 @@ int main(int argc, char **argv)
Video::Clear(); Video::Clear();
Ticker::TickRender(); Ticker::TickRender();
SDL_GL_SwapBuffers(); SDL_GL_SwapBuffers();
Ticker::ClampFps(1.0f / FPS); Ticker::ClampFps(1000.0f / FPS);
} }


SDL_Quit(); SDL_Quit();


+ 11
- 11
src/ticker.cpp 查看文件

@@ -50,7 +50,7 @@ private:


/* Fixed framerate management */ /* Fixed framerate management */
Timer timer; Timer timer;
float delta_time, bias; float deltams, bias;
} }
tickerdata; tickerdata;


@@ -76,8 +76,8 @@ void Ticker::TickGame()


Profiler::Start(Profiler::STAT_TICK_GAME); Profiler::Start(Profiler::STAT_TICK_GAME);


data->delta_time = data->timer.GetSeconds(); data->deltams = data->timer.GetMs();
data->bias += data->delta_time; data->bias += data->deltams;


/* Garbage collect objects that can be destroyed. We can do this /* Garbage collect objects that can be destroyed. We can do this
* before inserting awaiting objects, because there is no way these * before inserting awaiting objects, because there is no way these
@@ -117,7 +117,7 @@ void Ticker::TickGame()
fprintf(stderr, "ERROR: entity not idle for game tick\n"); fprintf(stderr, "ERROR: entity not idle for game tick\n");
a->state = Entity::STATE_PRETICK_GAME; a->state = Entity::STATE_PRETICK_GAME;
#endif #endif
a->TickGame(data->delta_time); a->TickGame(data->deltams);
#if !FINAL_RELEASE #if !FINAL_RELEASE
if (a->state != Entity::STATE_POSTTICK_GAME) if (a->state != Entity::STATE_POSTTICK_GAME)
fprintf(stderr, "ERROR: entity missed super game tick\n"); fprintf(stderr, "ERROR: entity missed super game tick\n");
@@ -142,7 +142,7 @@ void Ticker::TickRender()
fprintf(stderr, "ERROR: entity not idle for render tick\n"); fprintf(stderr, "ERROR: entity not idle for render tick\n");
a->state = Entity::STATE_PRETICK_RENDER; a->state = Entity::STATE_PRETICK_RENDER;
#endif #endif
a->TickRender(data->delta_time); a->TickRender(data->deltams);
#if !FINAL_RELEASE #if !FINAL_RELEASE
if (a->state != Entity::STATE_POSTTICK_RENDER) if (a->state != Entity::STATE_POSTTICK_RENDER)
fprintf(stderr, "ERROR: entity missed super render tick\n"); fprintf(stderr, "ERROR: entity missed super render tick\n");
@@ -154,14 +154,14 @@ void Ticker::TickRender()
Profiler::Start(Profiler::STAT_TICK_BLIT); Profiler::Start(Profiler::STAT_TICK_BLIT);
} }


void Ticker::ClampFps(float delta_time) void Ticker::ClampFps(float deltams)
{ {
Profiler::Stop(Profiler::STAT_TICK_BLIT); Profiler::Stop(Profiler::STAT_TICK_BLIT);


if (delta_time > data->bias + 0.2f) if (deltams > data->bias + 200.0f)
delta_time = data->bias + 0.2f; // Don't go below 5 fps deltams = data->bias + 200.0f; // Don't go below 5 fps
if (delta_time > data->bias) if (deltams > data->bias)
data->timer.WaitSeconds(delta_time - data->bias); data->timer.WaitMs(deltams - data->bias);
data->bias -= delta_time; data->bias -= deltams;
} }



+ 1
- 1
src/ticker.h 查看文件

@@ -23,7 +23,7 @@ public:


static void TickGame(); static void TickGame();
static void TickRender(); static void TickRender();
static void ClampFps(float delta_time); static void ClampFps(float deltams);
}; };


#endif // __DH_TICKER_H__ #endif // __DH_TICKER_H__


+ 2
- 2
src/tileset.cpp 查看文件

@@ -79,9 +79,9 @@ Entity::Group TileSet::GetGroup()
return GROUP_BEFORE; return GROUP_BEFORE;
} }


void TileSet::TickRender(float delta_time) void TileSet::TickRender(float deltams)
{ {
Entity::TickRender(delta_time); Entity::TickRender(deltams);


if (data->img) if (data->img)
{ {


+ 1
- 1
src/tileset.h 查看文件

@@ -29,7 +29,7 @@ public:
protected: protected:
/* Inherited from Entity */ /* Inherited from Entity */
virtual Group GetGroup(); virtual Group GetGroup();
virtual void TickRender(float delta_time); virtual void TickRender(float deltams);


public: public:
/* New methods */ /* New methods */


+ 21
- 20
src/timer.cpp 查看文件

@@ -40,7 +40,7 @@ private:
#elif defined _WIN32 #elif defined _WIN32
LARGE_INTEGER tmp; LARGE_INTEGER tmp;
QueryPerformanceFrequency(&tmp); QueryPerformanceFrequency(&tmp);
seconds_per_cycle = 1.0f / tmp.QuadPart; ms_per_cycle = 1e3f / tmp.QuadPart;
QueryPerformanceCounter(&cycles0); QueryPerformanceCounter(&cycles0);
#else #else
SDL_Init(SDL_INIT_TIMER); SDL_Init(SDL_INIT_TIMER);
@@ -48,36 +48,37 @@ private:
#endif #endif
} }


float GetOrWait(float seconds, bool update) float GetOrWait(float deltams, bool update)
{ {
float ret, delta_time; float ret, towait;
#if defined __linux__ #if defined __linux__
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
ret = 1e-6f * (tv.tv_usec - tv0.tv_usec) + (tv.tv_sec - tv0.tv_sec); ret = 1e-3f * (tv.tv_usec - tv0.tv_usec)
+ 1e3f * (tv.tv_sec - tv0.tv_sec);
if (update) if (update)
tv0 = tv; tv0 = tv;
delta_time = seconds - ret; towait = deltams - ret;
if (delta_time > 0.0f) if (towait > 0.0f)
usleep((int)(delta_time * 1e6f)); usleep((int)(towait * 1e3f));
#elif defined _WIN32 #elif defined _WIN32
LARGE_INTEGER cycles; LARGE_INTEGER cycles;
QueryPerformanceCounter(&cycles); QueryPerformanceCounter(&cycles);
ret = seconds_per_cycle * (cycles.QuadPart - cycles0.QuadPart); ret = ms_per_cycle * (cycles.QuadPart - cycles0.QuadPart);
if (update) if (update)
cycles0 = cycles; cycles0 = cycles;
delta_time = seconds - ret; towait = deltams - ret;
if (delta_time > 5e-4f) // FIXME: use native Win32 stuff if (towait > 5e-4f) // FIXME: use native Win32 stuff
SDL_Delay((int)(delta_time * 1e3f + 0.5f)); SDL_Delay((int)(towait * 1e3f + 0.5f));
#else #else
/* The crappy SDL fallback */ /* The crappy SDL fallback */
Uint32 ticks = SDL_GetTicks(); Uint32 ticks = SDL_GetTicks();
ret = 1e-3f * (ticks - ticks0); ret = 1e-6f * (ticks - ticks0);
if (update) if (update)
ticks0 = ticks; ticks0 = ticks;
delta_time = seconds - ret; towait = deltams - ret;
if (delta_time > 5e-4f) if (towait > 0.5f)
SDL_Delay((int)(delta_time * 1e3f + 0.5f)); SDL_Delay((int)(towait + 0.5f));
#endif #endif
return ret; return ret;
} }
@@ -85,7 +86,7 @@ private:
#if defined __linux__ #if defined __linux__
struct timeval tv0; struct timeval tv0;
#elif defined _WIN32 #elif defined _WIN32
float seconds_per_cycle; float ms_per_cycle;
LARGE_INTEGER cycles0; LARGE_INTEGER cycles0;
#else #else
Uint32 ticks0; Uint32 ticks0;
@@ -106,18 +107,18 @@ Timer::~Timer()
delete data; delete data;
} }


float Timer::GetSeconds() float Timer::GetMs()
{ {
return data->GetOrWait(0.0f, true); return data->GetOrWait(0.0f, true);
} }


float Timer::PollSeconds() float Timer::PollMs()
{ {
return data->GetOrWait(0.0f, false); return data->GetOrWait(0.0f, false);
} }


void Timer::WaitSeconds(float seconds) void Timer::WaitMs(float deltams)
{ {
(void)data->GetOrWait(seconds, false); (void)data->GetOrWait(deltams, false);
} }



+ 3
- 3
src/timer.h 查看文件

@@ -19,9 +19,9 @@ public:
Timer(); Timer();
~Timer(); ~Timer();


float GetSeconds(); float GetMs();
float PollSeconds(); float PollMs();
void WaitSeconds(float milliseconds); void WaitMs(float deltams);


private: private:
TimerData *data; TimerData *data;


||||||
x
 
000:0
Loading…
取消
儲存