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://www.wtfpl.net/ for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | /* This instructs our headers to let SDL override the "main" |
---|
16 | * symbol using its macros. */ |
---|
17 | #define LOL_DONT_DIVERT_MAIN 1 |
---|
18 | |
---|
19 | #if defined USE_SDL |
---|
20 | # if defined HAVE_SDL_SDL_H |
---|
21 | # include <SDL/SDL.h> |
---|
22 | # else |
---|
23 | # include <SDL.h> |
---|
24 | # endif |
---|
25 | # if defined USE_D3D9 |
---|
26 | # include <d3d9.h> |
---|
27 | # include <SDL_syswm.h> |
---|
28 | # endif |
---|
29 | #endif |
---|
30 | |
---|
31 | #include "core.h" |
---|
32 | #include "lolgl.h" |
---|
33 | #include "platform/sdl/sdlapp.h" |
---|
34 | #include "platform/sdl/sdlinput.h" |
---|
35 | #if defined USE_XINPUT |
---|
36 | # include "platform/d3d9/d3d9input.h" |
---|
37 | #endif |
---|
38 | |
---|
39 | #if defined USE_SDL && defined USE_D3D9 |
---|
40 | HWND g_hwnd = NULL; |
---|
41 | extern IDirect3DDevice9 *g_d3ddevice; |
---|
42 | #endif |
---|
43 | |
---|
44 | #if defined main |
---|
45 | # if defined _MSC_VER |
---|
46 | int lol_sdl_main(); |
---|
47 | int lol_sdl_main(int argc, char **argv); |
---|
48 | int lol_sdl_main(int argc, char **argv, char **envp); |
---|
49 | # define WRAPPER lol_sdl_main_msvc |
---|
50 | # else |
---|
51 | int lol_sdl_main() __attribute__((weak)); |
---|
52 | int lol_sdl_main(int argc, char **argv) __attribute__((weak)); |
---|
53 | int lol_sdl_main(int argc, char **argv, char **envp) __attribute__((weak)); |
---|
54 | # define WRAPPER lol_sdl_main |
---|
55 | # endif |
---|
56 | |
---|
57 | /* One of these wrappers will be overridden by the user's version */ |
---|
58 | |
---|
59 | int WRAPPER() { return 0; } |
---|
60 | int WRAPPER(int argc, char **argv) { return 0; } |
---|
61 | int WRAPPER(int argc, char **argv, char **envp) { return 0; } |
---|
62 | |
---|
63 | int main(int argc, char *argv[]) |
---|
64 | { |
---|
65 | int ret = 0; |
---|
66 | ret += lol_sdl_main(); |
---|
67 | ret += lol_sdl_main(argc, argv); |
---|
68 | ret += lol_sdl_main(argc, argv, NULL); |
---|
69 | return ret; |
---|
70 | } |
---|
71 | #endif |
---|
72 | |
---|
73 | namespace lol |
---|
74 | { |
---|
75 | |
---|
76 | /* |
---|
77 | * SDL App implementation class |
---|
78 | */ |
---|
79 | |
---|
80 | class SdlAppData |
---|
81 | { |
---|
82 | friend class SdlApp; |
---|
83 | |
---|
84 | private: |
---|
85 | int unused; |
---|
86 | }; |
---|
87 | |
---|
88 | /* |
---|
89 | * Public SdlApp class |
---|
90 | */ |
---|
91 | |
---|
92 | SdlApp::SdlApp(char const *title, ivec2 res, float fps) : |
---|
93 | data(new SdlAppData()) |
---|
94 | { |
---|
95 | #if defined USE_SDL |
---|
96 | /* Initialise SDL */ |
---|
97 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) |
---|
98 | { |
---|
99 | Log::Error("cannot initialise SDL: %s\n", SDL_GetError()); |
---|
100 | exit(EXIT_FAILURE); |
---|
101 | } |
---|
102 | |
---|
103 | # if defined USE_D3D9 |
---|
104 | SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 16, 0); |
---|
105 | SDL_SysWMinfo wminfo; |
---|
106 | SDL_VERSION(&wminfo.version); |
---|
107 | SDL_GetWMInfo(&wminfo); |
---|
108 | g_hwnd = wminfo.window; |
---|
109 | # else |
---|
110 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
---|
111 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); |
---|
112 | SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL); |
---|
113 | # endif |
---|
114 | if (!video) |
---|
115 | { |
---|
116 | Log::Error("cannot create rendering window: %s\n", SDL_GetError()); |
---|
117 | SDL_Quit(); |
---|
118 | exit(EXIT_FAILURE); |
---|
119 | } |
---|
120 | |
---|
121 | SDL_WM_SetCaption(title, NULL); |
---|
122 | |
---|
123 | /* Initialise everything */ |
---|
124 | Ticker::Setup(fps); |
---|
125 | Video::Setup(ivec2(video->w, video->h)); |
---|
126 | Audio::Setup(2); |
---|
127 | |
---|
128 | /* Autoreleased objects */ |
---|
129 | # if defined USE_XINPUT |
---|
130 | /* Prefer D3d9 for joysticks on Windows, because the X360 pads are not |
---|
131 | * advertised with the proper number of axes. */ |
---|
132 | new D3d9Input(); |
---|
133 | # endif |
---|
134 | new SdlInput(); |
---|
135 | #endif |
---|
136 | } |
---|
137 | |
---|
138 | void SdlApp::ShowPointer(bool show) |
---|
139 | { |
---|
140 | #if defined USE_SDL |
---|
141 | SDL_ShowCursor(show ? 1 : 0); |
---|
142 | #endif |
---|
143 | } |
---|
144 | |
---|
145 | void SdlApp::Tick() |
---|
146 | { |
---|
147 | #if defined USE_SDL && defined USE_D3D9 |
---|
148 | HRESULT hr; |
---|
149 | hr = g_d3ddevice->BeginScene(); |
---|
150 | if (FAILED(hr)) |
---|
151 | Abort(); |
---|
152 | #endif |
---|
153 | /* Tick the renderer, show the frame and clamp to desired framerate. */ |
---|
154 | Ticker::TickDraw(); |
---|
155 | #if defined USE_SDL |
---|
156 | # if defined USE_D3D9 |
---|
157 | hr = g_d3ddevice->EndScene(); |
---|
158 | if (FAILED(hr)) |
---|
159 | Abort(); |
---|
160 | hr = g_d3ddevice->Present(NULL, NULL, NULL, NULL); |
---|
161 | if (FAILED(hr)) |
---|
162 | Abort(); |
---|
163 | # else |
---|
164 | SDL_GL_SwapBuffers(); |
---|
165 | # endif |
---|
166 | #endif |
---|
167 | } |
---|
168 | |
---|
169 | SdlApp::~SdlApp() |
---|
170 | { |
---|
171 | #if defined USE_SDL |
---|
172 | SDL_Quit(); |
---|
173 | #endif |
---|
174 | delete data; |
---|
175 | } |
---|
176 | |
---|
177 | } /* namespace lol */ |
---|
178 | |
---|