From f593f7018c3f4a6797b3eb81b7e6eb82aa38dbc5 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Sun, 6 Mar 2016 11:48:54 +0100 Subject: [PATCH 01/11] fixed some error in A*, deleted struct PriorityNode --- src/main.cpp | 7 ++++--- src/tools/graph.cpp | 9 +++++++++ src/tools/graph.h | 9 ++++++++- src/tools/pathfinder.cpp | 29 ++++++++++++++++------------- src/tools/pathfinder.h | 12 +++++------- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 035941c..07596cd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,10 +31,11 @@ int main(){ GraphNode n4 = GraphNode(); n4.setValue(4); - n1.addNeighbours(&n2,2); + n1.addNeighbours(&n2,7); n1.addNeighbours(&n3,3); - n2.addNeighbours(&n3,5); - n3.addNeighbours(&n4,2); + n2.addNeighbours(&n4,2); + n3.addNeighbours(&n4,20); + n3.addNeighbours(&n2,2); std::vector path = PathFinder::a_star(&n1,&n4); std::cout << "Path Size: " << path.size() << std::endl; diff --git a/src/tools/graph.cpp b/src/tools/graph.cpp index cc97225..7782502 100644 --- a/src/tools/graph.cpp +++ b/src/tools/graph.cpp @@ -48,6 +48,15 @@ int GraphNode::getValue(){ return m_testvalue; } +void GraphNode::setPriority(float a){ + m_priority = a; +} + +float GraphNode::getPriority(){ + return m_priority; +} + + GraphEdge::GraphEdge(GraphNode* gn,float v):m_target(gn),m_weight(v) diff --git a/src/tools/graph.h b/src/tools/graph.h index 42f670d..b6abb2f 100644 --- a/src/tools/graph.h +++ b/src/tools/graph.h @@ -10,6 +10,8 @@ class GraphNode { std::vector m_neighbours; int m_testvalue; //temp variable + float m_priority; + public: GraphNode(); GraphNode(std::vector); @@ -18,10 +20,15 @@ public: void addNeighbours(GraphNode*,float); void addNeighbours(GraphEdge); int getNbNeighbours(); - virtual float heuristic(GraphNode*){return 1;}; + virtual float heuristic(GraphNode*){ + std::cout << "WARNING : You're using the default heuristic. For better result, redefine the heuristic function in GraphNode." << std::endl; + return 1; + }; void setValue(int); int getValue(); + void setPriority(float); + float getPriority(); void print(std::string prefix); }; diff --git a/src/tools/pathfinder.cpp b/src/tools/pathfinder.cpp index e692459..1099a98 100644 --- a/src/tools/pathfinder.cpp +++ b/src/tools/pathfinder.cpp @@ -8,27 +8,26 @@ PathFinder::PathFinder(){ } -std::vector PathFinder::a_star(GraphNode* start,GraphNode* goal) +std::vector PathFinder::a_star(GraphNode* start,GraphNode* goal,bool debug) { // Check if priorityqueue sort value in right order. - std::priority_queue frontier = std::priority_queue(); + std::priority_queue,ComparePriority> frontier = std::priority_queue,ComparePriority>(); std::map cost = std::map(); //cost of visited node std::map pred = std::map(); //pred of visited node // init frontier, cost, and pred with value for start - PriorityNode* pn = new PriorityNode(); - pn->node = std::pair(start,0); - frontier.push(pn); + frontier.push(start); cost.insert(std::pair(start,0)); pred.insert(std::pair(start,NULL)); GraphNode* current; while(!frontier.empty()){ //pick best element from frontier (with priority queue the best is in front) - pn = frontier.top(); - current = pn->node.first; + current = frontier.top(); + //current = pn->node.first; frontier.pop(); + if(debug) std::cout << "Exploring node " << current->getValue() << std::endl; // goal reached, end of a-star if (current == goal){ break; @@ -36,17 +35,20 @@ std::vector PathFinder::a_star(GraphNode* start,GraphNode* goal) // for all neighbours of current node for (GraphEdge next : current->getNeighbours()){ - float new_cost = cost[current] + next.getWeight(); + float new_cost = cost[current] + next.getWeight(); + if(debug) std::cout << "\tExploring neighbours node " << next.getTarget()->getValue() << " with cost " << new_cost << std::endl; - if ((cost.count(next.getTarget()) == 0) || (new_cost < cost[next.getTarget()])){ + bool never_visited = cost.count(next.getTarget()) == 0; + if ((never_visited) || (new_cost < cost[next.getTarget()])){ // affect processed cost to next node in cost_map cost[next.getTarget()] = new_cost; // calcul priority of node with heuristic,and add it to frontier - float priority = new_cost; //+ heuristic(next.getTarget(), goal); - PriorityNode* pn = new PriorityNode(); - pn->node = std::pair(next.getTarget(),priority); - frontier.push(pn); + float priority = new_cost + next.getTarget()->heuristic(goal); + if(debug) std::cout << "\t\t Priority: " << priority << std::endl; + next.getTarget()->setPriority(priority); + if(never_visited) + frontier.push(next.getTarget()); // memorize predecessor for next pred[next.getTarget()] = current; @@ -62,6 +64,7 @@ std::vector PathFinder::a_star(GraphNode* start,GraphNode* goal) } path.push_back(start); std::reverse(path.begin(),path.end()); + if(debug) std::cout << "path cost :" << cost[goal] << std::endl; return path; } diff --git a/src/tools/pathfinder.h b/src/tools/pathfinder.h index d4a3d94..47e75d6 100644 --- a/src/tools/pathfinder.h +++ b/src/tools/pathfinder.h @@ -8,16 +8,14 @@ class PathFinder { public: PathFinder(); - static std::vector a_star(GraphNode* start,GraphNode* goal); + static std::vector a_star(GraphNode* start,GraphNode* goal,bool debug = false); }; -struct PriorityNode -{ - std::pair node; - bool operator<(PriorityNode other) const +class ComparePriority{ +public: + bool operator() (GraphNode* a,GraphNode* b) { - return node.second < other.node.second; + return a->getPriority() > b->getPriority(); } }; - #endif // PATHFINDER_H From b38548de82e48ebbb44dbbdac4efdd318e2cf128 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Sun, 6 Mar 2016 14:24:26 +0100 Subject: [PATCH 02/11] fixed value in priorityqueue --- src/main.cpp | 7 +++++-- src/tools/graph.cpp | 2 -- src/tools/graph.h | 2 +- src/tools/pathfinder.cpp | 24 ++++++++++++++++++------ src/tools/pathfinder.h | 5 +---- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 07596cd..e4007cf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,14 +30,17 @@ int main(){ n3.setValue(3); GraphNode n4 = GraphNode(); n4.setValue(4); + GraphNode n5 = GraphNode(); + n5.setValue(5); - n1.addNeighbours(&n2,7); + n1.addNeighbours(&n2,6); n1.addNeighbours(&n3,3); n2.addNeighbours(&n4,2); n3.addNeighbours(&n4,20); n3.addNeighbours(&n2,2); + n3.addNeighbours(&n5,10); - std::vector path = PathFinder::a_star(&n1,&n4); + std::vector path = PathFinder::a_star(&n1,&n4,true); std::cout << "Path Size: " << path.size() << std::endl; for(GraphNode* gn: path){ std::cout << gn->getValue() << std::endl; diff --git a/src/tools/graph.cpp b/src/tools/graph.cpp index 7782502..84ef650 100644 --- a/src/tools/graph.cpp +++ b/src/tools/graph.cpp @@ -57,8 +57,6 @@ float GraphNode::getPriority(){ } - - GraphEdge::GraphEdge(GraphNode* gn,float v):m_target(gn),m_weight(v) {} diff --git a/src/tools/graph.h b/src/tools/graph.h index b6abb2f..0ac4c37 100644 --- a/src/tools/graph.h +++ b/src/tools/graph.h @@ -22,7 +22,7 @@ public: int getNbNeighbours(); virtual float heuristic(GraphNode*){ std::cout << "WARNING : You're using the default heuristic. For better result, redefine the heuristic function in GraphNode." << std::endl; - return 1; + return 0; }; void setValue(int); diff --git a/src/tools/pathfinder.cpp b/src/tools/pathfinder.cpp index 1099a98..dcbd6a1 100644 --- a/src/tools/pathfinder.cpp +++ b/src/tools/pathfinder.cpp @@ -3,6 +3,8 @@ #include #include #include +#include + //#include "fiboheap.h" PathFinder::PathFinder(){ @@ -15,6 +17,8 @@ std::vector PathFinder::a_star(GraphNode* start,GraphNode* goal,bool std::map cost = std::map(); //cost of visited node std::map pred = std::map(); //pred of visited node + std::set visited = std::set(); + // init frontier, cost, and pred with value for start frontier.push(start); cost.insert(std::pair(start,0)); @@ -26,6 +30,10 @@ std::vector PathFinder::a_star(GraphNode* start,GraphNode* goal,bool current = frontier.top(); //current = pn->node.first; frontier.pop(); + if (visited.count(current) == 0) + visited.insert(current); + else + continue; if(debug) std::cout << "Exploring node " << current->getValue() << std::endl; // goal reached, end of a-star @@ -38,8 +46,7 @@ std::vector PathFinder::a_star(GraphNode* start,GraphNode* goal,bool float new_cost = cost[current] + next.getWeight(); if(debug) std::cout << "\tExploring neighbours node " << next.getTarget()->getValue() << " with cost " << new_cost << std::endl; - bool never_visited = cost.count(next.getTarget()) == 0; - if ((never_visited) || (new_cost < cost[next.getTarget()])){ + if ((cost.count(next.getTarget()) == 0) || (new_cost < cost[next.getTarget()])){ // affect processed cost to next node in cost_map cost[next.getTarget()] = new_cost; @@ -47,11 +54,10 @@ std::vector PathFinder::a_star(GraphNode* start,GraphNode* goal,bool float priority = new_cost + next.getTarget()->heuristic(goal); if(debug) std::cout << "\t\t Priority: " << priority << std::endl; next.getTarget()->setPriority(priority); - if(never_visited) - frontier.push(next.getTarget()); - + visited.erase(next.getTarget()); + frontier.push(next.getTarget()); // memorize predecessor for next - pred[next.getTarget()] = current; + pred[next.getTarget()] = current; } } } @@ -69,3 +75,9 @@ std::vector PathFinder::a_star(GraphNode* start,GraphNode* goal,bool } +bool ComparePriority::operator ()(GraphNode* a, GraphNode* b) +{ + return a->getPriority() > b->getPriority(); +} + + diff --git a/src/tools/pathfinder.h b/src/tools/pathfinder.h index 47e75d6..faeaf94 100644 --- a/src/tools/pathfinder.h +++ b/src/tools/pathfinder.h @@ -13,9 +13,6 @@ public: class ComparePriority{ public: - bool operator() (GraphNode* a,GraphNode* b) - { - return a->getPriority() > b->getPriority(); - } + bool operator() (GraphNode*,GraphNode*s); }; #endif // PATHFINDER_H From 7911e7bddf277d79fdd15cba494fb6d37d9a403c Mon Sep 17 00:00:00 2001 From: Lendemor Date: Fri, 11 Mar 2016 21:49:10 +0100 Subject: [PATCH 03/11] added message system --- src/engine.cpp | 4 ++-- src/message.cpp | 7 +++++++ src/message.h | 18 ++++++++++++++++++ src/messagebus.cpp | 11 +++++++++++ src/messagebus.h | 18 ++++++++++++++++++ src/system.cpp | 7 +++++++ src/system.h | 20 ++++++++++++++++++++ src/tools/graph.h | 2 +- src/tools/loader.cpp | 1 + 9 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 src/message.cpp create mode 100644 src/message.h create mode 100644 src/messagebus.cpp create mode 100644 src/messagebus.h create mode 100644 src/system.cpp create mode 100644 src/system.h diff --git a/src/engine.cpp b/src/engine.cpp index cdcf083..ecb203b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -19,14 +19,14 @@ Engine::Engine() : m_clock = new sf::Clock(); m_clock->restart(); m_renderer = new SparrowRenderer(); - m_renderer->setCamera(NULL); + // m_renderer->setCamera(NULL); } Engine::~Engine() { delete m_clock; if(m_window != NULL) - m_renderer->destroyGL(); + // m_renderer->destroyGL(); delete m_renderer; if(m_window != NULL) { diff --git a/src/message.cpp b/src/message.cpp new file mode 100644 index 0000000..3a27417 --- /dev/null +++ b/src/message.cpp @@ -0,0 +1,7 @@ +#include "message.h" + + +Message::Message(std::string id, SystemType sender):m_id(id),m_sender(sender) +{ + +} diff --git a/src/message.h b/src/message.h new file mode 100644 index 0000000..61f9192 --- /dev/null +++ b/src/message.h @@ -0,0 +1,18 @@ +#ifndef MESSAGE_H +#define MESSAGE_H + +#include +#include +#include "system.h" + +class Message +{ + std::string m_id; + SystemType m_sender; +public: + Message(std::string,SystemType); +}; + + + +#endif // MESSAGE_H diff --git a/src/messagebus.cpp b/src/messagebus.cpp new file mode 100644 index 0000000..2cccec1 --- /dev/null +++ b/src/messagebus.cpp @@ -0,0 +1,11 @@ +#include "messagebus.h" + +MessageBus::MessageBus() +{ + +} + +void MessageBus::postMessage(Message *msg) +{ + message_list.push_back(msg); +} diff --git a/src/messagebus.h b/src/messagebus.h new file mode 100644 index 0000000..626c971 --- /dev/null +++ b/src/messagebus.h @@ -0,0 +1,18 @@ +#ifndef MESSAGEBUS_H +#define MESSAGEBUS_H + +#include + +class Message; + +class MessageBus +{ + std::vector message_list; //message file + +public: + MessageBus(); + void postMessage(Message* msg); + void handleMessage(); +}; + +#endif // MESSAGEBUS_H diff --git a/src/system.cpp b/src/system.cpp new file mode 100644 index 0000000..636b73c --- /dev/null +++ b/src/system.cpp @@ -0,0 +1,7 @@ +#include "system.h" + + +System::System() +{ + +} diff --git a/src/system.h b/src/system.h new file mode 100644 index 0000000..3cfb905 --- /dev/null +++ b/src/system.h @@ -0,0 +1,20 @@ +#ifndef SYSTEM_H +#define SYSTEM_H + +class MessageBus; +class Message; + +//TODO:complete this part with other existing system, +enum SystemType {INPUT_SYSTEM, RENDERER_SYSTEM, IA_SYSTEM}; + +class System +{ + SystemType m_type; + MessageBus* m_msgBus; + +public: + System(); + virtual void handleMessage(Message* message) = 0; +}; + +#endif // SYSTEM_H diff --git a/src/tools/graph.h b/src/tools/graph.h index 42f670d..aea86fe 100644 --- a/src/tools/graph.h +++ b/src/tools/graph.h @@ -18,7 +18,7 @@ public: void addNeighbours(GraphNode*,float); void addNeighbours(GraphEdge); int getNbNeighbours(); - virtual float heuristic(GraphNode*){return 1;}; + virtual float heuristic(GraphNode*){return 1;} void setValue(int); int getValue(); diff --git a/src/tools/loader.cpp b/src/tools/loader.cpp index 102bf4e..6f962fb 100644 --- a/src/tools/loader.cpp +++ b/src/tools/loader.cpp @@ -1,6 +1,7 @@ #include "loader.h" #include #include +#include #include #include From 01c14fefe3edcb4ed09efa6f69bed2b76cdea406 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Sun, 13 Mar 2016 15:49:30 +0100 Subject: [PATCH 04/11] replace GraphEdge with cost function --- src/engine.cpp | 4 ++-- src/main.cpp | 14 +++++++------- src/tools/graph.cpp | 35 +++++++---------------------------- src/tools/graph.h | 28 ++++++++++------------------ src/tools/loader.cpp | 1 + src/tools/pathfinder.cpp | 22 +++++++++++----------- 6 files changed, 38 insertions(+), 66 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index cdcf083..d7f2993 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -19,14 +19,14 @@ Engine::Engine() : m_clock = new sf::Clock(); m_clock->restart(); m_renderer = new SparrowRenderer(); - m_renderer->setCamera(NULL); + //m_renderer->setCamera(NULL); } Engine::~Engine() { delete m_clock; if(m_window != NULL) - m_renderer->destroyGL(); + // m_renderer->destroyGL(); delete m_renderer; if(m_window != NULL) { diff --git a/src/main.cpp b/src/main.cpp index 4b291cc..9eb1c9e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,7 @@ int main(){ scene.addChild("camera"); Engine engine; - engine.getRenderer()->setCamera(&camera); + //engine.getRenderer()->setCamera(&camera); engine.createWindow("test"); engine.setScene("testScene"); engine.start(); @@ -32,12 +32,12 @@ int main(){ GraphNode n5 = GraphNode(); n5.setValue(5); - n1.addNeighbours(&n2,6); - n1.addNeighbours(&n3,3); - n2.addNeighbours(&n4,2); - n3.addNeighbours(&n4,20); - n3.addNeighbours(&n2,2); - n3.addNeighbours(&n5,10); + n1.addNeighbours(&n2); + n1.addNeighbours(&n3); + n2.addNeighbours(&n4); + n3.addNeighbours(&n4); + n3.addNeighbours(&n2); + n3.addNeighbours(&n5); std::vector path = PathFinder::a_star(&n1,&n4,true); std::cout << "Path Size: " << path.size() << std::endl; diff --git a/src/tools/graph.cpp b/src/tools/graph.cpp index 84ef650..9e04a8e 100644 --- a/src/tools/graph.cpp +++ b/src/tools/graph.cpp @@ -5,27 +5,22 @@ using namespace std; GraphNode::GraphNode() { - m_neighbours = vector(); + m_neighbours = vector(); } -GraphNode::GraphNode(vector neighbours):m_neighbours(neighbours) +GraphNode::GraphNode(vector neighbours):m_neighbours(neighbours) { } -void GraphNode::addNeighbours(GraphNode* n,float f){ - GraphEdge edge = GraphEdge(n,f); - addNeighbours(edge); -} - -void GraphNode::addNeighbours(GraphEdge edge){ - m_neighbours.push_back(edge); +void GraphNode::addNeighbours(GraphNode* node){ + m_neighbours.push_back(node); } int GraphNode::getNbNeighbours(){ return m_neighbours.size(); } -vector GraphNode::getNeighbours(){ +vector GraphNode::getNeighbours(){ return m_neighbours; } @@ -33,11 +28,6 @@ void GraphNode::print(std::string prefix) { cout << prefix << "Node_id : "; cout << prefix << m_testvalue << endl; -// cout << prefix << "Neighbours list :" << endl; - //prefix += "\t"; - //for_each (m_neighbours.begin(), m_neighbours.end(),[prefix](GraphEdge voisin) { - // voisin.getTarget()->print(prefix); - //}); } void GraphNode::setValue(int a){ @@ -48,22 +38,11 @@ int GraphNode::getValue(){ return m_testvalue; } -void GraphNode::setPriority(float a){ - m_priority = a; +void GraphNode::setPriority(float priority){ + m_priority = priority; } float GraphNode::getPriority(){ return m_priority; } - -GraphEdge::GraphEdge(GraphNode* gn,float v):m_target(gn),m_weight(v) -{} - -GraphNode* GraphEdge::getTarget(){ - return m_target; -} - -float GraphEdge::getWeight(){ - return m_weight; -} diff --git a/src/tools/graph.h b/src/tools/graph.h index 0ac4c37..541967c 100644 --- a/src/tools/graph.h +++ b/src/tools/graph.h @@ -8,22 +8,25 @@ class GraphEdge; class GraphNode { - std::vector m_neighbours; + std::vector m_neighbours; int m_testvalue; //temp variable float m_priority; public: GraphNode(); - GraphNode(std::vector); + GraphNode(std::vector); - std::vector getNeighbours(); - void addNeighbours(GraphNode*,float); - void addNeighbours(GraphEdge); + std::vector getNeighbours(); + void addNeighbours(GraphNode*); int getNbNeighbours(); + virtual float cost(GraphNode*){ + std::cout << "WARNING : You're using the default cost function. For better result, redefine the cost function in GraphNode." << std::endl; + return 1; + } virtual float heuristic(GraphNode*){ - std::cout << "WARNING : You're using the default heuristic. For better result, redefine the heuristic function in GraphNode." << std::endl; + std::cout << "WARNING : You're using the default heuristic function. For better result, redefine the heuristic function in GraphNode." << std::endl; return 0; - }; + } void setValue(int); int getValue(); @@ -32,15 +35,4 @@ public: void print(std::string prefix); }; -class GraphEdge -{ - GraphNode* m_target; - float m_weight; - -public: - GraphEdge(GraphNode*,float); - GraphNode* getTarget(); - float getWeight(); -}; - #endif // GRAPH_H diff --git a/src/tools/loader.cpp b/src/tools/loader.cpp index 102bf4e..6f962fb 100644 --- a/src/tools/loader.cpp +++ b/src/tools/loader.cpp @@ -1,6 +1,7 @@ #include "loader.h" #include #include +#include #include #include diff --git a/src/tools/pathfinder.cpp b/src/tools/pathfinder.cpp index dcbd6a1..d5f6c91 100644 --- a/src/tools/pathfinder.cpp +++ b/src/tools/pathfinder.cpp @@ -10,7 +10,7 @@ PathFinder::PathFinder(){ } -std::vector PathFinder::a_star(GraphNode* start,GraphNode* goal,bool debug) +std::vector PathFinder::a_star(GraphNode* start, GraphNode* goal, bool debug) { // Check if priorityqueue sort value in right order. std::priority_queue,ComparePriority> frontier = std::priority_queue,ComparePriority>(); @@ -42,22 +42,22 @@ std::vector PathFinder::a_star(GraphNode* start,GraphNode* goal,bool } // for all neighbours of current node - for (GraphEdge next : current->getNeighbours()){ - float new_cost = cost[current] + next.getWeight(); - if(debug) std::cout << "\tExploring neighbours node " << next.getTarget()->getValue() << " with cost " << new_cost << std::endl; + for (GraphNode* next : current->getNeighbours()){ + float new_cost = cost[current] + current->cost(next); + if(debug) std::cout << "\tExploring neighbours node " << next->getValue() << " with cost " << new_cost << std::endl; - if ((cost.count(next.getTarget()) == 0) || (new_cost < cost[next.getTarget()])){ + if ((cost.count(next) == 0) || (new_cost < cost[next])){ // affect processed cost to next node in cost_map - cost[next.getTarget()] = new_cost; + cost[next] = new_cost; // calcul priority of node with heuristic,and add it to frontier - float priority = new_cost + next.getTarget()->heuristic(goal); + float priority = new_cost + next->heuristic(goal); if(debug) std::cout << "\t\t Priority: " << priority << std::endl; - next.getTarget()->setPriority(priority); - visited.erase(next.getTarget()); - frontier.push(next.getTarget()); + next->setPriority(priority); + visited.erase(next); + frontier.push(next); // memorize predecessor for next - pred[next.getTarget()] = current; + pred[next] = current; } } } From 5e82f3339fb5b126694045c202da82bce860759e Mon Sep 17 00:00:00 2001 From: Lendemor Date: Tue, 15 Mar 2016 16:24:49 +0100 Subject: [PATCH 05/11] added loadMesh and loadMTL, not tested yet --- src/tools/loader.cpp | 277 +++++++++++++++++++++++++++++++++++++++++++ src/tools/loader.h | 4 + 2 files changed, 281 insertions(+) diff --git a/src/tools/loader.cpp b/src/tools/loader.cpp index 6f962fb..fe3d85d 100644 --- a/src/tools/loader.cpp +++ b/src/tools/loader.cpp @@ -3,7 +3,12 @@ #include #include #include +#include "../resourcemanager.h" #include +#include +#include +//#include +#include "../sparrowrenderer/src/texture.h" std::string* Loader::loadTextFile(const std::string &filename) { @@ -40,3 +45,275 @@ Image* Loader::loadImage(const std::string &filename, bool hasAlpha) } return img; } + +std::vector Loader::loadMesh(const std::string &filename){ + std::vector meshes; + //MeshLoader* loader = new MeshLoader(wavefrontFilename); + //loader->textureLoader.setTexturePath(fileInfo.absolutePath()); + std::vector pos; + std::vector norm; + std::vector tex; + +// char line[LINEBUFFER_LENGTH]; + std::string line; + + Material* defaultMat = RESOURCE_GET(Material, "default"); + if(defaultMat == NULL) + { + defaultMat = new PhongMaterial(); + RESOURCE_ADD(defaultMat,Material,"default"); + } + Material* currentMat = defaultMat; + + std::ifstream file(filename); + + if(!file.is_open()) + { + fprintf(stderr, "can't load %s.\n", filename.c_str()); + return meshes; + } + + MeshBuilder* currentMesh = new MeshBuilder(); + meshes.push_back(currentMesh); + currentMesh->setMaterial(currentMat); + + std::getline(file, line); + while(!file.eof()) + { + if(line.length() == 0) // line vide + { + std::getline(file, line); + continue; + } + + switch(line[0]) + { + case 'v': + //vertex attribute + switch(line[1]) + { + case ' ': // vertex position + { + glm::vec3 p; + std::sscanf(line.c_str(),"v %f %f %f",&(p.x),&(p.y),&(p.z)); + pos.push_back(p); + break; + } + case 't': // texCoord + { + glm::vec2 t; + std::sscanf(line.c_str(),"vt %f %f",&(t.x),&(t.y)); + tex.push_back(t); + break; + } + case 'n': // normal + { + glm::vec3 n; + std::sscanf(line.c_str(),"vn %f %f %f",&(n.x),&(n.y),&(n.z)); + norm.push_back(n); + break; + } + } + break; + case 'f': // face + { + int tab[9]; + std::sscanf(line.c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d",tab,tab+1,tab+2,tab+3,tab+4,tab+5,tab+6,tab+7,tab+8); + //TODO: check sscanf success + + int nb_vertices = currentMesh->positions.size(); + + currentMesh->addTriangle(nb_vertices, nb_vertices+1, nb_vertices+2); + for(int i=0; i<3; ++i) + { + if(norm.size() == 0) + { + if(tex.size() == 0) + currentMesh->addPosition(pos[tab[i]-1]); + else + currentMesh->addVertex(pos[tab[i]-1], tex[tab[i+1]-1]); + } + else + { + if(tex.size() == 0) + currentMesh->addVertex(pos[tab[i]-1], norm[tab[i+2]-1]); + else + currentMesh->addVertex(pos[tab[i]-1], norm[tab[i+2]-1], tex[tab[i+1]-1]); + } + } + } + break; + case 'g': + currentMesh = new MeshBuilder(); + meshes.push_back(currentMesh); + currentMesh->setMaterial(currentMat); + break; + case 'm': // mtllib + { + std::string mat_filename; + std::sscanf(line.c_str(),"m %s",mat_filename); + loadMTL(mat_filename); + break; + } + case 'u': + { + // usemtl + std::string mat_name; + std::sscanf(line.c_str(),"u %s",mat_name); + currentMat = RESOURCE_GET(Material, mat_name); + if(currentMat == NULL) + { + fprintf(stderr, "cannot find any material named : %s.\n", mat_name.c_str()); + currentMat = new PhongMaterial(); + RESOURCE_ADD(currentMat,Material,mat_name); + } + currentMesh->setMaterial(currentMat); + } + break; + default: + case '#': + // comment + break; + } + std::getline(file,line); + } + + for(std::size_t i=0; iindices.size() == 0) + { + meshes[i] = meshes.back(); + meshes.pop_back(); + --i; + } + } + return meshes; +} + +std::vector split(const std::string &line, char sep){ + std::vector tokens; + std::size_t start=0, end=0; + while((end = line.find(sep,start)) != std::string::npos){ + tokens.push_back(line.substr(start,end-start)); + start=end+1; + } + tokens.push_back(line.substr(start)); + return tokens; +} + +//load MTL +bool Loader::loadMTL(const std::string &filename) +{ +// char line[1024]; + std::string line; + std::ifstream file(filename); + + if(!file.is_open()) + { + fprintf(stderr, "can't load %s.\n", filename.c_str()); + return false; + } + + PhongMaterial* mat = NULL; + bool hasNormalMap = false; + + std::getline(file,line); + + while(!file.eof()) + { + if(line.length() == 0) + { + std::getline(file,line); + continue; + } + //QStringList tokens = line.split(' '); + std::vector tokens = split(line,' '); + + if(tokens[0].substr(0,1) == "#") + { + // this is a comment + } + else if((tokens[0] == "newmtl") && tokens.size() == 2) + { + mat = new PhongMaterial(); + RESOURCE_ADD(mat,Material,tokens[1]); + } + else if((tokens[0].compare("Ka") == 0) && tokens.size() == 4) + { + mat->ambient.r = std::stof(tokens[1]); + mat->ambient.g = std::stof(tokens[2]); + mat->ambient.b = std::stof(tokens[3]); + } + else if(tokens[0].compare("Kd") == 0 && tokens.size() == 4) + { + mat->diffuse.r = std::stof(tokens[1]); + mat->diffuse.g = std::stof(tokens[2]); + mat->diffuse.b = std::stof(tokens[3]); + } + else if(tokens[0].compare("Ks") == 0 && tokens.size() == 4) + { + mat->specular.r = std::stof(tokens[1]); + mat->specular.g = std::stof(tokens[2]); + mat->specular.b = std::stof(tokens[3]); + } + else if(tokens[0].compare("Ns") == 0 && tokens.size() == 2) + { + mat->shininess = std::stof(tokens[1]); + } + else if((tokens[0].substr(0,4) == "map_") && tokens.size() == 2) + { + if(tokens[0].compare("map_Ka") == 0){ + mat->ambient_texture = RESOURCE_GET(Texture,tokens[1]); + if (mat->ambient_texture == NULL){ + mat->ambient_texture = new Texture(loadImage(tokens[1])); + RESOURCE_ADD(mat->ambient_texture,Texture,tokens[1]); + } + } else if(tokens[0].compare("map_Kd") == 0) { + mat->diffuse_texture = RESOURCE_GET(Texture,tokens[1]); + if (mat->diffuse_texture == NULL){ + mat->diffuse_texture = new Texture(loadImage(tokens[1])); + RESOURCE_ADD(mat->diffuse_texture,Texture,tokens[1]); + } + } else if(tokens[0].compare("map_Ks") == 0) { + mat->specular_texture = RESOURCE_GET(Texture,tokens[1]); + if (mat->specular_texture == NULL){ + mat->specular_texture = new Texture(loadImage(tokens[1])); + RESOURCE_ADD(mat->specular_texture,Texture,tokens[1]); + } + } else if(tokens[0].compare("map_Normal") == 0) { + mat->normal_map = RESOURCE_GET(Texture,tokens[1]); + if (mat->normal_map == NULL){ + mat->normal_map = new Texture(loadImage(tokens[1])); + RESOURCE_ADD(mat->normal_map,Texture,tokens[1]); + } + hasNormalMap = true; + } else if(tokens[0].compare("map_d") == 0) { + mat->alpha_mask = RESOURCE_GET(Texture,tokens[1]); + if (mat->alpha_mask == NULL){ + mat->alpha_mask = new Texture(loadImage(tokens[1])); + RESOURCE_ADD(mat->alpha_mask,Texture,tokens[1]); + } + } else + fprintf(stderr, "unsupported material property : \"%s\"\n", tokens[0].c_str()); + } + else + fprintf(stderr, "unsupported material property : \"%s\"\n", tokens[0].c_str()); + + std::getline(file,line); + } + return hasNormalMap; +} + +/* +//glfinish +void temp_glfinish(){ + for(std::size_t i=0; iinitGL(); + todos[i].target = images[i]->texture; + delete images[i]; + } + } +} +*/ diff --git a/src/tools/loader.h b/src/tools/loader.h index 3afef05..62bfbab 100644 --- a/src/tools/loader.h +++ b/src/tools/loader.h @@ -2,14 +2,18 @@ #define LOADER_H #include +#include class Image; +class Mesh; class Loader { public: static std::string* loadTextFile(const std::string &filename); static Image* loadImage(const std::string &filename, bool hasAlpha = true); + static std::vector loadMesh(const std::string &filename); + static bool loadMTL(const std::string &filename); }; #endif // LOADER_H From abde81c527843c47af1d35a2bb19e212898dc81c Mon Sep 17 00:00:00 2001 From: Lendemor Date: Wed, 16 Mar 2016 16:06:53 +0100 Subject: [PATCH 06/11] to debug : unknown segfault --- .gitignore | 3 ++- src/main.cpp | 10 ++++++---- src/tools/loader.cpp | 18 ++++++++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 2ce6cc3..a5294f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build* -*.user \ No newline at end of file +*.user +data/* diff --git a/src/main.cpp b/src/main.cpp index 9eb1c9e..553a57f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,9 +7,10 @@ #include "tools/graph.h" #include "tools/pathfinder.h" +#include "tools/loader.h" int main(){ - CameraNode camera; + /* CameraNode camera; RESOURCE_ADD(&camera, SceneNode, "camera"); Scene scene("testScene"); @@ -19,9 +20,9 @@ int main(){ //engine.getRenderer()->setCamera(&camera); engine.createWindow("test"); engine.setScene("testScene"); - engine.start(); + engine.start();*/ - GraphNode n1 = GraphNode(); + /* GraphNode n1 = GraphNode(); n1.setValue(1); GraphNode n2 = GraphNode(); n2.setValue(2); @@ -43,5 +44,6 @@ int main(){ std::cout << "Path Size: " << path.size() << std::endl; for(GraphNode* gn: path){ std::cout << gn->getValue() << std::endl; - } + }*/ + std::vector meshes = Loader::loadMesh("/data/Qt_workspace/sparrowengine/data/boulder.obj"); } diff --git a/src/tools/loader.cpp b/src/tools/loader.cpp index fe3d85d..e121354 100644 --- a/src/tools/loader.cpp +++ b/src/tools/loader.cpp @@ -7,9 +7,10 @@ #include #include #include -//#include #include "../sparrowrenderer/src/texture.h" +#include + std::string* Loader::loadTextFile(const std::string &filename) { std::ifstream t(filename); @@ -48,16 +49,15 @@ Image* Loader::loadImage(const std::string &filename, bool hasAlpha) std::vector Loader::loadMesh(const std::string &filename){ std::vector meshes; - //MeshLoader* loader = new MeshLoader(wavefrontFilename); - //loader->textureLoader.setTexturePath(fileInfo.absolutePath()); + std::vector pos; std::vector norm; std::vector tex; -// char line[LINEBUFFER_LENGTH]; std::string line; Material* defaultMat = RESOURCE_GET(Material, "default"); + if(defaultMat == NULL) { defaultMat = new PhongMaterial(); @@ -150,8 +150,8 @@ std::vector Loader::loadMesh(const std::string &filename){ break; case 'm': // mtllib { - std::string mat_filename; - std::sscanf(line.c_str(),"m %s",mat_filename); + char* mat_filename; + std::sscanf(line.c_str(),"mtllib %s",mat_filename); loadMTL(mat_filename); break; } @@ -204,13 +204,15 @@ std::vector split(const std::string &line, char sep){ //load MTL bool Loader::loadMTL(const std::string &filename) { -// char line[1024]; std::string line; std::ifstream file(filename); + std::string foo = filename; + std::cout << foo; if(!file.is_open()) { - fprintf(stderr, "can't load %s.\n", filename.c_str()); + std::wcout << "filename" << std::endl; +// fprintf(stderr, "can't load %s.\n", filename.c_str()); return false; } From b413515214b24f3d10e1a0ef10c21f235c003f9d Mon Sep 17 00:00:00 2001 From: Lendemor Date: Wed, 16 Mar 2016 17:33:18 +0100 Subject: [PATCH 07/11] fixed error in loader --- src/main.cpp | 6 +++++- src/tools/loader.cpp | 44 +++++++++++++++++++++++++++++--------------- src/tools/loader.h | 8 ++++++++ 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 553a57f..bd55e57 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,5 +45,9 @@ int main(){ for(GraphNode* gn: path){ std::cout << gn->getValue() << std::endl; }*/ - std::vector meshes = Loader::loadMesh("/data/Qt_workspace/sparrowengine/data/boulder.obj"); + Loader::setObjDirectory("../data/"); + Loader::setMtlDirectory("../data/"); + Loader::setTexDirectory("../data/"); + std::vector meshes = Loader::loadMesh("sword.obj"); + } diff --git a/src/tools/loader.cpp b/src/tools/loader.cpp index e121354..9eabf29 100644 --- a/src/tools/loader.cpp +++ b/src/tools/loader.cpp @@ -11,6 +11,11 @@ #include +std::string Loader::obj_directory = ""; +std::string Loader::mtl_directory = ""; +std::string Loader::tex_directory = ""; + + std::string* Loader::loadTextFile(const std::string &filename) { std::ifstream t(filename); @@ -28,7 +33,7 @@ std::string* Loader::loadTextFile(const std::string &filename) Image* Loader::loadImage(const std::string &filename, bool hasAlpha) { sf::Image sfImg; - sfImg.loadFromFile(filename); + sfImg.loadFromFile(tex_directory+filename); Image* img = new Image(); img->depth = hasAlpha ? 32 : 24; img->width = sfImg.getSize().x; @@ -64,8 +69,7 @@ std::vector Loader::loadMesh(const std::string &filename){ RESOURCE_ADD(defaultMat,Material,"default"); } Material* currentMat = defaultMat; - - std::ifstream file(filename); + std::ifstream file(obj_directory + filename); if(!file.is_open()) { @@ -150,22 +154,23 @@ std::vector Loader::loadMesh(const std::string &filename){ break; case 'm': // mtllib { - char* mat_filename; + char mat_filename[256]; std::sscanf(line.c_str(),"mtllib %s",mat_filename); - loadMTL(mat_filename); + loadMTL(std::string(mat_filename)); break; } case 'u': { // usemtl - std::string mat_name; - std::sscanf(line.c_str(),"u %s",mat_name); - currentMat = RESOURCE_GET(Material, mat_name); + char mat_name[256]; + std::sscanf(line.c_str(),"usemtl %s",mat_name); + std::string material_name(mat_name); + currentMat = RESOURCE_GET(Material, material_name); if(currentMat == NULL) { - fprintf(stderr, "cannot find any material named : %s.\n", mat_name.c_str()); + fprintf(stderr, "cannot find any material named : %s.\n", material_name.c_str()); currentMat = new PhongMaterial(); - RESOURCE_ADD(currentMat,Material,mat_name); + RESOURCE_ADD(currentMat,Material,material_name); } currentMesh->setMaterial(currentMat); } @@ -190,6 +195,8 @@ std::vector Loader::loadMesh(const std::string &filename){ return meshes; } + +//move this to an utils file ? std::vector split(const std::string &line, char sep){ std::vector tokens; std::size_t start=0, end=0; @@ -205,14 +212,11 @@ std::vector split(const std::string &line, char sep){ bool Loader::loadMTL(const std::string &filename) { std::string line; - std::ifstream file(filename); - std::string foo = filename; - std::cout << foo; + std::ifstream file(mtl_directory + filename); if(!file.is_open()) { - std::wcout << "filename" << std::endl; -// fprintf(stderr, "can't load %s.\n", filename.c_str()); + fprintf(stderr, "can't load %s.\n", filename.c_str()); return false; } @@ -306,6 +310,16 @@ bool Loader::loadMTL(const std::string &filename) return hasNormalMap; } +void Loader::setObjDirectory(std::string dir_){ + obj_directory = dir_; +} +void Loader::setMtlDirectory(std::string dir_){ + mtl_directory = dir_; +} +void Loader::setTexDirectory(std::string dir_){ + tex_directory = dir_; +} + /* //glfinish void temp_glfinish(){ diff --git a/src/tools/loader.h b/src/tools/loader.h index 62bfbab..7214602 100644 --- a/src/tools/loader.h +++ b/src/tools/loader.h @@ -9,11 +9,19 @@ class Mesh; class Loader { + static std::string obj_directory; + static std::string mtl_directory; + static std::string tex_directory; + public: static std::string* loadTextFile(const std::string &filename); static Image* loadImage(const std::string &filename, bool hasAlpha = true); static std::vector loadMesh(const std::string &filename); static bool loadMTL(const std::string &filename); + + static void setObjDirectory(std::string); + static void setMtlDirectory(std::string); + static void setTexDirectory(std::string); }; #endif // LOADER_H From aaab95a8c7bd71b5ee0262a91ad215906ad3ced1 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Sun, 27 Mar 2016 18:09:15 +0200 Subject: [PATCH 08/11] added minmax a_star --- src/main.cpp | 2 ++ src/tools/pathfinder.cpp | 67 +++++++++++++++++++++++++++++++++++++--- src/tools/pathfinder.h | 5 +++ 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index bd55e57..4ea0a4f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,4 +50,6 @@ int main(){ Loader::setTexDirectory("../data/"); std::vector meshes = Loader::loadMesh("sword.obj"); + RenderingWidget; + } diff --git a/src/tools/pathfinder.cpp b/src/tools/pathfinder.cpp index d5f6c91..29277f7 100644 --- a/src/tools/pathfinder.cpp +++ b/src/tools/pathfinder.cpp @@ -62,22 +62,79 @@ std::vector PathFinder::a_star(GraphNode* start, GraphNode* goal, bo } } + if(debug) std::cout << "path cost :" << cost[goal] << std::endl; + return backtrack_path(start,goal,pred); +} + +std::vector PathFinder::a_star_min_max(GraphNode* start,GraphNode* goal,bool debug = false) +{ + std::priority_queue,ComparePriority> frontier = std::priority_queue,ComparePriority>(); + std::map cost = std::map(); //cost of visited node + std::map pred = std::map(); //pred of visited node + + std::set visited = std::set(); + + // init frontier, cost, and pred with value for start + frontier.push(start); + cost.insert(std::pair(start,0)); + pred.insert(std::pair(start,NULL)); + + GraphNode* current; + while(!frontier.empty()){ + //pick best element from frontier (with priority queue the best is in front) + current = frontier.top(); + //current = pn->node.first; + frontier.pop(); + if (visited.count(current) == 0) + visited.insert(current); + else + continue; + + if(debug) std::cout << "Exploring node " << current->getValue() << std::endl; + // goal reached, end of a-star + if (current == goal){ + break; + } + + // for all neighbours of current node + for (GraphNode* next : current->getNeighbours()){ + float new_cost = std::max(cost[current],current->cost(next)); + if(debug) std::cout << "\tExploring neighbours node " << next->getValue() << " with cost " << new_cost << std::endl; + + if ((cost.count(next) == 0) || (new_cost < cost[next])){ + // affect processed cost to next node in cost_map + cost[next] = new_cost; + + // calcul priority of node with heuristic,and add it to frontier + float priority = new_cost + next->heuristic(goal); + if(debug) std::cout << "\t\t Priority: " << priority << std::endl; + next->setPriority(priority); + visited.erase(next); + frontier.push(next); + // memorize predecessor for next + pred[next] = current; + } + } + } + + if(debug) std::cout << "path cost :" << cost[goal] << std::endl; + return backtrack_path(start,goal,pred); +} + +std::vector PathFinder::backtrack_path(GraphNode* start,GraphNode* goal, std::map pred){ // reconstruct path by backtracking from goal to start std::vector path = std::vector(); + GraphNode* current = goal; while(current != start){ path.push_back(current); current = pred[current]; } path.push_back(start); std::reverse(path.begin(),path.end()); - if(debug) std::cout << "path cost :" << cost[goal] << std::endl; return path; } - bool ComparePriority::operator ()(GraphNode* a, GraphNode* b) { - return a->getPriority() > b->getPriority(); + return a->getPriority() > b->getPriority(); } - - diff --git a/src/tools/pathfinder.h b/src/tools/pathfinder.h index faeaf94..6dd674b 100644 --- a/src/tools/pathfinder.h +++ b/src/tools/pathfinder.h @@ -6,9 +6,14 @@ class PathFinder { +private: + static std::vector backtrack_path(GraphNode* start,GraphNode* goal, std::map pred); + public: PathFinder(); static std::vector a_star(GraphNode* start,GraphNode* goal,bool debug = false); + static std::vector a_star_min_max(GraphNode* start,GraphNode* goal,bool debug = false); + }; class ComparePriority{ From 1af9417dd4a5be66ee72cb0e5db8383588b0cda9 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Sun, 27 Mar 2016 18:13:00 +0200 Subject: [PATCH 09/11] fix compilation error in new function --- src/main.cpp | 3 --- src/tools/pathfinder.h | 1 + 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4ea0a4f..cb58092 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,7 +49,4 @@ int main(){ Loader::setMtlDirectory("../data/"); Loader::setTexDirectory("../data/"); std::vector meshes = Loader::loadMesh("sword.obj"); - - RenderingWidget; - } diff --git a/src/tools/pathfinder.h b/src/tools/pathfinder.h index 6dd674b..45a429f 100644 --- a/src/tools/pathfinder.h +++ b/src/tools/pathfinder.h @@ -2,6 +2,7 @@ #define PATHFINDER_H #include +#include #include "graph.h" class PathFinder From 90a03b6f51bf14fc7ab2d5e6a897869a15689f41 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Mon, 28 Mar 2016 21:43:12 +0200 Subject: [PATCH 10/11] started adding sparrowshell --- .gitignore | 3 ++- src/main.cpp | 14 +++++----- src/messagebus.cpp | 20 ++++++++++++++- src/messagebus.h | 12 +++++++-- src/sparrowshell.cpp | 51 +++++++++++++++++++++++++++++++++++++ src/sparrowshell.h | 55 ++++++++++++++++++++++++++++++++++++++++ src/system.cpp | 21 +++++++++++++++ src/system.h | 18 ++++++++++++- src/tools/pathfinder.cpp | 2 -- 9 files changed, 183 insertions(+), 13 deletions(-) create mode 100644 src/sparrowshell.cpp create mode 100644 src/sparrowshell.h diff --git a/.gitignore b/.gitignore index 2ce6cc3..fbb64cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build* -*.user \ No newline at end of file +*.user +data* diff --git a/src/main.cpp b/src/main.cpp index 2f4f09f..0790fc5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,7 +9,7 @@ #include "tools/pathfinder.h" int main(){ - CameraNode camera; + /* CameraNode camera; RESOURCE_ADD(&camera, SceneNode, "camera"); Scene scene("testScene"); @@ -30,14 +30,16 @@ int main(){ GraphNode n4 = GraphNode(); n4.setValue(4); - n1.addNeighbours(&n2,2); - n1.addNeighbours(&n3,3); - n2.addNeighbours(&n3,5); - n3.addNeighbours(&n4,2); + n1.addNeighbours(&n2); + n1.addNeighbours(&n3); + n2.addNeighbours(&n3); + n3.addNeighbours(&n4); std::vector path = PathFinder::a_star(&n1,&n4); std::cout << "Path Size: " << path.size() << std::endl; for(GraphNode* gn: path){ std::cout << gn->getValue() << std::endl; - } + }*/ + + } diff --git a/src/messagebus.cpp b/src/messagebus.cpp index 2cccec1..057003b 100644 --- a/src/messagebus.cpp +++ b/src/messagebus.cpp @@ -1,11 +1,29 @@ #include "messagebus.h" +#include "system.h" MessageBus::MessageBus() { } +void MessageBus::registerSystem(SystemType type, System* system){ + systems[type] = system; +} + +void MessageBus::update() +{ + while (!message_list.empty()){ + Message* msg = message_list.front(); + message_list.pop(); + for(auto const &entity : systems){ + entity.second->handleMessage(msg); + } + } +} + void MessageBus::postMessage(Message *msg) { - message_list.push_back(msg); + message_list.push(msg); } + + diff --git a/src/messagebus.h b/src/messagebus.h index 626c971..46e1d60 100644 --- a/src/messagebus.h +++ b/src/messagebus.h @@ -1,17 +1,25 @@ #ifndef MESSAGEBUS_H #define MESSAGEBUS_H -#include +#include +#include +#include + +#include "system.h" class Message; +class System; class MessageBus { - std::vector message_list; //message file + std::map systems; + std::queue message_list; //message file public: MessageBus(); + void registerSystem(SystemType,System*); void postMessage(Message* msg); + void update(); void handleMessage(); }; diff --git a/src/sparrowshell.cpp b/src/sparrowshell.cpp new file mode 100644 index 0000000..42d7602 --- /dev/null +++ b/src/sparrowshell.cpp @@ -0,0 +1,51 @@ +#include "sparrowshell.h" + +#include "message.h" +#include "input.h" + +const int SparrowShell::BUFFER_MAX_LENGTH = 50; +const int SparrowShell::BUFFER_DISPLAYED_NUMBER = 10; +const int SparrowShell::SCROLLBAR_PIXEL_WIDTH = 2; + +SparrowShell::SparrowShell(sf::Window* window, Input* input): m_position(glm::ivec2(0,0)),m_window(window),m_input(input),m_scrollbar(this) +{ + //m_dimension = glm::ivec2();// +} + +void SparrowShell::out(std::string s) +{ + if (m_buffer.size() == BUFFER_MAX_LENGTH) + m_buffer.pop_back(); + m_buffer.push_front(s); +} + +void SparrowShell::scrollUp(){ + if (m_index + BUFFER_DISPLAYED_NUMBER < m_buffer.size()) m_index++; +} + +void SparrowShell::scrollDown(){ + if (m_index > 0) m_index--; +} + +void SparrowShell::update() +{ + //TODO : update TextMesh + m_scrollbar.update(); +} + +SparrowShell::ScrollBar::ScrollBar(SparrowShell* shell):m_shell(shell){ + m_position = glm::ivec2(m_shell->m_dimension.x - SCROLLBAR_PIXEL_WIDTH,0); + m_dimension = glm::ivec2(SCROLLBAR_PIXEL_WIDTH,m_shell->m_dimension.y); +} + +void SparrowShell::ScrollBar::update(){ + m_position.y = m_shell->m_position.y; + m_dimension.y = m_shell->m_dimension.y; + + if (m_shell->m_buffer.size() > BUFFER_DISPLAYED_NUMBER){ + float cran = ((float)m_shell->m_dimension.y/(float)m_shell->m_buffer.size()); + int indexCursor = m_shell->m_buffer.size()-(m_shell->m_index+SCROLLBAR_PIXEL_WIDTH); + m_position.y += (int)(cran * indexCursor); + m_dimension.y = (int)(cran * BUFFER_DISPLAYED_NUMBER); + } +} diff --git a/src/sparrowshell.h b/src/sparrowshell.h new file mode 100644 index 0000000..baa89e2 --- /dev/null +++ b/src/sparrowshell.h @@ -0,0 +1,55 @@ +#ifndef SPARROWSHELL_H +#define SPARROWSHELL_H + +#include + +#include "system.h" +#include "scene.h" +#include "glm/glm.hpp" + +class Input; + +namespace sf { +class Window; +} + +class SparrowShell : public SceneNode +{ +private: + class ScrollBar{ + SparrowShell* m_shell; + glm::ivec2 m_position; + glm::ivec2 m_dimension; + //TODO : Add rectangle mesh + + public: + ScrollBar(); + ScrollBar(SparrowShell* shell); + void update(); + }; + + static const int BUFFER_MAX_LENGTH; + static const int BUFFER_DISPLAYED_NUMBER; + static const int SCROLLBAR_PIXEL_WIDTH; + + std::list m_buffer; + sf::Window* m_window; + Input* m_input; + int m_index = 0; + + glm::ivec2 m_position; + glm::ivec2 m_dimension; + + //textMesh + ScrollBar m_scrollbar; + +public: + SparrowShell(sf::Window*, Input*); + + void update(); + void scrollUp(); + void scrollDown(); + void out(std::string); +}; + +#endif // SPARROWSHELL_H diff --git a/src/system.cpp b/src/system.cpp index 636b73c..feee297 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -1,7 +1,28 @@ #include "system.h" +#include "messagebus.h" +#include "message.h" +#include "input.h" +#include System::System() { } + +InputSystem::InputSystem(){ + +} + +void InputSystem::initInput(sf::Window *window){ + m_input = new Input(window); +} + +void InputSystem::update(){ + int action; + Message* message; + while (action = m_input->getAction() != NO_ACTION){ + message = new Message(std::to_string(action),SystemType::INPUT_SYSTEM); + m_msgBus->postMessage(message); + } +} diff --git a/src/system.h b/src/system.h index 3cfb905..915a4dc 100644 --- a/src/system.h +++ b/src/system.h @@ -3,13 +3,19 @@ class MessageBus; class Message; +class Input; + +namespace sf{ +class Window; +} //TODO:complete this part with other existing system, -enum SystemType {INPUT_SYSTEM, RENDERER_SYSTEM, IA_SYSTEM}; +enum SystemType {INPUT_SYSTEM, RENDERER_SYSTEM, IA_SYSTEM,GAME_SYSTEM,LENGTH}; class System { SystemType m_type; +protected: MessageBus* m_msgBus; public: @@ -17,4 +23,14 @@ public: virtual void handleMessage(Message* message) = 0; }; +class InputSystem : public System{ +private: + Input* m_input; +public: + InputSystem(); + ~InputSystem(); + void initInput(sf::Window* window); + void update(); +}; + #endif // SYSTEM_H diff --git a/src/tools/pathfinder.cpp b/src/tools/pathfinder.cpp index e692459..00cfc93 100644 --- a/src/tools/pathfinder.cpp +++ b/src/tools/pathfinder.cpp @@ -64,5 +64,3 @@ std::vector PathFinder::a_star(GraphNode* start,GraphNode* goal) std::reverse(path.begin(),path.end()); return path; } - - From 174dc59732490c5298bbdb633e8dfdd13c68e625 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Sun, 1 May 2016 19:28:13 +0200 Subject: [PATCH 11/11] fix loader to compat with new renderer --- src/tools/loader.cpp | 10 +++++----- src/tools/pathfinder.cpp | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/tools/loader.cpp b/src/tools/loader.cpp index 9eabf29..70a9be5 100644 --- a/src/tools/loader.cpp +++ b/src/tools/loader.cpp @@ -5,7 +5,7 @@ #include #include "../resourcemanager.h" #include -#include +#include #include #include "../sparrowrenderer/src/texture.h" @@ -77,7 +77,7 @@ std::vector Loader::loadMesh(const std::string &filename){ return meshes; } - MeshBuilder* currentMesh = new MeshBuilder(); + Mesh* currentMesh = new Mesh(); meshes.push_back(currentMesh); currentMesh->setMaterial(currentMat); @@ -125,7 +125,7 @@ std::vector Loader::loadMesh(const std::string &filename){ std::sscanf(line.c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d",tab,tab+1,tab+2,tab+3,tab+4,tab+5,tab+6,tab+7,tab+8); //TODO: check sscanf success - int nb_vertices = currentMesh->positions.size(); + int nb_vertices = currentMesh->positions3D.size(); currentMesh->addTriangle(nb_vertices, nb_vertices+1, nb_vertices+2); for(int i=0; i<3; ++i) @@ -133,7 +133,7 @@ std::vector Loader::loadMesh(const std::string &filename){ if(norm.size() == 0) { if(tex.size() == 0) - currentMesh->addPosition(pos[tab[i]-1]); + currentMesh->addVertex(pos[tab[i]-1]); else currentMesh->addVertex(pos[tab[i]-1], tex[tab[i+1]-1]); } @@ -148,7 +148,7 @@ std::vector Loader::loadMesh(const std::string &filename){ } break; case 'g': - currentMesh = new MeshBuilder(); + currentMesh = new Mesh(); meshes.push_back(currentMesh); currentMesh->setMaterial(currentMat); break; diff --git a/src/tools/pathfinder.cpp b/src/tools/pathfinder.cpp index 29277f7..f386b6d 100644 --- a/src/tools/pathfinder.cpp +++ b/src/tools/pathfinder.cpp @@ -66,7 +66,7 @@ std::vector PathFinder::a_star(GraphNode* start, GraphNode* goal, bo return backtrack_path(start,goal,pred); } -std::vector PathFinder::a_star_min_max(GraphNode* start,GraphNode* goal,bool debug = false) +std::vector PathFinder::a_star_min_max(GraphNode* start,GraphNode* goal,bool debug) { std::priority_queue,ComparePriority> frontier = std::priority_queue,ComparePriority>(); std::map cost = std::map(); //cost of visited node