HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

OpenGL game edit view

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
editgameviewopengl

Problem

I'm working on a Cocoa project that uses OpenGL. I'm trying to keep things easily cross-platformable for later (which is the primary reason for my GL singleton; I hope to implement Linux versions of the IRGL methods that currently use NSOpenGL...). When doing Cocoa & OpenGL stuff, though, I'm never sure that I'm doing things the "right" way. What could I be doing better here? I've snipped out methods unrelated to GL or drawing.

Here is my NSView subclass:

```
//
// IRLevelViewView
// Iris
//
// Created by Andy Van Ness on 3/15/11.
// Copyright 2011 Andy Van Ness. All rights reserved.
//

#import "IRGameEditView.h"
#import
#import "IRGL.h"
//snip

@implementation IRGameEditView

  • (id)initWithFrame:(NSRect)frameRect


{
self = [super initWithFrame:frameRect];
if (self != nil)
{
//snip

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(surfaceNeedsUpdate:) name:NSViewGlobalFrameDidChangeNotification object:self];
}
return self;
}

  • (void)initDisplayLink


{
GLint swapInt = 1;
[[[IRGL gl] glContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];

CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, self);

CGLContextObj cglContext = [[[IRGL gl] glContext] CGLContextObj];
CGLPixelFormatObj cglPixelFormat = [[NSOpenGLView defaultPixelFormat] CGLPixelFormatObj];
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat);

CVDisplayLinkStart(displayLink);
}

  • (void)initGL


{
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

[[IRGL gl] initShaders];
[self reshape];

[self setReadyForGL:YES];
}

static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp now, const CVTimeStamp outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, vo

Solution

-
CVDisplayLink is asynchronous and in general AppKit is not threadsafe.

-
Using setNeedsDisplay, which gets you a refresh at some point in the future, in the draw callback misses the point of display links, which is drawing something in sync with the screen refresh.

-
You can drop the ARB, right? It's almost 2012.

Context

StackExchange Code Review Q#4093, answer score: 3

Revisions (0)

No revisions yet.