109 lines
3.3 KiB
C++
109 lines
3.3 KiB
C++
#include <sparrowrenderer.h>
|
|
#include "mapscene.h"
|
|
#include "dude.h"
|
|
|
|
#define HEX_TO_VEC3(hex) glm::vec3(float((hex & 0xFF0000) >> 16)/255, float((hex & 0x00FF00) >> 8)/255, float(hex & 0x0000FF)/255)
|
|
|
|
glm::vec3 MapScene::getColor(const Pixel &px) const
|
|
{
|
|
switch(px.type)
|
|
{
|
|
case WATER : return HEX_TO_VEC3(0x2033B0);
|
|
case GRASS : return HEX_TO_VEC3(0x719678);
|
|
case MARK : return HEX_TO_VEC3(0x5D7B62);
|
|
case ROCK : return HEX_TO_VEC3(0x8C8C8C);
|
|
case IRON_ORE : return HEX_TO_VEC3(0x917B61);
|
|
case TREE : return HEX_TO_VEC3(0x003800);
|
|
case BERRIES : return HEX_TO_VEC3(0x4D6394);
|
|
case FOOD : return HEX_TO_VEC3(0xFF7A7A);
|
|
case WOOD : return HEX_TO_VEC3(0x634A22);
|
|
case STONE : return HEX_TO_VEC3(0x454545);
|
|
case IRON : return HEX_TO_VEC3(0x4A4036);
|
|
case DUDE : return m_teamColors[px.data.dudePtr->getTeam()].dude;
|
|
case SPAWN : return m_teamColors[px.data.teamId].spawn;
|
|
case WALL : return HEX_TO_VEC3(0xE6B2A1);
|
|
case ROAD : return HEX_TO_VEC3(0xEDB287);
|
|
case SWORD : return HEX_TO_VEC3(0xEBEBEB);
|
|
case LIBRARY : return HEX_TO_VEC3(0xA37A50);
|
|
case DEAD_DUDE : return HEX_TO_VEC3(0xFF0000);
|
|
default : return HEX_TO_VEC3(0x0000FF); // bleu absolu = bug
|
|
}
|
|
}
|
|
|
|
MapScene::~MapScene()
|
|
{
|
|
if(m_vao != 0)
|
|
{
|
|
glDeleteBuffers(1, &m_vbo);
|
|
glDeleteVertexArrays(1, &m_vao);
|
|
m_vao = 0;
|
|
}
|
|
delete[] m_teamColors;
|
|
}
|
|
|
|
void MapScene::updatePixel(const Coord &c)
|
|
{
|
|
QMutexLocker lock(&m_mutex);
|
|
if(m_updatedCoordMap.count(c))
|
|
m_pixels[m_updatedCoordMap[c]].color = getColor(getPixel(c));
|
|
else
|
|
{
|
|
m_updatedCoordMap[c] = m_pixels.size();
|
|
m_pixels.push_back(Pix(glm::vec2(c.x+0.5f, c.y+0.5f), getColor(getPixel(c))));
|
|
}
|
|
}
|
|
|
|
void MapScene::setColors(int teamId, const glm::vec3 &spawnColor, const glm::vec3 &dudeColor)
|
|
{
|
|
if(teamId < getNbTeams())
|
|
{
|
|
m_teamColors[teamId].spawn = spawnColor;
|
|
m_teamColors[teamId].dude = dudeColor;
|
|
updatePixel(team(teamId));
|
|
}
|
|
}
|
|
|
|
void MapScene::initDraw()
|
|
{
|
|
glGenVertexArrays(1, &m_vao);
|
|
glBindVertexArray(m_vao);
|
|
|
|
glGenBuffers(1, &m_vbo);
|
|
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
|
glEnableVertexAttribArray(0);
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Pix), (void*)(0));
|
|
glEnableVertexAttribArray(1);
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Pix), (void*)(sizeof(glm::vec2)));
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
for(int i=0; i<getWidth(); ++i)
|
|
for(int j=0; j<getHeight(); ++j)
|
|
updatePixel(Coord(i, j));
|
|
}
|
|
|
|
void MapScene::draw()
|
|
{
|
|
// preparing opengl context
|
|
glDisable(GL_BLEND);
|
|
glDisable(GL_DEPTH_TEST);
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
glBindVertexArray(m_vao);
|
|
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
|
|
|
// updating VBO data
|
|
m_mutex.lock();
|
|
glBufferData(GL_ARRAY_BUFFER, m_pixels.size() * sizeof(Pix), m_pixels.data(), GL_DYNAMIC_DRAW);
|
|
m_mutex.unlock();
|
|
|
|
// drawing changes
|
|
glDrawArrays(GL_POINTS, 0, m_pixels.size());
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
glBindVertexArray(0);
|
|
|
|
// clearing temporary data structures
|
|
m_updatedCoordMap.clear();
|
|
m_pixels.clear();
|
|
}
|