Ver código fonte

Add support for using NSView for platform window handle

Francis Hart 7 anos atrás
pai
commit
43eb40b9e0
2 arquivos alterados com 38 adições e 9 exclusões
  1. 18 6
      src/glcontext_nsgl.mm
  2. 20 3
      src/renderer_mtl.mm

+ 18 - 6
src/glcontext_nsgl.mm

@@ -67,9 +67,21 @@ namespace bgfx { namespace gl
 		BX_CHECK(NULL != s_opengl, "OpenGL dynamic library is not found!");
 		BX_CHECK(NULL != s_opengl, "OpenGL dynamic library is not found!");
 
 
 		const AutoreleasePoolHolder pool;
 		const AutoreleasePoolHolder pool;
-		NSWindow* nsWindow = (NSWindow*)g_platformData.nwh;
+		NSObject* nwh = (NSObject*)g_platformData.nwh;
 		m_context = g_platformData.context;
 		m_context = g_platformData.context;
 
 
+		NSWindow* nsWindow = nil;
+		NSView* contentView = nil;
+		if ([nwh isKindOfClass:[NSView class]])
+		{
+			contentView = (NSView*)nwh;
+		}
+		else if ([nwh isKindOfClass:[NSWindow class]])
+		{
+			nsWindow = (NSWindow*)nwh;
+			contentView = [nsWindow contentView];
+		}
+
 		if (NULL == g_platformData.context)
 		if (NULL == g_platformData.context)
 		{
 		{
 #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
 #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
@@ -99,14 +111,13 @@ namespace bgfx { namespace gl
 			NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes];
 			NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes];
 			BGFX_FATAL(NULL != pixelFormat, Fatal::UnableToInitialize, "Failed to initialize pixel format.");
 			BGFX_FATAL(NULL != pixelFormat, Fatal::UnableToInitialize, "Failed to initialize pixel format.");
 
 
-			NSRect glViewRect = [[nsWindow contentView] bounds];
+			NSRect glViewRect = [contentView bounds];
 			NSOpenGLView* glView = [[NSOpenGLView alloc] initWithFrame:glViewRect pixelFormat:pixelFormat];
 			NSOpenGLView* glView = [[NSOpenGLView alloc] initWithFrame:glViewRect pixelFormat:pixelFormat];
 
 
 			[pixelFormat release];
 			[pixelFormat release];
 			// GLFW creates a helper contentView that handles things like keyboard and drag and
 			// 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
 			// 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.
 			// add ourselves as a subview and make the view resize automatically.
-			NSView *contentView = [nsWindow contentView];
 			if (nil != contentView)
 			if (nil != contentView)
 			{
 			{
 				[glView setAutoresizingMask:( NSViewHeightSizable |
 				[glView setAutoresizingMask:( NSViewHeightSizable |
@@ -119,7 +130,8 @@ namespace bgfx { namespace gl
 			}
 			}
 			else
 			else
 			{
 			{
-				[nsWindow setContentView:glView];
+				if (nil != nsWindow)
+					[nsWindow setContentView:glView];
 			}
 			}
 
 
 			NSOpenGLContext* glContext = [glView openGLContext];
 			NSOpenGLContext* glContext = [glView openGLContext];
@@ -179,8 +191,8 @@ namespace bgfx { namespace gl
 	{
 	{
 		uint64_t caps = 0;
 		uint64_t caps = 0;
 #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
 #if defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
-		NSWindow* nsWindow = (NSWindow*)g_platformData.nwh;
-		if ([nsWindow respondsToSelector:@selector(backingScaleFactor)] && (1.0f < [nsWindow backingScaleFactor]))
+		NSObject* nwh = (NSObject*)g_platformData.nwh;
+		if ([nwh respondsToSelector:@selector(backingScaleFactor)] && (1.0f < [nwh backingScaleFactor]))
 			caps |= BGFX_CAPS_HIDPI;
 			caps |= BGFX_CAPS_HIDPI;
 #endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
 #endif // defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1070)
 		return caps;
 		return caps;

+ 20 - 3
src/renderer_mtl.mm

@@ -2991,17 +2991,34 @@ namespace bgfx { namespace mtl
 				}
 				}
 				else
 				else
 				{
 				{
+					NSView *contentView;
+
+					if ([nvh isKindOfClass:[NSView class]])
+					{
+						contentView = (NSView*)nvh;
+					}
+					else if ([nvh isKindOfClass:[NSWindow class]])
+					{
+						NSWindow* nsWindow = (NSWindow*)nvh;
+						contentView = [nsWindow contentView];
+					}
+					else
+					{
+						BX_WARN(0, "Unable to create Metal device. Please set platform data window to an NSWindow, NSView, or CAMetalLayer");
+						return;
+					}
+
 					NSWindow* nsWindow = (NSWindow*)_nwh;
 					NSWindow* nsWindow = (NSWindow*)_nwh;
-					CALayer* layer = nsWindow.contentView.layer;
+					CALayer* layer = contentView.layer;
 					if(NULL != layer && [layer isKindOfClass:NSClassFromString(@"CAMetalLayer")])
 					if(NULL != layer && [layer isKindOfClass:NSClassFromString(@"CAMetalLayer")])
 					{
 					{
 						m_metalLayer = (CAMetalLayer*)layer;
 						m_metalLayer = (CAMetalLayer*)layer;
 					}
 					}
 					else
 					else
 					{
 					{
-						[nsWindow.contentView setWantsLayer:YES];
+						[contentView setWantsLayer:YES];
 						m_metalLayer = [CAMetalLayer layer];
 						m_metalLayer = [CAMetalLayer layer];
-						[nsWindow.contentView setLayer:m_metalLayer];
+						[contentView setLayer:m_metalLayer];
 					}
 					}
 				}
 				}
 			}
 			}