source: trunk/src/test-map.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.7 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#include <cmath>
12
13#include <SDL.h>
14
15#include "sdlinput.h"
16#include "debugfps.h"
17#include "game.h"
18#include "ticker.h"
19#include "video.h"
20
21int main(int argc, char **argv)
22{
23    /* Initialise SDL */
24    if (SDL_Init(SDL_INIT_VIDEO) < 0)
25    {
26        fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError());
27        return EXIT_FAILURE;
28    }
29
30    SDL_Surface *video = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
31    if (!video)
32    {
33        fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError());
34        SDL_Quit();
35        return EXIT_FAILURE;
36    }
37
38    SDL_WM_SetCaption("Deus Hax (SDL)", NULL);
39    SDL_ShowCursor(0);
40    SDL_WM_GrabInput(SDL_GRAB_ON);
41
42    /* Initialise timer */
43    Uint32 ticks = SDL_GetTicks();
44
45    /* Initialise OpenGL */
46    Video::Setup(video->w, video->h);
47
48    /* Create a game */
49    Game *game = new Game("maps/testmap.tmx");
50
51    /* Register an input driver and some debug stuff */
52    new SdlInput(game);
53    new DebugFps();
54
55    while (!game->Finished())
56    {
57        /* Compute delta time */
58        Uint32 newticks = SDL_GetTicks();
59        float delta_time = (float)(newticks - ticks);
60        ticks = newticks;
61
62        /* Tick the game */
63        Ticker::TickGame(delta_time);
64
65        /* Clear the screen, tick the renderer, and show the frame */
66        Video::Clear();
67        Ticker::TickRender(delta_time);
68        SDL_GL_SwapBuffers();
69
70        /* Clamp to desired framerate */
71        while (SDL_GetTicks() < ticks + (33.33333f - 0.5f))
72            SDL_Delay(1);
73    }
74
75    SDL_Quit();
76
77    return EXIT_SUCCESS;
78}
79
Note: See TracBrowser for help on using the repository browser.