src/video/windows/SDL_windowsopengl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:28:07 -0500
changeset 6138 4c64952a58fb
parent 6044 35448a5ea044
child 6296 b42657486c0d
permissions -rwxr-xr-x
Happy New Year!
slouken@1913
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@1913
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1913
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1913
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1913
    20
*/
slouken@1913
    21
#include "SDL_config.h"
slouken@1913
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_WINDOWS
slouken@6044
    24
slouken@5062
    25
#include "SDL_windowsvideo.h"
slouken@1913
    26
slouken@1913
    27
/* WGL implementation of SDL OpenGL support */
slouken@1913
    28
slouken@1952
    29
#if SDL_VIDEO_OPENGL_WGL
slouken@1913
    30
#include "SDL_opengl.h"
slouken@1913
    31
slouken@1952
    32
#define DEFAULT_OPENGL "OPENGL32.DLL"
slouken@1913
    33
slouken@3100
    34
#ifndef WGL_ARB_create_context
slouken@3100
    35
#define WGL_ARB_create_context
slouken@3100
    36
#define WGL_CONTEXT_MAJOR_VERSION_ARB   0x2091
slouken@3100
    37
#define WGL_CONTEXT_MINOR_VERSION_ARB   0x2092
slouken@3100
    38
#define WGL_CONTEXT_LAYER_PLANE_ARB     0x2093
slouken@3100
    39
#define WGL_CONTEXT_FLAGS_ARB           0x2093
slouken@3100
    40
#define WGL_CONTEXT_DEBUG_BIT_ARB       0x0001
slouken@3100
    41
#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB  0x0002
slouken@3100
    42
#endif
slouken@3100
    43
slouken@3139
    44
typedef HGLRC(APIENTRYP PFNWGLCREATECONTEXTATTRIBSARBPROC) (HDC hDC,
slouken@3139
    45
                                                            HGLRC
slouken@3139
    46
                                                            hShareContext,
slouken@3139
    47
                                                            const int
slouken@3139
    48
                                                            *attribList);
slouken@1913
    49
slouken@1913
    50
int
slouken@1913
    51
WIN_GL_LoadLibrary(_THIS, const char *path)
slouken@1913
    52
{
slouken@1913
    53
    LPTSTR wpath;
slouken@1913
    54
    HANDLE handle;
slouken@1913
    55
slouken@1913
    56
    if (path == NULL) {
slouken@1952
    57
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@1952
    58
    }
slouken@1952
    59
    if (path == NULL) {
slouken@1952
    60
        path = DEFAULT_OPENGL;
slouken@1913
    61
    }
slouken@1913
    62
    wpath = WIN_UTF8ToString(path);
slouken@3057
    63
    _this->gl_config.dll_handle = LoadLibrary(wpath);
slouken@1913
    64
    SDL_free(wpath);
slouken@3057
    65
    if (!_this->gl_config.dll_handle) {
slouken@1913
    66
        char message[1024];
slouken@1913
    67
        SDL_snprintf(message, SDL_arraysize(message), "LoadLibrary(\"%s\")",
slouken@1913
    68
                     path);
slouken@1913
    69
        WIN_SetError(message);
slouken@1913
    70
        return -1;
slouken@1913
    71
    }
slouken@3057
    72
    SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@3057
    73
                SDL_arraysize(_this->gl_config.driver_path));
slouken@3057
    74
slouken@3057
    75
    /* Allocate OpenGL memory */
slouken@3057
    76
    _this->gl_data =
slouken@3057
    77
        (struct SDL_GLDriverData *) SDL_calloc(1,
slouken@3057
    78
                                               sizeof(struct
slouken@3057
    79
                                                      SDL_GLDriverData));
slouken@3057
    80
    if (!_this->gl_data) {
slouken@3057
    81
        SDL_OutOfMemory();
slouken@3057
    82
        return -1;
slouken@3057
    83
    }
slouken@1913
    84
slouken@1913
    85
    /* Load function pointers */
slouken@3057
    86
    handle = _this->gl_config.dll_handle;
slouken@1913
    87
    _this->gl_data->wglGetProcAddress = (void *(WINAPI *) (const char *))
slouken@1913
    88
        GetProcAddress(handle, "wglGetProcAddress");
slouken@1913
    89
    _this->gl_data->wglCreateContext = (HGLRC(WINAPI *) (HDC))
slouken@1913
    90
        GetProcAddress(handle, "wglCreateContext");
