アプリのレンダリングループを特定の方法で整理する必要があります(理由があります)。
私が持っているとしましょう
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は、1つのフレームをレンダリングし、次のフレームまで実行を遅らせる必要があります(レンダリングではありません)。
@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関数)