Bindings for SDL2 in Rust
Rust-SDL2 is a library for talking to the new SDL2.0 libraries from Rust. Low-level C components are wrapped in Rust code to make them more idiomatic and abstract away inappropriate manual memory management.
Rust-SDL2 uses the MIT license, but SDL2 itself is under the zlib license.
gfx to link against SDL2_gfx and have access to gfx featuresimage to link against SDL2_image and have access to image reading and writing featuresmixer to link against SDL2_mixer and have access to sound mixing featuresttf to link against SDL2_ttf and have access to various font featuresraw-window-handle to enable the crate raw-window-handle, which is useful to interop with various other backends.unsafe-textures to not have a lifetime in Texture structs. Texture are only freed when the program exits, or can be done manually through unsafe.use-bindgen to customize bindings instead of using pre-generated sdl_bindings which were created from a Linux environment. It generates your own custom SDL2 bindings, tailored to your distro. Useful for specific window-related scenarios.use-vcpkg to pull SDL2 from vcpkg instead of looking in your system.use-pkgconfig use pkg-config to detect where your library is located on your system. Mostly useful on unix systems for static linking.static-link to link to SDL2 statically instead of dynamically.use_mac_framework to use SDL2 from a Framework, on macOS onlyuse_ios_framework to use SDL2 from a Framework, on iOS onlybundled, which pulls the SDL repository and compiles it from source. More information below.We currently target the latest stable release of Rust.
SDL2 >= 2.0.26 is recommended to use these bindings; below 2.0.26, you may experience link-time errors as some functions are used here but are not defined in SDL2. If you experience this issue because you are on a LTS machine (for instance, Ubuntu), we definitely recommend you to use the feature "bundled" which will compile the lastest stable version of SDL2 for your project.
Since 0.31, this crate supports a feature named "bundled" which compiles SDL2 from source and links it automatically. While this should work for any architecture, you will need a C compiler (like gcc, clang, or MS's own compiler) to use this feature properly.
By default, macOS and Linux only load libraries from system directories like /usr/lib. If you wish to distribute the newly built libSDL2.so/libSDL2.dylib alongside your executable, you will need to add rpath to your executable. Add the following lines to build.rs script:
#[cfg(target_os="macos")]
println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path");
#[cfg(target_os="linux")]
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN");
This ONLY works with SDL2, NOT SDL2_image, SDL2_mixer, SDL2_ttf, SDL2_gfx
Install these through your favourite package management tool, or via http://www.libsdl.org/
Ubuntu example:
sudo apt-get install libsdl2-dev
Fedora example:
sudo dnf install SDL2-devel
Arch example:
(Arch doesn't have separate regular and development packages, everything goes together.)
sudo pacman -S sdl2
You might also need a C compiler (gcc).
You can choose to link SDL2 statically instead of dynamically with the static-link feature.
On Linux, you will need to additionally do one of the following:
* use the bundled feature
* use the feature use-pkgconfig so that rustc knows where to look for your SDL2 libraries and its dependencies for static linking. This is required because there is no built-in way to find the resources needed to link statically SDL2 from your system
* install development libraries with [vcpkg][vcpkg]. Instructions to generate a static binary on Linux and other operating systems using vcpkg are [here][cargo-vcpkg-usage]
On macOS, it's a good idea to install these via [homebrew][homebrew].
brew install sdl2
In recent versions of Homebrew, the installed libraries are usually linked into $(brew --prefix)/lib.
If you are running an older version, the symlink for SDL might reside in /usr/local/lib.
To make linking libraries installed by Homebrew easier, do the following for your respective shell.
Add this line to your ~/.zshenv or ~/.bash_profile depending on whether you use ZSH or Bash.
export LIBRARY_PATH="$LIBRARY_PATH:$(brew --prefix)/lib"
You can also get sdl2 via macports.
sudo port install libsdl2
Then add the following to your ~/.bash_profile if not already present.
export LIBRARY_PATH="$LIBRARY_PATH:/opt/local/lib/"
If you're having issues with either Homebrew or MacPorts, [see here][pdev-issue].
You can download and install the SDL2 Mac OS X framework from: https://www.libsdl.org/download-2.0.php
To make the sdl2 crate link with the SDL2 framework, you will need to enable
the use_mac_framework feature. To build and test the sdl2 crate with this
feature, use:
cargo test --features use_mac_framework
To depend on the sdl2 crate with this feature enabled, put the following in
your project's Cargo.toml file:
[dependencies.sdl2]
features = ["use_mac_framework"]
version = ... # Whichever version you are using
Alternatively, you can re-export the feature in your package by putting the
following in your Cargo.toml file:
[features]
default = []
use_sdl2_mac_framework = ["sdl2/use_mac_framework"]
Similarly for iOS you can follow the same process using the use_ios_framework feature. However
official builds of the iOS framework are not available so you must compile your own SDL2.framework.
Using the iOS framework also requires adding the 'Frameworks' directory to your rpath so that the
dynamic linker can find SDL2.framework inside your app bundle. This is done by adding this to your
build.rs:
#[cfg(target_os="ios")]
println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path/Frameworks");
Instructions to generate a static binary on macOS and other operating systems using [vcpkg][vcpkg] are [here][cargo-vcpkg-usage].
Copy all lib files from > SDL2-devel-2.0.x-VC\SDL2-2.0.x\lib\x64\
to (for Rust 1.6 and above)
C:\Program Files\Rust\lib\rustlib\x86_64-pc-windows-msvc\lib
or to (for Rust versions 1.5 and below)
C:\Program Files\Rust\bin\rustlib\x86_64-pc-windows-msvc\lib
or to your library folder of choice, and ensure you have a system environment variable of
LIB = C:\your\rust\library\folder
For Rustup users, this folder will be in
C:\Users\{Your Username}\.rustup\toolchains\{current toolchain}\lib\rustlib\{current toolchain}\lib
Where current toolchain is likely stable-x86_64-pc-windows-msvc.
Copy SDL2.lib and SDL2.dll from
> SDL2-devel-2.0.x-VC\SDL2-2.0.x\lib\x64\
into your cargo project, right next to your Cargo.toml.
When you're shipping your game make sure to copy SDL2.dll to the same directory that your compiled exe is in, otherwise the game won't launch.
The MSVC development libraries provided by http://libsdl.org/ don't include a static library. This means that if you want to use the static-link feature with the windows-msvc toolchain, you have to do one of
lib directory; orbundled feature, which will build a static library for you; orCopy all lib files from > SDL2-devel-2.0.x-mingw\SDL2-2.0.x\x86_64-w64-mingw32\lib
to (for Rust 1.6 and above)
C:\Program Files\Rust\lib\rustlib\x86_64-pc-windows-gnu\lib
or to (for Rust versions 1.5 and below)
C:\Program Files\Rust\bin\rustlib\x86_64-pc-windows-gnu\lib
or to your library folder of choice, and ensure you have a system environment variable of
LIBRARY_PATH = C:\your\rust\library\folder
For Rustup users, this folder will be in
C:\Users\{Your Username}\.rustup\toolchains\{current toolchain}\lib\rustlib\{current toolchain}\lib
Where current toolchain is likely stable-x86_64-pc-windows-gnu.
Copy SDL2.dll from > SDL2-devel-2.0.x-mingw\SDL2-2.0.x\x86_64-w64-mingw32\bin
into your cargo project, right next to your Cargo.toml.
When you're shipping your game make sure to copy SDL2.dll to the same directory that your compiled exe is in, otherwise the game won't launch.
If you want to use the static-link feature with the windows-gnu toolchain, then you will also need the following libraries:
libimm32.a
libversion.a
libdinput8.a
libdxguid.a
These files are not currently included with the windows-gnu toolchain, but can be downloaded here. For the x86_64 toolchain, you want the x86_64-win32-seh package, and for i686 you want the i686-win32-dwarf one.
You will find the aforementioned libraries under mingw64/x86_64-w64-mingw32/lib/ (for x86_64) or mingw32/i686-w64-mingw32/lib/ (for i686). Copy them to your toolchain's lib directory (the same one you copied the SDL .a files to).
gnu-mingw\dll\32
gnu-mingw\dll\64
gnu-mingw\lib\32
gnu-mingw\lib\64
msvc\dll\32
msvc\dll\64
msvc\lib\32
msvc\lib\64
SDL2-devel-2.0.x-mingw.tar.gz\SDL2-2.0.x\i686-w64-mingw32\bin -> gnu-mingw\dll\32
SDL2-devel-2.0.x-mingw.tar.gz\SDL2-2.0.x\x86_64-w64-mingw32\bin -> gnu-mingw\dll\64
SDL2-devel-2.0.x-mingw.tar.gz\SDL2-2.0.x\i686-w64-mingw32\lib -> gnu-mingw\lib\32
SDL2-devel-2.0.x-mingw.tar.gz\SDL2-2.0.x\x86_64-w64-mingw32\lib -> gnu-mingw\lib\64
SDL2-devel-2.0.8-VC.zip\SDL2-2.0.x\lib\x86\*.dll -> msvc\dll\32
SDL2-devel-2.0.8-VC.zip\SDL2-2.0.x\lib\x64\*.dll -> msvc\dll\64
SDL2-devel-2.0.8-VC.zip\SDL2-2.0.x\lib\x86\*.lib -> msvc\lib\32
SDL2-devel-2.0.8-VC.zip\SDL2-2.0.x\lib\x64\*.lib -> msvc\lib\64
Create a build script, if you don't already have one put this in your Cargo.toml under [package]:
build = "build.rs"
Create a file in the same directory as Cargo.toml called build.rs (if you didn't already have a build script) and paste this into it:
use std::env;
use std::path::PathBuf;
fn main() {
let target = env::var("TARGET").unwrap();
if target.contains("pc-windows") {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let mut lib_dir = manifest_dir.clone();
let mut dll_dir = manifest_dir.clone();
if target.contains("msvc") {
lib_dir.push("msvc");
dll_dir.push("msvc");
}
else {
lib_dir.push("gnu-mingw");
dll_dir.push("gnu-mingw");
}
lib_dir.push("lib");
dll_dir.push("dll");
if target.contains("x86_64") {
lib_dir.push("64");
dll_dir.push("64");
}
else {
lib_dir.push("32");
dll_dir.push("32");
}
println!("cargo:rustc-link-search=all={}", lib_dir.display());
for entry in std::fs::read_dir(dll_dir).expect("Can't read DLL dir") {
let entry_path = entry.expect("Invalid fs entry").path();
let file_name_result = entry_path.file_name();
let mut new_file_path = manifest_dir.clone();
if let Some(file_name) = file_name_result {
let file_name = file_name.to_str().unwrap();
if file_name.ends_with(".dll") {
new_file_path.push(file_name);
std::fs::copy(&entry_path, new_file_path.as_path()).expect("Can't copy from DLL dir");
}
}
}
}
}
/*.dll
And now your project should build and run on any Windows computer!
vcpkg.exe install sdl2-ttf:x64-windows sdl2:x64-windows$ claude mcp add rust-sdl2 \
-- python -m otcore.mcp_server <graph>