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; } } }