From d8301ddc1cb420e7f513e647178dd7a8e148476a Mon Sep 17 00:00:00 2001 From: reuk Date: Wed, 5 Oct 2022 19:45:31 +0100 Subject: [PATCH] OpenGL: Tidy up macOS implementation --- modules/juce_opengl/native/juce_OpenGL_osx.h | 89 +++++++++----------- 1 file changed, 40 insertions(+), 49 deletions(-) diff --git a/modules/juce_opengl/native/juce_OpenGL_osx.h b/modules/juce_opengl/native/juce_OpenGL_osx.h index 8aa5c8935d..4996de7b66 100644 --- a/modules/juce_opengl/native/juce_OpenGL_osx.h +++ b/modules/juce_opengl/native/juce_OpenGL_osx.h @@ -38,10 +38,9 @@ public: OpenGLVersion version) : owner (component) { - NSOpenGLPixelFormatAttribute attribs[64] = { 0 }; - createAttribs (attribs, version, pixFormat, shouldUseMultisampling); + const auto attribs = createAttribs (version, pixFormat, shouldUseMultisampling); - NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs]; + NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs.data()]; static MouseForwardingNSOpenGLViewClass cls; view = [cls.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f) @@ -75,48 +74,47 @@ public: [view release]; } - static void createAttribs (NSOpenGLPixelFormatAttribute* attribs, OpenGLVersion version, - const OpenGLPixelFormat& pixFormat, bool shouldUseMultisampling) + static std::vector createAttribs (OpenGLVersion version, + const OpenGLPixelFormat& pixFormat, + bool shouldUseMultisampling) { - ignoreUnused (version); - int numAttribs = 0; - - attribs[numAttribs++] = NSOpenGLPFAOpenGLProfile; - attribs[numAttribs++] = [version] + std::vector attribs { - if (version == openGL3_2) - return NSOpenGLProfileVersion3_2Core; + NSOpenGLPFAOpenGLProfile, [version] + { + if (version == openGL3_2) + return NSOpenGLProfileVersion3_2Core; - if (version != defaultGLVersion) - if (@available (macOS 10.10, *)) - return NSOpenGLProfileVersion4_1Core; + if (version != defaultGLVersion) + if (@available (macOS 10.10, *)) + return NSOpenGLProfileVersion4_1Core; - return NSOpenGLProfileVersionLegacy; - }(); - - attribs[numAttribs++] = NSOpenGLPFADoubleBuffer; - attribs[numAttribs++] = NSOpenGLPFAClosestPolicy; - attribs[numAttribs++] = NSOpenGLPFANoRecovery; - attribs[numAttribs++] = NSOpenGLPFAColorSize; - attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.redBits + pixFormat.greenBits + pixFormat.blueBits); - attribs[numAttribs++] = NSOpenGLPFAAlphaSize; - attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.alphaBits; - attribs[numAttribs++] = NSOpenGLPFADepthSize; - attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.depthBufferBits; - attribs[numAttribs++] = NSOpenGLPFAStencilSize; - attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.stencilBufferBits; - attribs[numAttribs++] = NSOpenGLPFAAccumSize; - attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.accumulationBufferRedBits + pixFormat.accumulationBufferGreenBits - + pixFormat.accumulationBufferBlueBits + pixFormat.accumulationBufferAlphaBits); + return NSOpenGLProfileVersionLegacy; + }(), + NSOpenGLPFADoubleBuffer, + NSOpenGLPFAClosestPolicy, + NSOpenGLPFANoRecovery, + NSOpenGLPFAColorSize, static_cast (pixFormat.redBits + pixFormat.greenBits + pixFormat.blueBits), + NSOpenGLPFAAlphaSize, static_cast (pixFormat.alphaBits), + NSOpenGLPFADepthSize, static_cast (pixFormat.depthBufferBits), + NSOpenGLPFAStencilSize, static_cast (pixFormat.stencilBufferBits), + NSOpenGLPFAAccumSize, static_cast (pixFormat.accumulationBufferRedBits + pixFormat.accumulationBufferGreenBits + + pixFormat.accumulationBufferBlueBits + pixFormat.accumulationBufferAlphaBits) + }; if (shouldUseMultisampling) { - attribs[numAttribs++] = NSOpenGLPFAMultisample; - attribs[numAttribs++] = NSOpenGLPFASampleBuffers; - attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) 1; - attribs[numAttribs++] = NSOpenGLPFASamples; - attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.multisamplingLevel; + attribs.insert (attribs.cend(), + { + NSOpenGLPFAMultisample, + NSOpenGLPFASampleBuffers, static_cast (1), + NSOpenGLPFASamples, static_cast (pixFormat.multisamplingLevel) + }); } + + attribs.push_back (0); + + return attribs; } InitResult initialiseOnRenderThread (OpenGLContext&) { return InitResult::success; } @@ -274,24 +272,17 @@ public: //============================================================================== struct MouseForwardingNSOpenGLViewClass : public ObjCClass { - MouseForwardingNSOpenGLViewClass() : ObjCClass ("JUCEGLView_") + MouseForwardingNSOpenGLViewClass() : ObjCClass ("JUCEGLView_") { - addMethod (@selector (rightMouseDown:), rightMouseDown); - addMethod (@selector (rightMouseUp:), rightMouseUp); - addMethod (@selector (acceptsFirstMouse:), acceptsFirstMouse); - addMethod (@selector (accessibilityHitTest:), accessibilityHitTest); + addMethod (@selector (rightMouseDown:), [] (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseDown: ev]; }); + addMethod (@selector (rightMouseUp:), [] (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseUp: ev]; }); + addMethod (@selector (acceptsFirstMouse:), [] (id, SEL, NSEvent*) -> BOOL { return YES; }); + addMethod (@selector (accessibilityHitTest:), [] (id self, SEL, NSPoint p) -> id { return [[(NSOpenGLView*) self superview] accessibilityHitTest: p]; }); registerClass(); } - - private: - static void rightMouseDown (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseDown: ev]; } - static void rightMouseUp (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseUp: ev]; } - static BOOL acceptsFirstMouse (id, SEL, NSEvent*) { return YES; } - static id accessibilityHitTest (id self, SEL, NSPoint p) { return [[(NSOpenGLView*) self superview] accessibilityHitTest: p]; } }; - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext) };