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