drawRect not called by setNeedsDisplay during inactive resize

Discussion in 'Mac Programming' started by rossipoo, Feb 7, 2012.

  1. macrumors regular

    Joined:
    Jun 7, 2009
    #1
    I am having an issue with an opengl view that is not updating WHILE THE WINDOW IS BEING RESIZED, but not actually changing the size of the window. If the window size is changed continuously, then a redraw will happen. I have a method animate which is on a timer that is in the NSRunLoopCommonModes. The timer is firing like normal, but a call to setNeedsDisplay in my animate method does not cause a redraw.

    My understanding is that during a resize, the main run loop does not run, and I assume that setNeedsDisplay is only checked during the main run loop, which makes sense to me why it doesn't work, but I'm not sure what to do about it.

    I have "fixed" it by checking inLiveResize in my animate method and calling display if so:
    Code:
    if ([self inLiveResize]) {
    	[self display];
    } else {
    	[self setNeedsDisplay];
    }
    but I'm not sure that this is the optimal solution. It seems like some places online say to avoid using display, I can't find any other way to make a display happen.

    Is this the right way to deal with this? Does anyone know of a better solution?
     
  2. thread starter macrumors regular

    Joined:
    Jun 7, 2009
    #2
    After thinking about this for a bit I decided to change the code in animate to this:
    Code:
    [self setNeedsDisplay];
    if ([self inLiveResize]) {
    	[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    		[self displayIfNeeded];
    	}];
    }
    This seems better to me, in theory, since now setNeedsDisplay can be called multiple times from different timers in one loop, and still only cause one render to happen.
     
  3. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    Try adding your timer to the run loop like this after it's created:

    Code:
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
    If you can, avoid NSTimer for powering your OpenGL view. See Driving OpenGL Rendering Loops
     
  4. thread starter macrumors regular

    Joined:
    Jun 7, 2009
    #4
    Thanks, I hadn't seen this display link code before, it seems to work well. I can't tell any performance difference right now, but I'm sure that's because I don't have a whole lot in my draw call. I assume that because it uses a different thread, the display link code would be able to handle a lot more.
     

Share This Page