Skip to content

SDL2 exporter

Output

Packrat writes one generated C source file and one image per atlas page. The source contains SDL2 frame and atlas page metadata, loading through SDL_image, lookup, draw, and unload helpers.

Integrate

Compile the source with SDL2 and SDL_image. Initialize an SDL renderer, then call the generated packrat_sdl2_<stem>_load function with the renderer and atlas page directory. Find frames with packrat_sdl2_<stem>_find, draw with the generated draw helper, and call the matching unload function before shutdown.

Keep the generated image in the directory passed to the loader. The helper applies trim offsets.

Minimal program

This example uses the generated source directly so the loader, lookup, draw, and cleanup path is visible in one place. It assumes the export base name is atlas. Replace player_0001 with a frame name that exists in your atlas:

c
#include <SDL.h>
#include <SDL_image.h>
#include "atlas.c"

int main(void) {
    SDL_Init(SDL_INIT_VIDEO);
    IMG_Init(IMG_INIT_PNG);
    SDL_Window *window = SDL_CreateWindow("Packrat atlas demo",
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600,
        SDL_WINDOW_SHOWN);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    PackratSDL2_atlas atlas = {0};
    if (!packrat_sdl2_atlas_load(&atlas, renderer, ".")) return 1;

    const PackratSDL2_atlasFrame *frame =
        packrat_sdl2_atlas_find("player_0001", -1);
    bool running = true;
    while (running) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) if (event.type == SDL_QUIT) running = false;
        SDL_SetRenderDrawColor(renderer, 30, 35, 45, 255);
        SDL_RenderClear(renderer);
        packrat_sdl2_atlas_draw(renderer, &atlas, frame, 400, 300,
            0.0, 2.0, 2.0, SDL_FLIP_NONE);
        SDL_RenderPresent(renderer);
    }

    packrat_sdl2_atlas_unload(&atlas);
    SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window);
    IMG_Quit(); SDL_Quit();
    return 0;
}

Packrat documentation