Skip to content

raylib

stable

2D/3D game and graphics toolkit wrapping the raylib C library, providing window management, drawing primitives, input handling, audio, textures, text, models, and camera control.

use plugin raylib::{init_window, close_window, window_should_close, …}
21 functions Graphics
/ filter jk navigate Esc clear
Functions (21)
  1. init_window Open a window with given size and title
  2. close_window Close the window and free resources
  3. window_should_close True if window close was requested
  4. begin_drawing Begin frame rendering
  5. clear_background Fill background with a color
  6. set_target_fps Set desired frame rate
  7. color Build a packed color from r, g, b, a
  8. draw_rectangle Draw a filled rectangle
  9. draw_circle Draw a filled circle
  10. draw_line Draw a line between two points
  11. draw_text Draw text at a position
  12. get_frame_time Seconds elapsed since last frame
  13. get_time Total elapsed time in seconds
  14. get_fps Current frames per second
  15. is_key_pressed True if key was pressed this frame
  16. is_key_down True while key is held
  17. is_mouse_button_pressed True if mouse button pressed this frame
  18. get_mouse_position Current mouse cursor position
  19. take_screenshot Save a screenshot to a file
  20. get_screen_width Current window/screen width
  21. set_window_title Change the window title

Open a window with given size and title

Opens a new window. Must be called before any drawing or input functions.

use plugin raylib::{init_window, close_window, window_should_close,
                    begin_drawing, end_drawing, clear_background,
                    set_target_fps, WHITE}

init_window(800, 600, "My Game")
set_target_fps(60)

while !window_should_close() {
  begin_drawing()
  clear_background(WHITE())
  end_drawing()
}
close_window()

Close the window and free resources

Closes the window and frees all raylib resources. Call this at the end of your program.

close_window()

True if window close was requested

Returns true when the user presses the close button or the escape key (unless overridden with set_exit_key).

while !window_should_close() {
  // game loop
}

Begin frame rendering

Must bracket all drawing calls each frame. end_drawing swaps the back buffer to the screen.

begin_drawing()
clear_background(BLACK())
draw_text("Hello", 10, 10, 20, WHITE())
end_drawing()

Fill background with a color

Fills the entire frame with color. Call at the start of each frame inside begin_drawing.

use plugin raylib::{clear_background, RAYWHITE}

clear_background(RAYWHITE())

Set desired frame rate

Hints raylib to cap the frame rate. Does not guarantee exact timing.

set_target_fps(60)

Build a packed color from r, g, b, a

Packs RGBA byte values (0–255) into a single integer used by all drawing functions.

use plugin raylib::{color, draw_rectangle}

let red = color(255, 0, 0, 255)
draw_rectangle(10, 10, 100, 50, red)

Draw a filled rectangle

Draws a filled rectangle at pixel position (x, y).

use plugin raylib::{draw_rectangle, RED}

draw_rectangle(50, 50, 200, 100, RED())

Draw a filled circle

Draws a filled circle centered at (cx, cy).

use plugin raylib::{draw_circle, BLUE}

draw_circle(400, 300, 50.0, BLUE())

Draw a line between two points

Draws a 1-pixel-wide line between two points.

use plugin raylib::{draw_line, GREEN}

draw_line(0, 0, 800, 600, GREEN())

Draw text at a position

Draws a text string using raylib's built-in font.

use plugin raylib::{draw_text, DARKGRAY}

draw_text("Score: 42", 10, 10, 24, DARKGRAY())

Seconds elapsed since last frame

Returns the seconds elapsed since the last frame. Use this for frame-rate-independent movement.

use plugin raylib::{get_frame_time}

let dt = get_frame_time()
let player_x = player_x + speed * dt

Total elapsed time in seconds

Returns the total time in seconds since init_window was called.

let t = get_time()
let wave = (t * 2.0).sin()

Current frames per second

Returns the current measured frames per second.

use plugin raylib::{draw_text, get_fps, BLACK}

draw_text("FPS: {get_fps()}", 10, 10, 20, BLACK())

True if key was pressed this frame

Returns true only on the frame a key transitions from up to down. Use raylib key constants (integer codes).

use plugin raylib::{is_key_pressed}

if is_key_pressed(32) {  // SPACE
  print("jumped")
}

True while key is held

Returns true every frame while a key is held down.

use plugin raylib::{is_key_down}

if is_key_down(262) {  // KEY_RIGHT
  player_x = player_x + 200 * dt
}

True if mouse button pressed this frame

Returns true on the frame a mouse button is clicked (0 = left, 1 = right, 2 = middle).

use plugin raylib::{is_mouse_button_pressed}

if is_mouse_button_pressed(0) {
  print("left click")
}

Current mouse cursor position

Returns the current cursor position as #{x, y}.

use plugin raylib::{get_mouse_position}

let pos = get_mouse_position()
print("mouse at {pos["x"]}, {pos["y"]}")

Save a screenshot to a file

Saves the current frame to a PNG file.

use plugin raylib::{take_screenshot}

take_screenshot("screenshot.png")

Current window/screen width

Return the current window dimensions in pixels.

use plugin raylib::{get_screen_width, get_screen_height}

let cx = get_screen_width() / 2
let cy = get_screen_height() / 2

Change the window title

Updates the window title bar text at runtime.

use plugin raylib::{set_window_title}

set_window_title("Level 2 - Boss Fight")
enespt-br