Window and drawing
The window is opened by calling raylib::init().build(). The builder returns a
(RaylibHandle, RaylibThread) pair that controls the entire lifecycle: the handle
exposes every per-frame operation, and the thread token is the proof that you are on
the correct OS thread. Drawing happens between begin_drawing and the end of the
returned guard’s lifetime — all draw primitives live on the RaylibDraw trait
implemented by RaylibDrawHandle.
API surface
RaylibBuilder— fluent builder returned byraylib::init(); call.build()to open the window. Key builder methods:.size(w, h),.title(s),.vsync(),.msaa_4x(),.undecorated().RaylibHandle— the main handle; owns the window and is the receiver for most API calls.RaylibHandle::set_target_fps— cap the frame rate.set_target_fps(0)disables the limiter (busy-spin).RaylibHandle::begin_drawing— returns a scopedRaylibDrawHandle;EndDrawingfires automatically on drop.RaylibDraw— trait with all 2D drawing primitives:clear_background,draw_text,draw_rectangle,draw_line,draw_circle,draw_texture, and more.RaylibHandle::window_should_close— returnstruewhen the OS close event has been sent.
Example
extern crate raylib;
use raylib::prelude::*;
fn main() {
let (mut rl, thread) = raylib::init()
.size(640, 480)
.title("Hello, raylib-rs!")
.vsync()
.build();
rl.set_target_fps(60);
while !rl.window_should_close() {
let mut d = rl.begin_drawing(&thread);
d.clear_background(Color::RAYWHITE);
d.draw_text("Hello, world!", 12, 12, 20, Color::DARKGRAY);
}
// RaylibHandle is dropped here; CloseWindow() is called automatically.
}
Gotchas
- Draw only inside the guard. All methods on
RaylibDraware only accessible on theRaylibDrawHandlescope. You cannot calldraw_textdirectly onRaylibHandle— you must enterbegin_drawingfirst. - FPS pacing.
set_target_fps(0)removes the frame limiter entirely; raylib will busy-spin. Omitting the call defaults to the raylib default (60 on most platforms). - Window state queries.
is_window_minimized,is_window_resized, and related helpers live on theRaylibWindowtrait — check the trait’s documentation for the current set of methods. RaylibThreadis!Send. Never put it in aMutex,Arc, or hand it to another thread. See the Handle and thread chapter for why.
See also
- Handle and thread — lifecycle details and
the
!Sendinvariant. - Input — reading keyboard, mouse, and gamepad inside the loop.
- Shapes — the full 2D primitive catalogue.
Showcase examples
Showcase examples that exercise this module: