#include #include "shadow.h" static layer_t* create_layer(int z); static void destroy_layer(layer_t* l); /* Initialize a fresh scene with no layers */ lscene_t* create_lscene(void) { lscene_t* s = malloc(sizeof(lscene_t)); s->c = 0; s->l = NULL; return s; } /* Fully deallocate a scene */ void destroy_lscene(lscene_t* s) { int i; for (i = 0; i < s->c; i++) destroy_layer(s->l[i]); free(s->l); free(s); } /* Insert a layer with given z value */ /* TODO: reconsider what happens with equal z values */ BITMAP* add_layer(lscene_t* s, int z) { int i; /* Locate correct position to insert */ for (i = 0; i < s->c && s->l[i]->z > z; i++); /* Grow layer pointer array */ s->l = realloc(s->l, sizeof(layer_t*) * (s->c + 1)); /* Make room in the middle of the array for the new layer */ memmove(&s->l[i + 1], &s->l[i], (s->c - i) * sizeof(layer_t*)); /* Insert! */ s->c++; s->l[i] = create_layer(z); return s->l[i]->img; } /* Allocate a new layer */ static layer_t* create_layer(int z) { layer_t* l = malloc(sizeof(layer_t)); l->img = create_bitmap_ex(INTERNAL_DEPTH, SCREEN_WIDTH, SCREEN_HEIGHT); l->z = z; return l; } /* Deallocate a layer */ static void destroy_layer(layer_t* l) { destroy_bitmap(l->img); free(l); }