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