-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
353 lines (280 loc) · 9.22 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "stdafx.h"
#include "Loader.h"
#include "DrawableRegister.h"
#include "ImageManager.h"
#include "Material.h"
#include "Grid.h"
#include "WaypointManager.h"
int main(int argc, char** argv)
{
Loader loader;
ImageManager imMgr = loader.LoadImages();
MaterialManager matMgr = loader.LoadMaterials(imMgr, "material.txt");
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Window");
Grid grid = loader.LoadGrid(matMgr, "grid2.txt", App.GetWidth(), App.GetHeight());
WaypointManager waypointMgr(&grid);
DrawableRegister reg;
reg.AddStatic(grid);
while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
// Fenêtre fermée
if (Event.Type == sf::Event::Closed)
App.Close();
// Touche 'echap' appuyée
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
if ((Event.Type == sf::Event::MouseButtonPressed))
{
const sf::Input& input = App.GetInput();
float x = (float)input.GetMouseX();
float y = (float)input.GetMouseY();
if(Event.MouseButton.Button == sf::Mouse::Left)
{
reg.ClearTemp();
waypointMgr.AddStartPoint(x, y);
reg.AddTemp(waypointMgr);
}
if(Event.MouseButton.Button == sf::Mouse::Right)
{
reg.ClearTemp();
waypointMgr.AddEndPoint(x, y);
reg.AddTemp(waypointMgr);
}
if(Event.MouseButton.Button == sf::Mouse::Middle)
{
reg.ClearTemp();
waypointMgr.FindPath();
reg.AddTemp(waypointMgr);
}
}
}
// Efface l'écran (remplissage avec du noir)
App.Clear();
reg.DrawStatic(App);
reg.DrawTemp(App);
// Affichage du contenu de la fenêtre à l'écran
App.Display();
}
return EXIT_SUCCESS;
}
/*
#include <iostream> // for std::cout
#include <utility> // for std::pair
#include <algorithm> // for std::for_each
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
*/
/*
template <class Graph>
struct exercise_out_edge
{
typedef boost::graph_traits<Graph> GraphTraits;
typedef typename GraphTraits::vertex_descriptor Vertex;
typedef typename GraphTraits::edge_descriptor Edge;
typedef typename GraphTraits::vertex_iterator vertex_iter;
typedef typename boost::property_map<Graph, boost::vertex_index_t>::type Vertex_Index;
typedef typename GraphTraits::out_edge_iterator Out_Edge_Iter;
exercise_out_edge(Graph& g_) : g(g_) {}
void operator()(const Vertex& v) const
{
Vertex_Index index = boost::get(boost::vertex_index, g);
std::cout <<"Vertex : " << index[v] << " ";
std::cout << "out-edges: ";
Out_Edge_Iter out_i, out_end;
Edge e;
for (boost::tie(out_i, out_end) = boost::out_edges(v, g); out_i != out_end; ++out_i)
{
e = *out_i;
Vertex src = source(e, g);
Vertex targ = target(e, g);
std::cout << "(" << index[src] << "," << index[targ] << ") ";
}
std::cout << std::endl;
}
Graph& g;
};
template <class Graph>
struct exercise_in_edge
{
typedef boost::graph_traits<Graph> GraphTraits;
typedef typename GraphTraits::vertex_descriptor Vertex;
typedef typename GraphTraits::edge_descriptor Edge;
typedef typename GraphTraits::vertex_iterator vertex_iter;
typedef typename boost::property_map<Graph, boost::vertex_index_t>::type Vertex_Index;
typedef typename GraphTraits::out_edge_iterator Out_Edge_Iter;
exercise_in_edge(Graph& g_) : g(g_) {}
void operator()(const Vertex& v) const
{
Vertex_Index index = boost::get(boost::vertex_index, g);
std::cout <<"Vertex : " << index[v] << " ";
std::cout << "out-edges: ";
Out_Edge_Iter out_i, out_end;
Edge e;
for (boost::tie(out_i, out_end) = boost::in_edges(v, g); out_i != out_end; ++out_i)
{
e = *out_i;
Vertex src = source(e, g);
Vertex targ = target(e, g);
std::cout << "(" << index[src] << "," << index[targ] << ") ";
}
std::cout << std::endl;
}
Graph& g;
};
template <class Graph>
struct exercise_adjacency_vertex
{
typedef boost::graph_traits<Graph> GraphTraits;
typedef typename GraphTraits::vertex_descriptor Vertex;
typedef typename boost::property_map<Graph, boost::vertex_index_t>::type Vertex_Index;
typedef typename GraphTraits::adjacency_iterator Adj_Iter;
exercise_adjacency_vertex(Graph& g_) : g(g_) {}
void operator()(const Vertex& v) const
{
Vertex_Index index = boost::get(boost::vertex_index, g);
std::cout <<"Vertex : " << index[v] << " ";
std::cout << "adjacent vertices: ";
Adj_Iter ai, ai_end;
for (tie(ai, ai_end) = adjacent_vertices(v, g); ai != ai_end; ++ai)
{
std::cout << index[*ai] << " ";
}
std::cout << std::endl;
}
Graph& g;
};
*//*
struct MyEdge
{
float weight;
unsigned int dist;
};
template <class Edge>
class record_predecessors : public boost::dijkstra_visitor<>
{
public:
record_predecessors(std::vector<Edge>& a)
: m_a(a) { }
template <class Graph>
void edge_relaxed(Edge e, Graph& g) {
// set the parent of the target(e) to source(e)
m_a.push_back(e);
}
protected:
std::vector<Edge>& m_a;
};
template <class Edge>
record_predecessors<Edge>
make_predecessor_recorder(std::vector<Edge>& a)
{
return record_predecessors<Edge>(a);
}
*/
//int main(int,char*[])
//{
/*
// create a typedef for the Graph type
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS> Graph;
// Make convenient labels for the vertices
enum { A, B, C, D, E, N };
const int num_vertices = N;
const char* name = "ABCDE";
// writing out the edges in the graph
typedef std::pair<int, int> Edge;
Edge edge_array[] =
{ Edge(A,B), Edge(A,D), Edge(C,A), Edge(D,C),
Edge(C,E), Edge(B,D), Edge(D,E) };
const int num_edges = sizeof(edge_array)/sizeof(edge_array[0]);
// declare a graph object
Graph g(num_vertices);
// add the edges to the graph object
for (int i = 0; i < num_edges; ++i)
add_edge(edge_array[i].first, edge_array[i].second, g);
//get the property map for vertex indices
typedef boost::property_map<Graph, boost::vertex_index_t>::type IndexMap;
IndexMap index = boost::get(boost::vertex_index, g);
std::cout << "vertices(g) = ";
typedef boost::graph_traits<Graph>::vertex_iterator vertex_iter;
vertex_iter vi, vi_end;
for (boost::tie(vi, vi_end) = boost::vertices(g); vi != vi_end; ++vi)
std::cout << index[*vi] << " ";
std::cout << std::endl;
std::cout << "edges(g) = ";
typedef boost::graph_traits<Graph>::edge_iterator edge_iter;
edge_iter ei, ei_end;
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
{
std::cout << "(" << index[boost::source(*ei, g)]
<< "," << index[boost::target(*ei, g)] << ") ";
}
std::cout << std::endl;
std::for_each(vertices(g).first, vertices(g).second, exercise_out_edge<Graph>(g));
//std::for_each(vertices(g).first, vertices(g).second, exercise_in_edge<Graph>(g));
std::for_each(vertices(g).first, vertices(g).second, exercise_adjacency_vertex<Graph>(g));
return 0;
*/
/*
typedef boost::adjacency_list<
boost::listS,
boost::vecS,
boost::undirectedS,
boost::no_property,
//boost::property<boost::edge_weight_t, int>
MyEdge
> Graph;
typedef boost::graph_traits<Graph> GraphTraits;
typedef GraphTraits::vertex_descriptor Vertex;
typedef GraphTraits::edge_descriptor Edge;
typedef boost::graph_traits<Graph>::vertex_iterator Vertex_Iter;
typedef boost::graph_traits<Graph>::edge_iterator Edge_Iter;
typedef std::pair<int,int> E;
typedef boost::property_map<Graph, boost::vertex_index_t>::type Vertex_Index;
typedef boost::property_map<Graph, boost::edge_index_t>::type Edge_Index;
const int num_nodes = 5;
E edges[] = { E(0,2),
E(1,1), E(1,3), E(1,4),
E(2,1), E(2,3),
E(3,4),
E(4,0), E(4,1) };
int weights[] = { 1, 2, 1, 2, 7, 3, 1, 1, 1};
Graph G;
//Graph G(edges, edges + sizeof(edges) / sizeof(E), weights, num_nodes);
int num_edges = sizeof(edges) / sizeof(E);
for(int i = 0 ; i < num_edges ; i++)
{
Edge edge;
bool b;
boost::tie(edge, b) = boost::add_edge(edges[i].first, edges[i].second, G);
G[edge].weight = weights[i];
}
Vertex_Index index = boost::get(boost::vertex_index, G);
// vector for storing distance property
std::vector<int> d(num_vertices(G));
std::vector<Vertex> p(num_vertices(G), GraphTraits::null_vertex()); //the predecessor array
std::vector<Edge> a; //the predecessor array
Vertex_Iter vi = *(vertices(G).first);
std::cout << "Vertex : " << index[*vi] << "\n";
// invoke variant 2 of Dijkstra's algorithm
boost::dijkstra_shortest_paths(G, *vi,
boost::predecessor_map(&p[0]).
weight_map(boost::get(&MyEdge::weight, G)).
//distance_map(boost::get(&MyEdge::dist, G)).
distance_map(&d[0]).
visitor(make_predecessor_recorder(a)));
std::cout << "parents in the tree of shortest paths:" << std::endl;
GraphTraits::vertex_iterator vi2, vi2_end;
for(boost::tie(vi2, vi2_end) = boost::vertices(G); vi2 != vi2_end; ++vi2)
{
std::cout << "parent(" << *vi2;
if (p[*vi2] == GraphTraits::null_vertex())
std::cout << ") = no parent" << std::endl;
else
std::cout << ") = " << p[*vi2] << std::endl;
}
std::cout << std::endl;
}
*/