我需要通过特定方式在我的应用程序中组织渲染循环(这是有原因的)。
假设我有
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
它以某种方式工作并且精灵正在移动。但是当我回家时 applicationWillResignActive 没有被调用,当我回到应用程序时出现黑屏,一段时间后应用程序崩溃。
这是我尝试移植的内容:https ://bitbucket.org/lukas/openrgss/src/7d9228cc281207fe00a99f63b507198ea2596ead/src/graphics.c (Graphics_update函数)