source: trunk/src/video.cpp @ 2085

Last change on this file since 2085 was 2085, checked in by sam, 7 years ago

build: fix PS3 and Linux compilation issues.

  • Property svn:keywords set to Id
File size: 8.1 KB
Line 
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
32using namespace std;
33
34/* FIXME: g_d3ddevice should never be exported */
35#if defined USE_D3D9
36IDirect3DDevice9 *g_d3ddevice;
37#   if defined USE_SDL
38extern HWND g_hwnd;
39#   endif
40#elif defined _XBOX
41D3DDevice *g_d3ddevice;
42#endif
43
44namespace lol
45{
46
47class VideoData
48{
49    friend class Video;
50
51private:
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
67mat4 VideoData::proj_matrix;
68ivec2 VideoData::saved_viewport(0, 0);
69
70#if defined USE_D3D9 || defined _XBOX
71#   if defined USE_D3D9
72IDirect3D9 *VideoData::d3d_ctx;
73IDirect3DDevice9 *VideoData::d3d_dev;
74#   elif defined _XBOX
75Direct3D *VideoData::d3d_ctx;
76D3DDevice *VideoData::d3d_dev;
77#   endif
78D3DCOLOR VideoData::clear_color;
79float VideoData::clear_depth;
80#endif
81
82/*
83 * Public Video class
84 */
85
86void 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
158void 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
199void 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
213void 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
224void Video::SetClearDepth(float f)
225{
226#if defined USE_D3D9 || defined _XBOX
227    VideoData::clear_depth = f;
228#elif defined HAVE_GLES_2X
229    glClearDepthf(f);
230#else
231    glClearDepth(f);
232#endif
233}
234
235void Video::Clear(ClearMask m)
236{
237#if defined USE_D3D9 || defined _XBOX
238    int mask = 0;
239    if (m & ClearMask::Color)
240        mask |= D3DCLEAR_TARGET;
241    if (m & ClearMask::Depth)
242        mask |= D3DCLEAR_ZBUFFER;
243    if (m & ClearMask::Stencil)
244        mask |= D3DCLEAR_STENCIL;
245    if (FAILED(VideoData::d3d_dev->Clear(0, NULL, mask,
246                                         VideoData::clear_color,
247                                         VideoData::clear_depth, 0)))
248        Abort();
249#else
250    /* FIXME: is this necessary here? */
251    ivec2 size = GetSize();
252    glViewport(0, 0, size.x, size.y);
253
254    GLbitfield mask = 0;
255    if (m & ClearMask::Color)
256        mask |= GL_COLOR_BUFFER_BIT;
257    if (m & ClearMask::Depth)
258        mask |= GL_DEPTH_BUFFER_BIT;
259    if (m & ClearMask::Stencil)
260        mask |= GL_STENCIL_BUFFER_BIT;
261    glClear(mask);
262#endif
263
264    SetFov(0.0f);
265}
266
267void Video::Destroy()
268{
269    ;
270}
271
272void Video::Capture(uint32_t *buffer)
273{
274#if defined USE_D3D9 || defined _XBOX
275    /* TODO */
276#else
277    GLint v[4];
278#   if defined __CELLOS_LV2__
279    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
280    v[2] = 1920;
281    v[3] = 1080;
282#   else
283    glGetIntegerv(GL_VIEWPORT, v);
284#   endif
285    int width = v[2], height = v[3];
286
287#   if defined HAVE_GL_2X
288    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
289#   endif
290    glPixelStorei(GL_PACK_ALIGNMENT, 1);
291
292#   if defined GL_BGRA
293    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
294#   else
295    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
296#   endif
297
298    for (int j = 0; j < height / 2; j++)
299        for (int i = 0; i < width; i++)
300        {
301            uint32_t tmp = buffer[j * width + i];
302            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
303            buffer[(height - j - 1) * width + i] = tmp;
304        }
305#endif
306}
307
308ivec2 Video::GetSize()
309{
310#if defined USE_D3D9 || defined _XBOX
311    return VideoData::saved_viewport;
312#elif 1
313    /* GetSize() is called too often on the game thread; we cannot rely on
314     * the GL context at this point */
315    return VideoData::saved_viewport;
316#elif defined __CELLOS_LV2__
317    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
318#else
319    GLint v[4];
320    glGetIntegerv(GL_VIEWPORT, v);
321    return ivec2(v[2], v[3]);
322#endif
323}
324
325} /* namespace lol */
326
Note: See TracBrowser for help on using the repository browser.