slouken@1913
    91
    _this->gl_data->wglDeleteContext = (BOOL(WINAPI *) (HGLRC))
slouken@1913
    92
        GetProcAddress(handle, "wglDeleteContext");
slouken@1913
    93
    _this->gl_data->wglMakeCurrent = (BOOL(WINAPI *) (HDC, HGLRC))
slouken@1913
    94
        GetProcAddress(handle, "wglMakeCurrent");
slouken@1913
    95
    _this->gl_data->wglSwapIntervalEXT = (void (WINAPI *) (int))
slouken@1913
    96
        GetProcAddress(handle, "wglSwapIntervalEXT");
slouken@1913
    97
    _this->gl_data->wglGetSwapIntervalEXT = (int (WINAPI *) (void))
slouken@1913
    98
        GetProcAddress(handle, "wglGetSwapIntervalEXT");
slouken@1913
    99
slouken@1913
   100
    if (!_this->gl_data->wglGetProcAddress ||
slouken@1913
   101
        !_this->gl_data->wglCreateContext ||
slouken@1913
   102
        !_this->gl_data->wglDeleteContext ||
slouken@1913
   103
        !_this->gl_data->wglMakeCurrent) {
slouken@1913
   104
        SDL_SetError("Could not retrieve OpenGL functions");
slouken@5090
   105
        SDL_UnloadObject(handle);
slouken@1913
   106
        return -1;
slouken@1913
   107
    }
slouken@1913
   108
slouken@1913
   109
    return 0;
slouken@1913
   110
}
slouken@1913
   111
slouken@1913
   112
void *
slouken@1913
   113
WIN_GL_GetProcAddress(_THIS, const char *proc)
slouken@1913
   114
{
slouken@1913
   115
    void *func;
slouken@1913
   116
slouken@1913
   117
    /* This is to pick up extensions */
slouken@1913
   118
    func = _this->gl_data->wglGetProcAddress(proc);
slouken@1913
   119
    if (!func) {
slouken@1913
   120
        /* This is probably a normal GL function */
slouken@1913
   121
        func = GetProcAddress(_this->gl_config.dll_handle, proc);
slouken@1913
   122
    }
slouken@1913
   123
    return func;
slouken@1913
   124
}
slouken@1913
   125
slouken@3057
   126
void
slouken@1913
   127
WIN_GL_UnloadLibrary(_THIS)
slouken@1913
   128
{
slouken@3057
   129
    FreeLibrary((HMODULE) _this->gl_config.dll_handle);
slouken@3057
   130
    _this->gl_config.dll_handle = NULL;
slouken@3057
   131
slouken@3057
   132
    /* Free OpenGL memory */
slouken@3057
   133
    SDL_free(_this->gl_data);
slouken@3057
   134
    _this->gl_data = NULL;
slouken@1913
   135
}
slouken@1913
   136
slouken@1913
   137
static void
slouken@1913
   138
WIN_GL_SetupPixelFormat(_THIS, PIXELFORMATDESCRIPTOR * pfd)
slouken@1913
   139
{
slouken@1913
   140
    SDL_zerop(pfd);
slouken@1913
   141
    pfd->nSize = sizeof(*pfd);
slouken@1913
   142
    pfd->nVersion = 1;
slouken@1913
   143
    pfd->dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL);
slouken@1913
   144
    if (_this->gl_config.double_buffer) {
slouken@1913
   145
        pfd->dwFlags |= PFD_DOUBLEBUFFER;
slouken@1913
   146
    }
slouken@1913
   147
    if (_this->gl_config.stereo) {
slouken@1913
   148
        pfd->dwFlags |= PFD_STEREO;
slouken@1913
   149
    }
slouken@1913
   150
    pfd->iLayerType = PFD_MAIN_PLANE;
slouken@1913
   151
    pfd->iPixelType = PFD_TYPE_RGBA;
slouken@1913
   152
    pfd->cRedBits = _this->gl_config.red_size;
slouken@1913
   153
    pfd->cGreenBits = _this->gl_config.green_size;
slouken@1913
   154
    pfd->cBlueBits = _this->gl_config.blue_size;
slouken@1913
   155
    pfd->cAlphaBits = _this->gl_config.alpha_size;
slouken@1913
   156
    if (_this->gl_config.buffer_size) {
slouken@1913
   157
        pfd->cColorBits =
slouken@1913
   158
            _this->gl_config.buffer_size - _this->gl_config.alpha_size;
slouken@1913
   159
    } else {
slouken@1913
   160
        pfd->cColorBits = (pfd->cRedBits + pfd->cGreenBits + pfd->cBlueBits);
slouken@1913
   161
    }
