Swift wrapper around Simple Direct Media Layer (SDL2) for macOS, iOS, Linux and Windows

What's New


  • Fix iOS and macOS imports

Full Changelog: 1.3.1...1.3.2

SDL2 Swift SDL2

license Linux macOS Windows

This is a thin Swift wrapper around the popular and excellent Simple DirectMedia Layer library.
It provides a swifty and typesafe API.

Simple DirectMedia Layer is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. It is used by video playback software, emulators, and popular games including Valve's award winning catalog and many Humble Bundle games. SDL officially supports Windows, Mac OS X, Linux, iOS, and Android. Support for other platforms may be found in the source code. SDL is written in C, works natively with C++, and there are bindings available for several other languages, including C# and Python.

🚀 Getting Started

These instructions will get your copy of the project up and running on your local machine and provide a code example.

📋 Prerequisites

💻 Installing

Swift SDL2 is available for all platforms that support Swift 5.1 and higher and the Swift Package Manager (SPM).

Extend the following lines in your Package.swift file or use it to create a new project.

// swift-tools-version:5.1

import PackageDescription

let package = Package(
    name: "YourPackageName",
    dependencies: [
        .package(url: "", from: "1.2.0")
    targets: [
            name: "YourTargetName",
            dependencies: ["SDL2"])

Since it's a system library wrapper you need to install the SDL2 library either via

brew install sdl2


apt-get install libsdl2-dev

depending on your platform.

📝 Code Example

🤘 Minimal Metal example application

import SDL2
import Metal
import class QuartzCore.CAMetalLayer


let window = SDL_CreateWindow("SDL2 Metal Demo", 0, 0, 800, 600, SDL_WINDOW_SHOWN.rawValue | SDL_WINDOW_ALLOW_HIGHDPI.rawValue | SDL_WINDOW_METAL.rawValue)

let renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC.rawValue | SDL_RENDERER_ACCELERATED.rawValue)

guard let layerPointer = SDL_RenderGetMetalLayer(renderer) else {
    fatalError("could not get metal layer from renderer")

let layer: CAMetalLayer = unsafeBitCast(layerPointer, to: CAMetalLayer.self)

guard let device: MTLDevice = layer.device else {
    fatalError("metal device missing")

guard let queue: MTLCommandQueue = device.makeCommandQueue() else {
    fatalError("could not create command queue")

var color = MTLClearColorMake(0, 0, 0, 1)

var quit = false
var event: SDL_Event = SDL_Event()
while(!quit) {
    while SDL_PollEvent(&event) != 0 {
        switch SDL_EventType(event.type) {
        case SDL_QUIT,
             SDL_KEYUP where event.key.keysym.sym == SDLK_ESCAPE.rawValue:
            quit = true

    guard let surface = layer.nextDrawable() else {
    } = ( > 1.0) ? 0 : + 0.01

    let pass = MTLRenderPassDescriptor()
    pass.colorAttachments[0].clearColor = color
    pass.colorAttachments[0].loadAction = .clear
    pass.colorAttachments[0].storeAction = .store
    pass.colorAttachments[0].texture = surface.texture

    guard let buffer = queue.makeCommandBuffer() else {
        fatalError("could not create command buffer")

    guard let encoder = buffer.makeRenderCommandEncoder(descriptor: pass) else {
        fatalError("could not create render command encoder")





See the unit tests for more examples.

💁 Help needed

This project is in an early stage and needs a lot of love. If you are interested in contributing, please feel free to do so!

Things that need to be done are, among others:

  • Wrap more SDL2 functions and types
  • Write some additional tests to improve coverage

🏷️ Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

✍️ Authors

See also the list of contributors who participated in this project.

🔏 Licenses

This project is licensed under the zlib License - see the LICENSE file for details.

🙏 Original code

Since Swift SDL2 is merely a wrapper around SDL2 it obviously depends on it.
Support them if you can!

☮️ Alternatives


  • Swift Tools 5.1.0
View More Packages from this Author


  • None
Last updated: Sun Jan 15 2023 16:55:01 GMT-0500 (GMT-05:00)