Swiftpack.co -  ctreffs/SwiftSDL2 as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Swift wrapper around Simple Direct Media Layer (SDL2) for macOS, iOS and linux
.package(url: "https://github.com/ctreffs/SwiftSDL2.git", from: "1.2.0")

SDL2 Swift SDL2

Linux macOS license swift version platforms platforms

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.
~ www.libsdl.org

๐Ÿš€ 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: "https://github.com/ctreffs/SwiftSDL2.git", 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)

let renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC.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_KEYUP:
            if( event.key.keysym.sym == SDLK_ESCAPE ) {
                quit = true

    guard let surface = layer.nextDrawable() else {

    color.blue = (color.blue > 1.0) ? 0 : color.blue + 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!
See https://www.libsdl.org/credits.php

โ˜ฎ๏ธ Alternatives


Stars: 18
Last commit: 5 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jackโ€™d and SCRUFF. We are two of the worldโ€™s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

12 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API