NSOpenGLView isn't redrawing...

Discussion in 'Mac Programming' started by Stachelsk, Dec 18, 2009.

  1. Stachelsk macrumors regular

    Joined:
    Dec 17, 2008
    #1
    I'm stumped. I'm by no means familiar with objective-c++ or Apple's libraries, so please bear with me. My renderer code is only getting called once (when the application starts). It only gets called again if I minimize and restore the window. No idea how to make it refresh.

    Code in question:
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Code:
    [self setNeedsDisplay:YES];
    is the way to do it. But don't call that inside your drawRect: method. When/where are you actually wanting to redraw?

    Edit: you need this at the end of your drawRect:
    Code:
    [[self openGLContext] flushBuffer];
     
  3. Stachelsk thread starter macrumors regular

    Joined:
    Dec 17, 2008
    #3
    Thanks for the help, kainjow. Unfortunately, it did not work. :(

    It's still only calling DrawGL() once.
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  5. Stachelsk thread starter macrumors regular

    Joined:
    Dec 17, 2008
    #5
    Code:
    #import "GLView.h"
    
    @implementation GLView
    
    - (id)initWithFrame:(NSRect)frameRect {
        NSOpenGLPixelFormatAttribute attr[] = {
            NSOpenGLPFADoubleBuffer,
    		NSOpenGLPFAAccelerated,
    		NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) 32,
    		(NSOpenGLPixelFormatAttribute) 0
    	};
    	NSOpenGLPixelFormat *nsglFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attr] autorelease];
    	
        if ((self = [super initWithFrame:frameRect pixelFormat:nsglFormat]))
    		; //...
    	return self;
    }
    
    - (void)dealloc {
    	[super dealloc];
    }
    
    - (void)prepareOpenGL {
    	glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 400, 300, 0, 0, 1);
        glMatrixMode(GL_MODELVIEW);
    }
    
    - (void)drawRect:(NSRect)rect {
    	render();
        [[self openGLContext] flushBuffer];
        [self setNeedsDisplay:YES];
    }
    
    @end
    
    I want the code to redraw as soon as its done rendering a frame... so I put the [self setNeedsDisplay:YES]; after flushing the buffer. Can it just absolutely not be there? Where can I put it so that the screen is constantly redrawing as soon as the render method is done?
     
  6. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    'setNeedsDisplay' does nothing when placed within your draw loop. Instead you'll need to drive your rendering loop from an NSTimer or DisplayLink callback handler.

    Check out this document.
     
  7. Stachelsk thread starter macrumors regular

    Joined:
    Dec 17, 2008
    #7
    Works perfectly, thanks.
    Happy holidays!
     

Share This Page