source: trunk/src/test-map.cpp @ 111

Last change on this file since 111 was 111, checked in by sam, 11 years ago

The frame counter is now an asset object.

  • Property svn:keywords set to Id
File size: 1.8 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 start, ticks;
44    start = ticks = SDL_GetTicks();
45    int frames = 0;
46
47    /* Initialise OpenGL */
48    Video::Setup(video->w, video->h);
49
50    /* Create a game */
51    Game *game = new Game("maps/testmap.tmx");
52
53    /* Register an input driver and some debug stuff */
54    new SdlInput(game);
55    new DebugFps();
56
57    while (!game->Finished())
58    {
59        float const delta_time = 33.33333f;
60
61        /* Tick the game */
62        Ticker::TickGame(delta_time);
63
64        /* Clear the screen, tick the renderer, and show the frame */
65        Video::Clear();
66        Ticker::TickRender(delta_time);
67        SDL_GL_SwapBuffers();
68
69        /* Clamp to desired framerate */
70        while (SDL_GetTicks() < ticks + (delta_time - 0.5f))
71            SDL_Delay(1);
72        ticks = SDL_GetTicks();
73        frames++;
74    }
75
76    Uint32 total = SDL_GetTicks() - start;
77    printf("%f fps\n", 1000.0f * frames / total);
78    SDL_Quit();
79
80    return EXIT_SUCCESS;
81}
82
Note: See TracBrowser for help on using the repository browser.