MCPcopy Index your code
hub / github.com/deltaphc/raylib-rs

github.com/deltaphc/raylib-rs @v3.5.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.5.0 ↗ · + Follow
3,383 symbols 8,977 edges 331 files 726 documented · 21%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

logo

rust crates.io docs discord

raylib-rs

raylib-rs is a Rust binding for raylib 3.0. It currently targets the stable Rust toolchain, version 1.31 or higher.

Though this binding tries to stay close to the simple C API, it makes some changes to be more idiomatic for Rust.

  • Resources are automatically cleaned up when they go out of scope (or when std::mem::drop is called). This is essentially RAII. This means that "Unload" functions are not exposed (and not necessary unless you obtain a Weak resource using make_weak()).
  • Most of the Raylib API is exposed through RaylibHandle, which is for enforcing that Raylib is only initialized once, and for making sure the window is closed properly. RaylibHandle has no size and goes away at compile time. Because of mutability rules, Raylib-rs is thread safe!
  • A RaylibHandle and RaylibThread are obtained through raylib::init_window(...) or through the newer init() function which will allow you to build up some window options before initialization (replaces set_config_flags). RaylibThread should not be sent to any other threads, or used in a any syncronization primitives (Mutex, Arc) etc.
  • Manually closing the window is unnecessary, because CloseWindow is automatically called when RaylibHandle goes out of scope.
  • Model::set_material, Material::set_shader, and MaterialMap::set_texture methods were added since one cannot set the fields directly. Also enforces correct ownership semantics.
  • Font::from_data, Font::set_chars, and Font::set_texture methods were added to create a Font from loaded CharInfo data.
  • SubText and FormatText are omitted, and are instead covered by Rust's string slicing and Rust's format! macro, respectively.

Disclaimer: I created this binding as a way to learn Rust. There may be some things I can do better, or make more ergonomic for users. Feel free to make suggestions!

Installation

Supported Platforms

API Windows Linux macOS Web Android Raspberry Pi
core :heavy_check_mark: :heavy_check_mark: :heavy_check_mark: :construction:
rgui :heavy_check_mark: :heavy_check_mark: :heavy_check_mark:
physac :construction: :construction: :construction:
rlgl :x: :heavy_check_mark: :heavy_check_mark:

Build Dependencies

Requires glfw, cmake, and curl. Tips on making things work smoothly on all platforms is appreciated. Follow instructions for building raylib for your platform here

  1. Add the dependency to your Cargo.toml:
[dependencies]
raylib = "3.0"
  1. Start coding!
use raylib::prelude::*;

fn main() {
    let (mut rl, thread) = raylib::init()
        .size(640, 480)
        .title("Hello, World")
        .build();

    while !rl.window_should_close() {
        let mut d = rl.begin_drawing(&thread);

        d.clear_background(Color::WHITE);
        d.draw_text("Hello, world!", 12, 12, 20, Color::BLACK);
    }
}

Tech Notes

  • Structs holding resources have RAII/move semantics, including: Image, Texture2D, RenderTexture2D, Font, Mesh, Shader, Material, Model, Wave, Sound, Music, and AudioStream.
  • Functions dealing with string data take in &str and/or return an owned String, for the sake of safety. The exception to this is the gui draw functions which take &CStr to avoid per frame allocations. The rstr! macro helps make this easy.
  • In C, LoadFontData returns a pointer to a heap-allocated array of CharInfo structs. In this Rust binding, said array is copied into an owned Vec<CharInfo>, the original data is freed, and the owned Vec is returned.
  • In C, GetDroppedFiles returns a pointer to an array of strings owned by raylib. Again, for safety and also ease of use, this binding copies said array into a Vec<String> which is returned to the caller.
  • I've tried to make linking automatic, though I've only tested on Windows 10, Ubuntu, and MacOS 15. Other platforms may have other considerations.

Extras

  • In addition to the base library, there is also a convenient ease module which contains various interpolation/easing functions ported from raylib's easings.h, as well as a Tween struct to assist in using these functions.
  • Equivalent math and vector operations, ported from raymath.h, are impled on the various Vector and Matrix types. Operator overloading is used for more intuitive design.

Testing

The raylib-test crate tests the bindings by opening a window, and checking the results of various functions. It requires nightly to use.

Future Goals

  • Port raylib examples over to Rust.
  • More tests.
  • More platform testing.
  • Even more testing.
  • Physac port?

Contribution & Support

All contributions are welcome. Chat about raylib on discord

Extension points exported contracts — how you extend this code

RaylibDraw (Interface)
(no doc) [8 implementers]
raylib/src/core/drawing.rs
Testable (Interface)
(no doc) [3 implementers]
raylib-test/src/tests.rs
RectExt (Interface)
We can add custom methods to raylib types with extention traits [1 implementers]
samples/roguelike.rs
ShaderV (Interface)
(no doc) [12 implementers]
raylib/src/core/shaders.rs
AudioSample (Interface)
A marker trait specifying an audio sample (`u8`, `i16`, or `f32`). [3 implementers]
raylib/src/core/audio.rs
RaylibDraw3D (Interface)
(no doc) [4 implementers]
raylib/src/core/drawing.rs
RaylibTexture2D (Interface)
(no doc) [4 implementers]
raylib/src/core/texture.rs

Core symbols most depended-on inside this repo

GuiGetStyle
called by 763
showcase/original/shapes/raygui.h
GuiGetStyle
called by 579
showcase/src/example/shapes/raygui.h
GuiGetStyle
called by 472
raylib-sys/raygui.h
Fade
called by 375
showcase/original/shapes/raygui.h
draw_text
called by 288
raylib/src/core/texture.rs
GetColor
called by 271
showcase/original/shapes/raygui.h
into
called by 253
raylib/src/core/math.rs
rrect
called by 186
raylib/src/core/math.rs

Shape

Function 1,973
Method 702
Class 488
Enum 197
Interface 23

Languages

Rust54%
C++24%
TypeScript15%
C7%

Modules by API surface

docs/raylib-showcase.js524 symbols
raylib-sys/bindings_osx.rs306 symbols
raylib-sys/bindings_windows.rs175 symbols
raylib-sys/bindings_web.rs163 symbols
raylib-sys/bindings_linux.rs163 symbols
raylib-sys/rlgl.h140 symbols
raylib/src/core/drawing.rs102 symbols
raylib-sys/raymath.h101 symbols
showcase/original/shapes/raygui.h96 symbols
raylib/src/core/texture.rs91 symbols
raylib-sys/raygui.h86 symbols
samples/roguelike.rs83 symbols

For agents

$ claude mcp add raylib-rs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page