Turn SDL_GL_MakeCurrent() into a no-op if setting the same context twice.
authorRyan C. Gordon <icculus@icculus.org>
Fri, 15 Jul 2011 17:05:32 -0700
changeset 55632a152e7e82f2
parent 5562 aa0e501baca0
child 5564 7ed38a8ad304
Turn SDL_GL_MakeCurrent() into a no-op if setting the same context twice.
src/video/SDL_sysvideo.h
src/video/SDL_video.c
     1.1 --- a/src/video/SDL_sysvideo.h	Wed Jul 13 17:38:09 2011 -0700
     1.2 +++ b/src/video/SDL_sysvideo.h	Fri Jul 15 17:05:32 2011 -0700
     1.3 @@ -277,6 +277,11 @@
     1.4      } gl_config;
     1.5  
     1.6      /* * * */
     1.7 +    /* Cache current GL context; don't call the OS when it hasn't changed. */
     1.8 +    SDL_Window *current_glwin;
     1.9 +    SDL_GLContext current_glctx;
    1.10 +
    1.11 +    /* * * */
    1.12      /* Data private to this driver */
    1.13      void *driverdata;
    1.14      struct SDL_GLDriverData *gl_data;
     2.1 --- a/src/video/SDL_video.c	Wed Jul 13 17:38:09 2011 -0700
     2.2 +++ b/src/video/SDL_video.c	Fri Jul 15 17:05:32 2011 -0700
     2.3 @@ -1931,6 +1931,13 @@
     2.4  
     2.5      CHECK_WINDOW_MAGIC(window, );
     2.6  
     2.7 +    /* make no context current if this is the current context window. */
     2.8 +    if (window->flags & SDL_WINDOW_OPENGL) {
     2.9 +        if (_this->current_glwin == window) {
    2.10 +            SDL_GL_MakeCurrent(NULL, NULL);
    2.11 +        }
    2.12 +    }
    2.13 +
    2.14      /* Restore video mode, etc. */
    2.15      SDL_HideWindow(window);
    2.16  
    2.17 @@ -2462,18 +2469,31 @@
    2.18  }
    2.19  
    2.20  int
    2.21 -SDL_GL_MakeCurrent(SDL_Window * window, SDL_GLContext context)
    2.22 +SDL_GL_MakeCurrent(SDL_Window * window, SDL_GLContext ctx)
    2.23  {
    2.24 +    int retval;
    2.25 +
    2.26      CHECK_WINDOW_MAGIC(window, -1);
    2.27  
    2.28      if (!(window->flags & SDL_WINDOW_OPENGL)) {
    2.29          SDL_SetError("The specified window isn't an OpenGL window");
    2.30          return -1;
    2.31      }
    2.32 -    if (!context) {
    2.33 +    if (!ctx) {
    2.34          window = NULL;
    2.35      }
    2.36 -    return _this->GL_MakeCurrent(_this, window, context);
    2.37 +
    2.38 +    if ((window == _this->current_glwin) && (ctx == _this->current_glctx)) {
    2.39 +        retval = 0;  /* we're already current. */
    2.40 +    } else {
    2.41 +        retval = _this->GL_MakeCurrent(_this, window, ctx);
    2.42 +        if (retval == 0) {
    2.43 +            _this->current_glwin = window;
    2.44 +            _this->current_glctx = ctx;
    2.45 +        }
    2.46 +    }
    2.47 +
    2.48 +    return retval;
    2.49  }
    2.50  
    2.51  int