Jump to content
  • entries
  • comments
  • views

vortxEDIT : Cocoa multiple OpenGL views

Sign in to follow this  


Finally got 2 openGL views working in a single window. Cocoa seems to be very fussy about the order and timing of some of the calls to its helper context class. Here is my call flow

Here is my init

- (id)initWithFrame: (NSRect)frame
self = [super initWithFrame: frame];
if (self)
doDraw = NO;
doInit = YES;

NSOpenGLPixelFormatAttribute attrs[] = {
NSOpenGLPFADepthSize, 32,
0 };
m_PixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes: attrs];
/* Check if initWithAttributes succeeded. */
if( m_PixelFormat == nil )
/* initWithAttributes failed. Try to alloc/init with a different list of attributes. */
NSLog(@"ERROR: Pixel Formal NOT supported");

m_Context = [[NSOpenGLContext alloc] initWithFormat:m_PixelFormat shareContext:nil];
if( m_Context == nil )
/* initWithAttributes failed. Try to alloc/init with a different list of attributes. */
NSLog(@"ERROR: GL Context not created");
} else
[m_Context makeCurrentContext];
[m_Context setView:self];
return self;

and here is the drawRect

- (void)drawRect:(NSRect)rect
[m_Context makeCurrentContext];
[m_Context setView:self];

if( [self inLiveResize] )
// Not sure yet :-)
doDraw = YES;

// Only draw if timer fires
if (doDraw)
doDraw = NO;

[self draw];
[m_Context flushBuffer];

I use a Timer to set the doDraw BOOL as not to force a drawRect. I also still need to read more about inLiveResize to see if I can still update the engine or not.

Here I setup the timer and a default scene, this function is called in applicationDidFinishLaunching, as you can see my engine is C++ and I'm finding that mixing ObjC and C++ is working very well.

NSLog(@"startup: Init game...");

[m_Context makeCurrentContext];
[m_Context clearDrawable];
[m_Context setView:self];

// cpp code: Create our demoapp
// This sets up the engine and keeps the custom view clean
default_scene = new DefaultScene();
if( default_scene )
// cpp code: init our app, needs active GL context
default_scene->InitEngine( 60.0 );
// Add engine update timer
m_Timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0
target:self selector:@selector(triggerGameDraw:)
userInfo:nil repeats:YES];


I'm posting this in the hope it will help someone else having issues with Cocoa/OpenGL.

Screen Shot 2012-10-02 at 1.02.53 PM.png
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!