MCPcopy Index your code
hub / github.com/Rust-SDL2/rust-sdl2

github.com/Rust-SDL2/rust-sdl2 @0.38.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.38.0 ↗ · + Follow
1,900 symbols 3,594 edges 106 files 362 documented · 19%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Rust-SDL2 [![Build Status][workflows-ci-img]][workflows-ci] [![crates.io badge][crates-io-badge]][crates-io-url]

Bindings for SDL2 in Rust

Changelog for 0.38.0

Overview

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.

Available rust features

  • gfx to link against SDL2_gfx and have access to gfx features
  • image to link against SDL2_image and have access to image reading and writing features
  • mixer to link against SDL2_mixer and have access to sound mixing features
  • ttf to link against SDL2_ttf and have access to various font features
  • raw-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 only
  • use_ios_framework to use SDL2 from a Framework, on iOS only
  • bundled, which pulls the SDL repository and compiles it from source. More information below.

Documentation

Read the documentation here.

Requirements

Rust

We currently target the latest stable release of Rust.

SDL2.0 development libraries

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.

"Bundled" Feature

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

Linux

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).

Static linking in Linux

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]

macOS

Homebrew

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"

MacPorts

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].

If you are using the SDL2 framework

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");

Static linking on macOS using vcpkg

Instructions to generate a static binary on macOS and other operating systems using [vcpkg][vcpkg] are [here][cargo-vcpkg-usage].

Windows (MSVC)

  1. Download MSVC development libraries from http://www.libsdl.org/ (SDL2-devel-2.0.x-VC.zip).
  2. Unpack SDL2-devel-2.0.x-VC.zip to a folder of your choosing (You can delete it afterwards).
  3. 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.

  1. 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.

  2. 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.

Static linking with MSVC

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

  • build an SDL2 static library yourself and copy it to your toolchain's lib directory; or
  • also enable the bundled feature, which will build a static library for you; or
  • use a static SDL2 library from vcpkg as described below.

Windows (MinGW)

  1. Download mingw development libraries from http://www.libsdl.org/ (SDL2-devel-2.0.x-mingw.tar.gz).
  2. Unpack to a folder of your choosing (You can delete it afterwards).
  3. Copy 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.

  1. 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.

  2. 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.

Static linking with MinGW

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).

Windows with build script

  1. Download mingw and msvc development libraries from http://www.libsdl.org/ (SDL2-devel-2.0.x-mingw.tar.gz & SDL2-devel-2.0.x-VC.zip).
  2. Unpack to folders of your choosing (You can delete it afterwards).
  3. Create the following folder structure in the same folder as your Cargo.toml:
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
  1. Copy the lib and dll files from the source archive to the directories we created in step 3 like so:
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
  1. Create a build script, if you don't already have one put this in your Cargo.toml under [package]:

    build = "build.rs"

  2. 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");
                }
            }
        }
    }
}
  1. On build the build script will copy the needed DLLs into the same directory as your Cargo.toml, you probably don't want to commit these to any Git repositories though so add the following line to your .gitignore file

/*.dll

  1. When you're publish your game make sure to copy the corresponding SDL2.dll to the same directory that your compiled exe is in, otherwise the game won't launch.

And now your project should build and run on any Windows computer!

Windows (MSVC with vcpkg)

  1. Install MS build tools and [vcpkg][vcpkg]
  2. Install the needed SDL2 libs: vcpkg.exe install sdl2-ttf:x64-windows sdl2:x64-windows
  3. Open a x64 native tools prompt (x64 Native Tools Command Prompt for VS 2019)
  4. set env vars: ``` SET PATH=%PATH%;C:\Users\my_user\dev\vcpkg\installed\x64-windows\bin SET INCLUDE=%INCLUDE%;C:\Users

Extension points exported contracts — how you extend this code

AudioFormatNum (Interface)
A phantom type for retrieving the `SDL_AudioFormat` of a given generic type. All format types are returned as native-end [6 …
src/sdl2/audio.rs
ResourceLoader (Interface)
Generic trait to Load any Resource Kind [2 implementers]
examples/resource-manager.rs
AudioCallback (Interface)
(no doc) [5 implementers]
src/sdl2/audio.rs
ToColor (Interface)
generic Color type [3 implementers]
src/sdl2/gfx/primitives.rs
RenderTarget (Interface)
Represents structs which can be the target of a `SDL_Renderer` (or Canvas). This is intended for internal use only. It [2 …
src/sdl2/render.rs
GLAttrTypeUtil (Interface)
(no doc) [3 implementers]
src/sdl2/video.rs

Core symbols most depended-on inside this repo

get_error
called by 256
src/sdl2/sdl.rs
into
called by 101
src/sdl2/render.rs
raw
called by 86
src/sdl2/ttf/font.rs
len
called by 81
src/sdl2/rwops.rs
get
called by 73
examples/game-of-life.rs
clone
called by 47
src/sdl2/sdl.rs
init
called by 45
src/sdl2/sdl.rs
raw
called by 43
src/sdl2/surface.rs

Shape

Method 922
Function 488
Class 359
Enum 117
Interface 14

Languages

Rust100%

Modules by API surface

sdl2-sys/sdl_bindings.rs494 symbols
src/sdl2/video.rs170 symbols
src/sdl2/render.rs125 symbols
src/sdl2/rect.rs125 symbols
src/sdl2/event.rs75 symbols
src/sdl2/mixer/mod.rs73 symbols
src/sdl2/audio.rs66 symbols
src/sdl2/surface.rs58 symbols
src/sdl2/controller.rs50 symbols
src/sdl2/joystick.rs48 symbols
src/sdl2/image/mod.rs47 symbols
src/sdl2/ttf/font.rs45 symbols

For agents

$ claude mcp add rust-sdl2 \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact