-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrendering.cpp
More file actions
343 lines (254 loc) · 8.54 KB
/
rendering.cpp
File metadata and controls
343 lines (254 loc) · 8.54 KB
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
////////////////////////////////////////////////////////////////////////////
// //
// Implementation file for rendering functions of the chinese //
// checkers game. //
// //
// Author: Cédric Schoonen <cedric.schoonen1@gmail.com> //
// February 2020 //
// //
////////////////////////////////////////////////////////////////////////////
#ifndef RENDERING
#define RENDERING
#include <iostream>
#include <vector>
#include <math.h>
#include <SFML/Graphics.hpp>
#include "Board.h"
const double PI = 3.14159265358979;
sf::Color colorOfTeam(int team)
{
if (team==0)
return sf::Color::Red;
else if (team==1)
return sf::Color::Green;
else if (team==2)
return sf::Color::Blue;
else if (team==3)
return sf::Color::Yellow;
else if (team==4)
return sf::Color::Magenta;
else if (team==5)
return sf::Color::Cyan;
// default case
return sf::Color::White;
}
void renderBoardVertices(sf::RenderWindow &window, Board board)
{
#ifdef DEBUG_RENDERING
cout << "--- Rendering board vertices ---" << endl;
#endif
// vertex shape and colour
double vertexSize = 0.05;
sf::CircleShape vertexShape(vertexSize);
vertexShape.setFillColor(sf::Color::Black);
for (Vertex vertex : board.getVertices())
{
// vertex position in window
double x = vertex.getX() - vertexSize;
double y = vertex.getY() - vertexSize;
vertexShape.setPosition(sf::Vector2f(x,y));
window.draw(vertexShape);
}
}
void renderTextVertices(sf::RenderWindow &window, Board board)
{
#ifdef DEBUG_RENDERING
cout << "--- Rendering text on vertices ---" << endl;
#endif
sf::Font font;
if (!font.loadFromFile("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"));
// scale difference as text is set in pixels not in board coordinates
double pixelWidth = window.mapPixelToCoords(sf::Vector2i(0,0)).x
- window.mapPixelToCoords(sf::Vector2i(1,0)).x;
pixelWidth = abs(pixelWidth);
sf::Text text;
text.setFont(font);
text.setFillColor(sf::Color::Red);
text.setCharacterSize(20);
text.setScale(pixelWidth,pixelWidth);
vector<Vertex> vertices = board.getVertices();
for (int i=0; i<vertices.size(); i++)
{
text.setString(to_string(i));
text.setPosition(vertices[i].getX(),vertices[i].getY());
window.draw(text);
}
}
void renderBoardEdges(sf::RenderWindow &window, Board board)
{
#ifdef DEBUG_RENDERING
cout << "--- Rendering board edges ---" << endl;
#endif
vector<Vertex> vertices = board.getVertices();
for (Vertex vertex : vertices)
{
// vertex position in window
double x = vertex.getX();
double y = vertex.getY();
vector<int> neighbours = vertex.getNeighbours();
// draw edge for each vertex (will be overlapping but ok)
for (int neighbour : neighbours)
{
Vertex vertex2 = vertices[neighbour];
// neighbour position in window
double x2 = vertex2.getX();
double y2 = vertex2.getY();
double d = sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y));
double angle;
if ((x2-x)==0 && (y2-y)>0) angle = 90;
else if ((x2-x)==0 && (y2-y)<0) angle = -90;
else if ((x2-x)>0) angle = atan((y2-y)/(x2-x)) * 180/PI;
else if ((x2-x)<0) angle = atan((y2-y)/(x2-x)) * 180/PI + 180;
// difficult to have the same width for all edges when rendered
//double edgesWidth = 0.02;
double pixelWidth = window.mapPixelToCoords(sf::Vector2i(0,0)).x
- window.mapPixelToCoords(sf::Vector2i(1,0)).x;
double edgesWidth = abs(pixelWidth)*2;
sf::RectangleShape edgeShape(sf::Vector2f(d,edgesWidth));
edgeShape.setFillColor(sf::Color::Black);
edgeShape.setRotation(angle);
// correct position to take into account the rectangle width
double xc = x - edgesWidth/2*cos(PI/180*(angle+90));
double yc = y - edgesWidth/2*sin(PI/180*(angle+90));
edgeShape.setPosition(sf::Vector2f(xc,yc));
window.draw(edgeShape);
}
}
}
void renderPawns(sf::RenderWindow &window, Board board, int pawnSelected=-1)
{
#ifdef DEBUG_RENDERING
cout << "--- Rendering pawns ---" << endl;
#endif
double pawnSize = 0.2;
sf::CircleShape pawnShape(pawnSize);
vector<Pawn> pawns = board.getPawns();
for (int i=0; i<pawns.size(); i++)
{
// do not draw selected pawn
if (i==pawnSelected) continue;
// associated vertex
int j = board.getVertexFromPawn(i);
Vertex vertex = board.getVertices()[j];
// pawn position in window
double x = vertex.getX() - pawnSize;
double y = vertex.getY() - pawnSize;
pawnShape.setPosition(sf::Vector2f(x,y));
// color according to team
pawnShape.setFillColor(colorOfTeam(pawns[i].getTeam()));
window.draw(pawnShape);
}
}
void renderSelectedPawn(sf::RenderWindow &window, Board board,
int pawnSelected=-1)
{
#ifdef DEBUG_RENDERING
cout << "--- Rendering selected pawn ---" << endl;
#endif
vector<Pawn> pawns = board.getPawns();
if (pawnSelected<0 && pawnSelected>=pawns.size()) return ;
// pawn shape
double pawnSize = 0.2;
sf::CircleShape pawnShape(pawnSize);
// coordinates of mouse in graph
double graphX = window.mapPixelToCoords(sf::Mouse::getPosition(window)).x;
double graphY = window.mapPixelToCoords(sf::Mouse::getPosition(window)).y;
// pawn position in window
double x = graphX - pawnSize;
double y = graphY- pawnSize;
pawnShape.setPosition(sf::Vector2f(x,y));
// color according to team
pawnShape.setFillColor(colorOfTeam(pawns[pawnSelected].getTeam()));
window.draw(pawnShape);
}
void renderWinners(sf::RenderWindow &window, Hexagram board)
{
#ifdef DEBUG_RENDERING
cout << "--- Rendering winning order ---" << endl;
#endif
sf::Font font;
if (!font.loadFromFile("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"));
// scale difference as text is set in pixels not in board coordinates
double pixelWidth = window.mapPixelToCoords(sf::Vector2i(0,0)).x
- window.mapPixelToCoords(sf::Vector2i(1,0)).x;
pixelWidth = abs(pixelWidth);
sf::Text text;
text.setFont(font);
text.setCharacterSize(20);
text.setScale(pixelWidth,pixelWidth);
vector<int> winningOrder = board.getWinningOrder();
int nTeams = winningOrder.size();
for (int team=0; team<nTeams; team++)
{
// print if already won
if (winningOrder[team]>0)
text.setString(to_string(winningOrder[team]));
else
continue;
// text position in window
double d = board.getTotalSizeX()/2;
double x,y,angle;
board.getBranchAngleAndTipPosition(team,x,y,angle);
angle += 180;
angle += 10;
x = d * cos(PI/180*angle);
y = d * sin(PI/180*angle);
text.setPosition(x,y);
// color according to team
text.setFillColor(colorOfTeam(team));
window.draw(text);
}
}
void renderAvailableMoves(sf::RenderWindow &window, Board board,
int pawnSelected=-1)
{
#ifdef DEBUG_RENDERING
cout << "--- Rendering available moves ---" << endl;
#endif
double vertexSize = 0.05;
sf::CircleShape vertexShape(vertexSize);
// get pawn's team for color
vector<Pawn> pawns = board.getPawns();
int team = pawns[pawnSelected].getTeam();
vertexShape.setFillColor(colorOfTeam(team));
vector<Vertex> vertices = board.getVertices();
int ivertex = board.getVertexFromPawn(pawnSelected);
// list available moves
vector<int> availMoves;
for (int i : board.availableMovesDirect(ivertex))
availMoves.push_back(i);
for (int i : board.availableMovesHopping(ivertex))
availMoves.push_back(i);
for (int i : availMoves)
{
// vertex position in window
double x = vertices[i].getX() - vertexSize;
double y = vertices[i].getY() - vertexSize;
vertexShape.setPosition(sf::Vector2f(x,y));
window.draw(vertexShape);
}
}
void renderHomes(sf::RenderWindow &window, Hexagram board)
{
#ifdef DEBUG_RENDERING
cout << "--- Rendering homes ---" << endl;
#endif
// home shape and colour
double size = double(board.getSize())/sqrt(3);
sf::CircleShape triangleShape(size,3);
triangleShape.setOrigin(size,0);
for (int team=0; team<board.getNTeams(); team++)
{
// home position in window
double x,y,angle;
board.getBranchAngleAndTipPosition(team,x,y,angle);
triangleShape.setPosition(sf::Vector2f(x,y));
triangleShape.setRotation(angle+90);
// colour according to team
sf::Color colorTeam = colorOfTeam(team);
colorTeam.a = 48; // make it a bit transparent
triangleShape.setFillColor(colorTeam);
window.draw(triangleShape);
}
}
#endif