source: trunk/src/debugfps.cpp @ 112

Last change on this file since 112 was 112, checked in by sam, 10 years ago

Proper delta time computation in both the game and the editor.

  • Property svn:keywords set to Id
File size: 1.4 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <cstdio>
11
12#include "debugfps.h"
13#include "forge.h"
14
15/*
16 * DebugFps implementation class
17 */
18
19class DebugFpsData
20{
21    friend class DebugFps;
22
23    static int const HISTORY = 30;
24
25private:
26    float history[HISTORY];
27    Font *font;
28    int frame;
29};
30
31/*
32 * Public DebugFps class
33 */
34
35DebugFps::DebugFps()
36{
37    data = new DebugFpsData();
38
39    for (int i = 0; i < DebugFpsData::HISTORY; i++)
40        data->history[i] = 0.0f;
41    data->font = Forge::GetFont("gfx/font/ascii.png");
42    data->frame = 0;
43}
44
45Asset::Group DebugFps::GetGroup()
46{
47    return GROUP_AFTER;
48}
49
50void DebugFps::TickRender(float delta_time)
51{
52    Asset::TickGame(delta_time);
53
54    data->history[data->frame % DebugFpsData::HISTORY] = delta_time;
55    data->frame++;
56
57    float mean = 0.0f, max = 0.0f;
58    for (int i = 0; i < DebugFpsData::HISTORY; i++)
59    {
60        mean += data->history[i];
61        if (data->history[i] > max)
62            max = data->history[i];
63    }
64    mean /= DebugFpsData::HISTORY;
65
66    char buf[1024];
67    sprintf(buf, "%3.2f ms (%3.2f fps) -- max %3.2f ms -- #%i",
68            mean, 1000.0f / mean, max, data->frame);
69    data->font->Print(10, 10, buf);
70    data->font->Print(11, 10, buf);
71    data->font->Print(10, 11, buf);
72}
73
74DebugFps::~DebugFps()
75{
76    Forge::ReleaseFont(data->font);
77    delete data;
78}
79
Note: See TracBrowser for help on using the repository browser.