27#define _Class _ImageAtlas
40 for (
size_t i = 0; i < this->images->count; i++) {
47 release(this->images);
53#pragma mark - ImageAtlas
67 $(self->
images, addObject, atlasImage);
102 $(self->names, setObjectForKeyPath, atlasImage,
name);
116 return $(self->names, objectForKeyPath,
name);
124 const SDL_Surface *sa = (*(
const AtlasImage **) a)->image.surface;
125 const SDL_Surface *sb = (*(
const AtlasImage **) b)->image.surface;
127 if (sb->h != sa->h) {
128 return sb->h - sa->h;
131 return sb->w - sa->w;
141 int x = 0, y = 0, row = 0;
143 for (
size_t i = 0; i < count; i++) {
147 if (x + surface->w >
size) {
153 if (y + surface->h >
size) {
157 rects[i] = MakeRect(x, y, surface->w, surface->h);
160 row = max(row, surface->h);
174 const size_t count = self->
images->count;
182 SDL_Rect *rects = calloc(count,
sizeof(SDL_Rect));
188 for (
size_t i = 0; i < count; i++) {
191 const SDL_Surface *surface =
images[i]->image.surface;
192 area += (size_t) surface->w * (
size_t) surface->h;
193 size = max(
size, max(surface->w, surface->h));
198 const double side = ceil(sqrt((
double) area));
202 while (sheetSize <
size) {
207 while (sheetSize <= self->
maxSize) {
217 for (
size_t i = 0; i < count; i++) {
218 images[i]->rect = rects[i];
221 SDL_Surface *sheet = SDL_CreateSurface(sheetSize, sheetSize, SDL_PIXELFORMAT_RGBA32);
224 SDL_FillSurfaceRect(sheet, NULL, 0);
226 for (
size_t i = 0; i < count; i++) {
229 const SDL_Surface *surface =
image->image.surface;
230 const size_t pitch = surface->w * SDL_BYTESPERPIXEL(surface->format);
232 for (
int y = 0; y < surface->h; y++) {
233 memcpy((Uint8 *) sheet->pixels + (
image->rect.y + y) * sheet->pitch +
image->rect.x * SDL_BYTESPERPIXEL(sheet->format),
234 (
const Uint8 *) surface->pixels + y * surface->pitch,
239 release(self->
image);
243 SDL_DestroySurface(sheet);
245 self->texture = release(self->texture);
266 self->
images = $$(Array, array);
269 self->names = $$(Dictionary, dictionary);
283 self->texture = release(self->texture);
294 if (self->texture == NULL && self->
image) {
295 self->texture = $(device, createTextureFromSurface, self->
image->
surface, SDL_GPU_TEXTUREUSAGE_SAMPLER,
false);
296 assert(self->texture);
299 return self->texture;
302#pragma mark - Class lifecycle
309 ((ObjectInterface *) clazz->interface)->dealloc =
dealloc;
311 ((ImageAtlasInterface *) clazz->interface)->addImage =
addImage;
312 ((ImageAtlasInterface *) clazz->interface)->addImageWithName =
addImageWithName;
314 ((ImageAtlasInterface *) clazz->interface)->compile =
compile;
315 ((ImageAtlasInterface *) clazz->interface)->imageWithName =
imageWithName;
316 ((ImageAtlasInterface *) clazz->interface)->init =
init;
318 ((ImageAtlasInterface *) clazz->interface)->texture =
texture;
330 clazz = _initialize(&(
const ClassDef) {
331 .name =
"ImageAtlas",
332 .superclass = _Object(),
334 .interfaceSize =
sizeof(ImageAtlasInterface),
static AtlasImage * initWithImage(AtlasImage *self, ImageAtlas *atlas, Image *image)
Class * _AtlasImage(void)
static void initialize(Class *clazz)
static View * init(View *self)
static void dealloc(Object *self)
static String * name(const Font *self)
static Image * imageWithSurface(SDL_Surface *surface)
static Image * imageWithResourceName(const char *name, float pixelDensity)
static SDL_Size size(const Image *self)
static AtlasImage * addImageWithName(ImageAtlas *self, const char *name, Image *image)
static AtlasImage * imageWithName(const ImageAtlas *self, const char *name)
static void renderDeviceWillReset(ImageAtlas *self)
static ImageAtlas * init(ImageAtlas *self)
static bool compile(ImageAtlas *self)
static AtlasImage * addImageWithResourceName(ImageAtlas *self, const char *name, float pixelDensity)
Class * _ImageAtlas(void)
static Texture * texture(ImageAtlas *self, RenderDevice *device)
static int compareImages(const void *a, const void *b)
Sorts AtlasImages tallest first, then widest, so that shelf rows waste the least.
static void dealloc(Object *self)
static AtlasImage * addImage(ImageAtlas *self, Image *image)
static void initialize(Class *clazz)
static bool pack(AtlasImage **images, SDL_Rect *rects, size_t count, int size)
Lays out the sorted images in shelves on a square of the given size, writing their rects to rects rat...
Packs many Images into one sheet, backed by one Texture.
#define IMAGE_ATLAS_MAX_SIZE
View logging facilities via SDL_Log.
#define MVC_LogError(fmt,...)
static Image * image(Theme *self, const char *name, float pixelDensity)
An Image packed into an ImageAtlas.
Image image
The superclass.
Packs many Images into one sheet, backed by one Texture.
int maxSize
The largest sheet dimension ImageAtlas::compile will attempt.
ImageAtlas * init(ImageAtlas *self)
Initializes this ImageAtlas.
Image * image
The compiled sheet, or NULL until ImageAtlas::compile.
Array * images
The AtlasImages, in the order added.
AtlasImage * addImage(ImageAtlas *self, Image *image)
Adds the given Image to this atlas.
unsigned generation
Incremented by every ImageAtlas::addImageWithName and ImageAtlas::compile, so that anything derived f...
SDL_Surface * surface
The backing surface.