source: trunk/src/sdlapp.cpp @ 680

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

Create the SdlApp class to avoid code duplication. Ensure USE_SDL is
tested wherever relevant.

  • 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
22/*
23 * SDL App implementation class
24 */
25
26class SdlAppData
27{
28    friend class SdlApp;
29
30private:
31    int unused;
32};
33
34/*
35 * Public SdlApp class
36 */
37
38SdlApp::SdlApp(char const *title, vec2i res, float fps) :
39    data(new SdlAppData())
40{
41#if defined USE_SDL
42    /* Initialise SDL */
43    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
44    {
45        fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError());
46        exit(EXIT_FAILURE);
47    }
48
49    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
50    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
51    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
52    if (!video)
53    {
54        fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError());
55        SDL_Quit();
56        exit(EXIT_FAILURE);
57    }
58
59    SDL_WM_SetCaption(title, NULL);
60    SDL_ShowCursor(0);
61
62    /* Initialise everything */
63    Ticker::Setup(fps);
64    Video::Setup(video->w, video->h);
65    Audio::Setup(2);
66#endif
67}
68
69void SdlApp::Run()
70{
71    while (!Ticker::Finished())
72    {
73        /* Tick the game */
74        Ticker::TickGame();
75
76        /* Tick the renderer, show the frame and clamp to desired framerate. */
77        Ticker::TickDraw();
78#if defined USE_SDL
79        SDL_GL_SwapBuffers();
80#endif
81        Ticker::ClampFps();
82    }
83}
84
85SdlApp::~SdlApp()
86{
87#if defined USE_SDL
88    SDL_Quit();
89#endif
90    free(data);
91}
92
Note: See TracBrowser for help on using the repository browser.