//
// Created by sebastian on 30.07.26.
//

#include "Texture2D.h"

#include "glad/glad.h"

namespace engine {
    Texture2D::~Texture2D() {release();}

    Texture2D::Texture2D(Texture2D&& other) noexcept
        : m_id(other.m_id), m_width(other.m_width), m_height(other.m_height), m_channels(other.m_channels) {
        other.m_id = 0; // Ownership übertragen: das Original darf beim Zerstören nichts mehr löschen
    }


    Texture2D& Texture2D::operator=(Texture2D&& other) noexcept {
        if (this != &other) {
            release();
            m_id = other.m_id;
            m_width = other.m_width;
            m_height = other.m_height;
            m_channels = other.m_channels;
            other.m_id = 0;
        }
        return *this;
    }

    void Texture2D::release() {
        if (m_id != 0) {
            glDeleteTextures(1, &m_id);
            m_id = 0;
        }
    }

    void Texture2D::bind(unsigned int unit) const {
        glActiveTexture(GL_TEXTURE0 + unit);
        glBindTexture(GL_TEXTURE_2D, m_id);
    }

    void Texture2D::unbind(unsigned int unit) const {
        glActiveTexture(GL_TEXTURE0 + unit);
        glBindTexture(GL_TEXTURE_2D, 0);
    }
}
