source: trunk/src/platform/sdl/sdlapp.cpp @ 1063

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

core: implement Application::ShowPointer() so that applications can choose
whether to use the system mouse pointer or not.

  • Property svn:keywords set to Id
File size: 2.0 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 "lolgl.h"
21#include "platform/sdl/sdlapp.h"
22#include "platform/sdl/sdlinput.h"
23
24namespace lol
25{
26
27/*
28 * SDL App implementation class
29 */
30
31class SdlAppData
32{
33    friend class SdlApp;
34
35private:
36    int unused;
37};
38
39/*
40 * Public SdlApp class
41 */
42
43SdlApp::SdlApp(char const *title, ivec2 res, float fps) :
44    data(new SdlAppData())
45{
46#if defined USE_SDL
47    /* Initialise SDL */
48    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
49    {
50        Log::Error("cannot initialise SDL: %s\n", SDL_GetError());
51        exit(EXIT_FAILURE);
52    }
53
54    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
55    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
56    SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
57    if (!video)
58    {
59        Log::Error("cannot create OpenGL screen: %s\n", SDL_GetError());
60        SDL_Quit();
61        exit(EXIT_FAILURE);
62    }
63
64    SDL_WM_SetCaption(title, NULL);
65
66    /* Initialise everything */
67    Ticker::Setup(fps);
68    Video::Setup(ivec2(video->w, video->h));
69    Audio::Setup(2);
70
71    /* Autoreleased objects */
72    new SdlInput();
73#endif
74}
75
76void SdlApp::ShowPointer(bool show)
77{
78    SDL_ShowCursor(show ? 1 : 0);
79}
80
81void SdlApp::Run()
82{
83    while (!Ticker::Finished())
84    {
85        /* Tick the game */
86        Ticker::TickGame();
87
88        /* Tick the renderer, show the frame and clamp to desired framerate. */
89        Ticker::TickDraw();
90#if defined USE_SDL
91        SDL_GL_SwapBuffers();
92#endif
93        Ticker::ClampFps();
94    }
95}
96
97SdlApp::~SdlApp()
98{
99#if defined USE_SDL
100    SDL_Quit();
101#endif
102    delete data;
103}
104
105} /* namespace lol */
106
Note: See TracBrowser for help on using the repository browser.