Moving a Dot with MetalKit cover image

Moving a Dot with MetalKit

David Kanenwisher • September 23, 2021

swift

I got a dot to animate across the screen with MetalKit. It doesn't seem like much but it's been quite a trip to get here. All of the code to make it work can be found in Metal003GameLoop.

The key part of this project for me was figuring out how to set up a game loop that gets along with MetalKit while avoiding the more complex CADisplayLink. This meant using the draw method of MTKViewDelegate to update the game and draw the result.

extension Drawer: MTKViewDelegate {
    func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
        print(#function)
    }

    func draw(in view: MTKView) {
        let current = CACurrentMediaTime()
        let delta = current - previous
        previous = current

        world.update(elapsed: delta)

        render(in: view)
    }
}

I still have a lot to figure out but this is some exciting progress.