#include <pch.hpp>

#include <renderer/subtexture2d.hpp>

#include <ref_scope.hpp>

namespace OpenEngine {
    Subtexture2D::Subtexture2D(const Ref<Texture2D>& texture, glm::vec2& min, glm::vec2& max)
        : atlas(texture)
    {
        coords[0] = { min.x, min.y };
        coords[1] = { max.x, min.y };
        coords[2] = { max.x, max.y };
        coords[3] = { min.x, max.y };
    }

    Ref<Subtexture2D> Subtexture2D::CreateFromCoords(const Ref<Texture2D> texture,
            const glm::vec2& coordinates,
            const glm::vec2& cell_size,
            const glm::vec2& sprite_size)
    {
        glm::vec2 min = { (coordinates.x * cell_size.x) / texture->GetWidth(), (coordinates.y * cell_size.y) / texture->GetHeight() };
        glm::vec2 max = { ((coordinates.x + sprite_size.x) * cell_size.x) / texture->GetWidth(), ((coordinates.y + sprite_size.y) * cell_size.y) / texture->GetHeight() };

        return CreateRef<Subtexture2D>(texture, min, max);
    }
}
