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