spritesheet.c (993B)
1 #include <assert.h> 2 #include "spritesheet.h" 3 4 Spritesheet SpritesheetGen(Animation *img, int cols) { 5 int rows = (int)img->frame_count / cols; 6 if ((int)img->frame_count % cols > 0) { 7 ++rows; 8 } 9 size_t frame_size = img->width * img->height * sizeof(uint32_t); 10 size_t line_size = img->width * sizeof(uint32_t); 11 size_t full_line = line_size * cols; 12 uint8_t *merged = calloc(rows * cols, frame_size); 13 assert(merged!=NULL); 14 uint8_t *merged_orig = merged; 15 for (int row = 0; row < rows; ++row) { 16 for (int y = 0; y < img->height; ++y) { 17 for (int col = 0; col < cols; ++col) { 18 uint32_t offset = row*cols+col; 19 if (offset < img->frame_count) { 20 memcpy(merged, img->frames[offset].rgba+y*line_size, line_size); 21 } 22 merged += line_size; 23 } 24 } 25 } 26 int stride = full_line; 27 Spritesheet ss = {0}; 28 ss.data = merged_orig; 29 ss.stride = stride; 30 ss.width = cols * img->width; 31 ss.height = rows * img->height; 32 ss.len = ss.width * ss.height * sizeof(uint32_t); 33 34 return ss; 35 } 36