1 // Copyright (C) 2014 Patryk Nadrowski
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in Irrlicht.h
\r
5 #include "CNSOGLManager.h"
\r
7 #ifdef _IRR_COMPILE_WITH_NSOGL_MANAGER_
\r
16 CNSOGLManager::CNSOGLManager()
\r
17 : PrimaryContext(SExposedVideoData(0)), PixelFormat(nil)
\r
20 setDebugName("CNSOGLManager");
\r
24 CNSOGLManager::~CNSOGLManager()
\r
28 bool CNSOGLManager::initialize(const SIrrlichtCreationParameters& params, const SExposedVideoData& videodata)
\r
35 void CNSOGLManager::terminate()
\r
39 bool CNSOGLManager::generateSurface()
\r
41 if (Params.DriverType == video::EDT_OPENGL)
\r
43 int alphaSize = Params.WithAlphaChannel ? 4 : 0;
\r
44 int depthSize = Params.ZBufferBits;
\r
46 if (Params.WithAlphaChannel && Params.Bits == 32)
\r
49 NSOpenGLPixelFormatAttribute Attribs[] =
\r
51 NSOpenGLPFANoRecovery,
\r
52 NSOpenGLPFAAccelerated,
\r
53 NSOpenGLPFADoubleBuffer,
\r
54 NSOpenGLPFADepthSize, static_cast<NSOpenGLPixelFormatAttribute>(depthSize),
\r
55 NSOpenGLPFAColorSize, Params.Bits,
\r
56 NSOpenGLPFAAlphaSize, static_cast<NSOpenGLPixelFormatAttribute>(alphaSize),
\r
57 NSOpenGLPFASampleBuffers, 1,
\r
58 NSOpenGLPFASamples, Params.AntiAlias,
\r
59 NSOpenGLPFAStencilSize, static_cast<NSOpenGLPixelFormatAttribute>(Params.Stencilbuffer ? 1 : 0),
\r
60 //NSOpenGLPFAFullScreen,
\r
66 // Choose the best pixel format.
\r
71 case 6: // decrease step.
\r
75 if (Attribs[12] > 2)
\r
89 if (Params.AntiAlias)
\r
92 Attribs[12] = Params.AntiAlias;
\r
104 if (Params.AntiAlias)
\r
107 Attribs[12] = Params.AntiAlias;
\r
114 case 2: // depth size
\r
115 if (Attribs[4] > 16)
\r
117 Attribs[4] = Attribs[4] - 8;
\r
122 case 1: // buffer size
\r
123 if (Attribs[6] > 16)
\r
125 Attribs[6] = Attribs[6] - 8;
\r
131 os::Printer::log("Could not get pixel format.");
\r
135 PixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:Attribs];
\r
137 while (PixelFormat == nil);
\r
139 if (Params.AntiAlias && !Attribs[10])
\r
140 os::Printer::log("No multisampling.");
\r
142 if (Params.WithAlphaChannel && !Attribs[8])
\r
143 os::Printer::log("No alpha.");
\r
145 if (Params.Stencilbuffer && !Attribs[14])
\r
146 os::Printer::log("No stencil buffer.");
\r
148 if (Params.ZBufferBits > Attribs[4])
\r
149 os::Printer::log("No full depth buffer.");
\r
151 if (Params.Bits > Attribs[6])
\r
152 os::Printer::log("No full color buffer.");
\r
158 void CNSOGLManager::destroySurface()
\r
160 [PixelFormat release];
\r
164 bool CNSOGLManager::generateContext()
\r
166 NSOpenGLContext* Context = [[NSOpenGLContext alloc] initWithFormat:PixelFormat shareContext:nil];
\r
168 GLint Vsync = Params.Vsync ? 1 : 0;
\r
169 [Context setValues:&Vsync forParameter:NSOpenGLCPSwapInterval];
\r
171 if (Context == nil)
\r
173 os::Printer::log("Could not create OpenGL context.", ELL_ERROR);
\r
177 // set exposed data
\r
178 CurrentContext.OpenGLOSX.Context = Context;
\r
180 if (!PrimaryContext.OpenGLOSX.Context)
\r
181 PrimaryContext.OpenGLOSX.Context = CurrentContext.OpenGLOSX.Context;
\r
186 const SExposedVideoData& CNSOGLManager::getContext() const
\r
188 return CurrentContext;
\r
191 bool CNSOGLManager::activateContext(const SExposedVideoData& videoData)
\r
193 if (videoData.OpenGLOSX.Context)
\r
195 if ((NSOpenGLContext*)videoData.OpenGLOSX.Context != [NSOpenGLContext currentContext])
\r
197 [(NSOpenGLContext*)videoData.OpenGLOSX.Context makeCurrentContext];
\r
199 CurrentContext = videoData;
\r
202 // set back to main context
\r
205 if ((NSOpenGLContext*)PrimaryContext.OpenGLOSX.Context != [NSOpenGLContext currentContext])
\r
207 [(NSOpenGLContext*)PrimaryContext.OpenGLOSX.Context makeCurrentContext];
\r
209 CurrentContext = PrimaryContext;
\r
216 void CNSOGLManager::destroyContext()
\r
218 if (CurrentContext.OpenGLOSX.Context)
\r
220 if (PrimaryContext.OpenGLOSX.Context == CurrentContext.OpenGLOSX.Context)
\r
221 PrimaryContext.OpenGLOSX.Context = nil;
\r
223 [(NSOpenGLContext*)CurrentContext.OpenGLOSX.Context makeCurrentContext];
\r
224 [(NSOpenGLContext *)CurrentContext.OpenGLOSX.Context clearDrawable];
\r
225 [(NSOpenGLContext *)CurrentContext.OpenGLOSX.Context release];
\r
226 [NSOpenGLContext clearCurrentContext];
\r
228 CurrentContext.OpenGLOSX.Context = nil;
\r
232 bool CNSOGLManager::swapBuffers()
\r
234 [(NSOpenGLContext*)CurrentContext.OpenGLOSX.Context flushBuffer];
\r