[in progress] a 2d storytelling-oriented game engine, built on allegro 5
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

56 lines
1.2 KiB

#include "set.h"
#include "err.h"
#include <assert.h>
struct set* set_load(const char *fname, unsigned tw, unsigned th, unsigned bw)
{
unsigned w, h;
unsigned i;
unsigned x, y;
struct set *s;
char buf[1024] = "data/tileset/";
assert(tw);
assert(th);
assert(fname);
TRY(strlen(fname) - 1 < 1006, "bad tileset name");
strcpy(buf + 13, fname);
strcpy(buf + 13 + strlen(fname), ".png");
s = malloc(sizeof(*s));
assert(s != NULL);
s->body = al_load_bitmap(buf);
TRY(s->body != NULL, "could not load tileset `%s`", fname);
w = (unsigned)al_get_bitmap_width(s->body);
h = (unsigned)al_get_bitmap_height(s->body);
TRY(w % (tw + bw) == 0, "bad tile width for tileset `%s`", fname);
TRY(h % (th + bw) == 0, "bad tile width for tileset `%s`", fname);
s->tile_count = (w / (tw + bw)) * (h / (th + bw));
s->tiles = malloc(s->tile_count * sizeof(*(s->tiles)));
assert(s->tiles != NULL);
i = 0;
for (y = 0; y < h; y += (th + bw)) {
for (x = 0; x < w; x += (tw + bw)) {
s->tiles[i].b = al_create_sub_bitmap(s->body, x, y, tw, th);
i++;
}
}
return s;
}
struct set_tile* set_at(struct set *s, unsigned n)
{
assert(s != NULL);
assert(n < s->tile_count);
return &(s->tiles[n]);
}