<commit_before>/**
* Copyright(c) 2014, <NAME> (http://www.zamma.co.uk)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "tilemap.h"
#include "game.h"
#include "light.h"
#include "logger.h"
#include "shader.h"
#include "tinyxml2.h"
#include "util.h"
#include "window.h"
#include <fstream>
#include <iostream>

using namespace tinyxml2;
using namespace ZEGL;

Tile::Tile(const Texture& texture, const Texture& normalMap, const TextureAtlas& textureAtlas,
	const glm::vec3& pos, float rot, float scale) :
	m_tileSize(scale),
	RenderEntity(texture, normalMap, textureAtlas, pos, rot, scale)
{
}

Tile::Tile(const Texture& texture, const Texture& normalMap, const glm::vec2 textureCoords[4],
	const glm::vec3& pos, float rot, float scale) :
	m_tileSize(scale),
	RenderEntity(texture, normalMap, textureCoords, pos, rot, scale)
{
}

Tile::Tile(const Tile& tile) :
	m_tileSize(tile.m_tileSize),
	RenderEntity(tile) {}

TileMap::TileMap(const std::string& fileName) :
	m_VAO(0),
	m_VAB(0),
	m_bytesAllocated(0)
{
	Load(fileName);

	glGenVertexArrays(1, &m_VAO);
	glBindVertexArray(m_VAO);

	glGenBuffers(1, &m_VAB);
	glBindBuffer(GL_ARRAY_BUFFER, m_VAB);

	glEnableVertexAttribArray(0); // pos
	glEnableVertexAttribArray(1); // size
	glEnableVertexAttribArray(2); // texCoords0
	glEnableVertexAttribArray(3); // texCoords1
	glEnableVertexAttribArray(4); // texCoords2
	glEnableVertexAttribArray(5); // texCoords3

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(EntityData), (GLvoid*)0);
	glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, sizeof(EntityData), (GLvoid*)16);
	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(EntityData), (GLvoid*)20);
	glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(EntityData), (GLvoid*)28);
	glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(EntityData), (GLvoid*)36);
	glVertexAttribPointer(5, 2, GL_FLOAT, GL_FALSE, sizeof(EntityData), (GLvoid*)44);

	glVertexAttribDivisor(0, 1);
	glVertexAttribDivisor(1, 1);
	glVertexAttribDivisor(2, 1);
	glVertexAttribDivisor(3, 1);
	glVertexAttribDivisor(4, 1);
	glVertexAttribDivisor(5, 1);

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);
}

TileMap::~TileMap()
{
	glDeleteBuffers(1, &m_VAB);
	glDeleteVertexArrays(1, &m_VAO);
}

void TileMap::Load(const std::string& fileName)
{
	bool success = true;
	std::string error;

	XMLDocument doc;
	if (doc.LoadFile((fileName + ".tdef").c_str()) != XML_NO_ERROR)
	{
		success = false;
		error = "Failed loading tile definitions: " + fileName + ".tdef";
	}<commit_msg>Cleanup VAO and VBO in TileMap<commit_after>/**
* Copyright(c) 2014, <NAME> (http://www.zamma.co.uk)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "tilemap.h"
#include "game.h"
#include "light.h"
#include "logger.h"
#include "shader.h"
#include "tinyxml2.h"
#include "util.h"
#include "window.h"
#include <fstream>
#include <iostream>

using namespace tinyxml2;
using namespace ZEGL;

Tile::Tile(const Texture& texture, const Texture& normalMap, const TextureAtlas& textureAtlas,
	const glm::vec3& pos, float rot, float scale) :
	m_tileSize(scale),
	RenderEntity(texture, normalMap, textureAtlas, pos, rot, scale)
{
}

Tile::Tile(const Texture& texture, const Texture& normalMap, const glm::vec2 textureCoords[4],
	const glm::vec3& pos, float rot, float scale) :
	m_tileSize(scale),
	RenderEntity(texture, normalMap, textureCoords, pos, rot, scale)
{
}

Tile::Tile(const Tile& tile) :
	m_tileSize(tile.m_tileSize),
	RenderEntity(tile) {}

TileMap::TileMap(const std::string& fileName) :
	m_VAO(0),
	m_VAB(0),
	m_bytesAllocated(0)
{
	Load(fileName);

	glGenVertexArrays(1, &m_VAO);
	glBindVertexArray(m_VAO);

	glGenBuffers(1, &m_VAB);
	glBindBuffer(GL_ARRAY_BUFFER, m_VAB);

	glEnableVertexAttribArray(0); // pos
	glEnableVertexAttribArray(1); // size
	glEnableVertexAttribArray(2); // texCoords0
	glEnableVertexAttribArray(3); // texCoords1
	glEnableVertexAttribArray(4); // texCoords2
	glEnableVertexAttribArray(5); // texCoords3

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(EntityData), (GLvoid*)0);
	glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, sizeof(EntityData), (GLvoid*)16);
	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(EntityData), (GLvoid*)20);
	glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(EntityData), (GLvoid*)28);
	glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, sizeof(EntityData), (GLvoid*)36);
	glVertexAttribPointer(5, 2, GL_FLOAT, GL_FALSE, sizeof(EntityData), (GLvoid*)44);

	glVertexAttribDivisor(0, 1);
	glVertexAttribDivisor(1, 1);
	glVertexAttribDivisor(2, 1);
	glVertexAttribDivisor(3, 1);
	glVertexAttribDivisor(4, 1);
	glVertexAttribDivisor(5, 1);

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);
}

TileMap::~TileMap()
{
	if (m_VAB)
	{
		glDeleteBuffers(1, &m_VAB);
	}
	if (m_VAO)
	{
		glBindVertexArray(0);
		glDeleteVertexArrays(1, &m_VAO);
	}
}

void TileMap::Load(const std::string& fileName)
{
	bool success = true;
	std::string error;

	XMLDocument doc;
	if (doc.LoadFile((fileName + ".tdef").c_str()) != XML_NO_ERROR)
	{
		success = false;
		error = "Failed loading tile definitions: " + fileName + ".tdef";
	}
{'fraction_non_alphanumeric': 0.13543538637477537, 'fraction_numerical': 0.022381963731416436, 'mean_word_length': 6.143523920653442, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7207287', 'n_tokens_mistral': 2261, 'n_tokens_neox': 2234, 'n_words': 542}