source: trunk/src/video.cpp @ 2216

Last change on this file since 2216 was 2216, checked in by touky, 7 years ago

New year copyright update.

  • Property svn:keywords set to Id
File size: 8.1 KB
RevLine 
[105]1//
[221]2// Lol Engine
[105]3//
[2216]4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
[221]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
[2183]8//   http://www.wtfpl.net/ for more details.
[221]9//
[105]10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
[1233]15#if defined _XBOX
[1173]16#   include <xtl.h>
17#   undef near /* Fuck Microsoft */
18#   undef far /* Fuck Microsoft again */
19#elif defined _WIN32
[1233]20#   if defined USE_D3D9
21#      include <d3d9.h>
22#   endif
[105]23#   define WIN32_LEAN_AND_MEAN
24#   include <windows.h>
[1044]25#   undef near /* Fuck Microsoft */
26#   undef far /* Fuck Microsoft again */
[105]27#endif
28
[150]29#include "core.h"
[673]30#include "lolgl.h"
[105]31
[758]32using namespace std;
33
[1189]34/* FIXME: g_d3ddevice should never be exported */
[1208]35#if defined USE_D3D9
36IDirect3DDevice9 *g_d3ddevice;
37#   if defined USE_SDL
38extern HWND g_hwnd;
39#   endif
40#elif defined _XBOX
[1189]41D3DDevice *g_d3ddevice;
42#endif
43
[686]44namespace lol
45{
46
[784]47class VideoData
48{
49    friend class Video;
50
51private:
[1325]52    static mat4 proj_matrix;
[863]53    static ivec2 saved_viewport;
[1771]54#if defined USE_D3D9 || defined _XBOX
55#   if defined USE_D3D9
[1208]56    static IDirect3D9 *d3d_ctx;
57    static IDirect3DDevice9 *d3d_dev;
[1771]58#   elif defined _XBOX
[1173]59    static Direct3D *d3d_ctx;
60    static D3DDevice *d3d_dev;
[1771]61#   endif
[1199]62    static D3DCOLOR clear_color;
[1771]63    static float clear_depth;
[1173]64#endif
[784]65};
[707]66
[784]67mat4 VideoData::proj_matrix;
[1050]68ivec2 VideoData::saved_viewport(0, 0);
[653]69
[1771]70#if defined USE_D3D9 || defined _XBOX
71#   if defined USE_D3D9
[1208]72IDirect3D9 *VideoData::d3d_ctx;
73IDirect3DDevice9 *VideoData::d3d_dev;
[1771]74#   elif defined _XBOX
[1173]75Direct3D *VideoData::d3d_ctx;
76D3DDevice *VideoData::d3d_dev;
[1771]77#   endif
[1199]78D3DCOLOR VideoData::clear_color;
[1771]79float VideoData::clear_depth;
[1173]80#endif
81
[105]82/*
83 * Public Video class
84 */
85
[863]86void Video::Setup(ivec2 size)
[105]87{
[1208]88#if defined USE_D3D9 || defined _XBOX
[1173]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
[1208]96    HWND window = 0;
[1173]97    D3DPRESENT_PARAMETERS d3dpp;
98    memset(&d3dpp, 0, sizeof(d3dpp));
99
[1208]100#   if defined USE_SDL
101    window = g_hwnd;
102    d3dpp.hDeviceWindow = g_hwnd;
103    d3dpp.Windowed = TRUE;
104#   elif defined _XBOX
[1173]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;
[1208]111#   endif
[1173]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
[1208]123    HRESULT hr = VideoData::d3d_ctx->CreateDevice(0, D3DDEVTYPE_HAL, window,
124                                                  D3DCREATE_HARDWARE_VERTEXPROCESSING,
125                                                  &d3dpp, &VideoData::d3d_dev);
126    if (FAILED(hr))
[1173]127    {
128        Log::Error("cannot create D3D device\n");
129        exit(EXIT_FAILURE);
130    }
[1189]131
132    g_d3ddevice = VideoData::d3d_dev;
[1173]133#else
134#   if defined USE_GLEW && !defined __APPLE__
[808]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    }
[1173]142#   endif
[808]143
[105]144    /* Initialise OpenGL */
[755]145    glViewport(0, 0, size.x, size.y);
[784]146    VideoData::saved_viewport = size;
[707]147
[1173]148#   if defined HAVE_GL_2X && !defined __APPLE__
[207]149    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
[1173]150#   endif
[674]151#endif
[1771]152
153    /* Initialise reasonable scene default properties */
154    SetClearColor(vec4(0.1f, 0.2f, 0.3f, 1.0f));
155    SetClearDepth(1.f);
[209]156}
[207]157
[209]158void Video::SetFov(float theta)
159{
[755]160    vec2 size = GetSize();
161    float near = -size.x - size.y;
162    float far = size.x + size.y;
[209]163
[939]164#if defined __ANDROID__
[780]165    size = vec2(640.0f, 480.0f);
[727]166#endif
167
[209]168    /* Set the projection matrix */
169    if (theta < 1e-4f)
170    {
171        /* The easy way: purely orthogonal projection. */
[784]172        VideoData::proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far);
[209]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);
[755]180        float t2 = t1 * size.y / size.y;
181        float dist = size.x / (2.0f * t1);
[209]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
[784]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;
[209]196    }
[105]197}
198
[211]199void Video::SetDepth(bool set)
200{
[1208]201#if defined USE_D3D9 || defined _XBOX
[1179]202#   define STR0(x) #x
203#   define STR(x) STR0(x)
204#   pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetDepth() not implemented")
205#else
[211]206    if (set)
207        glEnable(GL_DEPTH_TEST);
208    else
209        glDisable(GL_DEPTH_TEST);
[1179]210#endif
[211]211}
212
[1199]213void Video::SetClearColor(vec4 color)
214{
[1208]215#if defined USE_D3D9 || defined _XBOX
[1199]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
[1771]224void Video::SetClearDepth(float f)
[105]225{
[1208]226#if defined USE_D3D9 || defined _XBOX
[1771]227    VideoData::clear_depth = f;
[2085]228#elif defined HAVE_GLES_2X
229    glClearDepthf(f);
[1771]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)))
[1236]248        Abort();
[1173]249#else
[1771]250    /* FIXME: is this necessary here? */
251    ivec2 size = GetSize();
[755]252    glViewport(0, 0, size.x, size.y);
[1771]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);
[1173]262#endif
[653]263
[662]264    SetFov(0.0f);
[105]265}
[139]266
[653]267void Video::Destroy()
268{
[784]269    ;
[653]270}
271
[140]272void Video::Capture(uint32_t *buffer)
273{
[1208]274#if defined USE_D3D9 || defined _XBOX
[1173]275    /* TODO */
276#else
[140]277    GLint v[4];
[1173]278#   if defined __CELLOS_LV2__
[758]279    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
280    v[2] = 1920;
281    v[3] = 1080;
[1173]282#   else
[140]283    glGetIntegerv(GL_VIEWPORT, v);
[1173]284#   endif
[140]285    int width = v[2], height = v[3];
286
[1173]287#   if defined HAVE_GL_2X
[140]288    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
[1173]289#   endif
[140]290    glPixelStorei(GL_PACK_ALIGNMENT, 1);
291
[1173]292#   if defined GL_BGRA
[615]293    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
[1173]294#   else
[216]295    glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
[1173]296#   endif
[140]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        }
[1173]305#endif
[140]306}
307
[863]308ivec2 Video::GetSize()
[139]309{
[1208]310#if defined USE_D3D9 || defined _XBOX
[1173]311    return VideoData::saved_viewport;
312#elif 1
[1108]313    /* GetSize() is called too often on the game thread; we cannot rely on
314     * the GL context at this point */
[784]315    return VideoData::saved_viewport;
[758]316#elif defined __CELLOS_LV2__
317    // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
[707]318#else
[139]319    GLint v[4];
320    glGetIntegerv(GL_VIEWPORT, v);
[863]321    return ivec2(v[2], v[3]);
[707]322#endif
[139]323}
324
[686]325} /* namespace lol */
326
Note: See TracBrowser for help on using the repository browser.