-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.c
335 lines (272 loc) · 8.91 KB
/
display.c
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
#include "display.h"
#include "marker.h"
#include "robot.h"
#include "map.h"
#include "position.h"
#include "allocations.h"
#include "graphics.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#define ROBOT_VERTICES 3
/*
Some frequently used expressions.
Require a local scope variable 'Display *display'.
*/
#define BORDER_WIDTH display->borderWidth
#define SQUARE_WIDTH display->pixelWidthOfGridSquare
#define WINDOW_WIDTH (display->columnCount + 2 * BORDER_WIDTH) * SQUARE_WIDTH
#define WINDOW_HEIGHT (display->rowCount + 2 * BORDER_WIDTH) * SQUARE_WIDTH
// Static type definitions.
struct Display
{
size_t rowCount;
size_t columnCount;
size_t pixelWidthOfGridSquare;
size_t borderWidth;
};
// End of static type definitions.
// Static declarations.
static int formatYCoordinate(Display *, int);
static void drawLineToDisplay(Display *, int, int, int, int);
static void fillRectOnDisplay(Display *, int, int, int, int);
static void fillPolygonOnDisplay(Display *, size_t, int[], int[]);
static void setDisplayWindowSize(Display *);
static void editDisplayForeground();
static void editDisplayBackground();
static void setDisplayDrawColour(int, int, int);
static void delayDisplayUpdate(int);
// Static background functions.
static void drawColumns(Display *, Map *);
static void drawRows(Display *, Map *);
static void drawGrid(Display *, Map *);
static void drawObstacle(Display *, int, int);
static void drawObstacles(Display *, Map *);
// Static foreground functions, and relevant static utilities.
static void swapXY(size_t, int[], int[]);
static void rotatePointSets(int, int, int, int[], int[]);
static void generateTriangleRobotPoints(int, int[], int[]);
static void generateRobotPointSet(Display *, Robot *, int[], int[]);
static void drawRobot(Display *, Robot *);
static void drawMarker(Display *, Marker *);
static void drawMarkers(Display *, Marker *[], size_t);
// End of static declarations.
// Static drawing general utility functions.
/*
Used to decouple from the dependance on drawapp interface.
Subsequent drawing implementations abstract the need to program to drawapp
interface.
*/
static int formatYCoordinate(Display *display, int yPoint)
{
return WINDOW_HEIGHT - yPoint;
}
static void drawLineToDisplay(Display *display, int x1, int y1, int x2, int y2)
{
drawLine(x1, formatYCoordinate(display, y1),
x2, formatYCoordinate(display, y2));
}
static void fillRectOnDisplay(Display *display, int x1, int y1, int width,
int height)
{
fillRect(x1, formatYCoordinate(display, y1) - height, width, height);
}
static void fillPolygonOnDisplay(Display *display, size_t count, int x[],
int y[])
{
for (size_t i = 0; i < count; i++)
{
y[i] = formatYCoordinate(display, y[i]);
}
fillPolygon(count, x, y);
}
static void setDisplayWindowSize(Display *display)
{
setWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
}
static void editDisplayForeground()
{
foreground();
}
static void editDisplayBackground()
{
background();
}
static void setDisplayDrawColour(int red, int green, int blue)
{
setRGBColour(red, green, blue);
}
static void delayDisplayUpdate(int milliseconds)
{
sleep(milliseconds);
}
// End of static drawing general utility functions.
// Static background functions.
static void drawColumns(Display *display, Map *map)
{
for (size_t i = BORDER_WIDTH; i < display->columnCount + 2 * BORDER_WIDTH;
i++)
{
drawLineToDisplay(display, i * SQUARE_WIDTH, 0, i * SQUARE_WIDTH,
(display->rowCount + BORDER_WIDTH) * SQUARE_WIDTH);
}
}
static void drawRows(Display *display, Map *map)
{
for (size_t i = BORDER_WIDTH; i < display->rowCount + 2 * BORDER_WIDTH; i++)
{
drawLineToDisplay(display, 0, i * SQUARE_WIDTH,
(display->columnCount + BORDER_WIDTH) * SQUARE_WIDTH,
i * SQUARE_WIDTH);
}
}
static void drawGrid(Display *display, Map *map)
{
setDisplayDrawColour(0x00, 0x00, 0x00);
drawColumns(display, map);
drawRows(display, map);
}
static void drawObstacle(Display *display, int row, int column)
{
fillRectOnDisplay(display, column * SQUARE_WIDTH, row * SQUARE_WIDTH,
SQUARE_WIDTH, SQUARE_WIDTH);
}
static void drawObstacles(Display *display, Map *map)
{
setDisplayDrawColour(0xFF, 0x00, 0x00);
for (size_t row = 0; row < getRowSize(map) + 2 * BORDER_WIDTH; row++)
{
for (size_t column = 0; column < getColumnSize(map) + 2 * BORDER_WIDTH;
column++)
{
Position position = {.x = column - BORDER_WIDTH,
.y = row - BORDER_WIDTH};
if (row < BORDER_WIDTH ||
row >= getRowSize(map) + BORDER_WIDTH ||
column < BORDER_WIDTH ||
column >= getColumnSize(map) + BORDER_WIDTH ||
!isMapPositionEmpty(map, position))
{
drawObstacle(display, row, column);
}
}
}
}
// End of static background functions.
// Static foreground functions, and relevant static utilities.
static void swapXY(size_t i, int xPoints[], int yPoints[])
{
int temp = xPoints[i];
xPoints[i] = yPoints[i];
yPoints[i] = temp;
}
static void rotatePointSets(int squareWidth, int numberOfRotations,
int numberOfPoints, int xPoints[], int yPoints[])
{
// Handles any values < -1 or > 4 of rotation amount.
// i.e. rotation amount will be 0, 1, 2, or 3.
numberOfRotations = ((numberOfRotations % 4) + 4) % 4;
for (int i = 0; i < numberOfPoints; i++)
{
if (numberOfRotations > 1)
{
yPoints[i] *= -1;
yPoints[i] += squareWidth;
}
if (numberOfRotations % 2)
{
swapXY(i, xPoints, yPoints);
}
}
}
static void generateTriangleRobotPoints(int squareWidth, int xPoints[],
int yPoints[])
{
for (size_t i = 0; i < 3; i++)
{
xPoints[i] = i * squareWidth / 2;
}
yPoints[0] = yPoints[2] = 0;
yPoints[1] = squareWidth;
}
static void generateRobotPointSet(Display *display, Robot *robot,
int xPoints[], int yPoints[])
{
generateTriangleRobotPoints(SQUARE_WIDTH, xPoints, yPoints);
rotatePointSets(SQUARE_WIDTH, getRotationalOffset(robot), ROBOT_VERTICES,
xPoints, yPoints);
for (size_t i = 0; i < ROBOT_VERTICES; i++)
{
xPoints[i] += (getRobotPosition(robot).x + BORDER_WIDTH) * SQUARE_WIDTH;
yPoints[i] += (getRobotPosition(robot).y + BORDER_WIDTH) * SQUARE_WIDTH;
}
}
static void drawRobot(Display *display, Robot *robot)
{
int xPoints[ROBOT_VERTICES];
int yPoints[ROBOT_VERTICES];
generateRobotPointSet(display, robot, xPoints, yPoints);
fillPolygonOnDisplay(display, ROBOT_VERTICES, xPoints, yPoints);
}
static void drawMarker(Display *display, Marker *marker)
{
fillRectOnDisplay(display,
(getMarkerPosition(marker).x + BORDER_WIDTH) *
SQUARE_WIDTH,
(getMarkerPosition(marker).y + BORDER_WIDTH) *
SQUARE_WIDTH,
SQUARE_WIDTH, SQUARE_WIDTH);
}
static void drawMarkers(Display *display, Marker *markers[],
size_t numberOfMarkers)
{
Marker *home;
setDisplayDrawColour(0x00, 0x00, 0xFF);
for (size_t i = 0; i < numberOfMarkers; i++)
{
if (isHomeMarker(markers[i]))
{
home = markers[i];
}
else if (isPlacedMarker(markers[i]))
{
drawMarker(display, markers[i]);
}
}
setDisplayDrawColour(0x75, 0x3F, 0xEA);
drawMarker(display, home);
}
// End of static foreground functions, and relevant static utilities.
// Public function definitions.
Display *initialiseDisplay(size_t rowCount, size_t columnCount,
size_t pixelWidthOfGridSquare, size_t borderWidth)
{
Display *display = (Display *)checkedMalloc(sizeof(Display), "Display");
display->rowCount = rowCount;
display->columnCount = columnCount;
display->pixelWidthOfGridSquare = pixelWidthOfGridSquare;
display->borderWidth = borderWidth;
setDisplayWindowSize(display);
return display;
}
void deallocateDisplay(Display *display)
{
free(display);
}
void drawBackground(Display *display, Map *map)
{
editDisplayBackground();
drawGrid(display, map);
drawObstacles(display, map);
}
void updateForeground(Display *display, Robot *robot, Marker *markers[],
size_t numberOfMarkers, int delayMilliseconds)
{
delayDisplayUpdate(delayMilliseconds);
editDisplayForeground();
clear();
drawMarkers(display, markers, numberOfMarkers);
setDisplayDrawColour(0x00, 0xFF, 0x00);
drawRobot(display, robot);
}
// End of public function definitions.