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