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;
}
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) {
PrintConsole(game, " - %s", tmp->frames[i].file);
}
@ -120,8 +120,13 @@ SYMBOL_EXPORT void LoadSpritesheets(struct Game* game, struct Character* charact
} else {
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);
} else if (!tmp->frames[i].source) {
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->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 width = al_get_bitmap_width(tmp->frames[i].bitmap) + tmp->frames[i].x;
if (width > tmp->width) {
tmp->width = width;
@ -130,9 +135,6 @@ SYMBOL_EXPORT void LoadSpritesheets(struct Game* game, struct Character* charact
if (height > tmp->height) {
tmp->height = height;
}
} 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);
}
}
if (progress) {
progress(game);
@ -153,8 +155,9 @@ SYMBOL_EXPORT void UnloadSpritesheets(struct Game* game, struct Character* chara
if (tmp->frames[i].filepath) {
RemoveBitmap(game, tmp->frames[i].filepath);
} 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) {
RemoveBitmap(game, tmp->filepath);
@ -258,9 +261,14 @@ SYMBOL_EXPORT void RegisterSpritesheet(struct Game* game, struct Character* char
snprintf(framename, 255, "frame%d", i);
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].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].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].flipY = strtolnull(al_get_config_value(config, framename, "flipY"), 0);

View file

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