我需要通过特定的方式在我的应用程序中组织渲染循环(这是有原因的)。在游戏中渲染循环
比方说,我有
Sprite *sprite = [[Sprite alloc] initWithContentsOfFile:@"sprite.png"]; // some sprite
while (true) {
[Graphics update];
sprite.x = (sprite.x + 1) % 1024 // moving sprite by one pixel each frame
[Input update];
[self update];
}
有Graphics.update应该渲染一帧和延迟执行(不渲染),直到下一帧
@interface Graphics() {
static BOOL _frozen;
static int _count;
static int _frameCount;
static float _frameRate;
static double _lastFrameTimestamp;
}
@end
@implementation Graphics
+ (void)initialize {
_frozen = NO
_count = 0
_frameCount = 0
_frameRate = 30.0
_lastFrameTimestamp = CACurrentMediaTime()
}
+ (void)freeze {
_frozen = YES;
}
+ (void)update {
if _frozen
return
end
Video.nextFrame // some OpenGL ES magic to render next frame
_count++
now = CACurrentMediaTime()
waitTill = _lastFrameTimestamp + _count * (1.0/_frameRate)
if now <= waitTill
sleep(waitTill - now)
else
_count = 0
_lastFrameTimestamp = CACurrentMediaTime()
end
_frameCount++
}
@end
不知怎的,它的工作原理和精灵移动。但是当我回家时,应用程序WillResignActive不会被调用,当我回到应用程序时,会出现黑屏,并在一段时间后应用程序崩溃。
这里是我尝试口的东西:https://bitbucket.org/lukas/openrgss/src/7d9228cc281207fe00a99f63b507198ea2596ead/src/graphics.c(Graphics_update功能)
我建议阅读Objective-C编程的介绍或复习。您将所有方法声明为类方法。以同样的方式,所有的ivars都是静态的。你的类接口不支持NSObject(或任何其他类)的子类。你可以在不调用超级实现的情况下重写初始化名单继续... – LearnCocos2D