Creating a context makes it current, per the documentation.
authorSam Lantinga <slouken@libsdl.org>
Thu, 12 Jul 2007 06:31:36 +0000
changeset 2178114a541cfae2
parent 2177 4f896c20caf6
child 2179 8b76cc268771
Creating a context makes it current, per the documentation.
Applied a variant of the multi-card OpenGL fix from SDL 1.2
src/video/cocoa/SDL_cocoaopengl.m
src/video/win32/SDL_win32opengl.c
src/video/x11/SDL_x11opengl.c
test/testgl2.c
     1.1 --- a/src/video/cocoa/SDL_cocoaopengl.m	Thu Jul 12 05:32:07 2007 +0000
     1.2 +++ b/src/video/cocoa/SDL_cocoaopengl.m	Thu Jul 12 06:31:36 2007 +0000
     1.3 @@ -150,7 +150,7 @@
     1.4      SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
     1.5      NSOpenGLPixelFormatAttribute attr[32];
     1.6      NSOpenGLPixelFormat *fmt;
     1.7 -    NSOpenGLContext *nscontext;
     1.8 +    NSOpenGLContext *context;
     1.9      int i = 0;
    1.10  
    1.11      pool = [[NSAutoreleasePool alloc] init];
    1.12 @@ -212,11 +212,11 @@
    1.13          return NULL;
    1.14      }
    1.15  
    1.16 -    nscontext = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
    1.17 +    context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
    1.18  
    1.19      [fmt release];
    1.20  
    1.21 -    if (nscontext == nil) {
    1.22 +    if (context == nil) {
    1.23          SDL_SetError ("Failed creating OpenGL context");
    1.24          [pool release];
    1.25          return NULL;
    1.26 @@ -240,7 +240,7 @@
    1.27  
    1.28      {
    1.29          long cache_max = 64;
    1.30 -        CGLContextObj ctx = [nscontext CGLContextObj];
    1.31 +        CGLContextObj ctx = [context CGLContextObj];
    1.32          CGLSetParameter (ctx, GLI_SUBMIT_FUNC_CACHE_MAX, &cache_max);
    1.33          CGLSetParameter (ctx, GLI_ARRAY_FUNC_CACHE_MAX, &cache_max);
    1.34      }
    1.35 @@ -248,7 +248,13 @@
    1.36      /* End Wisdom from Apple Engineer section. --ryan. */
    1.37  
    1.38      [pool release];
    1.39 -    return nscontext;
    1.40 +
    1.41 +    if ( Cocoa_GL_MakeCurrent(_this, window, context) < 0 ) {
    1.42 +        Cocoa_GL_DeleteContext(_this, context);
    1.43 +        return NULL;
    1.44 +    }
    1.45 +
    1.46 +    return context;
    1.47  }
    1.48  
    1.49  int
     2.1 --- a/src/video/win32/SDL_win32opengl.c	Thu Jul 12 05:32:07 2007 +0000
     2.2 +++ b/src/video/win32/SDL_win32opengl.c	Thu Jul 12 06:31:36 2007 +0000
     2.3 @@ -292,32 +292,11 @@
     2.4  }
     2.5  
     2.6  static void
     2.7 -WIN_GL_InitExtensions(_THIS)
     2.8 +WIN_GL_InitExtensions(_THIS, HDC hdc)
     2.9  {
    2.10 -    HWND hwnd;
    2.11 -    HDC hdc;
    2.12 -    PIXELFORMATDESCRIPTOR pfd;
    2.13 -    int pixel_format;
    2.14 -    HGLRC hglrc;
    2.15      const char *(WINAPI * wglGetExtensionsStringARB) (HDC) = 0;
    2.16      const char *extensions;
    2.17  
    2.18 -    hwnd =
    2.19 -        CreateWindow(SDL_Appname, SDL_Appname, (WS_POPUP | WS_DISABLED), 0, 0,
    2.20 -                     10, 10, NULL, NULL, SDL_Instance, NULL);
    2.21 -    WIN_PumpEvents(_this);
    2.22 -
    2.23 -    hdc = GetDC(hwnd);
    2.24 -
    2.25 -    WIN_GL_SetupPixelFormat(_this, &pfd);
    2.26 -    pixel_format = ChoosePixelFormat(hdc, &pfd);
    2.27 -    SetPixelFormat(hdc, pixel_format, &pfd);
    2.28 -
    2.29 -    hglrc = _this->gl_data->wglCreateContext(hdc);
    2.30 -    if (hglrc) {
    2.31 -        _this->gl_data->wglMakeCurrent(hdc, hglrc);
    2.32 -    }
    2.33 -
    2.34      wglGetExtensionsStringARB = (const char *(WINAPI *) (HDC))
    2.35          _this->gl_data->wglGetProcAddress("wglGetExtensionsStringARB");
    2.36      if (wglGetExtensionsStringARB) {
    2.37 @@ -350,15 +329,48 @@
    2.38              WIN_GL_GetProcAddress(_this, "wglSwapIntervalEXT");
    2.39          _this->gl_data->wglGetSwapIntervalEXT =
    2.40              WIN_GL_GetProcAddress(_this, "wglGetSwapIntervalEXT");
    2.41 +    } else {
    2.42 +        _this->gl_data->wglSwapIntervalEXT = NULL;
    2.43 +        _this->gl_data->wglGetSwapIntervalEXT = NULL;
    2.44      }
    2.45 +}
    2.46  
    2.47 +static int
    2.48 +WIN_GL_ChoosePixelFormatARB(_THIS, int *iAttribs, float *fAttribs)
    2.49 +{
    2.50 +    HWND hwnd;
    2.51 +    HDC hdc;
    2.52 +    HGLRC hglrc;
    2.53 +    int pixel_format = 0;
    2.54 +    unsigned int matching;
    2.55 +
    2.56 +    hwnd =
    2.57 +        CreateWindow(SDL_Appname, SDL_Appname, (WS_POPUP | WS_DISABLED), 0, 0,
    2.58 +                     10, 10, NULL, NULL, SDL_Instance, NULL);
    2.59 +    WIN_PumpEvents(_this);
    2.60 +
    2.61 +    hdc = GetDC(hwnd);
    2.62 +
    2.63 +    hglrc = _this->gl_data->wglCreateContext(hdc);
    2.64      if (hglrc) {
    2.65 +        _this->gl_data->wglMakeCurrent(hdc, hglrc);
    2.66 +
    2.67 +        WIN_GL_InitExtensions(_this, hdc);
    2.68 +
    2.69 +        if (_this->gl_data->WGL_ARB_pixel_format) {
    2.70 +            _this->gl_data->wglChoosePixelFormatARB(hdc, iAttribs, fAttribs,
    2.71 +                                                    1, &pixel_format,
    2.72 +                                                    &matching);
    2.73 +        }
    2.74 +
    2.75          _this->gl_data->wglMakeCurrent(NULL, NULL);
    2.76          _this->gl_data->wglDeleteContext(hglrc);
    2.77      }
    2.78      ReleaseDC(hwnd, hdc);
    2.79      DestroyWindow(hwnd);
    2.80      WIN_PumpEvents(_this);
    2.81 +
    2.82 +    return pixel_format;
    2.83  }
    2.84  
    2.85  static int
    2.86 @@ -383,9 +395,6 @@
    2.87          return -1;
    2.88      }
    2.89  
    2.90 -    /* Initialize extensions */
    2.91 -    WIN_GL_InitExtensions(_this);
    2.92 -
    2.93      return 0;
    2.94  }
    2.95  
    2.96 @@ -408,7 +417,6 @@
    2.97      HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
    2.98      PIXELFORMATDESCRIPTOR pfd;
    2.99      int pixel_format;
   2.100 -    unsigned int matching;
   2.101      int iAttribs[64];
   2.102      int *iAttr;
   2.103      float fAttribs[1] = { 0 };
   2.104 @@ -495,10 +503,8 @@
   2.105      *iAttr = 0;
   2.106  
   2.107      /* Choose and set the closest available pixel format */
   2.108 -    if (!_this->gl_data->WGL_ARB_pixel_format
   2.109 -        || !_this->gl_data->wglChoosePixelFormatARB(hdc, iAttribs, fAttribs,
   2.110 -                                                    1, &pixel_format,
   2.111 -                                                    &matching) || !matching) {
   2.112 +    pixel_format = WIN_GL_ChoosePixelFormatARB(_this, iAttribs, fAttribs);
   2.113 +    if (!pixel_format) {
   2.114          pixel_format = WIN_GL_ChoosePixelFormat(hdc, &pfd);
   2.115      }
   2.116      if (!pixel_format) {
   2.117 @@ -522,8 +528,22 @@
   2.118  WIN_GL_CreateContext(_THIS, SDL_Window * window)
   2.119  {
   2.120      HDC hdc = ((SDL_WindowData *) window->driverdata)->hdc;
   2.121 +    HGLRC context;
   2.122  
   2.123 -    return _this->gl_data->wglCreateContext(hdc);
   2.124 +    context = _this->gl_data->wglCreateContext(hdc);
   2.125 +    if (!context) {
   2.126 +        SDL_SetError("Could not create GL context");
   2.127 +        return NULL;
   2.128 +    }
   2.129 +
   2.130 +    if (WIN_GL_MakeCurrent(_this, window, context) < 0) {
   2.131 +        WIN_GL_DeleteContext(_this, context);
   2.132 +        return NULL;
   2.133 +    }
   2.134 +
   2.135 +    WIN_GL_InitExtensions(_this, hdc);
   2.136 +
   2.137 +    return context;
   2.138  }
   2.139  
   2.140  int
     3.1 --- a/src/video/x11/SDL_x11opengl.c	Thu Jul 12 05:32:07 2007 +0000
     3.2 +++ b/src/video/x11/SDL_x11opengl.c	Thu Jul 12 06:31:36 2007 +0000
     3.3 @@ -426,8 +426,15 @@
     3.4  
     3.5      if (!context) {
     3.6          SDL_SetError("Could not create GL context");
     3.7 +        return NULL;
     3.8      }
     3.9 -    return (SDL_GLContext) context;
    3.10 +
    3.11 +    if (X11_GL_MakeCurrent(_this, window, context) < 0) {
    3.12 +        X11_GL_DeleteContext(_this, context);
    3.13 +        return NULL;
    3.14 +    }
    3.15 +
    3.16 +    return context;
    3.17  }
    3.18  
    3.19  int
     4.1 --- a/test/testgl2.c	Thu Jul 12 05:32:07 2007 +0000
     4.2 +++ b/test/testgl2.c	Thu Jul 12 06:31:36 2007 +0000
     4.3 @@ -225,10 +225,6 @@
     4.4          fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
     4.5          quit(2);
     4.6      }
     4.7 -    if (SDL_GL_MakeCurrent(state->windows[0], context) < 0) {
     4.8 -        fprintf(stderr, "SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
     4.9 -        quit(2);
    4.10 -    }
    4.11  
    4.12      if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
    4.13          SDL_GL_SetSwapInterval(1);