From 865b64ede089f06fad4cbae0afba5898f5925f7a Mon Sep 17 00:00:00 2001 From: Anselme Date: Fri, 20 May 2016 13:24:13 +0200 Subject: [PATCH] fixed framebuffer bug, added map preview --- .gitignore | 1 - src/mapscene.cpp | 2 +- src/mapscene.h | 16 ++++++++-------- src/simulation.cpp | 2 +- src/simulation.h | 4 ++-- src/simulationdialog.cpp | 26 ++++++++++++++++++++++++-- src/simulationdialog.h | 1 + 7 files changed, 37 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index cf455fb..5ff5a69 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ *.user build* -*.so teams/*.dll teams/*.so generators/*.dll diff --git a/src/mapscene.cpp b/src/mapscene.cpp index f6a199a..93b7c45 100644 --- a/src/mapscene.cpp +++ b/src/mapscene.cpp @@ -3,7 +3,7 @@ #define HEX_TO_VEC3(hex) glm::vec3(float((hex & 0xFF0000) >> 16)/255, float((hex & 0x00FF00) >> 8)/255, float(hex & 0x0000FF)/255) -inline glm::vec3 getColor(Pixel::Type type) +glm::vec3 MapScene::getColor(Pixel::Type type) { switch(type){ case Pixel::BEDROCK : return HEX_TO_VEC3(0x101020); diff --git a/src/mapscene.h b/src/mapscene.h index ba18a32..ce41d93 100644 --- a/src/mapscene.h +++ b/src/mapscene.h @@ -9,13 +9,6 @@ struct MapScene : public Map, public BasicScene { public: - struct Pix - { - glm::vec2 pos; - glm::vec3 color; - Pix(glm::vec2 p, glm::vec3 c) : pos(p), color(c) {} - }; - MapScene(int n, int w, int h=0) : Map(n, w, h) {} ~MapScene(); @@ -24,11 +17,18 @@ public: void initDraw(); bool updateNecessary() { return !m_pixels.empty(); } void draw(); + static glm::vec3 getColor(Pixel::Type type); private: unsigned int m_vao; unsigned int m_vbo; - + + struct Pix + { + glm::vec2 pos; + glm::vec3 color; + Pix(glm::vec2 p, glm::vec3 c) : pos(p), color(c) {} + }; std::vector m_pixels; }; diff --git a/src/simulation.cpp b/src/simulation.cpp index 1dd1f14..ce10188 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -5,7 +5,7 @@ #include "dude.h" #include "mapscene.h" -Simulation::Simulation(Map *_map, std::vector &_behaviors): +Simulation::Simulation(MapScene *_map, std::vector &_behaviors): p_map(_map) { int i=0; diff --git a/src/simulation.h b/src/simulation.h index dc99f22..3566ae8 100644 --- a/src/simulation.h +++ b/src/simulation.h @@ -10,11 +10,11 @@ class MapScene; class Simulation { private: - Map *p_map; + MapScene *p_map; std::vector m_dudes; std::vector m_teams; public: - Simulation(Map *_map, std::vector &_behaviors); + Simulation(MapScene *_map, std::vector &_behaviors); MapScene* getMap() { return (MapScene*) p_map; } /** diff --git a/src/simulationdialog.cpp b/src/simulationdialog.cpp index 458c7b1..e7f5571 100644 --- a/src/simulationdialog.cpp +++ b/src/simulationdialog.cpp @@ -26,6 +26,7 @@ SimulationDialog::SimulationDialog(QWidget *parent) : refreshBehaviors(); refreshGenerators(); ui->generatorsList->setCurrentRow(0); + updatePreview(); } SimulationDialog::~SimulationDialog() @@ -117,13 +118,34 @@ void SimulationDialog::generateMap() { m_map = new MapScene(m_selectedBehaviors.size(), ui->mapSizeSpinBox->value()); func((Map*)m_map); - // QPixmap preview(); - // TODO : display map in preview + updatePreview(); } lib.unload(); } } +void SimulationDialog::updatePreview() +{ + int w = ui->mapSizeSpinBox->value(); + int h = w/2; + QImage preview(w, h, QImage::Format_RGB32); + // init pixels + if(m_map != NULL) + { + for(int i=0; igetPixel(Coord(i, j)); + glm::vec3 color = MapScene::getColor(p.type); + preview.setPixel(i, j, qRgb(int(color.r*255), int(color.g*255), int(color.b*255))); + } + } + else + preview.fill(Qt::black); + // update preview + ui->mapPreview->setPixmap(QPixmap::fromImage(preview)); +} + void SimulationDialog::selectBehavior(const QModelIndex &index) { QListWidgetItem* item = ui->behaviorsList->item(index.row()); diff --git a/src/simulationdialog.h b/src/simulationdialog.h index cc7b344..058e7e0 100644 --- a/src/simulationdialog.h +++ b/src/simulationdialog.h @@ -31,6 +31,7 @@ private slots: void refreshBehaviors(); void refreshGenerators(); void generateMap(); + void updatePreview(); void selectBehavior(const QModelIndex &index); private: