#include "tileset.h"
#include "image.h"

tileset::tileset(image const* image, vec2i tile_size) {
	auto tile_count = image->size() / tile_size;
	for(int y = 0; y < tile_count.y; ++y) {
		for(int x = 0; x < tile_count.x; ++x) {
			auto tile = ::image(tile_size);
			tile.clear(0);
			tile.draw(image, vec2i::zero, recti(tile_size * vec2i(x, y), tile_size));
			_tiles.push_back(tile);
		}
	}
}

tileset::tileset(resource_manager& rm, ini_category const* ini, std::string const& path, std::vector<uint8_t> const& data) {
	auto tile_size = vec2i(ini->get<int>("tile_width"), ini->get<int>("tile_height"));
	_tile_size = tile_size;
	auto image = rm.get<::image>(path);
	auto tile_count = image->size() / tile_size;
	for(int y = 0; y < tile_count.y; ++y) {
		for(int x = 0; x < tile_count.x; ++x) {
			auto tile = ::image(tile_size);
			tile.clear(0);
			tile.draw(image, vec2i::zero, recti(tile_size * vec2i(x, y), tile_size));
			_tiles.push_back(tile);
		}
	}
}
/*
tileset::tileset(std::string path, vec2i tile_size) :
	tileset(new image(path), tile_size)
{}
*/

image& tileset::operator[](int index) {
	return _tiles[index];
}

vec2i const& tileset::tile_size() {
	return _tile_size;
}

