Skip to content

KevinVitale/SwiftSDL

Repository files navigation

SwiftSDL — Cross-Platform Targets with Swift & SDL3

Overview

SwiftSDL is an open-source Swift library that provides a convenient interface for working with the C-based SDL (Simple DirectMedia Layer) library. This wrapper allows developers to leverage SDL's cross-platform multimedia and game development capabilities in Swift applications across macOS, Linux, Windows, and iOS.

The library integrates seamlessly with the Swift Package Manager (SPM), making it simple to manage dependencies and build your projects without requiring a framework file on macOS. In addition, the library includes a sample application for iOS to demonstrate how to integrate SDL within iOS apps using a bridging header.

Features

  • Swift wrapper for the SDL library (vers. 3), exposing SDL's core functionality in an easy-to-use Swift API.
  • Cross-platform support for macOS, Linux, and Windows, with simplified project management using the Swift Package Manager.
  • iOS support using Objective-C to Swift bridging for SDL integration.
  • A sample iOS application that demonstrates how to set up and use SDL3 with this library.

Sample Code

Below is a basic example of how you can use SwiftSDL to initialize an SDL window in Swift:

Example.swift Output
import SwiftSDL

@main final class Example: Game {
  func onReady(window: any Window) throws(SDL_Error) { }
  func onUpdate(window: any Window, _ delta: Uint64) throws(SDL_Error) {
    let surface = try window.surface.get()
    try surface.clear(color: .red)
    try window.updateSurface()
  }
  func onEvent(window: any Window, _ event: SDL_Event) throws(SDL_Error) { }
  func onShutdown(window: any SwiftSDL.Window) throws(SwiftSDL.SDL_Error) { }
}
Screenshot 2024-10-26 at 2 30 44 PM

Installation

Swift Package Manager (SPM)

You can add SwiftSDL as a dependency in your project by adding the following to your Package.swift file:

// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
  name: "SwiftSDLTest",
  platforms: [.macOS(.v10_15)],
  dependencies: [
    .package(url: "https://github.com/KevinVitale/SwiftSDL.git", from: "0.2.0-alpha.4"),
  ],
  targets: [
    .executableTarget(
      name: "SwiftSDLTest",
      dependencies: ["SwiftSDL"],

      // Optional: bundle resources!
      resources: [
        .process("../Resources/BMP")
      ],

      // Required: when using SPM YOU MUST
      // have libSDL3.{dylib|so|a} installed.
      linkerSettings: [.unsafeFlags(
        [
          "-Xlinker", "-F", "-Xlinker", "/usr/local/lib",
          "-Xlinker", "-rpath", "-Xlinker", "/usr/local/lib",
        ], .when(platforms: [.macOS])
      )]
    ),
  ]
)

CMake (for non-SPM platforms)

// TODO

Platform-Specific Instructions

macOS

Linux

Windows

iOS

Contributions

Fix Me: Something like:

We welcome contributions from the community! Feel free to fork the repository, submit issues, or create pull requests. Be sure to follow the contribution guidelines and coding standards outlined in CONTRIBUTING.md.

License

SwiftSDL is open-sourced under the MIT license. See the LICENSE file for details.

Contact

Fix Me: Something like:

For questions or further assistance, please reach out via GitHub issues or contact the maintainers directly at email@example.com.


With SwiftSDL, you can effortlessly bring SDL's cross-platform multimedia power into the world of Swift!