slouken@1913
   162
    pfd->cAccumRedBits = _this->gl_config.accum_red_size;
slouken@1913
   163
    pfd->cAccumGreenBits = _this->gl_config.accum_green_size;
slouken@1913
   164
    pfd->cAccumBlueBits = _this->gl_config.accum_blue_size;
slouken@1913
   165
    pfd->cAccumAlphaBits = _this->gl_config.accum_alpha_size;
slouken@1913
   166
    pfd->cAccumBits =
slouken@1913
   167
        (pfd->cAccumRedBits + pfd->cAccumGreenBits + pfd->cAccumBlueBits +
slouken@1913
   168
         pfd->cAccumAlphaBits);
slouken@1913
   169
    pfd->cDepthBits = _this->gl_config.depth_size;
slouken@1913
   170
    pfd->cStencilBits = _this->gl_config.stencil_size;
slouken@1913
   171
}
slouken@1913
   172
slouken@2150
   173
/* Choose the closest pixel format that meets or exceeds the target.
slouken@2150
   174
   FIXME: Should we weight any particular attribute over any other?
slouken@2150
   175
*/
slouken@2150
   176
static int
slouken@2150
   177
WIN_GL_ChoosePixelFormat(HDC hdc, PIXELFORMATDESCRIPTOR * target)
slouken@2150
   178
{
slouken@2150
   179
    PIXELFORMATDESCRIPTOR pfd;
slouken@2150
   180
    int count, index, best = 0;
slouken@2150
   181
    unsigned int dist, best_dist = ~0U;
slouken@2150
   182
slouken@2150
   183
    count = DescribePixelFormat(hdc, 1, sizeof(pfd), NULL);
slouken@2150
   184
slouken@2150
   185
    for (index = 1; index <= count; index++) {
slouken@2150
   186
slouken@2150
   187
        if (!DescribePixelFormat(hdc, index, sizeof(pfd), &pfd)) {
slouken@2150
   188
            continue;
slouken@2150
   189
        }
slouken@2150
   190
slouken@2150
   191
        if ((pfd.dwFlags & target->dwFlags) != target->dwFlags) {
slouken@2150
   192
            continue;
slouken@2150
   193
        }
slouken@2150
   194
slouken@2150
   195
        if (pfd.iLayerType != target->iLayerType) {
slouken@2150
   196
            continue;
slouken@2150
   197
        }
slouken@2150
   198
        if (pfd.iPixelType != target->iPixelType) {
slouken@2150
   199
            continue;
slouken@2150
   200
        }
slouken@2150
   201
slouken@2150
   202
        dist = 0;
slouken@2150
   203
slouken@2150
   204
        if (pfd.cColorBits < target->cColorBits) {
slouken@2150
   205
            continue;
slouken@2150
   206
        } else {
slouken@2150
   207
            dist += (pfd.cColorBits - target->cColorBits);
slouken@2150
   208
        }
slouken@2150
   209
        if (pfd.cRedBits < target->cRedBits) {
slouken@2150
   210
            continue;
slouken@2150
   211
        } else {
slouken@2150
   212
            dist += (pfd.cRedBits - target->cRedBits);
slouken@2150
   213
        }
slouken@2150
   214
        if (pfd.cGreenBits < target->cGreenBits) {
slouken@2150
   215
            continue;
slouken@2150
   216
        } else {
slouken@2150
   217
            dist += (pfd.cGreenBits - target->cGreenBits);
slouken@2150
   218
        }
slouken@2150
   219
        if (pfd.cBlueBits < target->cBlueBits) {
slouken@2150
   220
            continue;
slouken@2150
   221
        } else {
slouken@2150
   222
            dist += (pfd.cBlueBits - target->cBlueBits);
slouken@2150
   223
        }
slouken@2150
   224
        if (pfd.cAlphaBits < target->cAlphaBits) {
slouken@2150
   225
            continue;
slouken@2150
   226
        } else {
slouken@2150
   227
            dist += (pfd.cAlphaBits - target->cAlphaBits);
slouken@2150
   228
        }
slouken@2150
   229
        if (pfd.cAccumBits < target->cAccumBits) {
slouken@2150
   230
            continue;
slouken@2150
   231
        } else {
slouken@2150
   232
            dist += (pfd.cAccumBits - target->cAccumBits);
slouken@2150
   233
        }
slouken@2150
   234
        if (pfd.cAccumRedBits < target->cAccumRedBits) {
slouken@2150
   235
            continue;
slouken@2150
   236
        } else {
slouken@2150
   237
            dist += (pfd.cAccumRedBits - target->cAccumRedBits);
slouken@2150
   238
        }
slouken@2150
   239
        if (pfd.cAccumGreenBits < target->cAccumGreenBits) {
slouken@2150
   240
            continue;
slouken@2150
   241
        } else {
slouken@2150
   242
            dist += (pfd.cAccumGreenBits - target->cAccumGreenBits);
slouken@2150
   243
        }
slouken@2150
   244
        if (pfd.cAccumBlueBits < target->cAccumBlueBits) {
slouken@2150
   245
            continue;
slouken@2150
   246
        } else {
slouken@2150
   247
            dist += (pfd.cAccumBlueBits - target->cAccumBlueBits);
slouken@2150
   248
        }
slouken@2150
   249
        if (pfd.cAccumAlphaBits < target->cAccumAlphaBits) {
slouken@2150
   250
            continue;
slouken@2150
   251
        } else {
slouken@2150
   252
            dist += (pfd.cAccumAlphaBits - target->cAccumAlphaBits);
slouken@2150
   253
        }
slouken@2150
   254
        if (pfd.cDepthBits < target->cDepthBits) {
slouken@2150
   255
            continue;
slouken@2150
   256
        } else {
slouken@2150
   257
            dist += (pfd.cDepthBits - target->cDepthBits);
slouken@2150
   258
        }
slouken@2150
   259
        if (pfd.cStencilBits < target->cStencilBits) {
slouken@2150
   260
            continue;
slouken@2150
   261
        } else {
slouken@2150
   262
            dist += (pfd.cStencilBits - target->cStencilBits);
slouken@2150
   263
        }
slouken@2150
   264
slouken@2150
   265
        if (dist < best_dist) {
slouken@2150
   266
            best = index;
slouken@2150
   267
            best_dist = dist;
slouken@2150
   268
        }
slouken@2150
   269
    }
slouken@2150
   270
slouken@2150
   271
    return best;
slouken@2150
   272
}
slouken@2150
   273
