Changeset 112


Ignore:
Timestamp:
Aug 14, 2010, 11:15:44 PM (10 years ago)
Author:
sam
Message:

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

Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/debugfps.cpp

    r111 r112  
    2121    friend class DebugFps;
    2222
     23    static int const HISTORY = 30;
     24
    2325private:
     26    float history[HISTORY];
    2427    Font *font;
    2528    int frame;
     
    3437    data = new DebugFpsData();
    3538
     39    for (int i = 0; i < DebugFpsData::HISTORY; i++)
     40        data->history[i] = 0.0f;
    3641    data->font = Forge::GetFont("gfx/font/ascii.png");
    3742    data->frame = 0;
     
    4752    Asset::TickGame(delta_time);
    4853
     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
    4966    char buf[1024];
    50     sprintf(buf, "%3.2f fps (%i)", 1000.0f / delta_time, data->frame++);
     67    sprintf(buf, "%3.2f ms (%3.2f fps) -- max %3.2f ms -- #%i",
     68            mean, 1000.0f / mean, max, data->frame);
    5169    data->font->Print(10, 10, buf);
    5270    data->font->Print(11, 10, buf);
  • trunk/src/gtk/editor.cpp

    r111 r112  
    2020#include "game.h"
    2121
    22 volatile int quit = 0;
     22static volatile int quit = 0;
     23
     24static GTimer *timer;
     25static float delta_time;
     26static int ticking = 0;
    2327
    2428static gint main_quit(GtkWidget *widget, GdkEventExpose *event)
     
    3438static gboolean tick(void *widget)
    3539{
    36     float const delta_time = 33.33333f;
    37 
    3840    // FIXME: do not do anything if the previous tick was too recent?
     41    delta_time = 1000.0f * g_timer_elapsed(timer, NULL);
     42    g_timer_start(timer);
    3943
    4044    // FIXME: only quit if all assets have been cleaned
    4145    if (quit)
    4246        return FALSE;
     47
     48    ticking = 1;
    4349
    4450    /* Tick the game */
     
    7177
    7278    /* OpenGL functions can be called only if make_current returns true */
    73     if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
     79    if (ticking && gtk_gl_area_make_current(GTK_GL_AREA(widget)))
    7480    {
    75         // FIXME: do not do anything if the game tick wasn't called?
    76         float const delta_time = 33.33333f;
     81        ticking = 0;
    7782
    7883        /* Clear the screen, tick the renderer, and show the frame */
     
    8893{
    8994    /* Initialize GTK */
     95    g_thread_init(NULL);
    9096    gtk_init(&argc, &argv);
    9197
     
    152158    //gtk_idle_add(tick, glarea);
    153159    gtk_timeout_add(33, tick, glarea);
     160
     161    timer = g_timer_new();
    154162    gtk_main();
    155163
  • trunk/src/test-map.cpp

    r111 r112  
    4141
    4242    /* Initialise timer */
    43     Uint32 start, ticks;
    44     start = ticks = SDL_GetTicks();
    45     int frames = 0;
     43    Uint32 ticks = SDL_GetTicks();
    4644
    4745    /* Initialise OpenGL */
     
    5755    while (!game->Finished())
    5856    {
    59         float const delta_time = 33.33333f;
     57        /* Compute delta time */
     58        Uint32 newticks = SDL_GetTicks();
     59        float delta_time = (float)(newticks - ticks);
     60        ticks = newticks;
    6061
    6162        /* Tick the game */
     
    6869
    6970        /* Clamp to desired framerate */
    70         while (SDL_GetTicks() < ticks + (delta_time - 0.5f))
     71        while (SDL_GetTicks() < ticks + (33.33333f - 0.5f))
    7172            SDL_Delay(1);
    72         ticks = SDL_GetTicks();
    73         frames++;
    7473    }
    7574
    76     Uint32 total = SDL_GetTicks() - start;
    77     printf("%f fps\n", 1000.0f * frames / total);
    7875    SDL_Quit();
    7976
Note: See TracChangeset for help on using the changeset viewer.