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
These instructions will get your copy of the project up and running on your local machine and provide a code example.
- Swift Package Manager (SPM)
- Swiftlint for linting - (optional)
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.
A minimal example is located at Sources/Demos/Minimal.
import SDL2
// Initialize SDL video systems
guard SDL_Init(SDL_INIT_VIDEO) == 0 else {
fatalError("SDL could not initialize! SDL_Error: \(String(cString: SDL_GetError()))")
}
// Create a window at the center of the screen with 800x600 pixel resolution
let window = SDL_CreateWindow(
"SDL2 Minimal Demo",
Int32(SDL_WINDOWPOS_CENTERED_MASK), Int32(SDL_WINDOWPOS_CENTERED_MASK),
800, 600,
SDL_WINDOW_SHOWN.rawValue)
var quit = false
var event = SDL_Event()
// Run until app is quit
while !quit {
// Poll for (input) events
while SDL_PollEvent(&event) > 0 {
// if the quit event is triggered ...
if event.type == SDL_QUIT.rawValue {
// ... quit the run loop
quit = true
}
}
// wait 100 ms
SDL_Delay(100)
}
// Destroy the window
SDL_DestroyWindow(window)
// Quit all SDL systems
SDL_Quit()
There is another demo displaying a SDL2 demo window at Sources/Demos/MetalApp.
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
We use SemVer for versioning. For the versions available, see the tags on this repository.
See also the list of contributors who participated in this project.
This project is licensed under the zlib License - see the LICENSE file for details.
- SDL2 licensed under zlib license
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
- 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