-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraphics.c
417 lines (367 loc) · 13.7 KB
/
graphics.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* GRAPHICS MODULE SOURCE CODE
*
* This file contains all the definitions of the functions used in
* the application for drawing on the screen and for graphics related
* computations.
*
* Created on: 03/nov/2015
* Author: Paolo Sassi
*/
#include "graphics.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <allegro.h>
#include "events.h"
#include "globals.h"
/* MODULE GLOBAL VARIABLES */
int32_t line_color, txt_color, head_color, p_color, tc_color, tp_color;
int32_t t_colors[MAX_TARGETS];
BITMAP *city, *radar, *bkg, *box_buffer, *ss_buffer, *ts_buffer,
*radar_buffer, *patriot;
BITMAP *targets[MAX_TARGETS];
/* LOCAL FUNCTIONS DECLARATIONS */
static void drawInstructions();
/*----------------------------------------------------------------------------+
* initGraphics() |
* |
* Initializes the allegro library and the globals used for graphics | |
*----------------------------------------------------------------------------+
*/
void initGraphics()
{
int32_t i;
char t_path[13];
/* initialize allegro related stuff */
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
clear_to_color(screen, 0);
/* initialize used colors*/
txt_color = makecol(255, 255, 0); /* yellow color for text */
line_color = makecol(255, 0, 0); /* red color for lines */
head_color = makecol(160, 160, 160);/* gray color for heading text */
/* target colors */
t_colors[0] = makecol(255, 0, 0);
t_colors[1] = makecol(0, 0, 255);
t_colors[2] = makecol(255, 214, 0);
t_colors[3] = makecol(197, 197, 197);
p_color = makecol(0, 255, 0); /* green color for patriot */
tc_color = makecol(255, 255, 0); /* yellow color for target centroid */
tp_color = makecol(0, 255, 255); /* blue color for predicted centroid */
/* initialize screen buffers */
ss_buffer = create_bitmap(SS_WIDTH, SS_HEIGHT);
ts_buffer = create_bitmap(TS_WIDTH, TS_HEIGHT);
bkg = create_bitmap(BOX_WIDTH, BOX_HEIGHT);
box_buffer = create_bitmap(BOX_WIDTH, BOX_HEIGHT);
radar_buffer = create_bitmap(RAD_RANGE_X, RAD_RANGE_Y);
/* load images for the target and the patriot */
for (i = 0; i < 4; i++) {
sprintf(t_path, "img/target_%d.bmp", i);
targets[i] = load_bitmap(t_path, NULL);
}
patriot = load_bitmap(PATRIOT_PATH, NULL);
/* clear screen buffer to black */
clear_to_color(bkg, 0);
clear_to_color(box_buffer, 0);
}
/*----------------------------------------------------------------------------+
* endGraphics() |
* |
* Destroys all the used bitmaps and quits the allegro library |
*----------------------------------------------------------------------------+
*/
void endGraphics()
{
int32_t i;
destroy_bitmap(bkg);
destroy_bitmap(ss_buffer);
destroy_bitmap(ts_buffer);
destroy_bitmap(box_buffer);
destroy_bitmap(radar_buffer);
destroy_bitmap(patriot);
/* destroy all the targets bitmaps */
for (i = 0; i < 4; i++) destroy_bitmap(targets[i]);
allegro_exit();
}
/*----------------------------------------------------------------------------+
* drawGUI() |
* |
* Draws the static part of the GUI and draws the background image |
*----------------------------------------------------------------------------+
*/
void drawGUI() {
/* TOP GUI */
textout_centre_ex(screen, font, "Patriot System Simulation",
400, 5, txt_color, 0);
line(screen, 0, 19, 800, 19, line_color);
line(screen, 290, 0, 290, 19, line_color);
line(screen, 508, 0, 508, 19, line_color);
line(screen, 0, 79, 800, 79, line_color);
/* RIGHT SIDE GUI */
line(screen, BOX_WIDTH + 1, 0, BOX_WIDTH + 1, SCREEN_HEIGHT, line_color);
drawInstructions();
/* load background images */
city = load_bitmap(CITY_PATH, NULL);
if (city == NULL) {
printf("%s not found!\n", CITY_PATH);
exit(1);
}
radar = load_bitmap(RADAR_PATH, NULL);
if (radar == NULL) {
printf("%s not found!\n", RADAR_PATH);
exit(1);
}
blit(city, bkg, 0, 0, CITY_POS_X, CITY_POS_Y, city->w, city->h);
draw_sprite(bkg, radar, RADAR_POS_X, RADAR_POS_Y);
blit(bkg, screen, 0, 0, 0, 80, BOX_WIDTH, BOX_HEIGHT);
/* destroy the temporary bitmaps */
destroy_bitmap(city);
destroy_bitmap(radar);
}
/*----------------------------------------------------------------------------+
* updateScreen() |
* |
* Updates the simulation box on the screen |
*----------------------------------------------------------------------------+
*/
void updateScreen()
{
blit(box_buffer, screen, 0, 0, BOX_POS_X, BOX_POS_Y, BOX_WIDTH,
BOX_HEIGHT);
blit(bkg, box_buffer, 0, 0, 0, 0, BOX_WIDTH, BOX_HEIGHT);
}
/*----------------------------------------------------------------------------+
* updateStats() |
* |
* Updates the simulation statistics on the screen |
*----------------------------------------------------------------------------+
*/
void updateStats()
{
blit(ss_buffer, screen, 0, 0, BOX_WIDTH + 2, 0, SS_WIDTH, SS_HEIGHT);
clear_to_color(ss_buffer, 0);
}
/*----------------------------------------------------------------------------+
* scanArea(xc, yc, index) |
* |
* Copy a portion of the screen to a buffer and if the target is detected |
* computes its centroid. |
*----------------------------------------------------------------------------+
*/
uint8_t scanArea(int32_t *xc, int32_t *yc, int32_t index)
{
uint8_t found;
int32_t x, y, pixel_num, target_x, target_y;
target_x = target_y = found = pixel_num = 0;
/* copy a rectangular region near the radar to the radar buffer */
blit(screen, radar_buffer, RAD_AREA_X, RAD_AREA_Y, 0, 0,
RAD_RANGE_X, RAD_RANGE_Y);
/* scan the radar buffer looking for the target */
for (x = 0; x <= RAD_RANGE_X; x++) {
for (y = 0; y <= RAD_RANGE_Y - RAD_RANGE_MIN; y++) {
if (getpixel(radar_buffer, x, y) == t_colors[index]) {
found = 1;
target_x += x;
target_y += y;
pixel_num++;
}
}
}
/* if at least one target pixel is found, computes the centroid
* for the detected pixels */
if (found) {
*xc = (target_x / pixel_num);
*yc = (target_y / pixel_num);
return 1;
}
return 0;
}
/*----------------------------------------------------------------------------+
* checkCollision(t, p) |
* |
* Checks whether a collision between target and patriot has occurred |
*----------------------------------------------------------------------------+
*/
uint8_t checkCollision(stat t, stat p)
{
float32_t dist;
dist = sqrt((t.x - p.x) * (t.x - p.x) + (t.y - p.y) * (t.y - p.y));
if (dist <= COLL_DIST) {
return 1;
}
return 0;
}
/*----------------------------------------------------------------------------+
* drawTaskStats(dmiss) |
* |
* Draw statistics about the tasks into a buffer and then copy them |
* on the screen |
*----------------------------------------------------------------------------+
*/
void drawTaskStats(uint8_t *dmiss)
{
char_t s[20];
int32_t i, sum;
sum = 0;
clear_to_color(ts_buffer, 0);
textout_centre_ex(ts_buffer, font, "Display", 100, 10, head_color, 0);
sprintf(s, "DMiss: %d", dmiss[0]);
textout_centre_ex(ts_buffer, font, s, 100, 32, head_color, 0);
textout_centre_ex(ts_buffer, font, "Radar", 200, 10, head_color, 0);
sprintf(s, "DMiss: %d", dmiss[1]);
textout_centre_ex(ts_buffer, font, s, 200, 32, head_color, 0);
textout_centre_ex(ts_buffer, font, "Keyboard", 300, 10, head_color, 0);
sprintf(s, "DMiss: %d", dmiss[2]);
textout_centre_ex(ts_buffer, font, s, 300, 32, head_color, 0);
textout_centre_ex(ts_buffer, font, "ECS", 400, 10, head_color, 0);
sprintf(s, "DMiss: %d", dmiss[3]);
textout_centre_ex(ts_buffer, font, s, 400, 32, head_color, 0);
/* we show the sum of all the target tasks dmisses */
for (i = TARGET_INDEX; i < TARGET_INDEX + MAX_TARGETS; i++) {
sum += dmiss[i];
}
textout_centre_ex(ts_buffer, font, "Target", 500, 10, head_color, 0);
sprintf(s, "DMiss: %d", sum);
textout_centre_ex(ts_buffer, font, s, 500, 32, head_color, 0);
sum = 0;
/* we show the sum of all the patriot tasks dmisses */
for (i = PATRIOT_INDEX; i < PATRIOT_INDEX + MAX_TARGETS; i++) {
sum += dmiss[i];
}
textout_centre_ex(ts_buffer, font, "Patriot", 600, 10, head_color, 0);
sprintf(s, "DMiss: %d", sum);
textout_centre_ex(ts_buffer, font, s, 600, 32, head_color, 0);
blit(ts_buffer, screen, 0, 0, 0, 20, TS_WIDTH, TS_HEIGHT-2);
}
/*----------------------------------------------------------------------------+
* drawSimStats(ss, p_f, v_f, a_f) |
* |
* Draw statistics about the simulation into a buffer |
*----------------------------------------------------------------------------+
*/
void drawSimStats(sim_stats ss, float32_t p_f, float32_t v_f, float32_t a_f)
{
char_t s[20];
textout_centre_ex(ss_buffer, font, "Simulation", SS_WIDTH / 2, 20,
head_color, 0);
textout_centre_ex(ss_buffer, font, "Statistics", SS_WIDTH / 2, 40,
head_color, 0);
sprintf(s, "Target Fired: %d", ss.t_fired);
textout_centre_ex(ss_buffer, font, s, SS_WIDTH / 2, 60, txt_color, 0);
sprintf(s, "Target Hit: %d", ss.t_hit);
textout_centre_ex(ss_buffer, font, s, SS_WIDTH / 2, 80, txt_color, 0);
sprintf(s, "Target Missed: %d", ss.t_missed);
textout_centre_ex(ss_buffer, font, s, SS_WIDTH / 2, 100, txt_color, 0);
if (ss.t_fired == 0) {
sprintf(s, "Hit Ratio: %.2f%%", 0.00);
} else {
sprintf(s, "Hit Ratio: %.2f%%", ss.t_hitratio * 100);
}
textout_centre_ex(ss_buffer, font, s, SS_WIDTH / 2, 120, txt_color, 0);
sprintf(s, "Position Filter: %.2f", p_f);
textout_centre_ex(ss_buffer, font, s, SS_WIDTH / 2, 140, txt_color, 0);
sprintf(s, "Speed Filter: %.2f", v_f);
textout_centre_ex(ss_buffer, font, s, SS_WIDTH / 2, 160, txt_color, 0);
sprintf(s, "Acc. Filter: %.2f", a_f);
textout_centre_ex(ss_buffer, font, s, SS_WIDTH / 2, 180, txt_color, 0);
}
/*----------------------------------------------------------------------------+
* drawPatriotStats(pm, evts) |
* |
* Draws status information about the patriots |
*----------------------------------------------------------------------------+
*/
void drawPatriotStats(uint8_t pm, uint8_t evts_tmp)
{
char_t s[20], t[20];
uint8_t mask1, mask2;
int32_t i, y;
mask1 = 1; /* 0000 0001 */
mask2 = 16; /* 0001 0000 */
textout_centre_ex(ss_buffer, font, "Patriot Stats", SS_WIDTH / 2,
200, head_color, 0);
y = 220;
for (i = 0; i < MAX_TARGETS; i++) {
if (isEvent(evts_tmp, mask2)) { /* prediction ready */
sprintf(t, "Target lock");
} else if(isEvent(pm, mask1)) { /* patriot fired */
sprintf(t, "Fired");
} else { /* patriot not fired yet */
sprintf(t, "Ready");
}
sprintf(s, "P%d: %s", i, t);
textout_centre_ex(ss_buffer, font, s, SS_WIDTH / 2, y, txt_color, 0);
y += 20;
mask1 <<= 1;
mask2 <<= 1;
}
}
void drawInstructions()
{
textout_centre_ex(screen, font, "Instructions", BOX_WIDTH + SS_WIDTH / 2,
320, head_color, 0);
textout_centre_ex(screen, font, "Spacebar:", BOX_WIDTH + SS_WIDTH / 2, 340,
txt_color, 0);
textout_ex(screen, font, "Spawn a new target", BOX_WIDTH + 20, 355,
txt_color, 0);
textout_centre_ex(screen, font, "R:", BOX_WIDTH + SS_WIDTH / 2, 375,
txt_color, 0);
textout_ex(screen, font, "Reset the simulation", BOX_WIDTH + 20, 390,
txt_color, 0);
textout_centre_ex(screen, font, "C:", BOX_WIDTH + SS_WIDTH / 2, 410,
txt_color, 0);
textout_ex(screen, font, "Show real centroid", BOX_WIDTH + 20, 425,
txt_color, 0);
textout_centre_ex(screen, font, "P:", BOX_WIDTH + SS_WIDTH / 2, 445,
txt_color, 0);
textout_ex(screen, font, "Show sampled centroid", BOX_WIDTH + 20, 460,
txt_color, 0);
textout_centre_ex(screen, font, "1, 2:", BOX_WIDTH + SS_WIDTH / 2, 480,
txt_color, 0);
textout_ex(screen, font, "Pos Filter - / +", BOX_WIDTH + 20, 495,
txt_color, 0);
textout_centre_ex(screen, font, "3, 4:", BOX_WIDTH + SS_WIDTH / 2, 515,
txt_color, 0);
textout_ex(screen, font, "Speed Filter - / +", BOX_WIDTH + 20, 530,
txt_color, 0);
textout_centre_ex(screen, font, "5, 6:", BOX_WIDTH + SS_WIDTH / 2, 550,
txt_color, 0);
textout_ex(screen, font, "Acc Filter - / +", BOX_WIDTH + 20, 565,
txt_color, 0);
}
/*----------------------------------------------------------------------------+
* drawTarget(x, y, angle, index) |
* |
* Draws the rotated sprite of the enemy target into a buffer |
*----------------------------------------------------------------------------+
*/
void drawTarget(int32_t x, int32_t y, float32_t angle, int32_t index)
{
rotate_sprite(box_buffer, targets[index], x - TARGET_WIDTH / 2,
y - TARGET_HEIGHT / 2, radtofix(angle));
}
/*----------------------------------------------------------------------------+
* drawPatriot(x, y, angle) |
* |
* Draws the rotated sprite of the patriot target into a buffer |
*----------------------------------------------------------------------------+
*/
void drawPatriot(int32_t x, int32_t y, float32_t angle)
{
rotate_sprite(box_buffer, patriot, x - PATRIOT_WIDTH / 2,
y - PATRIOT_HEIGHT / 2, radtofix(angle));
}
/*----------------------------------------------------------------------------+
* drawCentroid(x, y, type) |
* |
* Draws a small circle in the specified coordinates |
*----------------------------------------------------------------------------+
*/
void drawCentroid(int32_t x, int32_t y, uint8_t type)
{
if (type == RADAR_CENTROID) circlefill(box_buffer, x, y , 3, tc_color);
if (type == PRED_CENTROID) circlefill(box_buffer, x, y, 3, tp_color);
}