Skip to content

Raylib

Including raylib in Your Project

#include "raylib.h"

Initializing and Closing

// Initialize the window
InitWindow(screenWidth, screenHeight, "Window Title");

// Set the target FPS
SetTargetFPS(60);

// Close the window and clean up resources
CloseWindow();

Basic Structure

Main Game Loop

while (!WindowShouldClose())    // Detect window close button or ESC key
{
    // Update game state

    // Draw everything
    BeginDrawing();

    ClearBackground(RAYWHITE);

    // Drawing code goes here

    EndDrawing();
}

Drawing Shapes

Drawing a Rectangle

DrawRectangle(x, y, width, height, color);

Drawing a Circle

DrawCircle(x, y, radius, color);

Drawing a Line

DrawLine(startX, startY, endX, endY, color);

Handling Input

Keyboard Input

if (IsKeyDown(KEY_RIGHT)) 
{
    // Right arrow key is being held down
}

if (IsKeyPressed(KEY_SPACE)) 
{
    // Space key was just pressed
}

Mouse Input

Vector2 mousePosition = GetMousePosition();

if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) 
{
    // Left mouse button is being held down
}

Working with Textures

Loading a Texture

Texture2D texture = LoadTexture("path/to/texture.png");

Unloading a Texture

UnloadTexture(texture);

Drawing a Texture

DrawTexture(texture, x, y, WHITE);

Fonts and Text

Loading a Font

Font font = LoadFont("path/to/font.ttf");

Unloading a Font

UnloadFont(font);

Drawing Text

DrawText("Hello, raylib!", x, y, fontSize, color);

Audio

Initializing and Closing Audio Device

InitAudioDevice();
CloseAudioDevice();

Loading a Sound

Sound sound = LoadSound("path/to/sound.wav");

Unloading a Sound

UnloadSound(sound);

Playing a Sound

PlaySound(sound);

Loading and Playing Music

Music music = LoadMusicStream("path/to/music.mp3");
PlayMusicStream(music);

Collision Detection

Checking for Rectangle Collision

bool collision = CheckCollisionRecs(rect1, rect2);

Checking for Circle Collision

bool collision = CheckCollisionCircles(center1, radius1, center2, radius2);

Advanced Drawing

Drawing Textured Shapes

DrawTexturePro(texture, sourceRec, destRec, origin, rotation, WHITE);

Drawing Text with a Font

DrawTextEx(font, "Hello, raylib!", position, fontSize, spacing, color);

Utility Functions

Getting Frame Time

float deltaTime = GetFrameTime();

Getting Random Values

int randomValue = GetRandomValue(min, max);

Example

Complete Example Program

#include "raylib.h"

int main(void)
{
    // Initialization
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");

    SetTargetFPS(60);

    // Main game loop
    while (!WindowShouldClose())
    {
        // Update

        // Draw
        BeginDrawing();

        ClearBackground(RAYWHITE);

        DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);

        EndDrawing();
    }

    // De-Initialization
    CloseWindow();

    return 0;
}