n_atlas multi-page sprite atlas test (headless).
n_atlas multi-page sprite atlas test (headless). Builds a 64x32 image in memory, encodes it to PNG, and loads it as a 4x2 cell grid. Everything asserted here needs no display: cell geometry is derived from the bitmap and the grid, so it can be checked without drawing.
The refusals matter as much as the happy path. A cell index past the end of the grid must report failure rather than blit garbage from the next row, and a caller handed FALSE can fall back to something sensible (literal text for a missing glyph, the previous atlas for a bad page).
#define ALLEGRO_UNSTABLE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "allegro5/allegro_image.h"
#include "allegro5/allegro_memfile.h"
if (!cond) {
} else {
}
}
static void expect_eqf(
const char* label,
float got,
float want) {
float d = got - want;
if (d < 0.0f) {
d = -d;
}
if (d > 0.01f) {
} else {
}
}
static unsigned char*
make_png(
int w,
int h,
size_t* out_len) {
ALLEGRO_BITMAP* bmp = NULL;
ALLEGRO_FILE* mf = NULL;
unsigned char* buf = NULL;
size_t cap = 256 * 1024;
int64_t n = 0;
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
bmp = al_create_bitmap(w, h);
if (!bmp) {
return NULL;
}
al_set_target_bitmap(bmp);
al_clear_to_color(al_map_rgb(10, 20, 30));
al_set_target_bitmap(NULL);
buf = malloc(cap);
if (!buf) {
al_destroy_bitmap(bmp);
return NULL;
}
mf = al_open_memfile(buf, (int64_t)cap, "w");
if (!mf || !al_save_bitmap_f(mf, ".png", bmp)) {
if (mf) {
al_fclose(mf);
}
al_destroy_bitmap(bmp);
free(buf);
return NULL;
}
n = al_ftell(mf);
al_fclose(mf);
al_destroy_bitmap(bmp);
if (n <= 0) {
free(buf);
return NULL;
}
(*out_len) = (size_t)n;
return buf;
}
unsigned char* png = NULL;
size_t png_len = 0;
float sx = 0.0f, sy = 0.0f, sw = 0.0f, sh = 0.0f;
if (!al_init()) {
return 1;
}
al_init_image_addon();
expect_true(
"png encoded", png != NULL && png_len > 0);
if (!png) {
return 1;
}
expect_true(
"destroy nulls the handle", atlas == NULL);
free(png);
return 1;
}
printf("ex_atlas: all checks passed\n");
return 0;
}
static void expect_eqf(const char *label, float got, float want)
static unsigned char * make_png(int w, int h, size_t *out_len)
static void expect_true(const char *label, int cond)
int n_atlas_max_bitmap_size(void)
get the largest bitmap dimension this display can hold
int n_atlas_set_page(N_ATLAS *atlas, size_t page, const void *bytes, size_t len, const char *ext, uint16_t cols, uint16_t rows)
decode bytes into one atlas page
void n_atlas_destroy(N_ATLAS **atlas)
destroy an atlas and every bitmap it owns
int n_atlas_page_ready(const N_ATLAS *atlas, size_t page)
test whether a page holds a usable bitmap
int n_atlas_cell_rect(const N_ATLAS *atlas, size_t page, size_t index, float *sx, float *sy, float *sw, float *sh)
compute the source rectangle of one cell
N_ATLAS * new_n_atlas(size_t npages)
allocate an atlas holding npages empty pages
a set of atlas pages addressed as (page, cell index)
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_INFO
informational
Multi-page sprite atlas: random-access cell blitting from a grid.
Common headers and low-level functions & define.