slouken@1913
   274
static SDL_bool
slouken@1913
   275
HasExtension(const char *extension, const char *extensions)
slouken@1913
   276
{
slouken@1913
   277
    const char *start;
slouken@1913
   278
    const char *where, *terminator;
slouken@1913
   279
slouken@1913
   280
    /* Extension names should not have spaces. */
slouken@1913
   281
    where = SDL_strchr(extension, ' ');
slouken@1913
   282
    if (where || *extension == '\0')
slouken@1913
   283
        return SDL_FALSE;
slouken@1913
   284
slouken@1913
   285
    if (!extensions)
slouken@1913
   286
        return SDL_FALSE;
slouken@1913
   287
slouken@1913
   288
    /* It takes a bit of care to be fool-proof about parsing the
slouken@1913
   289
     * OpenGL extensions string. Don't be fooled by sub-strings,
slouken@1913
   290
     * etc. */
slouken@1913
   291
slouken@1913
   292
    start = extensions;
slouken@1913
   293
slouken@1913
   294
    for (;;) {
slouken@1913
   295
        where = SDL_strstr(start, extension);
slouken@1913
   296
        if (!where)
slouken@1913
   297
            break;
slouken@1913
   298
slouken@1913
   299
        terminator = where + SDL_strlen(extension);
slouken@1913
   300
        if (where == start || *(where - 1) == ' ')
slouken@1913
   301
            if (*terminator == ' ' || *terminator == '\0')
slouken@1913
   302
                return SDL_TRUE;
slouken@1913
   303
slouken@1913
   304
        start = terminator;
slouken@1913
   305
    }
slouken@1913
   306
    return SDL_FALSE;
slouken@1913
   307
}
slouken@1913
   308
slouken@1913
   309
static void
slouken@2178
   310
