Blob Blame History Raw


#include "tile.h"



Tile tileAdd(TileMap *tm, int ts, double s) {
  if (ts <= 0) return (Tile){};
  if (ts > TM_SIZE) ts = TM_SIZE;
  if (ts > TM_SIZE - tm->x) { tm->x = 0; tm->y0 = tm->y1; }
  if (ts > TM_SIZE - tm->y0) {
    TileMap *ntm = (TileMap*)alloc(sizeof(*ntm));
    *ntm = *tm;
    memset(tm, 0, sizeof(*tm));
    tm->next = ntm;
  }

  if (!tm->fb) tm->fb = createFramebufferEx(TM_SIZE, TM_SIZE, NULL, FALSE, FALSE, TRUE);
  if (!tm->tex) tm->tex = createAnimationFromFramebuffer(tm->fb);

  Tile t = { {tm->x/(double)TM_SIZE, tm->y0/(double)TM_SIZE}, ts/(double)TM_SIZE, s, tm->fb, tm->tex };
  tm->x += ts;
  if (tm->y1 < tm->y0 + ts) tm->y1 = tm->y0 + ts;
  return t;
}


void tileClear(TileMap *tm) {
  if (tm->next) tileClear(tm->next);
  tm->x = tm->y0 = tm->y1 = 0;
}


void tileFree(TileMap *tm) {
  if (tm->next) { tileFree(tm->next); free(tm->next); }
  if (tm->tex) animationDestroy(tm->tex);
  if (tm->fb) framebufferDestroy(tm->fb);
  memset(tm, 0, sizeof(*tm));
}