-
Notifications
You must be signed in to change notification settings - Fork 0
/
allegro.cpp
415 lines (330 loc) · 17.8 KB
/
allegro.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
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
#include "declarations.h"
// A method that releases the memory that was allocated for allegro
// This function exits everything for no memory loss
void Allegro::releaseMemory() {
//Release the bitmap data and exit with no errors
al_destroy_display(display);
al_destroy_event_queue(eventQueue);
al_destroy_font(arial);
al_destroy_font(smallArial);
al_destroy_sample(gameMusic);
al_destroy_sample(buttonClick);
al_destroy_bitmap(background);
}
// Constant passed by reference for maximum efficienty
void Allegro::drawMain(const apvector <int> ¤tRectangleValues, const TRACKERS &trackers) {
prepareForDrawing();
printAllText(trackers);
drawButtons();
if (gameMode != MAIN_MENU && gameMode != CREDITS && gameMode != INSTRUCTIONS) {
displayRectangles(currentRectangleValues, trackers);
}
al_flip_display();
}
// This is the main method to display the rectangles
void Allegro::displayRectangles(const apvector <int> ¤tRectangleValues, const TRACKERS &trackers) {
// Declare the ap vectors for screen positions and the current rectangle values
apvector <SCREEN_POSITIONS> screenPositions = calculateScreenPositionsForRectangles(currentRectangleValues);
// Gets the rectangles colour
apvector <ALLEGRO_COLOR> rectangleColours = getRectangleColours(currentRectangleValues, trackers);
// Draws all the rectangles
drawRectangles(screenPositions, currentRectangleValues, rectangleColours);
}
apvector <ALLEGRO_COLOR> Allegro::getRectangleColours(const apvector <int> ¤tRectangleValues, const TRACKERS &trackers) {
int arrayLength = currentRectangleValues.length();
apvector <ALLEGRO_COLOR> rectangleColours(arrayLength);
for (int i = 0; i < arrayLength; i++) {
if (i == trackers.redIndex) {
rectangleColours[i] = RED;
} else if (i == trackers.leftBlackIndex || i == trackers.rightBlackIndex) {
rectangleColours[i] = BLACK;
} else if (i == trackers.middleGreenIndex || i == trackers.greenIndex[i]) {
rectangleColours[i] = GREEN;
} else if ((i < trackers.leftBlackIndex || (i > trackers.rightBlackIndex && trackers.rightBlackIndex != -1)) &&
(gameMode == BINARY_SEARCH || gameMode == START)) {
rectangleColours[i] = LIGHT_BLACK;
} else {
rectangleColours[i] = WHITE;
}
}
return rectangleColours;
}
// This method prepares the background
void Allegro::prepareForDrawing() {
al_clear_to_color(LIGHT_BLUE);
if (gameMode == MAIN_MENU) {
al_draw_bitmap(background, 0, 0, 0);
}
}
// This method calculates the screen positions
apvector<SCREEN_POSITIONS> Allegro::calculateScreenPositionsForRectangles(const apvector<int> ¤tRectangleValues) {
// Declare + initialize variables
int arrayLength = currentRectangleValues.length();
int rectangleWidth = (SCREEN_WIDTH - 100) / arrayLength;
int rectangleHeight = 0;
// An apvector of a struct for screen coordinates
apvector<SCREEN_POSITIONS> screenPositions(arrayLength);
// Iterate through each value
for (int i = 0; i < arrayLength; i++) {
rectangleHeight = currentRectangleValues[i] * ((double)(SCREEN_HEIGHT - 200) / arrayLength);
screenPositions[i].yBottomRightCorner = SCREEN_HEIGHT - 50;
screenPositions[i].yTopLeftCorner = screenPositions[i].yBottomRightCorner - rectangleHeight;
screenPositions[i].xTopLeftCorner = 50 + (rectangleWidth * i);
screenPositions[i].xBottonRightCorner = screenPositions[i].xTopLeftCorner + rectangleWidth;
}
return screenPositions;
}
void Allegro::drawRectangles(const apvector<SCREEN_POSITIONS> &screenPositions, const apvector<int> ¤tRectangleValues,
const apvector <ALLEGRO_COLOR> rectangleColours) {
for (int i = 0; i < currentRectangleValues.length(); i++) {
al_draw_filled_rectangle(screenPositions[i].xTopLeftCorner, screenPositions[i].yTopLeftCorner,
screenPositions[i].xBottonRightCorner, screenPositions[i].yBottomRightCorner, rectangleColours[i]);
// Draw rectangle border
al_draw_rectangle(screenPositions[i].xTopLeftCorner, screenPositions[i].yTopLeftCorner,
screenPositions[i].xBottonRightCorner, screenPositions[i].yBottomRightCorner, BLACK, 1.0);
}
}
/********************* Buttons ********************************************/
// This function setups the buttons properies based on the game mode the user is in
void Allegro::buttonSetup() {
// Switch the game mode so that the buttons are set up in the appropriate game mode
switch (gameMode) {
case RESET:
case START:
case MERGE_SORT:
case BUBBLE_SORT:
case SELECTION_SORT:
case BINARY_SEARCH:
// Set the vector size
button.resize(11);
// This modular method allows me to set all the button properties in 1 line for all types of button
/// Being able to reuse for multiple projects, saving me a lot of time
setButtonProperties(BUTTON_BUBBLE, 25, 25, 175, 70, false, "Bubble Sort", WHITE);
setButtonProperties(BUTTON_SELECTION, 25, 95, 175, 140, false, "Selection Sort", WHITE);
setButtonProperties(BUTTON_MERGE, 200, 25, 350, 70, false, "Merge Sort", WHITE);
setButtonProperties(BUTTON_MAIN_MENU, SCREEN_WIDTH - 350, 25, SCREEN_WIDTH - 200, 70, false, "Main Menu", ORANGE);
setButtonProperties(BUTTON_IN_GAME_QUIT, SCREEN_WIDTH - 175, 95, SCREEN_WIDTH - 25, 140, false, "Quit", RED);
setButtonProperties(BUTTON_RESET, SCREEN_WIDTH - 175, 25, SCREEN_WIDTH - 25, 70, false, "Reset", YELLOW);
setButtonProperties(BUTTON_PLUS_ONE, SCREEN_WIDTH / 2 + 135, 95, SCREEN_WIDTH / 2 + 185, 140, false, "+1", GREEN);
setButtonProperties(BUTTON_PLUS_TEN, SCREEN_WIDTH / 2 + 80, 95, SCREEN_WIDTH / 2 + 130, 140, false, "+10", GREEN);
setButtonProperties(BUTTON_MINUS_ONE, SCREEN_WIDTH / 2 - 185, 95, SCREEN_WIDTH / 2 - 135, 140, false, "-1", RED);
setButtonProperties(BUTTON_MINUS_TEN, SCREEN_WIDTH / 2 - 130, 95, SCREEN_WIDTH / 2 - 80, 140, false, "-10", RED);
setButtonProperties(BUTTON_BINARY_SEARCH, SCREEN_WIDTH / 2 - 75, 25, SCREEN_WIDTH / 2 + 75, 70, false, "Search", WHITE);
break;
case MAIN_MENU:
button.resize(4);
setButtonProperties(BUTTON_START, SCREEN_WIDTH / 3, 150,
SCREEN_WIDTH / 3 * 2, 250, true, "START", GREEN);
setButtonProperties(BUTTON_INSTRUCTIONS, SCREEN_WIDTH / 3, 285,
SCREEN_WIDTH / 3 * 2, 385, true, "INSTRUCTIONS", ORANGE);
setButtonProperties(BUTTON_CREDITS, SCREEN_WIDTH / 3, 420,
SCREEN_WIDTH / 3 * 2, 520, true, "CREDITS", ORANGE);
setButtonProperties(BUTTON_MAIN_MENU_QUIT, SCREEN_WIDTH / 3, 555,
SCREEN_WIDTH / 3 * 2, 655, true, "QUIT", RED);
break;
case INSTRUCTIONS:
case CREDITS:
button.resize(3);
setButtonProperties(BUTTON_CREDITS_MAIN_MENU, SCREEN_WIDTH / 10 + 25, SCREEN_HEIGHT - 100,
(SCREEN_WIDTH / 10 * 3.2) + 25, SCREEN_HEIGHT - 25, true, "MAIN MENU", YELLOW);
setButtonProperties(BUTTON_CREDITS_START, SCREEN_WIDTH / 10 + 325, SCREEN_HEIGHT - 100,
(SCREEN_WIDTH / 10 * 3.2) + 325, SCREEN_HEIGHT - 25, true, "START", GREEN);
setButtonProperties(BUTTON_CREDITS_QUIT, SCREEN_WIDTH / 10 + 625, SCREEN_HEIGHT - 100,
(SCREEN_WIDTH / 10 * 3.2) + 625, SCREEN_HEIGHT - 25, true, "QUIT", RED);
break;
}
// Reset the button booleans
for (int i = 0; i < button.length(); i++) {
button[i].clicked = false;
button[i].filled = false;
}
}
// This reusable function takes the properties from the button setup and allocates them to each specific button
// essentially a modifier method
void Allegro::setButtonProperties(int buttonNumber, int upperLeftX, int upperLeftY, int lowerRightX,
int lowerRightY, bool largeText, string buttonTitle, ALLEGRO_COLOR buttonColour) {
button[buttonNumber].upperLeftXCoordinate = upperLeftX;
button[buttonNumber].upperLeftYCoordinate = upperLeftY;
button[buttonNumber].lowerRightYCoordinate = lowerRightY;
button[buttonNumber].lowerRightXCoordinate = lowerRightX;
button[buttonNumber].largeText = largeText;
button[buttonNumber].buttonTitle = buttonTitle;
button[buttonNumber].colour = buttonColour;
}
/// This method draws the buttons
// The button is either drawn with just the outline or filled in, based on the users mouses position
void Allegro::drawButtons() {
for (int i = 0; i < button.length(); i++) {
// Based on if the bool is true or not either it draws the box filled or just the outline
if (!button[i].filled) {
al_draw_rectangle(button[i].upperLeftXCoordinate, button[i].upperLeftYCoordinate,
button[i].lowerRightXCoordinate, button[i].lowerRightYCoordinate, button[i].colour, 3);
} else {
al_draw_filled_rectangle(button[i].upperLeftXCoordinate, button[i].upperLeftYCoordinate,
button[i].lowerRightXCoordinate, button[i].lowerRightYCoordinate, button[i].colour);
}
// Initialize the text font and size based on the buttons properties
ALLEGRO_FONT *tempFont = NULL;
int tempFontSize = 0;
if (button[i].largeText) {
tempFont = arial;
tempFontSize = LARGE_FONT_SIZE;
} else {
tempFont = smallArial;
tempFontSize = SMALL_FONT_SIZE;
}
// Draw out the button text - 1 line that does it for all buttons
al_draw_text(tempFont, BLACK, (button[i].upperLeftXCoordinate + button[i].lowerRightXCoordinate) / 2,
(button[i].upperLeftYCoordinate + button[i].lowerRightYCoordinate) / 2 - (tempFontSize / 2),
ALLEGRO_ALIGN_CENTER, button[i].buttonTitle.c_str());
}
}
// This function checks if the users mouse is hovering over the button and if the button was clicked
// Need to pass the button, the mouse coordinates, game mode, and the event type (as a bool clicked or not)
void Allegro::checkButtonAction() {
int mouseXCoordinate = event.mouse.x;
int mouseYCoordinate = event.mouse.y;
int eventType = event.type;
for (int i = 0; i < button.length(); i++) {
// Check the mouse is hovering over the button
if (mouseXCoordinate >= button[i].upperLeftXCoordinate &&
mouseXCoordinate <= button[i].lowerRightXCoordinate &&
mouseYCoordinate >= button[i].upperLeftYCoordinate &&
mouseYCoordinate <= button[i].lowerRightYCoordinate) {
if (eventType == ALLEGRO_EVENT_MOUSE_AXES || eventType == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
button[i].filled = true;
}
if (eventType == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
button[i].clicked = true;
button[i].filled = false;
}
// If the mouse is not hovering over any buttons then the buttons will just be hallow
} else if (eventType == ALLEGRO_EVENT_MOUSE_AXES) {
button[i].filled = false;
}
}
}
/// This function converts which button was clicked to an the appropriate action (game mode or end the game)
void Allegro::checkButtonState(bool &continuePlaying) {
GAMEMODE previousGamemode = gameMode;
// Switch game mode
switch (gameMode) {
case MAIN_MENU:
// Based on which button was clicked, the appropriate action is performed
if (button[BUTTON_START].clicked) {
gameMode = START;
} else if (button[BUTTON_INSTRUCTIONS].clicked) {
gameMode = INSTRUCTIONS;
} else if (button[BUTTON_CREDITS].clicked) {
gameMode = CREDITS;
} else if (button[BUTTON_MAIN_MENU_QUIT].clicked) {
// End the game
continuePlaying = false;
}
break;
case INSTRUCTIONS:
case CREDITS:
if (button[BUTTON_CREDITS_MAIN_MENU].clicked) {
gameMode = MAIN_MENU;
} else if (button[BUTTON_CREDITS_START].clicked) {
gameMode = START;
} else if (button[BUTTON_CREDITS_QUIT].clicked) {
continuePlaying = false;
}
break;
case START:
if (button[BUTTON_MERGE].clicked) {
gameMode = MERGE_SORT;
} else if (button[BUTTON_BUBBLE].clicked) {
gameMode = BUBBLE_SORT;
} else if (button[BUTTON_SELECTION].clicked) {
gameMode = SELECTION_SORT;
} else if (button[BUTTON_PLUS_ONE].clicked) {
searchTarget++;
} else if (button[BUTTON_PLUS_TEN].clicked) {
searchTarget += 10;
} else if (button[BUTTON_MINUS_ONE].clicked) {
searchTarget--;
} else if (button[BUTTON_MINUS_TEN].clicked) {
searchTarget -= 10;
}
// There is no break as for some of the modes the buttons are visually there but are disabled
case BUBBLE_SORT:
case SELECTION_SORT:
case MERGE_SORT:
case RESET:
case BINARY_SEARCH:
if (button[BUTTON_RESET].clicked) {
gameMode = RESET;
} else if (button[BUTTON_BINARY_SEARCH].clicked) {
gameMode = BINARY_SEARCH;
} else if (button[BUTTON_IN_GAME_QUIT].clicked) {
continuePlaying = false;
} else if (button[BUTTON_MAIN_MENU].clicked) {
gameMode = MAIN_MENU;
}
break;
}
// Setting the bool of the button back to false
for (int i = 0; i < button.length(); i++) {
button[i].clicked = false;
}
// Set up the new button coordinates if the gamemode was changed
if (previousGamemode != gameMode) {
buttonSetup();
}
}
// This method prints most of the text onto the screen for the user
void Allegro::printAllText(const TRACKERS &trackers) {
switch (gameMode) {
case MAIN_MENU:
al_draw_text(arial, BLACK, 5, SCREEN_HEIGHT - 30, ALLEGRO_ALIGN_LEFT, "By Eric Karpovits");
al_draw_text(arial, BLACK, SCREEN_WIDTH / 2, 75, ALLEGRO_ALIGN_CENTER, "Graphical Sorting Tool");
break;
case INSTRUCTIONS:
al_draw_text(arial, BLACK, SCREEN_WIDTH / 2, 40, ALLEGRO_ALIGN_CENTER, "INSTRUCTIONS");
al_draw_text(smallArial, BLACK, 100, 100, ALLEGRO_ALIGN_LEFT,
"This program is a tool that aids the visualization of the algorithms associated with sorting.");
al_draw_text(smallArial, BLACK, 100, 140, ALLEGRO_ALIGN_LEFT,
"In the program, users can pick three different sorting algorithms and one search algorithm such as:");
al_draw_text(smallArial, BLACK, 150, 180, ALLEGRO_ALIGN_LEFT,
"1. Bubble Sort (Simple Sort) - Time Complexity: O(n^2)");
al_draw_text(smallArial, BLACK, 150, 220, ALLEGRO_ALIGN_LEFT,
"2. Selection Sort (Simple Sort) - Time Complexity: O(n^2)");
al_draw_text(smallArial, BLACK, 150, 260, ALLEGRO_ALIGN_LEFT,
"3. Merge Sort (Advanced Sort) - Time Complexity: O(n*log(n))");
al_draw_text(smallArial, BLACK, 150, 300, ALLEGRO_ALIGN_LEFT,
"4. Binary Search (Advanced Search) - Time Complexity: O(log(n))");
al_draw_text(smallArial, BLACK, 100, 340, ALLEGRO_ALIGN_LEFT,
"Clicking one of the sorts will visually sort the random vector in front of the user.");
al_draw_text(smallArial, BLACK, 100, 380, ALLEGRO_ALIGN_LEFT,
"To use the search feature the user will need to pick their number first and then click search.");
al_draw_text(smallArial, BLACK, 100, 420, ALLEGRO_ALIGN_LEFT,
"The array will be automatically sorted if it was not already done so.");
al_draw_text(smallArial, BLACK, 100, 460, ALLEGRO_ALIGN_LEFT,
"Note that the vector size is 150 elements.");
break;
case CREDITS:
al_draw_text(arial, BLACK, SCREEN_WIDTH / 2, 40, ALLEGRO_ALIGN_CENTER, "CREDITS");
al_draw_text(smallArial, BLACK, SCREEN_WIDTH / 2, 100, ALLEGRO_ALIGN_CENTER,
"Version 1.0 - Eric Karpovits - Copyright 2020 - All rights reserved");
break;
case START:
al_draw_textf(arial, BLACK, SCREEN_WIDTH / 2 + 47, (SCREEN_HEIGHT - 50 + LARGE_FONT_SIZE / 2),
ALLEGRO_ALIGN_LEFT, "%.3fs", trackers.timeDuration);
case BUBBLE_SORT:
case SELECTION_SORT:
case MERGE_SORT:
case RESET:
case BINARY_SEARCH:
al_draw_textf(arial, BLACK, SCREEN_WIDTH / 4 - 60, (SCREEN_HEIGHT - 50 + LARGE_FONT_SIZE / 2),
ALLEGRO_ALIGN_CENTER, "Comparisons: %d", trackers.numberOfComparisons);
al_draw_textf(arial, BLACK, SCREEN_WIDTH / 4 * 3 + 60, (SCREEN_HEIGHT - 50 + LARGE_FONT_SIZE / 2),
ALLEGRO_ALIGN_CENTER, "Moves / Splits: %d", trackers.numberOfMoves);
al_draw_rectangle(SCREEN_WIDTH / 2 - 75, 95, SCREEN_WIDTH / 2 + 75, 140, WHITE, 3);
al_draw_textf(arial, BLACK, SCREEN_WIDTH / 2, 117 - LARGE_FONT_SIZE / 2, ALLEGRO_ALIGN_CENTER, "%d", searchTarget);
al_draw_text(arial, BLACK, SCREEN_WIDTH / 2 - 43, (SCREEN_HEIGHT - 50 + LARGE_FONT_SIZE / 2),
ALLEGRO_ALIGN_CENTER, "Running Time:");
break;
}
}