src/video/windows/SDL_windowsopengl.c
changeset 5062 e8916fe9cfc8
parent 3697 f7b03b6838cb
child 5090 327f181542f1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/windows/SDL_windowsopengl.c	Thu Jan 20 18:04:05 2011 -0800
     1.3 @@ -0,0 +1,612 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#include "SDL_windowsvideo.h"
    1.28 +
    1.29 +/* WGL implementation of SDL OpenGL support */
    1.30 +
    1.31 +#if SDL_VIDEO_OPENGL_WGL
    1.32 +#include "SDL_opengl.h"
    1.33 +
    1.34 +#define DEFAULT_OPENGL "OPENGL32.DLL"
    1.35 +
    1.36 +#ifndef WGL_ARB_create_context
    1.37 +#define WGL_ARB_create_context
    1.38 +#define WGL_CONTEXT_MAJOR_VERSION_ARB   0x2091
    1.39 +#define WGL_CONTEXT_MINOR_VERSION_ARB   0x2092
    1.40 +#define WGL_CONTEXT_LAYER_PLANE_ARB     0x2093
    1.41 +#define WGL_CONTEXT_FLAGS_ARB           0x2093
    1.42 +#define WGL_CONTEXT_DEBUG_BIT_ARB       0x0001
    1.43 +#define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB  0x0002
    1.44 +#endif
    1.45 +
    1.46 +typedef HGLRC(APIENTRYP PFNWGLCREATECONTEXTATTRIBSARBPROC) (HDC hDC,
    1.47 +                                                            HGLRC
    1.48 +                                                            hShareContext,
    1.49 +                                                            const int
    1.50 +                                                            *attribList);
    1.51 +
    1.52 +int
    1.53 +WIN_GL_LoadLibrary(_THIS, const char *path)
    1.54 +{
    1.55 +    LPTSTR wpath;
    1.56 +    HANDLE handle;
    1.57 +
    1.58 +    if (path == NULL) {
    1.59 +        path = SDL_getenv("SDL_OPENGL_LIBRARY");
    1.60 +    }
    1.61 +    if (path == NULL) {
    1.62 +        path = DEFAULT_OPENGL;
    1.63 +    }
    1.64 +    wpath = WIN_UTF8ToString(path);
    1.65 +    _this->gl_config.dll_handle = LoadLibrary(wpath);
    1.66 +    SDL_free(wpath);
    1.67 +    if (!_this->gl_config.dll_handle) {
    1.68 +        char message[1024];
    1.69 +        SDL_snprintf(message, SDL_arraysize(message), "LoadLibrary(\"%s\")",
    1.70 +                     path);
    1.71 +        WIN_SetError(message);
    1.72 +        return -1;
    1.73 +    }
    1.74 +    SDL_strlcpy(_this->gl_config.driver_path, path,
    1.75 +                SDL_arraysize(_this->gl_config.driver_path));
    1.76 +
    1.77 +    /* Allocate OpenGL memory */
    1.78 +    _this->gl_data =
    1.79 +        (struct SDL_GLDriverData *) SDL_calloc(1,
    1.80 +                                               sizeof(struct
    1.81 +                                                      SDL_GLDriverData));
    1.82 +    if (!_this->gl_data) {
    1.83 +        SDL_OutOfMemory();
    1.84 +        return -1;
    1.85 +    }
    1.86 +
    1.87 +    /* Load function pointers */
    1.88 +    handle = _this->gl_config.dll_handle;
    1.89 +    _this->gl_data->wglGetProcAddress = (void *(WINAPI *) (const char *))
    1.90 +        GetProcAddress(handle, "wglGetProcAddress");
    1.91 +    _this->gl_data->wglCreateContext = (HGLRC(WINAPI *) (HDC))
    1.92 +        GetProcAddress(handle, "wglCreateContext");
    1.93 +    _this->gl_data->wglDeleteContext = (BOOL(WINAPI *) (HGLRC))
    1.94 +        GetProcAddress(handle, "wglDeleteContext");
    1.95 +    _this->gl_data->wglMakeCurrent = (BOOL(WINAPI *) (HDC, HGLRC))
    1.96 +        GetProcAddress(handle, "wglMakeCurrent");
    1.97 +    _this->gl_data->wglSwapIntervalEXT = (void (WINAPI *) (int))
    1.98 +        GetProcAddress(handle, "wglSwapIntervalEXT");
    1.99 +    _this->gl_data->wglGetSwapIntervalEXT = (int (WINAPI *) (void))
   1.100 +        GetProcAddress(handle, "wglGetSwapIntervalEXT");
   1.101 +
   1.102 +    if (!_this->gl_data->wglGetProcAddress ||
   1.103 +        !_this->gl_data->wglCreateContext ||
   1.104 +        !_this->gl_data->wglDeleteContext ||
   1.105 +        !_this->gl_data->wglMakeCurrent) {
   1.106 +        SDL_SetError("Could not retrieve OpenGL functions");
   1.107 +        FreeLibrary(handle);
   1.108 +        return -1;
   1.109 +    }
   1.110 +
   1.111 +    return 0;
   1.112 +}
   1.113 +
   1.114 +void *
   1.115 +WIN_GL_GetProcAddress(_THIS, const char *proc)
   1.116 +{
   1.117 +    void *func;
   1.118 +
   1.119 +    /* This is to pick up extensions */
   1.120 +    func = _this->gl_data->wglGetProcAddress(proc);
   1.121 +    if (!func) {
   1.122 +        /* This is probably a normal GL function */
   1.123 +        func = GetProcAddress(_this->gl_config.dll_handle, proc);
   1.124 +    }
   1.125 +    return func;
   1.126 +}
   1.127 +
   1.128 +void
   1.129 +WIN_GL_UnloadLibrary(_THIS)
   1.130 +{
   1.131 +    FreeLibrary((HMODULE) _this->gl_config.dll_handle);
   1.132 +    _this->gl_config.dll_handle = NULL;
   1.133 +
   1.134 +    /* Free OpenGL memory */
   1.135 +    SDL_free(_this->gl_data);
   1.136 +    _this->gl_data = NULL;
   1.137 +}
   1.138 +
   1.139 +static void
   1.140 +WIN_GL_SetupPixelFormat(_THIS, PIXELFORMATDESCRIPTOR * pfd)
   1.141 +{
   1.142 +    SDL_zerop(pfd);
   1.143 +    pfd->nSize = sizeof(*pfd);
   1.144 +    pfd->nVersion = 1;
   1.145 +    pfd->dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL);
   1.146 +    if (_this->gl_config.double_buffer) {
   1.147 +        pfd->dwFlags |= PFD_DOUBLEBUFFER;
   1.148 +    }
   1.149 +    if (_this->gl_config.stereo) {
   1.150 +        pfd->dwFlags |= PFD_STEREO;
   1.151 +    }
   1.152 +    pfd->iLayerType = PFD_MAIN_PLANE;
   1.153 +    pfd->iPixelType = PFD_TYPE_RGBA;
   1.154 +    pfd->cRedBits = _this->gl_config.red_size;
   1.155 +    pfd->cGreenBits = _this->gl_config.green_size;
   1.156 +    pfd->cBlueBits = _this->gl_config.blue_size;
   1.157 +    pfd->cAlphaBits = _this->gl_config.alpha_size;
   1.158 +    if (_this->gl_config.buffer_size) {
   1.159 +        pfd->cColorBits =
   1.160 +            _this->gl_config.buffer_size - _this->gl_config.alpha_size;
   1.161 +    } else {
   1.162 +        pfd->cColorBits = (pfd->cRedBits + pfd->cGreenBits + pfd->cBlueBits);
   1.163 +    }
   1.164 +    pfd->cAccumRedBits = _this->gl_config.accum_red_size;
   1.165 +    pfd->cAccumGreenBits = _this->gl_config.accum_green_size;
   1.166 +    pfd->cAccumBlueBits = _this->gl_config.accum_blue_size;
   1.167 +    pfd->cAccumAlphaBits = _this->gl_config.accum_alpha_size;
   1.168 +    pfd->cAccumBits =
   1.169 +        (pfd->cAccumRedBits + pfd->cAccumGreenBits + pfd->cAccumBlueBits +
   1.170 +         pfd->cAccumAlphaBits);
   1.171 +    pfd->cDepthBits = _this->gl_config.depth_size;
   1.172 +    pfd->cStencilBits = _this->gl_config.stencil_size;
   1.173 +}
   1.174 +
   1.175 +/* Choose the closest pixel format that meets or exceeds the target.
   1.176 +   FIXME: Should we weight any particular attribute over any other?
   1.177 +*/
   1.178 +static int
   1.179 +WIN_GL_ChoosePixelFormat(HDC hdc, PIXELFORMATDESCRIPTOR * target)
   1.180 +{
   1.181 +    PIXELFORMATDESCRIPTOR pfd;
   1.182 +    int count, index, best = 0;
   1.183 +    unsigned int dist, best_dist = ~0U;
   1.184 +
   1.185 +    count = DescribePixelFormat(hdc, 1, sizeof(pfd), NULL);
   1.186 +
   1.187 +    for (index = 1; index <= count; index++) {
   1.188 +
   1.189 +        if (!DescribePixelFormat(hdc, index, sizeof(pfd), &pfd)) {
   1.190 +            continue;
   1.191 +        }
   1.192 +
   1.193 +        if ((pfd.dwFlags & target->dwFlags) != target->dwFlags) {
   1.194 +            continue;
   1.195 +        }
   1.196 +
   1.197 +        if (pfd.iLayerType != target->iLayerType) {
   1.198 +            continue;
   1.199 +        }
   1.200 +        if (pfd.iPixelType != target->iPixelType) {
   1.201 +            continue;
   1.202 +        }
   1.203 +
   1.204 +        dist = 0;
   1.205 +
   1.206 +        if (pfd.cColorBits < target->cColorBits) {
   1.207 +            continue;
   1.208 +        } else {
   1.209 +            dist += (pfd.cColorBits - target->cColorBits);
   1.210 +        }
   1.211 +        if (pfd.cRedBits < target->cRedBits) {
   1.212 +            continue;
   1.213 +        } else {
   1.214 +            dist += (pfd.cRedBits - target->cRedBits);
   1.215 +        }
   1.216 +        if (pfd.cGreenBits < target->cGreenBits) {
   1.217 +            continue;
   1.218 +        } else {
   1.219 +            dist += (pfd.cGreenBits - target->cGreenBits);
   1.220 +        }
   1.221 +        if (pfd.cBlueBits < target->cBlueBits) {
   1.222 +            continue;
   1.223 +        } else {
   1.224 +            dist += (pfd.cBlueBits - target->cBlueBits);
   1.225 +        }
   1.226 +        if (pfd.cAlphaBits < target->cAlphaBits) {
   1.227 +            continue;
   1.228 +        } else {
   1.229 +            dist += (pfd.cAlphaBits - target->cAlphaBits);
   1.230 +        }
   1.231 +        if (pfd.cAccumBits < target->cAccumBits) {
   1.232 +            continue;
   1.233 +        } else {
   1.234 +            dist += (pfd.cAccumBits - target->cAccumBits);
   1.235 +        }
   1.236 +        if (pfd.cAccumRedBits < target->cAccumRedBits) {
   1.237 +            continue;
   1.238 +        } else {
   1.239 +            dist += (pfd.cAccumRedBits - target->cAccumRedBits);
   1.240 +        }
   1.241 +        if (pfd.cAccumGreenBits < target->cAccumGreenBits) {
   1.242 +            continue;
   1.243 +        } else {
   1.244 +            dist += (pfd.cAccumGreenBits - target->cAccumGreenBits);
   1.245 +        }
   1.246 +        if (pfd.cAccumBlueBits < target->cAccumBlueBits) {
   1.247 +            continue;
   1.248 +        } else {
   1.249 +            dist += (pfd.cAccumBlueBits - target->cAccumBlueBits);
   1.250 +        }
   1.251 +        if (pfd.cAccumAlphaBits < target->cAccumAlphaBits) {
   1.252 +            continue;
   1.253 +        } else {
   1.254 +            dist += (pfd.cAccumAlphaBits - target->cAccumAlphaBits);
   1.255 +        }
   1.256 +        if (pfd.cDepthBits < target->cDepthBits) {
   1.257 +            continue;
   1.258 +        } else {
   1.259 +            dist += (pfd.cDepthBits - target->cDepthBits);
   1.260 +        }
   1.261 +        if (pfd.cStencilBits < target->cStencilBits) {
   1.262 +            continue;
   1.263 +        } else {
   1.264 +            dist += (pfd.cStencilBits - target->cStencilBits);
   1.265 +        }
   1.266 +
   1.267 +        if (dist < best_dist) {
   1.268 +            best = index;
   1.269 +            best_dist = dist;
   1.270 +        }
   1.271 +    }
   1.272 +
   1.273 +    return best;
   1.274 +}
   1.275 +
   1.276 +static SDL_bool
   1.277 +HasExtension(const char *extension, const char *extensions)
   1.278 +{
   1.279 +    const char *start;
   1.280 +    const char *where, *terminator;
   1.281 +
   1.282 +    /* Extension names should not have spaces. */
   1.283 +    where = SDL_strchr(extension, ' ');
   1.284 +    if (where || *extension == '\0')
   1.285 +        return SDL_FALSE;
   1.286 +
   1.287 +    if (!extensions)
   1.288 +        return SDL_FALSE;
   1.289 +
   1.290 +    /* It takes a bit of care to be fool-proof about parsing the
   1.291 +     * OpenGL extensions string. Don't be fooled by sub-strings,
   1.292 +     * etc. */
   1.293 +
   1.294 +    start = extensions;
   1.295 +
   1.296 +    for (;;) {
   1.297 +        where = SDL_strstr(start, extension);
   1.298 +        if (!where)
   1.299 +            break;
   1.300 +
   1.301 +        terminator = where + SDL_strlen(extension);
   1.302 +        if (where == start || *(where - 1) == ' ')
   1.303 +            if (*terminator == ' ' || *terminator == '\0')
   1.304 +                return SDL_TRUE;
   1.305 +
   1.306 +        start = terminator;
   1.307 +    }
   1.308 +    return SDL_FALSE;
   1.309 +}
   1.310 +
   1.311 +static void
   1.312 +WIN_GL_InitExtensions(_THIS, HDC hdc)
   1.313 +{
   1.314 +    const char *(WINAPI * wglGetExtensionsStringARB) (HDC) = 0;
   1.315 +    const char *extensions;
   1.316 +
   1.317 +    wglGetExtensionsStringARB = (const char *(WINAPI *) (HDC))
   1.318 +        _this->gl_data->wglGetProcAddress("wglGetExtensionsStringARB");
   1.319 +    if (wglGetExtensionsStringARB) {
   1.320 +        extensions = wglGetExtensionsStringARB(hdc);
   1.321 +    } else {
   1.322 +        extensions = NULL;
   1.323 +    }
   1.324 +
   1.325 +    /* Check for WGL_ARB_pixel_format */
   1.326 +    _this->gl_data->WGL_ARB_pixel_format = 0;
   1.327 +    if (HasExtension("WGL_ARB_pixel_format", extensions)) {
   1.328 +        _this->gl_data->wglChoosePixelFormatARB = (BOOL(WINAPI *)
   1.329 +                                                   (HDC, const int *,
   1.330 +                                                    const FLOAT *, UINT,
   1.331 +                                                    int *, UINT *))
   1.332 +            WIN_GL_GetProcAddress(_this, "wglChoosePixelFormatARB");
   1.333 +        _this->gl_data->wglGetPixelFormatAttribivARB =
   1.334 +            (BOOL(WINAPI *) (HDC, int, int, UINT, const int *, int *))
   1.335 +            WIN_GL_GetProcAddress(_this, "wglGetPixelFormatAttribivARB");
   1.336 +
   1.337 +        if ((_this->gl_data->wglChoosePixelFormatARB != NULL) &&
   1.338 +            (_this->gl_data->wglGetPixelFormatAttribivARB != NULL)) {
   1.339 +            _this->gl_data->WGL_ARB_pixel_format = 1;
   1.340 +        }
   1.341 +    }
   1.342 +
   1.343 +    /* Check for WGL_EXT_swap_control */
   1.344 +    if (HasExtension("WGL_EXT_swap_control", extensions)) {
   1.345 +        _this->gl_data->wglSwapIntervalEXT =
   1.346 +            WIN_GL_GetProcAddress(_this, "wglSwapIntervalEXT");
   1.347 +        _this->gl_data->wglGetSwapIntervalEXT =
   1.348 +            WIN_GL_GetProcAddress(_this, "wglGetSwapIntervalEXT");
   1.349 +    } else {
   1.350 +        _this->gl_data->wglSwapIntervalEXT = NULL;
   1.351 +        _this->gl_data->wglGetSwapIntervalEXT = NULL;
   1.352 +    }
   1.353 +}
   1.354 +
   1.355 +static int
   1.356 +WIN_GL_ChoosePixelFormatARB(_THIS, int *iAttribs, float *fAttribs)
   1.357 +{
   1.358 +    HWND hwnd;
   1.359 +    HDC hdc;
   1.360 +    PIXELFORMATDESCRIPTOR pfd;
   1.361 +    HGLRC hglrc;
   1.362 +    int pixel_format = 0;
   1.363 +    unsigned int matching;
   1.364 +
   1.365 +    hwnd =
   1.366 +        CreateWindow(SDL_Appname, SDL_Appname, (WS_POPUP | WS_DISABLED), 0, 0,
   1.367 +                     10, 10, NULL, NULL, SDL_Instance, NULL);
   1.368 +    WIN_PumpEvents(_this);
   1.369 +
   1.370 +    hdc = GetDC(hwnd);
   1.371 +
   1.372 +    WIN_GL_SetupPixelFormat(_this, &pfd);
   1.373 +
   1.374 +    SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd);
   1.375 +
   1.376 +    hglrc = _this->gl_data->wglCreateContext(hdc);
   1.377 +    if (hglrc) {
   1.378 +        _this->gl_data->wglMakeCurrent(hdc, hglrc);
   1.379 +
   1.380 +        WIN_GL_InitExtensions(_this, hdc);
   1.381 +
   1.382 +        if (_this->gl_data->WGL_ARB_pixel_format) {
   1.383 +            _this->gl_data->wglChoosePixelFormatARB(hdc, iAttribs, fAttribs,
   1.384 +                                                    1, &pixel_format,
   1.385 +                                                    &matching);
   1.386 +        }
   1.387 +
   1.388 +        _this->gl_data->wglMakeCurrent(NULL, NULL);
   1.389 +        _this->gl_data->wglDeleteContext(hglrc);
   1.390 +    }
   1.391 +    ReleaseDC(hwnd, hdc);
   1.392 +    DestroyWindow(hwnd);
   1.393 +    WIN_PumpEvents(_this);
   1.394 +
   1.395 +    return pixel_format;
   1.396 +}
   1.397 +
   1.398 +int
   1.399 +WIN_GL_SetupWindow(_THIS, SDL_Window * window)
   1.400 +{
   1.401 +    HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
   1.402 +    PIXELFORMATDESCRIPTOR pfd;
   1.403 +    int pixel_format;
   1.404 +    int iAttribs[64];
   1.405 +    int *iAttr;
   1.406 +    float fAttribs[1] = { 0 };
   1.407 +
   1.408 +    WIN_GL_SetupPixelFormat(_this, &pfd);
   1.409 +
   1.410 +    /* setup WGL_ARB_pixel_format attribs */
   1.411 +    iAttr = &iAttribs[0];
   1.412 +
   1.413 +    *iAttr++ = WGL_DRAW_TO_WINDOW_ARB;
   1.414 +    *iAttr++ = GL_TRUE;
   1.415 +    *iAttr++ = WGL_ACCELERATION_ARB;
   1.416 +    *iAttr++ = WGL_FULL_ACCELERATION_ARB;
   1.417 +    *iAttr++ = WGL_RED_BITS_ARB;
   1.418 +    *iAttr++ = _this->gl_config.red_size;
   1.419 +    *iAttr++ = WGL_GREEN_BITS_ARB;
   1.420 +    *iAttr++ = _this->gl_config.green_size;
   1.421 +    *iAttr++ = WGL_BLUE_BITS_ARB;
   1.422 +    *iAttr++ = _this->gl_config.blue_size;
   1.423 +
   1.424 +    if (_this->gl_config.alpha_size) {
   1.425 +        *iAttr++ = WGL_ALPHA_BITS_ARB;
   1.426 +        *iAttr++ = _this->gl_config.alpha_size;
   1.427 +    }
   1.428 +
   1.429 +    *iAttr++ = WGL_DOUBLE_BUFFER_ARB;
   1.430 +    *iAttr++ = _this->gl_config.double_buffer;
   1.431 +
   1.432 +    *iAttr++ = WGL_DEPTH_BITS_ARB;
   1.433 +    *iAttr++ = _this->gl_config.depth_size;
   1.434 +
   1.435 +    if (_this->gl_config.stencil_size) {
   1.436 +        *iAttr++ = WGL_STENCIL_BITS_ARB;
   1.437 +        *iAttr++ = _this->gl_config.stencil_size;
   1.438 +    }
   1.439 +
   1.440 +    if (_this->gl_config.accum_red_size) {
   1.441 +        *iAttr++ = WGL_ACCUM_RED_BITS_ARB;
   1.442 +        *iAttr++ = _this->gl_config.accum_red_size;
   1.443 +    }
   1.444 +
   1.445 +    if (_this->gl_config.accum_green_size) {
   1.446 +        *iAttr++ = WGL_ACCUM_GREEN_BITS_ARB;
   1.447 +        *iAttr++ = _this->gl_config.accum_green_size;
   1.448 +    }
   1.449 +
   1.450 +    if (_this->gl_config.accum_blue_size) {
   1.451 +        *iAttr++ = WGL_ACCUM_BLUE_BITS_ARB;
   1.452 +        *iAttr++ = _this->gl_config.accum_blue_size;
   1.453 +    }
   1.454 +
   1.455 +    if (_this->gl_config.accum_alpha_size) {
   1.456 +        *iAttr++ = WGL_ACCUM_ALPHA_BITS_ARB;
   1.457 +        *iAttr++ = _this->gl_config.accum_alpha_size;
   1.458 +    }
   1.459 +
   1.460 +    if (_this->gl_config.stereo) {
   1.461 +        *iAttr++ = WGL_STEREO_ARB;
   1.462 +        *iAttr++ = GL_TRUE;
   1.463 +    }
   1.464 +
   1.465 +    if (_this->gl_config.multisamplebuffers) {
   1.466 +        *iAttr++ = WGL_SAMPLE_BUFFERS_ARB;
   1.467 +        *iAttr++ = _this->gl_config.multisamplebuffers;
   1.468 +    }
   1.469 +
   1.470 +    if (_this->gl_config.multisamplesamples) {
   1.471 +        *iAttr++ = WGL_SAMPLES_ARB;
   1.472 +        *iAttr++ = _this->gl_config.multisamplesamples;
   1.473 +    }
   1.474 +
   1.475 +    if (_this->gl_config.accelerated >= 0) {
   1.476 +        *iAttr++ = WGL_ACCELERATION_ARB;
   1.477 +        *iAttr++ =
   1.478 +            (_this->gl_config.accelerated ? WGL_GENERIC_ACCELERATION_ARB :
   1.479 +             WGL_NO_ACCELERATION_ARB);
   1.480 +    }
   1.481 +
   1.482 +    *iAttr = 0;
   1.483 +
   1.484 +    /* Choose and set the closest available pixel format */
   1.485 +    pixel_format = WIN_GL_ChoosePixelFormatARB(_this, iAttribs, fAttribs);
   1.486 +    if (!pixel_format) {
   1.487 +        pixel_format = WIN_GL_ChoosePixelFormat(hdc, &pfd);
   1.488 +    }
   1.489 +    if (!pixel_format) {
   1.490 +        SDL_SetError("No matching GL pixel format available");
   1.491 +        return -1;
   1.492 +    }
   1.493 +    if (!SetPixelFormat(hdc, pixel_format, &pfd)) {
   1.494 +        WIN_SetError("SetPixelFormat()");
   1.495 +        return (-1);
   1.496 +    }
   1.497 +    return 0;
   1.498 +}
   1.499 +
   1.500 +SDL_GLContext
   1.501 +WIN_GL_CreateContext(_THIS, SDL_Window * window)
   1.502 +{
   1.503 +    HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
   1.504 +    HGLRC context;
   1.505 +
   1.506 +    if (_this->gl_config.major_version < 3) {
   1.507 +        context = _this->gl_data->wglCreateContext(hdc);
   1.508 +    } else {
   1.509 +        PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
   1.510 +        HGLRC temp_context = _this->gl_data->wglCreateContext(hdc);
   1.511 +        if (!temp_context) {
   1.512 +            SDL_SetError("Could not create GL context");
   1.513 +            return NULL;
   1.514 +        }
   1.515 +
   1.516 +        /* Make the context current */
   1.517 +        if (WIN_GL_MakeCurrent(_this, window, temp_context) < 0) {
   1.518 +            WIN_GL_DeleteContext(_this, temp_context);
   1.519 +            return NULL;
   1.520 +        }
   1.521 +
   1.522 +        wglCreateContextAttribsARB =
   1.523 +            (PFNWGLCREATECONTEXTATTRIBSARBPROC) _this->gl_data->
   1.524 +            wglGetProcAddress("wglCreateContextAttribsARB");
   1.525 +        if (!wglCreateContextAttribsARB) {
   1.526 +            SDL_SetError("GL 3.x is not supported");
   1.527 +            context = temp_context;
   1.528 +        } else {
   1.529 +            int attribs[] = {
   1.530 +                WGL_CONTEXT_MAJOR_VERSION_ARB, _this->gl_config.major_version,
   1.531 +                WGL_CONTEXT_MINOR_VERSION_ARB, _this->gl_config.minor_version,
   1.532 +                0
   1.533 +            };
   1.534 +            /* Create the GL 3.x context */
   1.535 +            context = wglCreateContextAttribsARB(hdc, 0, attribs);
   1.536 +            /* Delete the GL 2.x context */
   1.537 +            _this->gl_data->wglDeleteContext(temp_context);
   1.538 +        }
   1.539 +    }
   1.540 +
   1.541 +    if (!context) {
   1.542 +        WIN_SetError("Could not create GL context");
   1.543 +        return NULL;
   1.544 +    }
   1.545 +
   1.546 +    if (WIN_GL_MakeCurrent(_this, window, context) < 0) {
   1.547 +        WIN_GL_DeleteContext(_this, context);
   1.548 +        return NULL;
   1.549 +    }
   1.550 +
   1.551 +    WIN_GL_InitExtensions(_this, hdc);
   1.552 +
   1.553 +    return context;
   1.554 +}
   1.555 +
   1.556 +int
   1.557 +WIN_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
   1.558 +{
   1.559 +    HDC hdc;
   1.560 +    int status;
   1.561 +
   1.562 +    if (window) {
   1.563 +        hdc = ((SDL_WindowData *) window->driverdata)->hdc;
   1.564 +    } else {
   1.565 +        hdc = NULL;
   1.566 +    }
   1.567 +    if (!_this->gl_data->wglMakeCurrent(hdc, (HGLRC) context)) {
   1.568 +        WIN_SetError("wglMakeCurrent()");
   1.569 +        status = -1;
   1.570 +    } else {
   1.571 +        status = 0;
   1.572 +    }
   1.573 +    return status;
   1.574 +}
   1.575 +
   1.576 +int
   1.577 +WIN_GL_SetSwapInterval(_THIS, int interval)
   1.578 +{
   1.579 +    if (_this->gl_data->wglSwapIntervalEXT) {
   1.580 +        _this->gl_data->wglSwapIntervalEXT(interval);
   1.581 +        return 0;
   1.582 +    } else {
   1.583 +        SDL_Unsupported();
   1.584 +        return -1;
   1.585 +    }
   1.586 +}
   1.587 +
   1.588 +int
   1.589 +WIN_GL_GetSwapInterval(_THIS)
   1.590 +{
   1.591 +    if (_this->gl_data->wglGetSwapIntervalEXT) {
   1.592 +        return _this->gl_data->wglGetSwapIntervalEXT();
   1.593 +    } else {
   1.594 +        SDL_Unsupported();
   1.595 +        return -1;
   1.596 +    }
   1.597 +}
   1.598 +
   1.599 +void
   1.600 +WIN_GL_SwapWindow(_THIS, SDL_Window * window)
   1.601 +{
   1.602 +    HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
   1.603 +
   1.604 +    SwapBuffers(hdc);
   1.605 +}
   1.606 +
   1.607 +void
   1.608 +WIN_GL_DeleteContext(_THIS, SDL_GLContext context)
   1.609 +{
   1.610 +    _this->gl_data->wglDeleteContext((HGLRC) context);
   1.611 +}
   1.612 +
   1.613 +#endif /* SDL_VIDEO_OPENGL_WGL */
   1.614 +
   1.615 +/* vi: set ts=4 sw=4 expandtab: */