From 69890b57cb4bc3184177a89f9626ea54ec4f15d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anselme=20FRAN=C3=87OIS?= Date: Thu, 26 May 2016 21:39:41 +0200 Subject: [PATCH] mapscene now handle duplicates, and team colors --- src/coord.h | 2 ++ src/map.h | 5 ++++- src/mapscene.cpp | 46 ++++++++++++++++++++++++++++------------ src/mapscene.h | 27 +++++++++++++++++++---- src/simulation.cpp | 4 ++-- src/simulationdialog.cpp | 2 +- src/team.cpp | 4 ++-- src/team.h | 4 +++- 8 files changed, 69 insertions(+), 25 deletions(-) diff --git a/src/coord.h b/src/coord.h index 8d0887e..aca5977 100644 --- a/src/coord.h +++ b/src/coord.h @@ -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 diff --git a/src/map.h b/src/map.h index f90e790..0ff3fb7 100644 --- a/src/map.h +++ b/src/map.h @@ -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; diff --git a/src/mapscene.cpp b/src/mapscene.cpp index 14071a1..28c10de 100644 --- a/src/mapscene.cpp +++ b/src/mapscene.cpp @@ -1,11 +1,13 @@ #include #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 #include #include +#include +#include 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 m_pixels; + std::set m_updatedCoordSet; }; #endif // MAPSCENE_H diff --git a/src/simulation.cpp b/src/simulation.cpp index 4896274..5140658 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -14,9 +14,9 @@ Simulation::Simulation(MapScene *_map, std::vector &_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 } } diff --git a/src/simulationdialog.cpp b/src/simulationdialog.cpp index 0598725..0d21942 100644 --- a/src/simulationdialog.cpp +++ b/src/simulationdialog.cpp @@ -144,7 +144,7 @@ void SimulationDialog::updatePreview() for(int j=0; jgetPixel(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))); } } diff --git a/src/team.cpp b/src/team.cpp index b54536a..b71172b 100644 --- a/src/team.cpp +++ b/src/team.cpp @@ -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), diff --git a/src/team.h b/src/team.h index 3e1f76a..d6f3ffb 100644 --- a/src/team.h +++ b/src/team.h @@ -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