Swift SDL2
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
- Swift Package Manager (SPM)
- Swiftlint for linting - (optional)
💻 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: [
.target(
name: "YourTargetName",
dependencies: ["SDL2"])
]
)
Since it's a system library wrapper you need to install the SDL2 library either via
brew install sdl2
or
apt-get install libsdl2-dev
depending on your platform.
📝 Code Example
🤘 Minimal Metal example application
import SDL2
import Metal
import class QuartzCore.CAMetalLayer
SDL_Init(SDL_INIT_VIDEO)
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "metal")
SDL_InitSubSystem(SDL_INIT_VIDEO)
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
default:
break
}
}
guard let surface = layer.nextDrawable() else {
break
}
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")
}
encoder.endEncoding()
buffer.present(surface)
buffer.commit()
}
SDL_DestroyRenderer(renderer)
SDL_DestroyWindow(window)
SDL_Quit()
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.
- SDL2 licensed under zlib license
🙏 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
- https://github.com/PureSwift/CSDL2
- https://github.com/jaz303/CSDL2.swift
- https://github.com/latencyzero/CSDL2
- https://github.com/lightive/CSDL2
- https://github.com/sunlubo/CSDL2
- https://github.com/KevinVitale/SwiftSDL
- https://github.com/adagio/swiftsdl2
- https://github.com/latencyzero/SwiftSDL
- https://github.com/sunlubo/SwiftSDL2
- https://github.com/SwiftSDL/Demo