Skip to content

Raylib exporter ​

How it works ​

Packrat writes a self-contained C header and one image file per atlas page. The header defines the generic Packrat_* types and helpers, followed by this atlas's read-only frame and animation data. packrat_load loads the page images, and packrat_draw places a trimmed frame in its original logical frame.

Including several generated headers is supported: the shared types and helper functions are defined once, while each header keeps its own definition object.

API ​

The generated header defines these Raylib-backed types:

c
typedef struct Packrat_frame {
    const char *name;
    int page;
    Rectangle source;
    Vector2 original_size;
    Vector2 offset;
    bool rotated;
    int index;
} Packrat_frame;

typedef struct Packrat_animation {
    const char *name;
    const Packrat_frame *const *frames;
    size_t frame_count;
} Packrat_animation;

typedef struct Packrat_atlas_definition {
    const char *const *page_paths;
    size_t page_count;
    const Packrat_frame *frames;
    size_t frame_count;
    const Packrat_animation *animations;
    size_t animation_count;
} Packrat_atlas_definition;

typedef struct Packrat_atlas {
    const Packrat_atlas_definition *definition;
    Texture2D *pages;
    size_t page_count;
} Packrat_atlas;

The generated definition object is named packrat_<stem>_definition. The header also provides these exact helpers:

c
static inline bool packrat_load(
    Packrat_atlas *atlas,
    const Packrat_atlas_definition *definition,
    const char *asset_directory);
static inline void packrat_unload(Packrat_atlas *atlas);
static inline const Packrat_frame *packrat_frame_at(
    const Packrat_atlas *atlas, size_t index);
static inline const Packrat_frame *packrat_find(
    const Packrat_atlas *atlas, const char *name, int index);
static inline const Packrat_animation *packrat_find_animation(
    const Packrat_atlas *atlas, const char *name);
static inline void packrat_draw(
    const Packrat_atlas *atlas,
    const Packrat_frame *frame,
    Vector2 position,
    float rotation,
    Vector2 scale,
    Color tint);

packrat_find treats a negative index as the first matching frame. packrat_load returns false on invalid input or an image-loading failure; the other lookup helpers return NULL when they cannot find a result. packrat_draw uses the original size and trim offset; negative scale components mirror the frame.

Integrate ​

Include the generated header, keep its image files in the asset directory, and use the generated definition object:

c
#include "raylib.h"
#include "atlas.h"

int main(void) {
    InitWindow(800, 450, "Packrat");

    Packrat_atlas atlas = {0};
    if (!packrat_load(&atlas, &packrat_atlas_definition, "assets")) {
        CloseWindow();
        return 1;
    }

    const Packrat_animation *run = packrat_find_animation(&atlas, "run_side");
    const Packrat_frame *frame = run && run->frame_count > 0
        ? run->frames[0]
        : NULL;

    while (!WindowShouldClose()) {
        BeginDrawing();
        ClearBackground(RAYWHITE);
        packrat_draw(&atlas, frame, (Vector2){400, 300}, 0.0f,
            (Vector2){2, 2}, WHITE);
        EndDrawing();
    }

    packrat_unload(&atlas);
    CloseWindow();
    return 0;
}

The definition symbol uses the export base name, for example packrat_characters_definition for characters.h. Use that symbol when more than one generated header is included. Replace .png in generated page paths with the image extension selected for the export.

Packrat documentation