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