【发布时间】:2016-06-12 09:06:02
【问题描述】:
我的目标是构建一个面向 OSX 10.4+ 的原生 Cocoa 应用程序,该应用程序使用 OpenGL 进行渲染,以便移植我正在构建的游戏。我的问题是,按照 Apple 的技术说明(请参阅下面的链接),我无法弄清楚如何在 Cocoa 中分离更新和渲染。这就是我的问题:Cocoa 应用程序如何以与其呈现不同的速度更新?换句话说,Cocoa 的渲染是基于事件的,我不想等待一个事件来更新我的模拟。
这是我工作的重点:
static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext)
{
CVReturn result = [(__bridge GameView*)displayLinkContext getFrameForTime:outputTime];
return result;
}
- (CVReturn)getFrameForTime:(const CVTimeStamp*)outputTime
{
//TODO: find somewhere better to put this.
//TODO: the magic number is not doing what I expect it to.
float dt = (outputTime->videoTime - lastCapturedTime) / 36000000.0f;
lastCapturedTime = outputTime->videoTime;
update(dt);
NSOpenGLContext *currentContext = [self openGLContext];
[currentContext makeCurrentContext];
// must lock GL context because display link is threaded
CGLLockContext((CGLContextObj)[currentContext CGLContextObj]);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render();
glFlush();
[currentContext flushBuffer];
CGLUnlockContext((CGLContextObj)[currentContext CGLContextObj]);
return kCVReturnSuccess;
}
我不喜欢上面的代码是渲染和更新发生在同一个事件上,而事件的目的是操作系统告诉我的应用程序它应该渲染一个帧。
在 Windows 上,将此代码放在下面的 WinMain() 中会得到我正在寻找的内容。
while(true) {
process_events(); //PeekMessage, handle WM_ events
int elapsedTime = GetElapsedSeconds();
prepare(elapsedTime);
if (fps_lock(elapsedTime, 60)) {
render();
}
swapBuffers();
}
这是我查看的内容(我会发布不止两个链接,因为我已经完成了大量研究,但我的低代表阻止了我这样做):
提前感谢您指出我可能遗漏的内容,或完全偏离主题的内容。
【问题讨论】:
-
这里有很多关于基本问题的信息。您可能想要简化它,例如“如何以不同的速率渲染和更新?”并包含您的代码示例。祝你好运。 :-)