PDA

View Full Version : drawRect not called by setNeedsDisplay during inactive resize




rossipoo
Feb 7, 2012, 08:12 PM
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: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?



rossipoo
Feb 8, 2012, 12:10 AM
After thinking about this for a bit I decided to change the code in animate to this:[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.

kainjow
Feb 8, 2012, 11:16 AM
Try adding your timer to the run loop like this after it's created:

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];

If you can, avoid NSTimer for powering your OpenGL view. See Driving OpenGL Rendering Loops (https://developer.apple.com/library/mac/#qa/qa1385/_index.html)

rossipoo
Feb 8, 2012, 06:30 PM
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.