在针对Mojave框架的Xcode 10上编译时,我遇到了一个非常奇怪的错误。
事实证明OpenGL停止显示。当快速更新window(!)的可视区域或最小化(精灵效果所使用的快照将其显示!)时,它似乎实际上在该处(衬里层覆盖在它上面?)
最奇怪的是,我在另一个项目中使用几乎相同的类,并且在为Mojave进行构建时效果很好!
我看到了为使用单独上下文的用户向OpenGLContext发送更新的解决方案,但这是NSOpenGLView的简单子类。在这种情况下,请关闭SpriteBuilder for Cocos2D。
我尝试更新底层上下文,将视图设置为支持高分辨率,并尝试防止创建底层。到目前为止,什么都没有。
代码:
@implementation CCGLView {
NSMutableArray *_fences;
}
- (id) initWithFrame:(NSRect)frameRect
{
self = [self initWithFrame:frameRect shareContext:nil];
return self;
}
- (id) initWithFrame:(NSRect)frameRect shareContext:(NSOpenGLContext*)context
{
NSOpenGLPixelFormatAttribute attribs[] =
{
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24,
0
};
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
if (!pixelFormat)
CCLOG(@"No OpenGL pixel format");
if ((self = [super initWithFrame:frameRect pixelFormat:pixelFormat])) {
if (context) [self setOpenGLContext:context];
}
return self;
}
- (void) prepareOpenGL
{
[super prepareOpenGL];
// Make this openGL context current to the thread
// (i.e. all openGL on this thread calls will go to this context)
[[self openGLContext] makeCurrentContext];
// Synchronize buffer swaps with vertical refresh rate
GLint swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
}
- (NSUInteger) depthFormat
{
return 24;
}
- (void) reshape
{
[self lockOpenGLContext];
NSRect rect = [self convertRectToBacking:self.bounds];
CCDirector *director = [CCDirector sharedDirector];
[director reshapeProjection: NSSizeToCGSize(rect.size) ];
if (director.runningScene) {
[director drawScene];
}
[self unlockOpenGLContext];
}
- (void)lockOpenGLContext
{
NSOpenGLContext *glContext = [self openGLContext];
NSAssert( glContext, @"FATAL: could not get openGL context");
[glContext makeCurrentContext];
CGLLockContext([glContext CGLContextObj]);
}
-(void) unlockOpenGLContext
{
NSOpenGLContext *glContext = [self openGLContext];
NSAssert( glContext, @"FATAL: could not get openGL context");
CGLUnlockContext([glContext CGLContextObj]);
}
// Find or make a fence that is ready to use.
-(CCGLViewFence *)getReadyFence
{
// First checkf oldest (first in the array) fence is ready again.
CCGLViewFence *fence = _fences.firstObject;;
if(fence.isReady){
// Remove the fence so it can be inserted at the end of the queue again.
[_fences removeObjectAtIndex:0];
return fence;
} else {
// No existing fences ready. Make a new one.
return [[CCGLViewFence alloc] init];
}
}
-(void)addFrameCompletionHandler:(dispatch_block_t)handler
{
if(_fences == nil){
_fences = [NSMutableArray arrayWithObject:[[CCGLViewFence alloc] init]];
}
CCGLViewFence *fence = _fences.lastObject;
if(!fence.isReady){
fence = [self getReadyFence];
[_fences addObject:fence];
}
[fence.handlers addObject:handler];
}
-(void)beginFrame
{
[self lockOpenGLContext];
}
-(void)presentFrame
{
{
CCGLViewFence *fence = _fences.lastObject;
if(fence.isReady){
// If the fence is ready to be added, insert a sync point for it.
[fence insertFence];
}
}
[self.openGLContext flushBuffer];
// Check the fences for completion.
for(CCGLViewFence *fence in _fences){
if(fence.isComplete){
for(dispatch_block_t handler in fence.handlers) handler();
[fence.handlers removeAllObjects];
} else {
break;
}
}
[self unlockOpenGLContext];
}
-(GLuint)fbo
{
return 0;
}
@end