WIN_GL_InitExtensions(_THIS, HDC hdc)
slouken@1913
   311
{
slouken@1913
   312
    const char *(WINAPI * wglGetExtensionsStringARB) (HDC) = 0;
slouken@1913
   313
    const char *extensions;
slouken@1913
   314
slouken@1913
   315
    wglGetExtensionsStringARB = (const char *(WINAPI *) (HDC))
slouken@1913
   316
        _this->gl_data->wglGetProcAddress("wglGetExtensionsStringARB");
slouken@1913
   317
    if (wglGetExtensionsStringARB) {
slouken@1913
   318
        extensions = wglGetExtensionsStringARB(hdc);
slouken@1913
   319
    } else {
slouken@1913
   320
        extensions = NULL;
slouken@1913
   321
    }
slouken@1913
   322
slouken@1913
   323
    /* Check for WGL_ARB_pixel_format */
slouken@1913
   324
    _this->gl_data->WGL_ARB_pixel_format = 0;
slouken@1913
   325
    if (HasExtension("WGL_ARB_pixel_format", extensions)) {
slouken@1913
   326
        _this->gl_data->wglChoosePixelFormatARB = (BOOL(WINAPI *)
slouken@1913
   327
                                                   (HDC, const int *,
slouken@1913
   328
                                                    const FLOAT *, UINT,
slouken@1913
   329
                                                    int *, UINT *))
slouken@1913
   330
            WIN_GL_GetProcAddress(_this, "wglChoosePixelFormatARB");
slouken@1913
   331
        _this->gl_data->wglGetPixelFormatAttribivARB =
slouken@1913
   332
            (BOOL(WINAPI *) (HDC, int, int, UINT, const int *, int *))
slouken@1913
   333
            WIN_GL_GetProcAddress(_this, "wglGetPixelFormatAttribivARB");
slouken@1913
   334
slouken@1913
   335
        if ((_this->gl_data->wglChoosePixelFormatARB != NULL) &&
slouken@1913
   336
            (_this->gl_data->wglGetPixelFormatAttribivARB != NULL)) {
slouken@1913
   337
            _this->gl_data->WGL_ARB_pixel_format = 1;
slouken@1913
   338
        }
slouken@1913
   339
    }
slouken@1913
   340
slouken@1913
   341
    /* Check for WGL_EXT_swap_control */
slouken@1913
   342
    if (HasExtension("WGL_EXT_swap_control", extensions)) {
slouken@1913
   343
        _this->gl_data->wglSwapIntervalEXT =
slouken@1913
   344
            WIN_GL_GetProcAddress(_this, "wglSwapIntervalEXT");
slouken@1913
   345
        _this->gl_data->wglGetSwapIntervalEXT =
slouken@1913
   346
            WIN_GL_GetProcAddress(_this, "wglGetSwapIntervalEXT");
slouken@2178
   347
    } else {
slouken@2178
   348
        _this->gl_data->wglSwapIntervalEXT = NULL;
slouken@2178
   349
        _this->gl_data->wglGetSwapIntervalEXT = NULL;
slouken@1913
   350
    }
slouken@2178
   351
}
slouken@1913
   352
slouken@2178
   353
static int
slouken@2178
   354
WIN_GL_ChoosePixelFormatARB(_THIS, int *iAttribs, float *fAttribs)
slouken@2178
   355
{
slouken@2178
   356
    HWND hwnd;
slouken@2178
   357
    HDC hdc;
slouken@2180
   358
    PIXELFORMATDESCRIPTOR pfd;
slouken@2178
   359
    HGLRC hglrc;
slouken@2178
   360
    int pixel_format = 0;
slouken@2178
   361
    unsigned int matching;
slouken@2178
   362
slouken@2178
   363
    hwnd =
slouken@2178
   364
        CreateWindow(SDL_Appname, SDL_Appname, (WS_POPUP | WS_DISABLED), 0, 0,
slouken@2178
   365
                     10, 10, NULL, NULL, SDL_Instance, NULL);
slouken@2178
   366
    WIN_PumpEvents(_this);
slouken@2178
   367
slouken@2178
   368
    hdc = GetDC(hwnd);
slouken@2178
   369
slouken@2180
   370
    WIN_GL_SetupPixelFormat(_this, &pfd);
slouken@2180
   371
slouken@2180
   372
    SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd);
slouken@2180
   373
slouken@2178
   374
    hglrc = _this->gl_data->wglCreateContext(hdc);
