30#include "allegro5/allegro_image.h"
31#include "allegro5/allegro_memfile.h"
44 ALLEGRO_DISPLAY* d = al_get_current_display();
45 int m = d ? al_get_display_option(d, ALLEGRO_MAX_BITMAP_SIZE) : 0;
46 return m > 0 ? m : 2048;
78 if ((*atlas)->pages) {
80 for (it = 0; it < (*atlas)->npages; it++) {
81 if ((*atlas)->pages[it].bmp) {
82 al_destroy_bitmap((*atlas)->pages[it].bmp);
83 (*atlas)->pages[it].bmp = NULL;
86 Free((*atlas)->pages);
112int 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) {
113 ALLEGRO_FILE* mf = NULL;
114 ALLEGRO_BITMAP* bmp = NULL;
121 __n_assert(page < atlas->npages,
return FALSE);
126 mf = al_open_memfile((
void*)bytes, (int64_t)len,
"r");
128 n_log(
LOG_ERR,
"could not open a memfile over %zu bytes", len);
131 bmp = al_load_bitmap_f(mf, ext);
134 n_log(
LOG_ERR,
"could not decode a '%s' atlas page from %zu bytes", ext, len);
139 if (al_get_bitmap_width(bmp) > maxpx || al_get_bitmap_height(bmp) > maxpx) {
140 n_log(
LOG_ERR,
"atlas page %zu is %dx%d, over this display's %d limit", page, al_get_bitmap_width(bmp), al_get_bitmap_height(bmp), maxpx);
141 al_destroy_bitmap(bmp);
146 al_destroy_bitmap(atlas->
pages[page].
bmp);
163 if (page >= atlas->
npages) {
184 size_t col = 0,
row = 0;
185 float cw = 0.0, ch = 0.0;
190 col = index % (size_t)atlas->
pages[page].
cols;
195 cw = (float)al_get_bitmap_width(atlas->
pages[page].
bmp) / (float)atlas->
pages[page].
cols;
196 ch = (float)al_get_bitmap_height(atlas->
pages[page].
bmp) / (float)atlas->
pages[page].
rows;
199 (*sx) = (float)col * cw;
202 (*sy) = (float)
row * ch;
227 float sx = 0.0, sy = 0.0, sw = 0.0, sh = 0.0;
233 al_draw_scaled_bitmap(atlas->
pages[page].
bmp, sx, sy, sw, sh, x, y, size, size, 0);
static const char * row(N_GUI_TREE *tree, int index)
the text of a visible row, "" past the end
uint16_t cols
grid width in cells
N_ATLAS_PAGE * pages
page array, npages entries
ALLEGRO_BITMAP * bmp
the decoded page bitmap, NULL until set
uint16_t rows
grid height in cells
size_t npages
number of pages
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
int n_atlas_draw_cell(N_ATLAS *atlas, size_t page, size_t index, float x, float y, float size)
draw one cell as a size x size square
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)
one atlas page: a bitmap holding cols*rows equally sized cells
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
#define Free(__ptr)
Free Handler to get errors.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
Multi-page sprite atlas: random-access cell blitting from a grid.