source: trunk/src/video.cpp @ 2506

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

base: start removing occurrences of NULL on our long journey to nullptr.

  • Property svn:keywords set to Id
File size: 9.8 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 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#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    static DebugRenderMode render_mode;
55    static bool face_culling;
56#if defined USE_D3D9 || defined _XBOX
57#   if defined USE_D3D9
58    static IDirect3D9 *d3d_ctx;
59    static IDirect3DDevice9 *d3d_dev;
60#   elif defined _XBOX
61    static Direct3D *d3d_ctx;
62    static D3DDevice *d3d_dev;
63#   endif
64    static D3DCOLOR clear_color;
65    static float clear_depth;
66#endif
67};
68
69mat4 VideoData::proj_matrix;
70ivec2 VideoData::saved_viewport(0, 0);
71DebugRenderMode VideoData::render_mode = DebugRenderMode::Default;
72bool VideoData::face_culling;
73
74#if defined USE_D3D9 || defined _XBOX
75#   if defined USE_D3D9
76IDirect3D9 *VideoData::d3d_ctx;
77IDirect3DDevice9 *VideoData::d3d_dev;
78#   elif defined _XBOX
79Direct3D *VideoData::d3d_ctx;
80D3DDevice *VideoData::d3d_dev;
81#   endif
82D3DCOLOR VideoData::clear_color;
83float VideoData::clear_depth;
84#endif
85
86/*
87 * Public Video class
88 */
89
90void Video::Setup(ivec2 size)
91{
92#if defined USE_D3D9 || defined _XBOX
93    VideoData::d3d_ctx = Direct3DCreate9(D3D_SDK_VERSION);
94    if (!VideoData::d3d_ctx)
95    {
96        Log::Error("cannot initialise D3D\n");
97        exit(EXIT_FAILURE);
98    }
99
100    HWND window = 0;
101    D3DPRESENT_PARAMETERS d3dpp;
102    memset(&d3dpp, 0, sizeof(d3dpp));
103
104#   if defined USE_SDL
105    window = g_hwnd;
106    d3dpp.hDeviceWindow = g_hwnd;
107    d3dpp.Windowed = TRUE;
108#   elif defined _XBOX
109    XVIDEO_MODE VideoMode;
110    XGetVideoMode( &VideoMode );
111    if (size.x > VideoMode.dwDisplayWidth)
112        size.x = VideoMode.dwDisplayWidth;
113    if (size.y > VideoMode.dwDisplayHeight)
114        size.y = VideoMode.dwDisplayHeight;
115#   endif
116    VideoData::saved_viewport = size;
117
118    d3dpp.BackBufferWidth = size.x;
119    d3dpp.BackBufferHeight = size.y;
120    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
121    d3dpp.BackBufferCount = 1;
122    d3dpp.EnableAutoDepthStencil = TRUE;
123    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
124    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
125    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
126
127    HRESULT hr = VideoData::d3d_ctx->CreateDevice(0, D3DDEVTYPE_HAL, window,
128                                                  D3DCREATE_HARDWARE_VERTEXPROCESSING,
129                                                  &d3dpp, &VideoData::d3d_dev);
130    if (FAILED(hr))
131    {
132        Log::Error("cannot create D3D device\n");
133        exit(EXIT_FAILURE);
134    }
135
136    g_d3ddevice = VideoData::d3d_dev;
137#else
138#   if defined USE_GLEW && !defined __APPLE__
139    /* Initialise GLEW if necessary */
140    GLenum glerr = glewInit();
141    if (glerr != GLEW_OK)
142    {
143        Log::Error("cannot initialise GLEW: %s\n", glewGetErrorString(glerr));
144        exit(EXIT_FAILURE);
145    }
146#   endif
147
148    /* Initialise OpenGL */
149    glViewport(0, 0, size.x, size.y);
150    VideoData::saved_viewport = size;
151
152#   if defined HAVE_GL_2X && !defined __APPLE__
153    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
154#   endif
155#endif
156
157    /* Initialise reasonable scene default properties */
158    SetClearColor(vec4(0.1f, 0.2f, 0.3f, 1.0f));
159    SetClearDepth(1.f);
160    SetDebugRenderMode(DebugRenderMode::Default);
161}
162
163void Video::SetFov(float theta)
164{
165    vec2 size = GetSize();
166    float near = -size.x - size.y;
167    float far = size.x + size.y;
168
169#if defined __ANDROID__
170    size = vec2(640.0f, 480.0f);
171#endif
172
173    /* Set the projection matrix */
174    if (theta < 1e-4f)
175    {
176        /* The easy way: purely orthogonal projection. */
177        VideoData::proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far);
178    }
179    else
180    {
181        /* Compute a view that approximates the glOrtho view when theta
182         * approaches zero. This view ensures that the z=0 plane fills
183         * the screen. */
184        float t1 = tanf(theta / 2);
185        float t2 = t1 * size.y / size.y;
186        float dist = size.x / (2.0f * t1);
187
188        near += dist;
189        far += dist;
190
191        if (near <= 0.0f)
192        {
193            far -= (near - 1.0f);
194            near = 1.0f;
195        }
196
197        mat4 proj = mat4::frustum(-near * t1, near * t1,
198                                  -near * t2, near * t2, near, far);
199        mat4 trans = mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist);
200        VideoData::proj_matrix = proj * trans;
201    }
202}
203
204void Video::SetDepth(bool set)
205{
206#if defined USE_D3D9 || defined _XBOX
207#   define STR0(x) #x
208#   define STR(x) STR0(x)
209#   pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetDepth() not implemented")
210#else
211    if (set)
212        glEnable(GL_DEPTH_TEST);
213    else
214        glDisable(GL_DEPTH_TEST);
215#endif
216}
217
218void Video::SetClearColor(vec4 color)
219{
220#if defined USE_D3D9 || defined _XBOX
221    VideoData::clear_color = D3DCOLOR_XRGB((int)(color.r * 255.999f),
222                                           (int)(color.g * 255.999f),
223                                           (int)(color.b * 255.999f));
224#else
225    glClearColor(color.r, color.g, color.b, color.a);
226#endif
227}
228
229void Video::SetClearDepth(float f)
230{
231#if defined USE_D3D9 || defined _XBOX
232    VideoData::clear_depth = f;
233#elif defined HAVE_GLES_2X
234    glClearDepthf(f);
235#else
236    glClearDepth(f);
237#endif
238}
239
240void Video::SetDebugRenderMode(DebugRenderMode d)
241{
242    switch(d)
243    {
244        //All these modes are handled in the shaders.
245        case DebugRenderMode::Default:
246        case DebugRenderMode::Lighting:
247        case DebugRenderMode::Normal:
248        case DebugRenderMode::UV:
249        {
250#if defined USE_D3D9 || defined _XBOX
251#elif defined HAVE_GLES_2X
252            glEnable(GL_CULL_FACE);
253#else
254            if (VideoData::render_mode == d && glIsEnabled(GL_CULL_FACE) == GL_TRUE)
255                glDisable(GL_CULL_FACE);
256            else
257                glEnable(GL_CULL_FACE);
258            glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
259#endif
260            break;
261        }
262        case DebugRenderMode::Wireframe:
263        {
264            if (VideoData::render_mode == d)
265            {
266#if defined USE_D3D9 || defined _XBOX
267#else
268                VideoData::face_culling = !VideoData::face_culling;
269                if (VideoData::face_culling)
270                    glEnable(GL_CULL_FACE);
271                else
272                    glDisable(GL_CULL_FACE);
273#endif
274            }
275            else
276            {
277#if defined USE_D3D9 || defined _XBOX
278#else
279                glDisable(GL_CULL_FACE);
280#endif
281            }
282#if defined USE_D3D9 || defined _XBOX
283#elif defined HAVE_GLES_2X
284#else
285            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
286#endif
287            break;
288        }
289        default:
290            return; /* Unknown render mode */
291    }
292    VideoData::render_mode = d;
293}
294
295DebugRenderMode Video::GetDebugRenderMode()
296{
297    return VideoData::render_mode;
298}
299
300void Video::Clear(ClearMask m)
301{
302#if defined USE_D3D9 || defined _XBOX
303    int mask = 0;
304    if (m & ClearMask::Color)
305        mask |= D3DCLEAR_TARGET;
306    if (m & ClearMask::Depth)
307        mask |= D3DCLEAR_ZBUFFER;
308    if (m & ClearMask::Stencil)
309        mask |= D3DCLEAR_STENCIL;
310    if (FAILED(VideoData::d3d_dev->Clear(0, nullptr, mask,
311                                         VideoData::clear_color,
312                                         VideoData::clear_depth, 0)))
313        Abort();
314#else
315    /* FIXME: is this necessary here? */
316    ivec2 size = GetSize();
317    glViewport(0, 0, size.x, size.y);
318
319    GLbitfield mask = 0;
320    if (m & ClearMask::Color)
321        mask |= GL_COLOR_BUFFER_BIT;
322    if (m & ClearMask::Depth)
323        mask |= GL_DEPTH_BUFFER_BIT;
324    if (m & ClearMask::Stencil)
325        mask |= GL_STENCIL_BUFFER_BIT;
326    glClear(mask);
327#endif
328
329    SetFov(0.0f);
330}
331
332void Video::Destroy()
333{
334    ;
335}
336
337void Video::Capture(uint32_t *buffer)
338{
339#if defined USE_D3D9 || defined _XBOX
340    /* TODO */
341#else
342    GLint v[4];
343#   if defined __CELLOS_LV2__
344    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
345    v[2] = 1920;
346    v[3] = 1080;
347#   else
348    glGetIntegerv(GL_VIEWPORT, v);
349#   endif
350    int width = v[2], height = v[3];
351
352#   if defined HAVE_GL_2X
353    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
354#   endif
355    glPixelStorei(GL_PACK_ALIGNMENT, 1);
356
357#   if defined GL_BGRA
358    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
359#   else
360    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
361#   endif
362
363    for (int j = 0; j < height / 2; j++)
364        for (int i = 0; i < width; i++)
365        {
366            uint32_t tmp = buffer[j * width + i];
367            buffer[j * width + i] = buffer[(height - j - 1) * width + i];
368            buffer[(height - j - 1) * width + i] = tmp;
369        }
370#endif
371}
372
373ivec2 Video::GetSize()
374{
375#if defined USE_D3D9 || defined _XBOX
376    return VideoData::saved_viewport;
377#elif 1
378    /* GetSize() is called too often on the game thread; we cannot rely on
379     * the GL context at this point */
380    return VideoData::saved_viewport;
381#elif defined __CELLOS_LV2__
382    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
383#else
384    GLint v[4];
385    glGetIntegerv(GL_VIEWPORT, v);
386    return ivec2(v[2], v[3]);
387#endif
388}
389
390} /* namespace lol */
391
Note: See TracBrowser for help on using the repository browser.