slouken@1913
   375
    if (hglrc) {
slouken@2178
   376
        _this->gl_data->wglMakeCurrent(hdc, hglrc);
slouken@2178
   377
slouken@2178
   378
        WIN_GL_InitExtensions(_this, hdc);
slouken@2178
   379
slouken@2178
   380
        if (_this->gl_data->WGL_ARB_pixel_format) {
slouken@2178
   381
            _this->gl_data->wglChoosePixelFormatARB(hdc, iAttribs, fAttribs,
slouken@2178
   382
                                                    1, &pixel_format,
slouken@2178
   383
                                                    &matching);
slouken@2178
   384
        }
slouken@2178
   385
slouken@1913
   386
        _this->gl_data->wglMakeCurrent(NULL, NULL);
slouken@1913
   387
        _this->gl_data->wglDeleteContext(hglrc);
slouken@1913
   388
    }
slouken@1913
   389
    ReleaseDC(hwnd, hdc);
slouken@1913
   390
    DestroyWindow(hwnd);
slouken@1913
   391
    WIN_PumpEvents(_this);
slouken@2178
   392
slouken@2178
   393
    return pixel_format;
slouken@1913
   394
}
slouken@1913
   395
slouken@1913
   396
int
slouken@1913
   397
WIN_GL_SetupWindow(_THIS, SDL_Window * window)
slouken@1913
   398
{
slouken@1913
   399
    HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
slouken@1913
   400
    PIXELFORMATDESCRIPTOR pfd;
slouken@1913
   401
    int pixel_format;
slouken@1913
   402
    int iAttribs[64];
slouken@1913
   403
    int *iAttr;
slouken@1913
   404
    float fAttribs[1] = { 0 };
slouken@1913
   405
slouken@1913
   406
    WIN_GL_SetupPixelFormat(_this, &pfd);
slouken@1913
   407
slouken@1913
   408
    /* setup WGL_ARB_pixel_format attribs */
slouken@1913
   409
    iAttr = &iAttribs[0];
slouken@1913
   410
slouken@1913
   411
    *iAttr++ = WGL_DRAW_TO_WINDOW_ARB;
slouken@1913
   412
    *iAttr++ = GL_TRUE;
slouken@1913
   413
    *iAttr++ = WGL_RED_BITS_ARB;
slouken@1913
   414
    *iAttr++ = _this->gl_config.red_size;
slouken@1913
   415
    *iAttr++ = WGL_GREEN_BITS_ARB;
slouken@1913
   416
    *iAttr++ = _this->gl_config.green_size;
slouken@1913
   417
    *iAttr++ = WGL_BLUE_BITS_ARB;
slouken@1913
   418
    *iAttr++ = _this->gl_config.blue_size;
slouken@1913
   419
slouken@1913
   420
    if (_this->gl_config.alpha_size) {
slouken@1913
   421
        *iAttr++ = WGL_ALPHA_BITS_ARB;
slouken@1913
   422
        *iAttr++ = _this->gl_config.alpha_size;
slouken@1913
   423
    }
slouken@1913
   424
slouken@1913
   425
    *iAttr++ = WGL_DOUBLE_BUFFER_ARB;
slouken@1913
   426
    *iAttr++ = _this->gl_config.double_buffer;
slouken@1913
   427
slouken@1913
   428
    *iAttr++ = WGL_DEPTH_BITS_ARB;
slouken@1913
   429
    *iAttr++ = _this->gl_config.depth_size;
slouken@1913
   430
slouken@1913
   431
    if (_this->gl_config.stencil_size) {
slouken@1913
   432
        *iAttr++ = WGL_STENCIL_BITS_ARB;
slouken@1913
   433
        *iAttr++ = _this->gl_config.stencil_size;
slouken@1913
   434
    }
slouken@1913
   435
slouken@1913
   436
    if (_this->gl_config.accum_red_size) {
slouken@1913
   437
        *iAttr++ = WGL_ACCUM_RED_BITS_ARB;
slouken@1913
   438
        *iAttr++ = _this->gl_config.accum_red_size;
slouken@1913
   439
    }
slouken@1913
   440
slouken@1913
   441
    if (_this->gl_config.accum_green_size) {
slouken@1913
   442
        *iAttr++ = WGL_ACCUM_GREEN_BITS_ARB;
slouken@1913
   443
        *iAttr++ = _this->gl_config.accum_green_size;
slouken@1913
   444
    }
slouken@1913
   445
slouken@1913
   446
    if (_this->gl_config.accum_blue_size) {
slouken@1913
   447
        *iAttr++ = WGL_ACCUM_BLUE_BITS_ARB;
slouken@1913
   448
        *iAttr++ = _this->gl_config.accum_blue_size;
slouken@1913
   449
    }
slouken@1913
   450
slouken@1913
   451
    if (_this->gl_config.accum_alpha_size) {
slouken@1913
   452
        *iAttr++ = WGL_ACCUM_ALPHA_BITS_ARB;
slouken@1913
   453
        *iAttr++ = _this->gl_config.accum_alpha_size;
slouken@1913
   454
    }
slouken@1913
   455
slouken@1913
   456
    if (_this->gl_config.stereo) {
slouken@1913
   457
        *iAttr++ = WGL_STEREO_ARB;
slouken@1913
   458
        *iAttr++ = GL_TRUE;
slouken@1913
   459
    }
slouken@1913
   460
slouken@1913
   461
    if (_this->gl_config.multisamplebuffers) {
slouken@1913
   462
        *iAttr++ = WGL_SAMPLE_BUFFERS_ARB;
slouken@1913
   463
        *iAttr++ = _this->gl_config.multisamplebuffers;
slouken@1913
   464
    }
slouken@1913
   465
slouken@1913
   466
    if (_this->gl_config.multisamplesamples) {
slouken@1913
   467
        *iAttr++ = WGL_SAMPLES_ARB;
slouken@1913
   468
        *iAttr++ = _this->gl_config.multisamplesamples;
slouken@1913
   469
    }
slouken@1913
   470
icculus@5644
   471
    if (_this->gl_config.accelerated >= 0) {
icculus@5624
   472
        *iAttr++ = WGL_ACCELERATION_ARB;
icculus@5624
   473
        *iAttr++ = (_this->gl_config.accelerated ? WGL_FULL_ACCELERATION_ARB :
icculus@5624
   474
                                                   WGL_NO_ACCELERATION_ARB);
icculus@5624
   475
    }
slouken@1913
   476
slouken@1913
   477
    *iAttr = 0;
slouken@1913
   478
slouken@1913
   479
    /* Choose and set the closest available pixel format */
slouken@2178
   480
    pixel_format = WIN_GL_ChoosePixelFormatARB(_this, iAttribs, fAttribs);
slouken@2178
   481
    if (!pixel_format) {
slouken@2150
   482
        pixel_format = WIN_GL_ChoosePixelFormat(hdc, &pfd);
slouken@1913
   483
    }
slouken@1913
   484
    if (!pixel_format) {
slouken@1913
   485
        SDL_SetError("No matching GL pixel format available");
slouken@1913
   486
        return -1;
slouken@1913
   487
    }
slouken@1913
   488
    if (!SetPixelFormat(hdc, pixel_format, &pfd)) {
slouken@1913
   489
        WIN_SetError("SetPixelFormat()");
slouken@1913
   490
        return (-1);
slouken@1913
   491
    }
slouken@1913
   492
    return 0;
slouken@1913
   493
}
slouken@1913
   494
