README.md
raylib-rs is a Rust binding for raylib 5.5. It currently targets Rust toolchain version 1.78 or higher.
Please checkout the showcase directory to find usage examples!
Though this binding tries to stay close to the simple C API, it makes some changes to be more idiomatic for Rust.
</td> </tr> </table>Most development happens over at: https://github.com/raylib-rs/raylib-rs
Versions normally match Raylib's own, with the minor number incremented for any patches (i.e. 5.5.1 for Raylib v5.5). On occasion, if enough breaking changes are made in between Raylib releases, we'll release a 5.6, which is 5.5 but with breaking changes.
| API | Windows | Linux | macOS | Web | Android |
|---|---|---|---|---|---|
| core | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: |
| rgui | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | ❔ | :x: |
| rlgl | :heavy_check_mark: | :x: | :x: | ❔ | :x: |
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
Cargo.toml:[dependencies]
raylib = { version = "5.7.0", features = [] }
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);
}
}
To use raylib-rs on NixOS there's a provided nix-shell file shell.nix at the root of the repo that should get you up and running, which can be used like so:
nix-shell ./shell.nix
You'll also need to enable the Wayland feature on the raylib crate:
cargo add raylib -F wayland
Contributions are welcome to improve or fix the shell.nix!
git clone --recurse-submodulescargo buildlibglfw3-dev wayland-devel libxkbcommon-devel wayland-protocols wayland-protocols-devel libecm-devfeatures=["wayland"] to your dependency definitioncrossCross compiling with raylib-rs can be made easier with cross. See more on the wiki
cd samplescargo run --bin 3d_camera_first_persondefault-features = false, but beware that there are mandatory flags that when compiled without will break raylib(such as SUPPORT_STANDARD_FILEIO)cargo test and cargo test --doc while in raylib safe bindings directory and make sure no tests failcd samples and cargo run --bin <sample_name>#[inline] , #[must_use] , and constThe raygui.h file has to have this ifdef modified to point to where raylib.h is:
#if !defined(RAYGUI_STANDALONE)
#include "../raylib/src/raylib.h"
#endif
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()).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!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 synchronization primitives (Mutex, Arc) etc.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.Image, Texture2D, RenderTexture2D, Font, Mesh, Shader, Material, and Model.Wave, Sound, Music, and AudioStream have lifetimes bound to AudioHandle.&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.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.LoadDroppedFiles 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.["opengl_33"], ["opengl_21"] or ["opengl_es_20] to the features array in your Cargo.toml dependency definition.["drm", "opengl_es_20"] to features. Note that drm should usually be used together with opengl_es_20.The raylib-test crate tests the bindings by opening a window, and checking the results of various functions. It requires nightly to use.
All contributions are welcome. Chat about raylib on discord