Mercurial > molko
view librpg/rpg/tileset-file.c @ 228:2734223d3daf
core: add a alloc_pool module
A miniamlist expandable array that gros each time new data is required, ideal
where data must be allocated dynamically without knowing in advance the number
of items.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 19 Nov 2020 14:11:11 +0100 |
parents | befa2e855d3b |
children | b30c3af37a01 |
line wrap: on
line source
/* * tileset-file.c -- tileset file loader * * Copyright (c) 2020 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #define _XOPEN_SOURCE 700 #include <assert.h> #include <errno.h> #include <libgen.h> #include <limits.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <core/alloc.h> #include <core/animation.h> #include <core/error.h> #include <core/image.h> #include <core/util.h> #include "tileset-file.h" #include "tileset.h" #define MAX_F(v) MAX_F_(v) #define MAX_F_(v) "%" #v "[^|]" /* * This is how memory for animations is allocated in the tileset_file * structure. * * As animations require a texture and a sprite to be present, we need to store * them locally in the tileset structure. * * tileset_file->anims[0] array (struct anim): * * [0] [1] [N] * | texture | texture | texture * | sprite | sprite | sprite * | animation | animation | animation * * tileset_file->anims[1] array (struct tileset_animation): * * [0] [1] [N] * | id | id | id * | animation ^ | animation ^ | animation ^ * * The second array is the exposed array through the tileset->anims pointer, * animations are referenced from the first array. This is because user may need * or replace the tileset by itself and as such we need to keep track of the * resource the tileset_file have allocated itself. */ struct anim { struct texture texture; struct sprite sprite; struct animation animation; }; struct context { struct tileset_file *tf; struct tileset *tileset; FILE *fp; char basedir[PATH_MAX]; /* * The following properties aren't stored in the tileset because they * are not needed after loading. */ unsigned int tilewidth; unsigned int tileheight; /* Number of rows/columns in the image. */ unsigned int nrows; unsigned int ncolumns; }; static int tileset_tiledef_cmp(const void *d1, const void *d2) { const struct tileset_tiledef *mtd1 = d1; const struct tileset_tiledef *mtd2 = d2; if (mtd1->id < mtd2->id) return -1; if (mtd1->id > mtd2->id) return 1; return 0; } static int tileset_animation_cmp(const void *d1, const void *d2) { const struct tileset_animation *mtd1 = d1; const struct tileset_animation *mtd2 = d2; if (mtd1->id < mtd2->id) return -1; if (mtd1->id > mtd2->id) return 1; return 0; } static bool parse_tilewidth(struct context *ctx, const char *line) { if (sscanf(line, "tilewidth|%u", &ctx->tilewidth) != 1 || ctx->tilewidth == 0) return errorf("tilewidth is null"); return true; } static bool parse_tileheight(struct context *ctx, const char *line) { if (sscanf(line, "tileheight|%u", &ctx->tileheight) != 1 || ctx->tileheight == 0) return errorf("tileheight is null"); return true; } static bool parse_tiledefs(struct context *ctx, const char *line) { (void)line; short x, y; unsigned short id, w, h; struct tileset_file *tf = ctx->tf; struct tileset_tiledef *td; if (!alloc_pool_init(&tf->tiledefs, sizeof (*td), NULL)) return false; while (fscanf(ctx->fp, "%hu|%hd|%hd|%hu|%hu\n", &id, &x, &y, &w, &h) == 5) { if (!(td = alloc_pool_new(&tf->tiledefs))) return false; td->id = id; td->x = x; td->y = y; td->w = w; td->h = h; } /* * Sort the array and expose it through the tileset->tiledefs pointer. */ qsort(tf->tiledefs.data, tf->tiledefs.size, tf->tiledefs.elemsize, tileset_tiledef_cmp); ctx->tileset->tiledefs = tf->tiledefs.data; ctx->tileset->tiledefsz = tf->tiledefs.size; return true; } static void anim_finish(void *data) { struct anim *anim = data; texture_finish(&anim->texture); } static bool parse_animations(struct context *ctx, const char *line) { (void)line; unsigned short id; unsigned int delay; char filename[FILENAME_MAX + 1], path[PATH_MAX]; struct tileset *tileset = ctx->tileset; struct tileset_file *tf = ctx->tf; struct tileset_animation *ta; struct anim *anim; if (!alloc_pool_init(&tf->anims[0], sizeof (*anim), anim_finish) || !alloc_pool_init(&tf->anims[1], sizeof (*ta), NULL)) return false; while (fscanf(ctx->fp, "%hu|" MAX_F(FILENAME_MAX) "|%u", &id, filename, &delay) == 3) { if (!(anim = alloc_pool_new(&tf->anims[0]))) return false; snprintf(path, sizeof (path), "%s/%s", ctx->basedir, filename); if (!image_open(&anim->texture, path)) return false; /* Initialize animation. */ sprite_init(&anim->sprite, &anim->texture, ctx->tilewidth, ctx->tileheight); animation_init(&anim->animation, &anim->sprite, delay); /* * Now create the real tileset_animation visible in * tileset->anims. */ if (!(ta = alloc_pool_new(&tf->anims[1]))) return false; ta->id = id; ta->animation = &anim->animation; } /* * Sort and expose the animation array through the tileset->animsz * pointer. */ qsort(tf->anims[1].data, tf->anims[1].size, tf->anims[1].elemsize, tileset_animation_cmp); tileset->anims = tf->anims[1].data; tileset->animsz = tf->anims[1].size; return true; } static bool parse_image(struct context *ctx, const char *line) { char path[PATH_MAX], *p; if (ctx->tilewidth == 0 || ctx->tileheight == 0) return errorf("missing tile dimensions before image"); if (!(p = strchr(line, '|'))) return errorf("could not parse image"); snprintf(path, sizeof (path), "%s/%s", ctx->basedir, p + 1); if (!image_open(&ctx->tf->image, path)) return false; sprite_init(&ctx->tf->sprite, &ctx->tf->image, ctx->tilewidth, ctx->tileheight); ctx->tileset->sprite = &ctx->tf->sprite; return true; } static bool parse_line(struct context *ctx, const char *line) { static const struct { const char *property; bool (*read)(struct context *, const char *); } props[] = { { "tilewidth", parse_tilewidth }, { "tileheight", parse_tileheight }, { "tiledefs", parse_tiledefs }, { "animations", parse_animations }, { "image", parse_image } }; for (size_t i = 0; i < NELEM(props); ++i) { if (strncmp(line, props[i].property, strlen(props[i].property)) == 0) return props[i].read(ctx, line); } return true; } static bool parse(struct context *ctx, const char *path) { char line[1024]; char basedir[PATH_MAX]; snprintf(basedir, sizeof (basedir), "%s", path); snprintf(ctx->basedir, sizeof (ctx->basedir), "%s", dirname(basedir)); while (fgets(line, sizeof (line), ctx->fp)) { /* Remove \n if any */ line[strcspn(line, "\n")] = '\0'; if (!parse_line(ctx, line)) return false; } return true; } static bool check(const struct tileset *tileset) { if (!tileset->sprite) return errorf("missing tileset image"); return true; } bool tileset_file_open(struct tileset_file *tf, struct tileset *tileset, const char *path) { assert(tf); assert(tileset); assert(path); struct context ctx = { .tf = tf, .tileset = tileset }; bool ret = true; memset(tileset, 0, sizeof (*tileset)); if (!(ctx.fp = fopen(path, "r"))) return errorf("%s", strerror(errno)); if (!(ret = parse(&ctx, path)) || !(ret = check(tileset))) tileset_file_finish(tf); return ret; } void tileset_file_finish(struct tileset_file *tf) { assert(tf); alloc_pool_finish(&tf->tiledefs); alloc_pool_finish(&tf->anims[0]); alloc_pool_finish(&tf->anims[1]); texture_finish(&tf->image); memset(tf, 0, sizeof (*tf)); }