1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2012 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 _XBOX |
---|
16 | # include <xtl.h> |
---|
17 | # undef near /* Fuck Microsoft */ |
---|
18 | # undef far /* Fuck Microsoft again */ |
---|
19 | #elif defined _WIN32 |
---|
20 | # if defined USE_D3D9 |
---|
21 | # include <d3d9.h> |
---|
22 | # endif |
---|
23 | # define WIN32_LEAN_AND_MEAN |
---|
24 | # include <windows.h> |
---|
25 | # undef near /* Fuck Microsoft */ |
---|
26 | # undef far /* Fuck Microsoft again */ |
---|
27 | #endif |
---|
28 | |
---|
29 | #include "core.h" |
---|
30 | #include "lolgl.h" |
---|
31 | |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | /* FIXME: g_d3ddevice should never be exported */ |
---|
35 | #if defined USE_D3D9 |
---|
36 | IDirect3DDevice9 *g_d3ddevice; |
---|
37 | # if defined USE_SDL |
---|
38 | extern HWND g_hwnd; |
---|
39 | # endif |
---|
40 | #elif defined _XBOX |
---|
41 | D3DDevice *g_d3ddevice; |
---|
42 | #endif |
---|
43 | |
---|
44 | namespace lol |
---|
45 | { |
---|
46 | |
---|
47 | class VideoData |
---|
48 | { |
---|
49 | friend class Video; |
---|
50 | |
---|
51 | private: |
---|
52 | static mat4 proj_matrix; |
---|
53 | static ivec2 saved_viewport; |
---|
54 | #if defined USE_D3D9 || defined _XBOX |
---|
55 | # if defined USE_D3D9 |
---|
56 | static IDirect3D9 *d3d_ctx; |
---|
57 | static IDirect3DDevice9 *d3d_dev; |
---|
58 | # elif defined _XBOX |
---|
59 | static Direct3D *d3d_ctx; |
---|
60 | static D3DDevice *d3d_dev; |
---|
61 | # endif |
---|
62 | static D3DCOLOR clear_color; |
---|
63 | static float clear_depth; |
---|
64 | #endif |
---|
65 | }; |
---|
66 | |
---|
67 | mat4 VideoData::proj_matrix; |
---|
68 | ivec2 VideoData::saved_viewport(0, 0); |
---|
69 | |
---|
70 | #if defined USE_D3D9 || defined _XBOX |
---|
71 | # if defined USE_D3D9 |
---|
72 | IDirect3D9 *VideoData::d3d_ctx; |
---|
73 | IDirect3DDevice9 *VideoData::d3d_dev; |
---|
74 | # elif defined _XBOX |
---|
75 | Direct3D *VideoData::d3d_ctx; |
---|
76 | D3DDevice *VideoData::d3d_dev; |
---|
77 | # endif |
---|
78 | D3DCOLOR VideoData::clear_color; |
---|
79 | float VideoData::clear_depth; |
---|
80 | #endif |
---|
81 | |
---|
82 | /* |
---|
83 | * Public Video class |
---|
84 | */ |
---|
85 | |
---|
86 | void Video::Setup(ivec2 size) |
---|
87 | { |
---|
88 | #if defined USE_D3D9 || defined _XBOX |
---|
89 | VideoData::d3d_ctx = Direct3DCreate9(D3D_SDK_VERSION); |
---|
90 | if (!VideoData::d3d_ctx) |
---|
91 | { |
---|
92 | Log::Error("cannot initialise D3D\n"); |
---|
93 | exit(EXIT_FAILURE); |
---|
94 | } |
---|
95 | |
---|
96 | HWND window = 0; |
---|
97 | D3DPRESENT_PARAMETERS d3dpp; |
---|
98 | memset(&d3dpp, 0, sizeof(d3dpp)); |
---|
99 | |
---|
100 | # if defined USE_SDL |
---|
101 | window = g_hwnd; |
---|
102 | d3dpp.hDeviceWindow = g_hwnd; |
---|
103 | d3dpp.Windowed = TRUE; |
---|
104 | # elif defined _XBOX |
---|
105 | XVIDEO_MODE VideoMode; |
---|
106 | XGetVideoMode( &VideoMode ); |
---|
107 | if (size.x > VideoMode.dwDisplayWidth) |
---|
108 | size.x = VideoMode.dwDisplayWidth; |
---|
109 | if (size.y > VideoMode.dwDisplayHeight) |
---|
110 | size.y = VideoMode.dwDisplayHeight; |
---|
111 | # endif |
---|
112 | VideoData::saved_viewport = size; |
---|
113 | |
---|
114 | d3dpp.BackBufferWidth = size.x; |
---|
115 | d3dpp.BackBufferHeight = size.y; |
---|
116 | d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; |
---|
117 | d3dpp.BackBufferCount = 1; |
---|
118 | d3dpp.EnableAutoDepthStencil = TRUE; |
---|
119 | d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; |
---|
120 | d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; |
---|
121 | d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; |
---|
122 | |
---|
123 | HRESULT hr = VideoData::d3d_ctx->CreateDevice(0, D3DDEVTYPE_HAL, window, |
---|
124 | D3DCREATE_HARDWARE_VERTEXPROCESSING, |
---|
125 | &d3dpp, &VideoData::d3d_dev); |
---|
126 | if (FAILED(hr)) |
---|
127 | { |
---|
128 | Log::Error("cannot create D3D device\n"); |
---|
129 | exit(EXIT_FAILURE); |
---|
130 | } |
---|
131 | |
---|
132 | g_d3ddevice = VideoData::d3d_dev; |
---|
133 | #else |
---|
134 | # if defined USE_GLEW && !defined __APPLE__ |
---|
135 | /* Initialise GLEW if necessary */ |
---|
136 | GLenum glerr = glewInit(); |
---|
137 | if (glerr != GLEW_OK) |
---|
138 | { |
---|
139 | Log::Error("cannot initialise GLEW: %s\n", glewGetErrorString(glerr)); |
---|
140 | exit(EXIT_FAILURE); |
---|
141 | } |
---|
142 | # endif |
---|
143 | |
---|
144 | /* Initialise OpenGL */ |
---|
145 | glViewport(0, 0, size.x, size.y); |
---|
146 | VideoData::saved_viewport = size; |
---|
147 | |
---|
148 | # if defined HAVE_GL_2X && !defined __APPLE__ |
---|
149 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); |
---|
150 | # endif |
---|
151 | #endif |
---|
152 | |
---|
153 | /* Initialise reasonable scene default properties */ |
---|
154 | SetClearColor(vec4(0.1f, 0.2f, 0.3f, 1.0f)); |
---|
155 | SetClearDepth(1.f); |
---|
156 | } |
---|
157 | |
---|
158 | void Video::SetFov(float theta) |
---|
159 | { |
---|
160 | vec2 size = GetSize(); |
---|
161 | float near = -size.x - size.y; |
---|
162 | float far = size.x + size.y; |
---|
163 | |
---|
164 | #if defined __ANDROID__ |
---|
165 | size = vec2(640.0f, 480.0f); |
---|
166 | #endif |
---|
167 | |
---|
168 | /* Set the projection matrix */ |
---|
169 | if (theta < 1e-4f) |
---|
170 | { |
---|
171 | /* The easy way: purely orthogonal projection. */ |
---|
172 | VideoData::proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far); |
---|
173 | } |
---|
174 | else |
---|
175 | { |
---|
176 | /* Compute a view that approximates the glOrtho view when theta |
---|
177 | * approaches zero. This view ensures that the z=0 plane fills |
---|
178 | * the screen. */ |
---|
179 | float t1 = tanf(theta / 2); |
---|
180 | float t2 = t1 * size.y / size.y; |
---|
181 | float dist = size.x / (2.0f * t1); |
---|
182 | |
---|
183 | near += dist; |
---|
184 | far += dist; |
---|
185 | |
---|
186 | if (near <= 0.0f) |
---|
187 | { |
---|
188 | far -= (near - 1.0f); |
---|
189 | near = 1.0f; |
---|
190 | } |
---|
191 | |
---|
192 | mat4 proj = mat4::frustum(-near * t1, near * t1, |
---|
193 | -near * t2, near * t2, near, far); |
---|
194 | mat4 trans = mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist); |
---|
195 | VideoData::proj_matrix = proj * trans; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | void Video::SetDepth(bool set) |
---|
200 | { |
---|
201 | #if defined USE_D3D9 || defined _XBOX |
---|
202 | # define STR0(x) #x |
---|
203 | # define STR(x) STR0(x) |
---|
204 | # pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetDepth() not implemented") |
---|
205 | #else |
---|
206 | if (set) |
---|
207 | glEnable(GL_DEPTH_TEST); |
---|
208 | else |
---|
209 | glDisable(GL_DEPTH_TEST); |
---|
210 | #endif |
---|
211 | } |
---|
212 | |
---|
213 | void Video::SetClearColor(vec4 color) |
---|
214 | { |
---|
215 | #if defined USE_D3D9 || defined _XBOX |
---|
216 | VideoData::clear_color = D3DCOLOR_XRGB((int)(color.r * 255.999f), |
---|
217 | (int)(color.g * 255.999f), |
---|
218 | (int)(color.b * 255.999f)); |
---|
219 | #else |
---|
220 | glClearColor(color.r, color.g, color.b, color.a); |
---|
221 | #endif |
---|
222 | } |
---|
223 | |
---|
224 | void Video::SetClearDepth(float f) |
---|
225 | { |
---|
226 | #if defined USE_D3D9 || defined _XBOX |
---|
227 | VideoData::clear_depth = f; |
---|
228 | #else |
---|
229 | glClearDepth(f); |
---|
230 | #endif |
---|
231 | } |
---|
232 | |
---|
233 | void Video::Clear(ClearMask m) |
---|
234 | { |
---|
235 | #if defined USE_D3D9 || defined _XBOX |
---|
236 | int mask = 0; |
---|
237 | if (m & ClearMask::Color) |
---|
238 | mask |= D3DCLEAR_TARGET; |
---|
239 | if (m & ClearMask::Depth) |
---|
240 | mask |= D3DCLEAR_ZBUFFER; |
---|
241 | if (m & ClearMask::Stencil) |
---|
242 | mask |= D3DCLEAR_STENCIL; |
---|
243 | if (FAILED(VideoData::d3d_dev->Clear(0, NULL, mask, |
---|
244 | VideoData::clear_color, |
---|
245 | VideoData::clear_depth, 0))) |
---|
246 | Abort(); |
---|
247 | #else |
---|
248 | /* FIXME: is this necessary here? */ |
---|
249 | ivec2 size = GetSize(); |
---|
250 | glViewport(0, 0, size.x, size.y); |
---|
251 | |
---|
252 | GLbitfield mask = 0; |
---|
253 | if (m & ClearMask::Color) |
---|
254 | mask |= GL_COLOR_BUFFER_BIT; |
---|
255 | if (m & ClearMask::Depth) |
---|
256 | mask |= GL_DEPTH_BUFFER_BIT; |
---|
257 | if (m & ClearMask::Stencil) |
---|
258 | mask |= GL_STENCIL_BUFFER_BIT; |
---|
259 | glClear(mask); |
---|
260 | #endif |
---|
261 | |
---|
262 | SetFov(0.0f); |
---|
263 | } |
---|
264 | |
---|
265 | void Video::Destroy() |
---|
266 | { |
---|
267 | ; |
---|
268 | } |
---|
269 | |
---|
270 | void Video::Capture(uint32_t *buffer) |
---|
271 | { |
---|
272 | #if defined USE_D3D9 || defined _XBOX |
---|
273 | /* TODO */ |
---|
274 | #else |
---|
275 | GLint v[4]; |
---|
276 | # if defined __CELLOS_LV2__ |
---|
277 | // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions |
---|
278 | v[2] = 1920; |
---|
279 | v[3] = 1080; |
---|
280 | # else |
---|
281 | glGetIntegerv(GL_VIEWPORT, v); |
---|
282 | # endif |
---|
283 | int width = v[2], height = v[3]; |
---|
284 | |
---|
285 | # if defined HAVE_GL_2X |
---|
286 | glPixelStorei(GL_PACK_ROW_LENGTH, 0); |
---|
287 | # endif |
---|
288 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
---|
289 | |
---|
290 | # if defined GL_BGRA |
---|
291 | glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer); |
---|
292 | # else |
---|
293 | glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
---|
294 | # endif |
---|
295 | |
---|
296 | for (int j = 0; j < height / 2; j++) |
---|
297 | for (int i = 0; i < width; i++) |
---|
298 | { |
---|
299 | uint32_t tmp = buffer[j * width + i]; |
---|
300 | buffer[j * width + i] = buffer[(height - j - 1) * width + i]; |
---|
301 | buffer[(height - j - 1) * width + i] = tmp; |
---|
302 | } |
---|
303 | #endif |
---|
304 | } |
---|
305 | |
---|
306 | ivec2 Video::GetSize() |
---|
307 | { |
---|
308 | #if defined USE_D3D9 || defined _XBOX |
---|
309 | return VideoData::saved_viewport; |
---|
310 | #elif 1 |
---|
311 | /* GetSize() is called too often on the game thread; we cannot rely on |
---|
312 | * the GL context at this point */ |
---|
313 | return VideoData::saved_viewport; |
---|
314 | #elif defined __CELLOS_LV2__ |
---|
315 | // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions |
---|
316 | #else |
---|
317 | GLint v[4]; |
---|
318 | glGetIntegerv(GL_VIEWPORT, v); |
---|
319 | return ivec2(v[2], v[3]); |
---|
320 | #endif |
---|
321 | } |
---|
322 | |
---|
323 | } /* namespace lol */ |
---|
324 | |
---|