slouken@1913
   495
SDL_GLContext
slouken@1913
   496
WIN_GL_CreateContext(_THIS, SDL_Window * window)
slouken@1913
   497
{
slouken@1913
   498
    HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
slouken@2178
   499
    HGLRC context;
slouken@1913
   500
slouken@3100
   501
    if (_this->gl_config.major_version < 3) {
slouken@3100
   502
        context = _this->gl_data->wglCreateContext(hdc);
slouken@3100
   503
    } else {
slouken@3100
   504
        PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
slouken@3100
   505
        HGLRC temp_context = _this->gl_data->wglCreateContext(hdc);
slouken@3100
   506
        if (!temp_context) {
slouken@3139
   507
            SDL_SetError("Could not create GL context");
slouken@3100
   508
            return NULL;
slouken@3100
   509
        }
slouken@3139
   510
slouken@3100
   511
        /* Make the context current */
slouken@3100
   512
        if (WIN_GL_MakeCurrent(_this, window, temp_context) < 0) {
slouken@3100
   513
            WIN_GL_DeleteContext(_this, temp_context);
slouken@3100
   514
            return NULL;
slouken@3100
   515
        }
slouken@3139
   516
slouken@3139
   517
        wglCreateContextAttribsARB =
slouken@3139
   518
            (PFNWGLCREATECONTEXTATTRIBSARBPROC) _this->gl_data->
slouken@3139
   519
            wglGetProcAddress("wglCreateContextAttribsARB");
slouken@3100
   520
        if (!wglCreateContextAttribsARB) {
slouken@3100
   521
            SDL_SetError("GL 3.x is not supported");
slouken@3100
   522
            context = temp_context;
slouken@3100
   523
        } else {
slouken@3100
   524
            int attribs[] = {
slouken@3100
   525
                WGL_CONTEXT_MAJOR_VERSION_ARB, _this->gl_config.major_version,
slouken@3100
   526
                WGL_CONTEXT_MINOR_VERSION_ARB, _this->gl_config.minor_version,
slouken@3139
   527
                0
slouken@3100
   528
            };
slouken@3100
   529
            /* Create the GL 3.x context */
slouken@3100
   530
            context = wglCreateContextAttribsARB(hdc, 0, attribs);
slouken@3100
   531
            /* Delete the GL 2.x context */
slouken@3105
   532
            _this->gl_data->wglDeleteContext(temp_context);
slouken@3100
   533
        }
slouken@3100
   534
    }
slouken@3139
   535
slouken@2178
   536
    if (!context) {
slouken@3565
   537
        WIN_SetError("Could not create GL context");
slouken@2178
   538
        return NULL;
slouken@2178
   539
    }
slouken@2178
   540
slouken@2178
   541
    if (WIN_GL_MakeCurrent(_this, window, context) < 0) {
slouken@2178
   542
        WIN_GL_DeleteContext(_this, context);
slouken@2178
   543
        return NULL;
slouken@2178
   544
    }
slouken@2178
   545
slouken@2178
   546
    WIN_GL_InitExtensions(_this, hdc);
slouken@2178
   547
slouken@2178
   548
    return context;
slouken@1913
   549
}
slouken@1913
   550
