/*
 * DESCRIPTION:
 * Texture atlas functions.
*/

#include "atlas.h"

/* Initializes an atlas. */
void
atlas_init(Atlas* self, Texture texture, const ivec2 frameSize, const ivec2 size)
{
	memset(self, '\0', sizeof(Atlas));

	self->texture = texture;

	glm_ivec2_copy((s32*)frameSize, self->frameSize);
	glm_ivec2_copy((s32*)size, self->size);
}

/* Obtains the texture coordinates for an atlas frame. */
void
atlas_uv_get(Atlas* self, vec2 uvMin, vec2 uvMax)
{
	vec2 position;
	s32 col;
	s32 row;

	/* left to right, top to bottom */
	position[0] = (f32)(self->index % self->size[1]) * self->frameSize[1];
	position[1] = (f32)(self->index / self->size[1]) * self->frameSize[1];

	uvMin[0] = (f32)position[0] / self->texture.size[0];
	uvMin[1] = (f32)position[1] / self->texture.size[1];
	uvMax[0] = (f32)(position[0] + self->frameSize[0]) / self->texture.size[0];
	uvMax[1] = (f32)(position[1] + self->frameSize[1]) / self->texture.size[1];
}
