character: support advanced atlases in spritesheets

This commit is contained in:
Sebastian Krzyszkowiak 2018-12-12 01:46:16 +01:00
parent f1933fc7ba
commit fe23d33273
No known key found for this signature in database
GPG key ID: E8F235CF3BDBC3FF
2 changed files with 26 additions and 14 deletions

View file

@ -110,7 +110,7 @@ SYMBOL_EXPORT void LoadSpritesheets(struct Game* game, struct Character* charact
tmp->height = al_get_bitmap_height(tmp->bitmap) / tmp->rows; tmp->height = al_get_bitmap_height(tmp->bitmap) / tmp->rows;
} }
for (int i = 0; i < tmp->frameCount; i++) { for (int i = 0; i < tmp->frameCount; i++) {
if ((!tmp->frames[i].bitmap) && (tmp->frames[i].file)) { if ((!tmp->frames[i].source) && (tmp->frames[i].file)) {
if (game->config.debug) { if (game->config.debug) {
PrintConsole(game, " - %s", tmp->frames[i].file); PrintConsole(game, " - %s", tmp->frames[i].file);
} }
@ -120,18 +120,20 @@ SYMBOL_EXPORT void LoadSpritesheets(struct Game* game, struct Character* charact
} else { } else {
snprintf(filename, 255, "sprites/%s/%s", character->name, tmp->frames[i].file); snprintf(filename, 255, "sprites/%s/%s", character->name, tmp->frames[i].file);
} }
tmp->frames[i].bitmap = AddBitmap(game, filename); tmp->frames[i].source = AddBitmap(game, filename);
tmp->frames[i].filepath = strdup(filename); tmp->frames[i].filepath = strdup(filename);
int width = al_get_bitmap_width(tmp->frames[i].bitmap) + tmp->frames[i].x; } else if (!tmp->frames[i].source) {
if (width > tmp->width) { tmp->frames[i].source = al_create_sub_bitmap(tmp->bitmap, tmp->frames[i].col * tmp->width, tmp->frames[i].row * tmp->height, tmp->width, tmp->height);
tmp->width = width; }
} tmp->frames[i].bitmap = al_create_sub_bitmap(tmp->frames[i].source, tmp->frames[i].sx, tmp->frames[i].sy, (tmp->frames[i].sw > 0) ? tmp->frames[i].sw : al_get_bitmap_width(tmp->frames[i].source), (tmp->frames[i].sh > 0) ? tmp->frames[i].sh : al_get_bitmap_height(tmp->frames[i].source));
int height = al_get_bitmap_height(tmp->frames[i].bitmap) + tmp->frames[i].y;
if (height > tmp->height) { int width = al_get_bitmap_width(tmp->frames[i].bitmap) + tmp->frames[i].x;
tmp->height = height; if (width > tmp->width) {
} tmp->width = width;
} else if (!tmp->frames[i].bitmap) { }
tmp->frames[i].bitmap = al_create_sub_bitmap(tmp->bitmap, tmp->frames[i].col * tmp->width, tmp->frames[i].row * tmp->height, tmp->width, tmp->height); int height = al_get_bitmap_height(tmp->frames[i].bitmap) + tmp->frames[i].y;
if (height > tmp->height) {
tmp->height = height;
} }
} }
if (progress) { if (progress) {
@ -153,8 +155,9 @@ SYMBOL_EXPORT void UnloadSpritesheets(struct Game* game, struct Character* chara
if (tmp->frames[i].filepath) { if (tmp->frames[i].filepath) {
RemoveBitmap(game, tmp->frames[i].filepath); RemoveBitmap(game, tmp->frames[i].filepath);
} else { } else {
al_destroy_bitmap(tmp->frames[i].bitmap); al_destroy_bitmap(tmp->frames[i].source);
} }
al_destroy_bitmap(tmp->frames[i].bitmap);
} }
if (tmp->bitmap) { if (tmp->bitmap) {
RemoveBitmap(game, tmp->filepath); RemoveBitmap(game, tmp->filepath);
@ -258,9 +261,14 @@ SYMBOL_EXPORT void RegisterSpritesheet(struct Game* game, struct Character* char
snprintf(framename, 255, "frame%d", i); snprintf(framename, 255, "frame%d", i);
s->frames[i].duration = strtodnull(al_get_config_value(config, framename, "duration"), s->duration); s->frames[i].duration = strtodnull(al_get_config_value(config, framename, "duration"), s->duration);
s->frames[i].source = NULL;
s->frames[i].bitmap = NULL; s->frames[i].bitmap = NULL;
s->frames[i].x = strtolnull(al_get_config_value(config, framename, "x"), 0); s->frames[i].x = strtolnull(al_get_config_value(config, framename, "x"), 0);
s->frames[i].y = strtolnull(al_get_config_value(config, framename, "y"), 0); s->frames[i].y = strtolnull(al_get_config_value(config, framename, "y"), 0);
s->frames[i].sx = strtolnull(al_get_config_value(config, framename, "sx"), 0);
s->frames[i].sy = strtolnull(al_get_config_value(config, framename, "sy"), 0);
s->frames[i].sw = strtolnull(al_get_config_value(config, framename, "sw"), 0);
s->frames[i].sh = strtolnull(al_get_config_value(config, framename, "sh"), 0);
s->frames[i].flipX = strtolnull(al_get_config_value(config, framename, "flipX"), 0); s->frames[i].flipX = strtolnull(al_get_config_value(config, framename, "flipX"), 0);
s->frames[i].flipY = strtolnull(al_get_config_value(config, framename, "flipY"), 0); s->frames[i].flipY = strtolnull(al_get_config_value(config, framename, "flipY"), 0);

View file

@ -26,13 +26,17 @@
struct SpritesheetFrame { struct SpritesheetFrame {
char* file; char* file;
char* filepath; char* filepath;
ALLEGRO_BITMAP* bitmap; ALLEGRO_BITMAP *bitmap, *source;
double duration; double duration;
ALLEGRO_COLOR tint; ALLEGRO_COLOR tint;
int row; int row;
int col; int col;
int x; int x;
int y; int y;
int sx;
int sy;
int sw;
int sh;
bool flipX; bool flipX;
bool flipY; bool flipY;
}; };