0

I need to organize rendering loop in my app by a specific way (there is a reasons).

Let's say I have

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

There Graphics.update should render one frame and delay execution (not rendering) until next frame

@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

Somehow it works and sprite is moving. But when I go to home applicationWillResignActive isn't called and when I go back to app there is black screen and after some time app crashes.

Here is the thing I try to port: https://bitbucket.org/lukas/openrgss/src/7d9228cc281207fe00a99f63b507198ea2596ead/src/graphics.c (Graphics_update function)

1
  • 1
    I recommend reading an intro or refresher on Objective-C programming. You're declaring all methods as class methods. In the same way all ivars are static. Your class interface doesn't subclass NSObject (or any other class for that matter). You override initialize without calling super implementation. The list goes on ...
    – CodeSmile
    Jan 3, 2013 at 11:47

2 Answers 2

2

You can try using Core Animation DisplayLink instead of a while loop. That's how it's usually done in the graphics frameworks. The currentRunLoop calls your update method every 1/60 seconds.

You should remove the sleep call in your update if using NSRunLoop.

CADisplayLink *displayLink;

// Set your update method
displayLink = [CADisplayLink displayLinkWithTarget:[Graphics class] 
                                          selector:@selector(update)];
// Set fps to device refresh rate (60)
[displayLink setFrameInterval:1.0f];

// Add your display link to current run loop
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

// Stop updating
[displayLink invalidate];

The last line stops the execution, so do not call that until you're done with your loop.

1

If you are using Sparrow, this is how you should be handling it:

SPSprite *sprite = [[SPSprite alloc] initWithContentsOfFile:@"sprite.png"]; // some sprite

[self addEventListener:@selector(enterFrame:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];

-(void)enterFrame:(SPEnterFrameEvent*)e {
    [Graphics update];
    sprite.x += 1; // moving sprite by one pixel each frame
    [Input update];
    [self update];
}

Sparrow manages the game loop for you using an SP_ENTER_FRAME_EVENT. It will be called every time it's time to render again. Roughly 30 times per second (though it can be configured).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.