如何在不使用NSTimer的情况下在iPhone上进行游戏循环 [英] How make a game loop on the iPhone without using NSTimer

查看:101
本文介绍了如何在不使用NSTimer的情况下在iPhone上进行游戏循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了将我的游戏干净地移植到iPhone,我正在尝试制作一个不使用NSTimer的游戏循环。

In order to cleanly port my game to the iPhone, I'm trying to make a game loop that doesn't use NSTimer.

我注意到了一些样本代码,如果使用NSTimer,你可以在开头设置它,例如

I noticed in some sample code that, if using NSTimer, you'd set it up at the beginning with something like

    self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];

其中drawView看起来像:

where drawView would look something like:


- (void)drawView 
{
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    mFooModel->render();
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

当使用这种技术时mFooModel渲染得很好,但我反而想做自己的游戏循环调用drawView而不是让NSTimer每秒调用drawView 60次。我希望如下:

When using this technique mFooModel renders fine, but I instead want to make my own game loop that calls drawView instead of having NSTimer call drawView 60 times a second. I would like something like:


while(gGameState != kShutDown)
{
    [self drawView]
}

不幸的是,当我这样做时,我得到的只是黑色屏幕。为什么会这样?无论如何我可以实现我在这里描述的内容吗?

Unfortunately when I do this, all I get is a black screen. Why does this happen? Is there anyway I can implement what I'm describing here?

我想避免NSTimer的原因是因为我想在游戏循环中进行物理和AI更新。我使用自己的时钟/计时器来跟踪已经过的时间,以便我可以准确地完成这项工作。渲染尽可能快。我尝试使用这篇文章中描述的一些技术

The reason I want to avoid NSTimer is because I want to do physics and AI updates in the game loop. I use my own clock/timer to keep track of the amount of time that has elapsed so that I can do this accurately. Rendering happens as fast as possible. I try to use some of the techniques as described in this article

这有点是一个冲动的问题(你在整天编码之后做的那个,卡住了,希望早上答案就在那里)

This is somewhat of an impulsive question (the one you do after you've been coding all day, get stuck, and hope the answer is there by morning)

干杯伙伴。

推荐答案

如果您不想使用 NSTimer 您可以尝试运行 NSRunLoop 手动:

If you don't wish to use NSTimer you can try running the NSRunLoop manually:

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];
}

这篇关于如何在不使用NSTimer的情况下在iPhone上进行游戏循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