slouken@1913
   551
int
slouken@1913
   552
WIN_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@1913
   553
{
slouken@1913
   554
    HDC hdc;
slouken@1913
   555
    int status;
slouken@1913
   556
slouken@1913
   557
    if (window) {
slouken@1913
   558
        hdc = ((SDL_WindowData *) window->driverdata)->hdc;
slouken@1913
   559
    } else {
slouken@1913
   560
        hdc = NULL;
slouken@1913
   561
    }
slouken@1913
   562
    if (!_this->gl_data->wglMakeCurrent(hdc, (HGLRC) context)) {
slouken@1913
   563
        WIN_SetError("wglMakeCurrent()");
slouken@1913
   564
        status = -1;
slouken@1913
   565
    } else {
slouken@1913
   566
        status = 0;
slouken@1913
   567
    }
slouken@1913
   568
    return status;
slouken@1913
   569
}
slouken@1913
   570
slouken@1913
   571
int
slouken@1913
   572
WIN_GL_SetSwapInterval(_THIS, int interval)
slouken@1913
   573
{
slouken@1913
   574
    if (_this->gl_data->wglSwapIntervalEXT) {
slouken@1913
   575
        _this->gl_data->wglSwapIntervalEXT(interval);
slouken@1913
   576
        return 0;
slouken@1913
   577
    } else {
slouken@1913
   578
        SDL_Unsupported();
slouken@1913
   579
        return -1;
slouken@1913
   580
    }
slouken@1913
   581
}
slouken@1913
   582
slouken@1913
   583
int
slouken@1913
   584
WIN_GL_GetSwapInterval(_THIS)
slouken@1913
   585
{
slouken@1913
   586
    if (_this->gl_data->wglGetSwapIntervalEXT) {
slouken@1913
   587
        return _this->gl_data->wglGetSwapIntervalEXT();
slouken@1913
   588
    } else {
slouken@1913
   589
        SDL_Unsupported();
slouken@1913
   590
        return -1;
slouken@1913
   591
    }
slouken@1913
   592
}
slouken@1913
   593
slouken@1913
   594
void
slouken@1913
   595
WIN_GL_SwapWindow(_THIS, SDL_Window * window)
slouken@1913
   596
{
slouken@1913
   597
    HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
slouken@1913
   598
slouken@1913
   599
    SwapBuffers(hdc);
slouken@1913
   600
}
slouken@1913
   601
slouken@1913
   602
void
slouken@1913
   603
WIN_GL_DeleteContext(_THIS, SDL_GLContext context)
slouken@1913
   604
{
slouken@1936
   605
    _this->gl_data->wglDeleteContext((HGLRC) context);
slouken@1913
   606
}
slouken@1913
   607
slouken@1952
   608
#endif /* SDL_VIDEO_OPENGL_WGL */
slouken@1913
   609
slouken@6044
   610
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
   611
slouken@1913
   612
/* vi: set ts=4 sw=4 expandtab: */