Annotated raylib code samples from beginner to advanced. Every example compiles and runs.
#include "raylib.h"
int main(void)
{
const int W = 800, H = 450;
InitWindow(W, H, "Bouncing Ball");
SetTargetFPS(60);
Vector2 pos = { W/2.0f, H/2.0f };
Vector2 vel = { 300.0f, 250.0f };
float radius = 20.0f;
while (!WindowShouldClose())
{
float dt = GetFrameTime();
// Move
pos.x += vel.x * dt;
pos.y += vel.y * dt;
// Bounce off edges
if (pos.x - radius < 0 || pos.x + radius > W) vel.x *= -1;
if (pos.y - radius < 0 || pos.y + radius > H) vel.y *= -1;
BeginDrawing();
ClearBackground(RAYWHITE);
DrawCircleV(pos, radius, RED);
DrawText(TextFormat("FPS: %d", GetFPS()), 10, 10, 20, DARKGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}
GetFrameTime() returns delta time for frame-rate-independent movement. DrawCircleV() takes a Vector2 instead of separate x,y. TextFormat() is raylib's sprintf replacement that returns a static buffer.
#include "raylib.h"
int main(void)
{
InitWindow(800, 450, "Draggable Sprite");
SetTargetFPS(60);
Texture2D tex = LoadTexture("sprite.png");
Vector2 pos = { 400, 225 };
bool dragging = false;
while (!WindowShouldClose())
{
Vector2 mouse = GetMousePosition();
Rectangle rect = { pos.x, pos.y, tex.width, tex.height };
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)
&& CheckCollisionPointRec(mouse, rect))
dragging = true;
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
dragging = false;
if (dragging) {
pos.x += GetMouseDelta().x;
pos.y += GetMouseDelta().y;
}
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTexture(tex, (int)pos.x, (int)pos.y, WHITE);
DrawText(dragging ? "DRAGGING" : "Click sprite to drag",
10, 10, 20, DARKGRAY);
EndDrawing();
}
UnloadTexture(tex);
CloseWindow();
return 0;
}
LoadTexture() reads an image and uploads it to the GPU. GetMouseDelta() returns frame-to-frame mouse movement. CheckCollisionPointRec() tests if a point is inside a rectangle. Always UnloadTexture() before closing.
#include "raylib.h"
int main(void)
{
InitWindow(1280, 720, "First Person");
Camera3D cam = { 0 };
cam.position = (Vector3){ 4.0f, 2.0f, 4.0f };
cam.target = (Vector3){ 0.0f, 1.8f, 0.0f };
cam.up = (Vector3){ 0.0f, 1.0f, 0.0f };
cam.fovy = 60.0f;
cam.projection = CAMERA_PERSPECTIVE;
DisableCursor(); // Lock mouse for FPS look
SetTargetFPS(60);
while (!WindowShouldClose())
{
UpdateCamera(&cam, CAMERA_FIRST_PERSON);
BeginDrawing();
ClearBackground(SKYBLUE);
BeginMode3D(cam);
DrawPlane((Vector3){0,0,0}, (Vector2){32,32}, LIGHTGRAY);
DrawCube((Vector3){0,1,0}, 2, 2, 2, RED);
DrawCubeWires((Vector3){0,1,0}, 2, 2, 2, MAROON);
DrawGrid(20, 1.0f);
EndMode3D();
DrawFPS(10, 10);
EndDrawing();
}
CloseWindow();
return 0;
}
Camera3D struct holds position, target, up vector, FOV, and projection type. UpdateCamera() handles all WASD + mouse movement internally — one function call. BeginMode3D() / EndMode3D() bracket 3D drawing.
#include "raylib.h"
int main(void)
{
InitWindow(800, 450, "Music Player");
InitAudioDevice();
SetTargetFPS(60);
Music music = LoadMusicStream("song.ogg");
PlayMusicStream(music);
while (!WindowShouldClose())
{
UpdateMusicStream(music); // MUST call every frame
if (IsKeyPressed(KEY_SPACE)) {
if (IsMusicStreamPlaying(music))
PauseMusicStream(music);
else
ResumeMusicStream(music);
}
if (IsKeyPressed(KEY_R)) {
StopMusicStream(music);
PlayMusicStream(music);
}
float played = GetMusicTimePlayed(music);
float total = GetMusicTimeLength(music);
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("SPACE = play/pause R = restart", 10, 10, 20, DARKGRAY);
DrawRectangle(50, 200, (int)(700 * (played/total)), 30, MAROON);
DrawRectangleLines(50, 200, 700, 30, GRAY);
EndDrawing();
}
UnloadMusicStream(music);
CloseAudioDevice();
CloseWindow();
return 0;
}
LoadMusicStream() streams audio from disk (unlike LoadSound() which loads entirely into memory). You must call UpdateMusicStream() every frame or the music will stutter. Supports OGG, MP3, FLAC, WAV, XM, and MOD.
#include "raylib.h"
int main(void)
{
InitWindow(800, 450, "Shader Example");
SetTargetFPS(60);
// Render to texture, then draw texture with shader
RenderTexture2D target = LoadRenderTexture(800, 450);
Shader shader = LoadShader(0, "grayscale.fs");
while (!WindowShouldClose())
{
// Draw scene into render texture
BeginTextureMode(target);
ClearBackground(RAYWHITE);
DrawCircle(400, 225, 100, RED);
DrawRectangle(50, 50, 200, 150, BLUE);
DrawText("Shader applied!", 280, 380, 30, DARKGRAY);
EndTextureMode();
// Draw render texture to screen with shader
BeginDrawing();
ClearBackground(RAYWHITE);
BeginShaderMode(shader);
DrawTextureRec(target.texture,
(Rectangle){0, 0, 800, -450},
(Vector2){0, 0}, WHITE);
EndShaderMode();
EndDrawing();
}
UnloadShader(shader);
UnloadRenderTexture(target);
CloseWindow();
return 0;
}
RenderTexture2D, then draw that texture to screen inside a BeginShaderMode() block. The -450 height in the source rectangle flips the texture (OpenGL convention). LoadShader(0, "file.fs") uses the default vertex shader with your custom fragment shader.
#include "raylib.h"
int main(void)
{
InitWindow(800, 450, "Collision");
SetTargetFPS(60);
Rectangle player = { 100, 200, 40, 40 };
Rectangle wall = { 350, 150, 100, 150 };
float speed = 300.0f;
while (!WindowShouldClose())
{
float dt = GetFrameTime();
if (IsKeyDown(KEY_RIGHT)) player.x += speed * dt;
if (IsKeyDown(KEY_LEFT)) player.x -= speed * dt;
if (IsKeyDown(KEY_DOWN)) player.y += speed * dt;
if (IsKeyDown(KEY_UP)) player.y -= speed * dt;
bool hit = CheckCollisionRecs(player, wall);
BeginDrawing();
ClearBackground(RAYWHITE);
DrawRectangleRec(wall, GRAY);
DrawRectangleRec(player, hit ? RED : GREEN);
DrawText(hit ? "COLLISION!" : "Arrow keys to move",
10, 10, 20, DARKGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}
CheckCollisionRecs() performs AABB (axis-aligned bounding box) collision between two rectangles. raylib also offers CheckCollisionCircles(), CheckCollisionCircleRec(), CheckCollisionPointRec(), and 3D variants like GetRayCollisionBox().
# Linux
gcc example.c -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -o example
# macOS
clang example.c -lraylib -framework OpenGL -framework Cocoa -framework IOKit -o example
# Windows (MinGW)
gcc example.c -lraylib -lopengl32 -lgdi32 -lwinmm -o example.exe
# Web (Emscripten)
emcc example.c -lraylib -s USE_GLFW=3 -s ASYNCIFY -o example.html