use*_*848 5 iphone loops opengl-es
为了将我的游戏干净地移植到iPhone,我正在尝试制作一个不使用NSTimer的游戏循环.
我在一些示例代码中注意到,如果使用NSTimer,您可以在开头设置类似的代码
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
其中drawView看起来像:
- (void)drawView
{
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
mFooModel->render();
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
Run Code Online (Sandbox Code Playgroud)
当使用这种技术时,mFooModel渲染得很好,但我反而想让我自己的游戏循环调用drawView而不是让NSTimer调用drawView 60次.我想要像:
while(gGameState != kShutDown)
{
[self drawView]
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我这样做时,我得到的只是一个黑屏.为什么会这样?无论如何我可以实现我在这里描述的内容吗?
我想避免NSTimer的原因是因为我想在游戏循环中进行物理和AI更新.我使用自己的时钟/计时器来跟踪已经过的时间,以便我可以准确地完成这项工作.渲染尽可能快.我尝试使用本文中描述的一些技术
这有点像一个冲动的问题(你在整天编码,卡住之后所做的那个问题,希望早上的答案就在那里)
干杯伙计们.
Jen*_*ult 21
iPhoneOS 3.1的另一个选择是使用新的CADisplayLink api.当需要更新屏幕内容时,这将调用您指定的选择器.
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(renderAndUpdate)];
[displayLink setFrameInterval:2];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
Run Code Online (Sandbox Code Playgroud)
如果您需要更多示例代码,XCode中的新OpenGL项目模板也会使用CADisplayLink.
如果您不想使用,NSTimer
可以尝试NSRunLoop
手动运行:
static BOOL shouldContinueGameLoop;
static void RunGameLoop() {
NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
NSDate *destDate = [[NSDate alloc] init];
do {
// Create an autorelease pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Run the runloop to process OS events
[currentRunLoop runUntilDate:destDate];
// Your logic/draw code goes here
// Drain the pool
[pool drain];
// Calculate the new date
NSDate *newDate = [[NSDate alloc] initWithTimeInterval:1.0f/45 sinceDate:destDate];
[destDate release];
destDate = newDate;
} while(shouldContinueGameLoop);
[destDate release];
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12959 次 |
最近记录: |