From b2b0b237296df2e68491d0ea8a7d0da490ee3d1a Mon Sep 17 00:00:00 2001 From: Christopher Kohnert Date: Fri, 12 Aug 2016 14:28:17 -0700 Subject: [PATCH] Don't clobber existing contentView if one exists. Mainly this is a compatibility fix for GLFW, but it would apply to any custom window implementation that sets a contentView prior to initialization. --- src/glcontext_nsgl.mm | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/glcontext_nsgl.mm b/src/glcontext_nsgl.mm index ac51bf631..61a392722 100644 --- a/src/glcontext_nsgl.mm +++ b/src/glcontext_nsgl.mm @@ -101,7 +101,24 @@ namespace bgfx { namespace gl NSOpenGLView* glView = [[NSOpenGLView alloc] initWithFrame:glViewRect pixelFormat:pixelFormat]; [pixelFormat release]; - [nsWindow setContentView:glView]; + // GLFW creates a helper contentView that handles things like keyboard and drag and + // drop events. We don't want to clobber that view if it exists. Instead we just + // add ourselves as a subview and make the view resize automatically. + NSView *contentView = [nsWindow contentView]; + if( contentView != nil ) + { + [glView setAutoresizingMask:( NSViewHeightSizable | + NSViewWidthSizable | + NSViewMinXMargin | + NSViewMaxXMargin | + NSViewMinYMargin | + NSViewMaxYMargin )]; + [contentView addSubview:glView]; + } + else + { + [nsWindow setContentView:glView]; + } NSOpenGLContext* glContext = [glView openGLContext]; BGFX_FATAL(NULL != glContext, Fatal::UnableToInitialize, "Failed to initialize GL context."); @@ -109,6 +126,12 @@ namespace bgfx { namespace gl [glContext makeCurrentContext]; GLint interval = 0; [glContext setValues:&interval forParameter:NSOpenGLCPSwapInterval]; + + // When initializing NSOpenGLView programatically (as we are), this sometimes doesn't + // get hooked up properly (especially when there are existing window elements). This ensures + // we are valid. Otherwise, you'll probably get a GL_INVALID_FRAMEBUFFER_OPERATION when + // trying to glClear() for the first time. + [glContext setView:glView]; m_view = glView; m_context = glContext;