source: trunk/src/sdlapp.cpp @ 686

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

Put everything in the "lol" namespace. Better late than never.

  • Property svn:keywords set to Id
File size: 1.8 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined USE_SDL
16#   include <SDL.h>
17#endif
18
19#include "core.h"
20#include "sdlapp.h"
21
22namespace lol
23{
24
25/*
26 * SDL App implementation class
27 */
28
29class SdlAppData
30{
31    friend class SdlApp;
32
33private:
34    int unused;
35};
36
37/*
38 * Public SdlApp class
39 */
40
41SdlApp::SdlApp(char const *title, vec2i res, float fps) :
42    data(new SdlAppData())
43{
44#if defined USE_SDL
45    /* Initialise SDL */
46    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
47    {
48        fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError());
49        exit(EXIT_FAILURE);
50    }
51
52    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
53    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
54    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
55    if (!video)
56    {
57        fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError());
58        SDL_Quit();
59        exit(EXIT_FAILURE);
60    }
61
62    SDL_WM_SetCaption(title, NULL);
63    SDL_ShowCursor(0);
64
65    /* Initialise everything */
66    Ticker::Setup(fps);
67    Video::Setup(video->w, video->h);
68    Audio::Setup(2);
69#endif
70}
71
72void SdlApp::Run()
73{
74    while (!Ticker::Finished())
75    {
76        /* Tick the game */
77        Ticker::TickGame();
78
79        /* Tick the renderer, show the frame and clamp to desired framerate. */
80        Ticker::TickDraw();
81#if defined USE_SDL
82        SDL_GL_SwapBuffers();
83#endif
84        Ticker::ClampFps();
85    }
86}
87
88SdlApp::~SdlApp()
89{
90#if defined USE_SDL
91    SDL_Quit();
92#endif
93    free(data);
94}
95
96} /* namespace lol */
97
Note: See TracBrowser for help on using the repository browser.