Variable, semi fixed, or fixed timestep runloops for iOS
Swift Ruby Objective-C
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Example
SBRunLoop
.gitignore
LICENSE
README.md
SBRunLoop.podspec

README.md

SBRunLoop

Version License Platform

SBRunLoop is a wrapper for CADisplayLink that allows for easy creation of run loops with either a variable, fixed, or semi-fixed timestep.

WTF is a timestep?

Usage

Create an instance of SBRunLoop, you will want to reatin it so that it doesn't become deallocated. Pass in the callback functions that you want to use.

Complete implementation of a run loop in a view controller:

class ViewController: UIViewController {
    
    var runLoop: SBRunLoop!

    override func viewDidLoad() {
        super.viewDidLoad()
        
    	 runLoop = SBRunLoop(mode: .variable)
    	 runLoop.update = update(dt:)
    	 runLoop.start()
    }
    
    func update(dt: CFTimeInterval) {
    	 // This method will be called every frame
    }
}

Timesteps

Variable

The update function will be called once each frame. dt will be equal to the amount of time that it took to render the last frame.

SBRunLoop(mode: .variable)

Fixed

The update function will be called only in increments of the provided step size, and any left over time will be carried over to the next frame. Update may be alled multiple times per frame. For instance, for 120hz update, pass in 1/120. If the device is able to render at 60fps, this update will be called on average twice per frame. dt will always be the fixed step size.

SBRunLoop(mode: .fixed(timeStep: 1/120))

Semi Fixed

The update function will be called in the increments provided, and the once again with the remainder. For instance, if there is 0.1s to render, and the step size is 0.03s, update will be called with dt 0.03, 0.03, 0.03, 0.01. With this time step, there is not time carried over to the next frame.

.semiFixed(timeStep: 1/120)

Callbacks

Pre Update: Called Once before update occurs. dt is always complete frame time.

public var preUpdate: (CFTimeInterval) -> () = {_ in}

Update: Called once or multiple times. dt according to time step.

public var update: (CFTimeInterval) -> () = {_ in}

Post Update: Called Once after update occurs. dt is always complete frame time.

public var postUpdate: (CFTimeInterval) -> () = {_ in}

Installation

SBRunLoop is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SBRunLoop"

Author

Steve Barnegren, steve.barnegren@gmail.com

License

SBRunLoop is available under the MIT license. See the LICENSE file for more info.