raylib-rs is a simple, minimal Rust binding for raylib 2.0. It currently targets the stable Rust toolchain, version 1.26 or higher.
This is more or less a 1:1 mapping of C functions to Rust functions. However, resources are automatically cleaned up when they go out of scope (or when std::mem::drop is called), just like all other resources in Rust. This means that "Unload" functions are not exposed (and not necessary). Additional changes include:
Model::set_material,Material::set_shader, andMaterialMap::set_texturemethods were added since one cannot set the fields directly. Also enforces correct ownership semantics.Font::from_data,Font::set_chars, andFont::set_texturemethods were added to create aFontfrom loadedCharInfodata.SubTextandFormatTextare omitted, and are instead covered by Rust's string slicing and Rust'sformat!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!
So far, I have only tested on Windows. Tips on making things work smoothly on all platforms is appreciated.
(the git path below is temporary until raylib-rs is published to crates.io)
- Add the dependency to your
Cargo.toml:
[dependencies]
raylib-rs = { git = "https://github.com/deltaphc/raylib-rs" }-
Download raylib 2.0 from https://github.com/raysan5/raylib/releases/tag/2.0.0, and pick the one that matches your Rust toolchain. MSVC with MSVC, MinGW with GNU, 32-bit or 64-bit.
-
Copy
libraylib.a(for MinGW) orraylib.lib(for MSVC) to the appropriate path in your Rust toolchain.- For rustup/MSVC:
.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib - For rustup/GNU:
.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib
- For rustup/MSVC:
-
Start coding!
extern crate raylib_rs;
use raylib_rs as ray; // or raylib_rs::* if you prefer
fn main() {
ray::init_window(640, 480, "raylib-rs");
while !ray::window_should_close() {
ray::begin_drawing();
ray::clear_background(ray::WHITE);
ray::draw_text("Hello, world!", 12, 12, 20, ray::BLACK);
ray::end_drawing();
}
ray::close_window();
}- Structs holding resources have RAII/move semantics, including:
Image,Texture2D,RenderTexture2D,Font,Mesh,Shader,Material,Model,Wave,Sound,Music, andAudioStream. - Functions dealing with string data take in
&strand/or return an ownedString, for the sake of safety. - In C,
LoadFontDatareturns a pointer to a heap-allocated array ofCharInfostructs. In this Rust binding, said array is copied into an ownedVec<CharInfo>, the original data is freed, and the owned Vec is returned. - In C,
GetDroppedFilesreturns a pointer to an array of strings owned by raylib. Again, for safety and also ease of use, this binding copies said array into aVec<String>which is returned to the caller. TraceLogfunctionality is actually reimplemented in Rust since the language does not have variadic functions in safe code, nor a way to unpack sequences into arguments (to my knowledge). However, sinceformat!is variadic, one could use that macro in combination withtrace_logto achieve the same effect.- The raw FFI binding was initially generated by
bindgenand then hand-tweaked a bit. - I've tried to make linking automatic, though I've only tested on Windows. Other platforms may have other considerations.
- In addition to the base library, there is also a convenient
easemodule which contains various interpolation/easing functions ported from raylib'seasings.h, as well as aTweenstruct to assist in using these functions. - Equivalent math and vector operations, ported from
raymath.h, areimpled on the various Vector and Matrix types. Operator overloading is used for more intuitive design.
- Port raylib examples over to Rust.
- Perhaps use Rust enums instead of consts.
- More tests.
- More platform testing.
- Even more testing.
- Physac port?