src/video/cocoa/SDL_cocoaopengl.m
changeset 7595 ede2237fcebf
parent 7594 6abcf951af68
child 7738 10981a646a6d
     1.1 --- a/src/video/cocoa/SDL_cocoaopengl.m	Wed Aug 07 16:29:21 2013 -0700
     1.2 +++ b/src/video/cocoa/SDL_cocoaopengl.m	Wed Aug 07 16:29:25 2013 -0700
     1.3 @@ -51,8 +51,12 @@
     1.4  - (id)initWithFormat:(NSOpenGLPixelFormat *)format
     1.5          shareContext:(NSOpenGLContext *)share
     1.6  {
     1.7 -    SDL_AtomicSet(&self->dirty, 0);
     1.8 -    return [super initWithFormat:format shareContext:share];
     1.9 +    self = [super initWithFormat:format shareContext:share];
    1.10 +    if (self) {
    1.11 +        SDL_AtomicSet(&self->dirty, 0);
    1.12 +        self->window = NULL;
    1.13 +    }
    1.14 +    return self;
    1.15  }
    1.16  
    1.17  - (void)scheduleUpdate
    1.18 @@ -78,6 +82,40 @@
    1.19      [self updateIfNeeded];
    1.20  }
    1.21  
    1.22 +/* Updates the drawable for the contexts and manages related state. */
    1.23 +- (void)setWindow:(SDL_Window *)newWindow
    1.24 +{
    1.25 +    if (self->window) {
    1.26 +        SDL_WindowData *oldwindowdata = (SDL_WindowData *)self->window->driverdata;
    1.27 +
    1.28 +        /* Make sure to remove us from the old window's context list, or we'll get scheduled updates from it too. */
    1.29 +        NSMutableArray *contexts = oldwindowdata->nscontexts;
    1.30 +        @synchronized (contexts) {
    1.31 +            [contexts removeObject:self];
    1.32 +        }
    1.33 +    }
    1.34 +
    1.35 +    self->window = newWindow;
    1.36 +
    1.37 +    if (newWindow) {
    1.38 +        SDL_WindowData *windowdata = (SDL_WindowData *)newWindow->driverdata;
    1.39 +
    1.40 +        /* Now sign up for scheduled updates for the new window. */
    1.41 +        NSMutableArray *contexts = windowdata->nscontexts;
    1.42 +        @synchronized (contexts) {
    1.43 +            [contexts addObject:self];
    1.44 +        }
    1.45 +
    1.46 +        if ([self view] != [windowdata->nswindow contentView]) {
    1.47 +            [self setView:[windowdata->nswindow contentView]];
    1.48 +            [self scheduleUpdate];
    1.49 +        }
    1.50 +    } else {
    1.51 +        [self clearDrawable];
    1.52 +        [self scheduleUpdate];
    1.53 +    }
    1.54 +}
    1.55 +
    1.56  @end
    1.57  
    1.58  
    1.59 @@ -244,14 +282,8 @@
    1.60      pool = [[NSAutoreleasePool alloc] init];
    1.61  
    1.62      if (context) {
    1.63 -        SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
    1.64          SDLOpenGLContext *nscontext = (SDLOpenGLContext *)context;
    1.65 -        windowdata->nscontext = nscontext;
    1.66 -        if ([nscontext view] != [windowdata->nswindow contentView]) {
    1.67 -            [nscontext setView:[windowdata->nswindow contentView]];
    1.68 -            [nscontext scheduleUpdate];
    1.69 -        }
    1.70 -
    1.71 +        [nscontext setWindow:window];
    1.72          [nscontext updateIfNeeded];
    1.73          [nscontext makeCurrentContext];
    1.74      } else {
    1.75 @@ -309,11 +341,10 @@
    1.76  Cocoa_GL_SwapWindow(_THIS, SDL_Window * window)
    1.77  {
    1.78      NSAutoreleasePool *pool;
    1.79 -    SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
    1.80 -    SDLOpenGLContext *nscontext = windowdata->nscontext;
    1.81  
    1.82      pool = [[NSAutoreleasePool alloc] init];
    1.83  
    1.84 +    SDLOpenGLContext* nscontext = (NSOpenGLContext*)SDL_GL_GetCurrentContext();
    1.85      [nscontext flushBuffer];
    1.86      [nscontext updateIfNeeded];
    1.87  
    1.88 @@ -324,11 +355,11 @@
    1.89  Cocoa_GL_DeleteContext(_THIS, SDL_GLContext context)
    1.90  {
    1.91      NSAutoreleasePool *pool;
    1.92 -    NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
    1.93 +    SDLOpenGLContext *nscontext = (SDLOpenGLContext *)context;
    1.94  
    1.95      pool = [[NSAutoreleasePool alloc] init];
    1.96  
    1.97 -    [nscontext clearDrawable];
    1.98 +    [nscontext setWindow:NULL];
    1.99      [nscontext release];
   1.100  
   1.101      [pool release];