mapscene now handle duplicates, and team colors

This commit is contained in:
Anselme FRANÇOIS 2016-05-26 21:39:41 +02:00
parent 927fb17353
commit 69890b57cb
8 changed files with 69 additions and 25 deletions

View File

@ -51,6 +51,8 @@ struct Coord
{ x*=c.x; y*=c.y; return *this; }
Coord operator/(const Coord &c) const
{ return Coord(x/c.x, y/c.y); }
bool operator<(const Coord &c) const // less comparator is used for sorted containers (std::set, std::map, ...)
{ if(x < c.x) return true; else return y < c.y; }
Coord& operator/=(const Coord &c)
{ x/=c.x; y/=c.y; return *this; }
int dist() const // manhatthan distance

View File

@ -4,11 +4,14 @@
#include "pixeltype.h"
#include "coord.h"
class Dude;
struct Pixel
{
union PixelData {
int nbRes;
void* knowledge;
int teamId;
Dude* dudePtr;
};
PixelType type;

View File

@ -1,11 +1,13 @@
#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(PixelType type)
glm::vec3 MapScene::getColor(const Pixel &px) const
{
switch(type){
switch(px.type)
{
case BEDROCK : return HEX_TO_VEC3(0x101020);
case GRASS : return HEX_TO_VEC3(0x719678);
case MARK : return HEX_TO_VEC3(0x5D7B62);
@ -17,12 +19,8 @@ glm::vec3 MapScene::getColor(PixelType type)
case WOOD : return HEX_TO_VEC3(0x634A22);
case STONE : return HEX_TO_VEC3(0x454545);
case IRON : return HEX_TO_VEC3(0x4A4036);
case DUDE :
// TODO
return HEX_TO_VEC3(0x0000FF);
case SPAWN :
// TODO
return HEX_TO_VEC3(0x0000FF);
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);
@ -40,16 +38,22 @@ MapScene::~MapScene()
glDeleteVertexArrays(1, &m_vao);
m_vao = 0;
}
delete m_teamColors;
}
void MapScene::changePixel(const Coord &c, glm::vec3 color)
void MapScene::updatePixel(const Coord &c)
{
m_pixels.push_back(Pix(glm::vec2(c.x, c.y), color));
m_updatedCoordSet.emplace(c);
}
void MapScene::changePixel(const Coord &c, PixelType type)
void MapScene::setColors(int teamId, const glm::vec3 &spawnColor, const glm::vec3 &dudeColor)
{
m_pixels.push_back(Pix(glm::vec2(c.x, c.y), getColor(type)));
if(teamId < getNbTeams())
{
m_teamColors[teamId].spawn = spawnColor;
m_teamColors[teamId].dude = dudeColor;
updatePixel(team(teamId));
}
}
void MapScene::initDraw()
@ -69,19 +73,33 @@ void MapScene::initDraw()
for(int i=0; i<getWidth(); ++i)
for(int j=0; j<getHeight(); ++j)
m_pixels.push_back(Pix(glm::vec2(i+0.5f, j+0.5f), getColor(getPixel(i, j).type)));
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();
for(const Coord &c : m_updatedCoordSet) // WARNING : potential bottleneck
m_pixels.push_back(Pix(glm::vec2(c.x+0.5f, c.y+0.5f), getColor(getPixel(c))));
glBufferData(GL_ARRAY_BUFFER, m_pixels.size() * sizeof(Pix), m_pixels.data(), GL_DYNAMIC_DRAW);
m_mutex.unlock();
// clearing temporary data structures
m_updatedCoordSet.clear();
m_pixels.clear();
// drawing changes
glDrawArrays(GL_POINTS, 0, m_pixels.size());
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
m_pixels.clear();
}

View File

@ -5,23 +5,41 @@
#include <scene.h>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <QMutex>
#include <set>
struct MapScene : public Map, public BasicScene
{
public:
MapScene(int n, int w, int h=0) : Map(n, w, h) {}
MapScene(int n, int w, int h=0) : Map(n, w, h), m_teamColors(new TeamColor[n]) {}
~MapScene();
void changePixel(const Coord &c, glm::vec3 color);
void changePixel(const Coord &c, PixelType type);
/**
* @brief updatePixel marks a pixel coordinate as changed,
* duplicates are removed and the pixel will be updated
* only once on the next draw with the latest map pixel information.
*/
void updatePixel(const Coord &c);
void setColors(int teamId, const glm::vec3 &spawnColor, const glm::vec3 &dudeColor);
void initDraw();
bool updateNecessary() { return !m_pixels.empty(); }
void draw();
static glm::vec3 getColor(PixelType type);
glm::vec3 getColor(const Pixel &px) const;
private:
struct TeamColor
{
glm::vec3 spawn;
glm::vec3 dude;
TeamColor() : spawn(glm::vec3(0, 0, 1)), dude(glm::vec3(0, 0, 0.8f)) {}
};
TeamColor* m_teamColors;
unsigned int m_vao;
unsigned int m_vbo;
QMutex m_mutex;
struct Pix
{
@ -30,6 +48,7 @@ private:
Pix(glm::vec2 p, glm::vec3 c) : pos(p), color(c) {}
};
std::vector<Pix> m_pixels;
std::set<Coord> m_updatedCoordSet;
};
#endif // MAPSCENE_H

View File

@ -14,9 +14,9 @@ Simulation::Simulation(MapScene *_map, std::vector<BehaviorFunction> &_behaviors
for(BehaviorFunction &behavior : _behaviors){
glm::vec3 color;
color[i%3]=0.5f*(1 + i/3);
m_teams.push_back(Team(color, color, behavior));
m_teams.push_back(Team(color, color*0.8f, behavior));
p_map->setColors(i, color, color*0.8f);
i++;
// TODO : display the spawns on the map
}
}

View File

@ -144,7 +144,7 @@ void SimulationDialog::updatePreview()
for(int j=0; j<h; ++j)
{
const Pixel &p = m_map->getPixel(Coord(i, j));
glm::vec3 color = MapScene::getColor(p.type);
glm::vec3 color = m_map->getColor(p);
m_currentPreview->setPixel(i, j, qRgb(int(color.r*255), int(color.g*255), int(color.b*255)));
}
}

View File

@ -1,8 +1,8 @@
#include "team.h"
#include "dude.h"
Team::Team(const glm::vec3 &teamColor, const glm::vec3 &dudeColor, BehaviorFunction _behavior):
m_teamColor(teamColor),
Team::Team(const glm::vec3 &spawnColor, const glm::vec3 &dudeColor, BehaviorFunction _behavior):
m_spawnColor(spawnColor),
m_dudeColor(dudeColor),
m_spawnCooldown(1),
m_foodQuantity(NB_STARTING_FOOD),

View File

@ -15,7 +15,7 @@ class Dude;
class Team
{
glm::vec3 m_teamColor;
glm::vec3 m_spawnColor;
glm::vec3 m_dudeColor;
int m_spawnCooldown;
int m_foodQuantity;
@ -26,6 +26,8 @@ public:
void addFood() {m_foodQuantity++; }
void destroySpawn();
Action* update(Dude*);
glm::vec3 getSpawnColor() { return m_spawnColor; }
glm::vec3 getDudeColor() { return m_dudeColor; }
};
#endif // TEAM_H