Skip to content
Tech News
← Back to articles

Testing the Swift C compatibility with Raylib (+WASM)

read original get Raylib Programming Book → more articles
Why This Matters

This article highlights the growing potential of Swift in the broader programming ecosystem, demonstrating its seamless interoperability with C libraries like Raylib without the need for manual FFI bindings. Such advancements can simplify cross-platform development and challenge misconceptions about Swift's accessibility and versatility beyond Apple platforms, offering developers more flexible options for game development and system programming.

Key Takeaways

Since Ladybird team abandoned their Swift adoption for the browser I heard a lot of criticism about the Swift ecosystem and the interaction between Swift and C/C++ projects.

My usage of Swift is mainly for command line tools, recreational programming (like Advent of Code 2023 and previous years) or Metal programming.

In my previous experiments I really enjoyed Swift, and actually preferred it to some other programming languages like Rust. However it seems that programmers have wrong opinions about this programming language, especially about its accessibility (no it is not only for Apple platforms) and its actual power wrapping C/C++ libraries.

Today, I will demonstrate how easy I built a very basic Raylib game using Swift, with no FFI, and for macOS and web (using WASI).

This article is for demonstration purposes, and is not a tutorial. To this end I will not explain how to install Swift, the WASM SDK for Swift, etc.

However, if you are interested in reproducing this demonstration at home, you can find the finished project on my github and adapt for your own needs.

Swift <3 Raylib #

Unlike other languages, Swift does not require you to write manual FFI bindings or wrapper layers to interact with C code. FFIs are engineered to be completely invisible and automatic via the Clang importer (more about that later).

This means you can directly drop your C headers in your project, the static C library, and use the power of the Swift Package Manager to tell the compiler how the project needs to be compiled.

The code I want to run is very simple: initialize raylib, a window, and drawing a text.

... continue reading