/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "game/renderer/legacy_gl/legacy_gl_mesh.hpp"

#include <cstring>

using namespace polygun::renderer;

LegacyGLMesh::LegacyGLMesh() :
	m_vertices(nullptr),
	m_vertices_length(0),
	m_indices(nullptr),
	m_indices_length(0),
	m_uvs(nullptr),
	m_normals(nullptr)
{}

LegacyGLMesh::~LegacyGLMesh() {
	if(m_vertices)
		delete[] m_vertices;
	if(m_indices)
		delete[] m_indices;
	if(m_uvs)
		delete[] m_uvs;
	if(m_normals)
		delete[] m_normals;
}

void LegacyGLMesh::load_vertices(const std::vector<float>& vertices) {
	if(m_vertices)
		delete[] m_vertices;
	m_vertices = new float[vertices.size()];
	memcpy(m_vertices, vertices.data(), vertices.size()*sizeof(float));
	m_vertices_length = vertices.size();
}

void LegacyGLMesh::load_indices(const std::vector<unsigned>& indices) {
	if(m_indices)
		delete[] m_indices;
	m_indices = new unsigned[indices.size()];
	memcpy(m_indices, indices.data(), indices.size()*sizeof(unsigned));
	m_indices_length = indices.size();
}

void LegacyGLMesh::load_uvs(const std::vector<float>& uvs) {
	if(m_uvs)
		delete[] m_uvs;
	m_uvs = new float[uvs.size()];
	memcpy(m_uvs, uvs.data(), uvs.size()*sizeof(float));
}

void LegacyGLMesh::load_normals(const std::vector<float>& normals) {
	if(m_normals)
		delete[] m_normals;
	m_normals = new float[normals.size()];
	memcpy(m_normals, normals.data(), normals.size()*sizeof(float));
}
