Swiftpack.co - Package - libergo/SwiftSDL

Swift SDL

SwiftSDL aims to provide a Swift overlay to SDL - the Simple DirectMedia Layer commonly used to power game engines and other cross-platform multimedia applications. This project is in the early stages, but should be more or less usable in your applications.

Using SwiftSDL

Getting Started

Simply add:

.package(url: "https://github.com/libergo/SwiftSDL", from: "0.0.1")

to your project's Package.swift. It will be necesary to have SDL available as a system library.

Using SDL

SwiftSDL takes an incremental approach to wrapping SDL to make it usable from Swift. All SDL_ methods & public macros should be available on the SDL namespace with very minimal changes to naming.

The only real changes made include:

  1. Using named parameters where they make sense and help differentiate parameters. This does mean some methods sound a bit redundant - this is by design.

  2. Minimizing inout pointer passing, preferring to utilize multiple return values when appropriate.

  3. Automatically propagating errored SDL calls into throwing methods. (All methods marked with throws will return an SDLError struct including the original message from SDL, as well as some information about the call site).

  4. Automatically bridge Swift arrays to C pointers, removing redundant count parameters.

  5. Some attempt has been made to reduce the amount of Unsafe[Mutable|Raw|etc...]Pointers exposed as part of the SwiftSDL API, but a lot of work remains to make this more Swift-native.

  6. Many raw Uint32, int, etc... parameters have been rolled into appropriate RawRepresentable structs, so you know what values are valid to pass.

--

As a result, you can follow along with C tutorials (or port existing C code) with minimal (and hopefully obvious) code changes. For instance:

	int result = SDL_Init(SDL_INIT_VIDEO);
	if (result != 0) {
		return 1;
	}
	
	SDL_Window *window = SDL_CreateWindow("Hello SDL!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
	if (win == nullptr) {
		return 1;
	}

	SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

becomes

    try SDL.initialize(.video)
    let window = try SDL.createWindow(title: "Hello SwiftSDL!", x: .fixed(100), y: .fixed(100), width: 800, height: 600, flags: [.shown])
    let renderer = try SDL.createRenderer(window: window, index: -1, flags: [.accelerated, .presentVsync])

Which is Swift code that should be familiar to people who have worked with SDL in the past, while utilizing some of Swift's features to enhance legibility.

The various classes & structs have helper methods in order to try to make working with SDL* objects feel more like standard Swift code, and less like C code in Swift.

SDL* classes like SDLWindow will automatically free their internal pointers, so it is not necessary to call SDL.destroyWindow(window:) - though if you do, SwiftSDL will be smart enough to not double-free the pointer.

API Coverage

Most of the basic SDL API is currently covered, though many methods haven't been made particularly ergonomic in Swift at this point. Methods whose implementation is simply SDLUnimplemented() will fail at runtime. Removing the need for SDLUnimplemented() is one of the primary first goals of this project.

OpenGL / Metal / Vulkan integration methods have not been mapped as of yet, but will be soon.

C-Interop

Most (if not all) types added by SwiftSDL are backed by their C-native SDL primitives, and as such can be bridged back and forth to C with minimal cost.

func toSDL()

Will return the native SDL primitive.

static func from(sdl:)

Will return a wrapped copy of an existing SDL primitive.

Contributing

Pull requests, bug reports & suggestions are all appreciated. Anyone who works on this project is expected to uphold the contributor covenant code of conduct. You can read more here.

Github

link
Stars: 0

Dependencies

Used By

Total: 0

Releases

v0.0.4 - 2020-10-21 03:13:11

  • Fixes mapping the wrong mouse event states to SDLMouseButtonState

v0.0.3 - 2020-10-21 02:58:43

  • Mouse button state should now be mapped to SDLMouseButtonState
  • SDLMouseWheelEvent.direction is now SDLMouseWheelDirection

v0.0.2 - 2020-10-21 00:19:19

  • SDLDisplayEvent.event is now SDLDisplayEventID
  • SDLWindowEvent.event is now SDLWindowEventID
  • SDLDollarGestureEvent.touchID is now a SDLTouchID (was SDL_TouchID, both are aliases)
  • SDLKeyboardEvent.repeat is now a bool to better represent it's semantic meaning.

v0.0.1 - 2020-10-15 22:26:35

Initial SDL overlay