raylib-rs is a Rust binding for raylib 2.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), just like all other resources in Rust. This means that "Unload" functions are not exposed (and not necessary). - 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. - A
RaylibHandle
is obtained throughraylib::init_window(...)
or through the newerinit()
function which will allow you tobuild
up some window options before initialization (replacesset_config_flags
). - Manually closing the window is unnecessary, because
CloseWindow
is automatically called whenRaylibHandle
goes out of scope. Model::set_material
,Material::set_shader
, andMaterialMap::set_texture
methods were added since one cannot set the fields directly. Also enforces correct ownership semantics.Font::from_data
,Font::set_chars
, andFont::set_texture
methods were added to create aFont
from loadedCharInfo
data.SubText
andFormatText
are 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.
- Add the dependency to your
Cargo.toml
:
[dependencies]
raylib = "0.9"
-
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 GCC/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!
use raylib::prelude::*;
fn main() {
let rl = raylib::init()
.size(640, 480)
.title("Hello, World")
.build();
while !rl.window_should_close() {
rl.begin_drawing();
rl.clear_background(Color::WHITE);
rl.draw_text("Hello, world!", 12, 12, 20, Color::BLACK);
rl.end_drawing();
}
}
- 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
&str
and/or return an ownedString
, for the sake of safety. - In C,
LoadFontData
returns a pointer to a heap-allocated array ofCharInfo
structs. 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,
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 aVec<String>
which is returned to the caller. - 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
ease
module which contains various interpolation/easing functions ported from raylib'seasings.h
, as well as aTween
struct to assist in using these functions. - Equivalent math and vector operations, ported from
raymath.h
, areimpl
ed on the various Vector and Matrix types. Operator overloading is used for more intuitive design.
- Port raylib examples over to Rust.
- More tests.
- More platform testing.
- Even more testing.
- Physac port?