#include "tile.hpp"


TileMap::TileMap(const char* fn, unsigned dim, unsigned cx, unsigned cy): tex(Texture::load(fn)), px(dim), clamp_x(cx), clamp_y(cy) {
    assert(tex);
    assert(tex->w % (px + (2 * clamp_x)) == 0);
    assert(tex->h % (px + (2 * clamp_y)) == 0);
    num_x = tex->w / (px + (2 * clamp_x));
    num_y = tex->h / (px + (2 * clamp_y));
}


TileMap::~TileMap() {
    delete tex;
}


int TileMap::index() {
    return tex->index;
}


float TileMap::os(unsigned clamp, unsigned pos, unsigned max, unsigned len) const {
    return ((float)clamp + ((float)(pos%max) * ((float)px + (2.0f*(float)clamp)))) / (float)len;
}

const float* TileMap::tilespec(tile_t::tex_t tile, unsigned tick) const {
    static float rv[4];
    rv[0] = px / (float)tex->w;
    rv[1] = os(clamp_x, tile, num_x, tex->w);
    rv[2] = px / (float)tex->h;
    rv[3] = os(clamp_y, tick, num_y, tex->h);
    return rv;
}