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