Dependencies:   mbed

Committer:
HenryWTriff
Date:
Fri May 22 16:27:56 2020 +0000
Revision:
26:f1d3b7e31091
Parent:
25:31761087a83f
Added Spectators and helicopters to the maps!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HenryWTriff 2:d08b6a1eaf2b 1 #include "Graphics.h"
HenryWTriff 2:d08b6a1eaf2b 2
HenryWTriff 17:4c5f25d5c4d5 3 Graphics::Graphics()
HenryWTriff 17:4c5f25d5c4d5 4 {
HenryWTriff 17:4c5f25d5c4d5 5 }
HenryWTriff 17:4c5f25d5c4d5 6
HenryWTriff 17:4c5f25d5c4d5 7 Graphics::~Graphics()
HenryWTriff 17:4c5f25d5c4d5 8 {
HenryWTriff 17:4c5f25d5c4d5 9 }
HenryWTriff 17:4c5f25d5c4d5 10
HenryWTriff 18:5fcb0514fb70 11 //-----------
HenryWTriff 18:5fcb0514fb70 12 // SPRITES
HenryWTriff 18:5fcb0514fb70 13 //-----------
HenryWTriff 3:ceed6d026b8b 14
HenryWTriff 26:f1d3b7e31091 15 #include "Sprites.h"
HenryWTriff 6:5f76dd718dc3 16
HenryWTriff 18:5fcb0514fb70 17 //--------------------------
HenryWTriff 2:d08b6a1eaf2b 18 // MAIN GRAPHICS FUNCTION
HenryWTriff 18:5fcb0514fb70 19 //--------------------------
HenryWTriff 18:5fcb0514fb70 20 // This is the main function for the graphics.
HenryWTriff 18:5fcb0514fb70 21 // It is a LARGE function but it reduces the clutter in main.cpp
HenryWTriff 18:5fcb0514fb70 22 // This is run once every frame.
HenryWTriff 2:d08b6a1eaf2b 23
HenryWTriff 26:f1d3b7e31091 24 void Graphics::Draw_Map(Point_2D translation, int angle, float squish, float horizon_factor, Line_2D *Track_Lines, Line_2D *Track_Dotted_Lines, Line_2D *Track_Walls, Sprite_2D *Track_Sprites, Triangle_2D *Track_Boost_Plates, Map_Data map_info, int car_type, Point_2D ghost_position, N5110 &LCD)
HenryWTriff 2:d08b6a1eaf2b 25 {
HenryWTriff 10:29126a41b1da 26 //Variable declaration
HenryWTriff 7:2ce6e90f6d47 27 enum cars {Basic, Offroad, Drifter, Sportscar, Racecar, Stupid};
HenryWTriff 3:ceed6d026b8b 28 Line_2D Track_Lines_Transformed[map_info.number_of_track_lines];
HenryWTriff 3:ceed6d026b8b 29 Line_2D Track_Dotted_Lines_Transformed[map_info.number_of_dotted_lines];
HenryWTriff 26:f1d3b7e31091 30 Point_2D Transformed_Sprites[map_info.number_of_sprites];
HenryWTriff 5:2d9f3c36bcb9 31 Line_2D Transformed_Walls[map_info.number_of_walls];
HenryWTriff 6:5f76dd718dc3 32 Point_2D Transformed_Ghost;
HenryWTriff 17:4c5f25d5c4d5 33
HenryWTriff 10:29126a41b1da 34 //Transforms the points of the track lines
HenryWTriff 10:29126a41b1da 35 for(int i = 0; i < map_info.number_of_track_lines; i++) { //Iterates through the given track array
HenryWTriff 2:d08b6a1eaf2b 36 Track_Lines_Transformed[i] = Track_Lines[i];
HenryWTriff 2:d08b6a1eaf2b 37 //Translation
HenryWTriff 2:d08b6a1eaf2b 38 Track_Lines_Transformed[i].from = Translate_Point(Track_Lines_Transformed[i].from, Round(translation.x), Round(translation.y));
HenryWTriff 2:d08b6a1eaf2b 39 Track_Lines_Transformed[i].to = Translate_Point(Track_Lines_Transformed[i].to, Round(translation.x), Round(translation.y));
HenryWTriff 2:d08b6a1eaf2b 40 //Rotation
HenryWTriff 2:d08b6a1eaf2b 41 Track_Lines_Transformed[i].from = Rotate_Point(Track_Lines_Transformed[i].from, angle);
HenryWTriff 2:d08b6a1eaf2b 42 Track_Lines_Transformed[i].to = Rotate_Point(Track_Lines_Transformed[i].to, angle);
HenryWTriff 25:31761087a83f 43 //Horizon
HenryWTriff 25:31761087a83f 44 Track_Lines_Transformed[i].from = Horizon_Point(Track_Lines_Transformed[i].from, horizon_factor);
HenryWTriff 25:31761087a83f 45 Track_Lines_Transformed[i].to = Horizon_Point(Track_Lines_Transformed[i].to, horizon_factor);
HenryWTriff 2:d08b6a1eaf2b 46 //Squish
HenryWTriff 2:d08b6a1eaf2b 47 Track_Lines_Transformed[i].from = Squish_Point(Track_Lines_Transformed[i].from, squish);
HenryWTriff 2:d08b6a1eaf2b 48 Track_Lines_Transformed[i].to = Squish_Point(Track_Lines_Transformed[i].to, squish);
HenryWTriff 2:d08b6a1eaf2b 49 }
HenryWTriff 17:4c5f25d5c4d5 50
HenryWTriff 10:29126a41b1da 51 //Transforms the points of the dotted lines on the track
HenryWTriff 10:29126a41b1da 52 for(int i = 0; i < map_info.number_of_dotted_lines; i++) { //Iterates through the given dotted lines array
HenryWTriff 3:ceed6d026b8b 53 Track_Dotted_Lines_Transformed[i] = Track_Dotted_Lines[i];
HenryWTriff 3:ceed6d026b8b 54 //Translation
HenryWTriff 3:ceed6d026b8b 55 Track_Dotted_Lines_Transformed[i].from = Translate_Point(Track_Dotted_Lines_Transformed[i].from, Round(translation.x), Round(translation.y));
HenryWTriff 3:ceed6d026b8b 56 Track_Dotted_Lines_Transformed[i].to = Translate_Point(Track_Dotted_Lines_Transformed[i].to, Round(translation.x), Round(translation.y));
HenryWTriff 3:ceed6d026b8b 57 //Rotation
HenryWTriff 3:ceed6d026b8b 58 Track_Dotted_Lines_Transformed[i].from = Rotate_Point(Track_Dotted_Lines_Transformed[i].from, angle);
HenryWTriff 3:ceed6d026b8b 59 Track_Dotted_Lines_Transformed[i].to = Rotate_Point(Track_Dotted_Lines_Transformed[i].to, angle);
HenryWTriff 25:31761087a83f 60 //Horizon
HenryWTriff 25:31761087a83f 61 Track_Dotted_Lines_Transformed[i].from = Horizon_Point(Track_Dotted_Lines_Transformed[i].from, horizon_factor);
HenryWTriff 25:31761087a83f 62 Track_Dotted_Lines_Transformed[i].to = Horizon_Point(Track_Dotted_Lines_Transformed[i].to, horizon_factor);
HenryWTriff 3:ceed6d026b8b 63 //Squish
HenryWTriff 3:ceed6d026b8b 64 Track_Dotted_Lines_Transformed[i].from = Squish_Point(Track_Dotted_Lines_Transformed[i].from, squish);
HenryWTriff 3:ceed6d026b8b 65 Track_Dotted_Lines_Transformed[i].to = Squish_Point(Track_Dotted_Lines_Transformed[i].to, squish);
HenryWTriff 3:ceed6d026b8b 66 }
HenryWTriff 17:4c5f25d5c4d5 67
HenryWTriff 10:29126a41b1da 68 //Transforms the points of the start flags
HenryWTriff 26:f1d3b7e31091 69 for(int i = 0; i < map_info.number_of_sprites; i++) { //Iterates through the given flags array
HenryWTriff 26:f1d3b7e31091 70 Transformed_Sprites[i].x = Track_Sprites[i].x;
HenryWTriff 26:f1d3b7e31091 71 Transformed_Sprites[i].y = Track_Sprites[i].y;
HenryWTriff 3:ceed6d026b8b 72 //Translation
HenryWTriff 26:f1d3b7e31091 73 Transformed_Sprites[i] = Translate_Point(Transformed_Sprites[i], Round(translation.x), Round(translation.y));
HenryWTriff 3:ceed6d026b8b 74 //Rotation
HenryWTriff 26:f1d3b7e31091 75 Transformed_Sprites[i] = Rotate_Point(Transformed_Sprites[i], angle);
HenryWTriff 25:31761087a83f 76 //Horizon
HenryWTriff 26:f1d3b7e31091 77 Transformed_Sprites[i] = Horizon_Point(Transformed_Sprites[i], horizon_factor);
HenryWTriff 3:ceed6d026b8b 78 //Squish
HenryWTriff 26:f1d3b7e31091 79 Transformed_Sprites[i] = Squish_Point(Transformed_Sprites[i], squish);
HenryWTriff 3:ceed6d026b8b 80 }
HenryWTriff 17:4c5f25d5c4d5 81
HenryWTriff 10:29126a41b1da 82 //Transforms the points of the walls
HenryWTriff 10:29126a41b1da 83 for(int i = 0; i < map_info.number_of_walls; i++) { //Iterates throught the given walls array
HenryWTriff 5:2d9f3c36bcb9 84 Transformed_Walls[i] = Track_Walls[i];
HenryWTriff 5:2d9f3c36bcb9 85 //Translation
HenryWTriff 5:2d9f3c36bcb9 86 Transformed_Walls[i].from = Translate_Point(Transformed_Walls[i].from, Round(translation.x), Round(translation.y));
HenryWTriff 5:2d9f3c36bcb9 87 Transformed_Walls[i].to = Translate_Point(Transformed_Walls[i].to, Round(translation.x), Round(translation.y));
HenryWTriff 5:2d9f3c36bcb9 88 //Rotation
HenryWTriff 5:2d9f3c36bcb9 89 Transformed_Walls[i].from = Rotate_Point(Transformed_Walls[i].from, angle);
HenryWTriff 5:2d9f3c36bcb9 90 Transformed_Walls[i].to = Rotate_Point(Transformed_Walls[i].to, angle);
HenryWTriff 25:31761087a83f 91 //Horizon
HenryWTriff 25:31761087a83f 92 Transformed_Walls[i].from = Horizon_Point(Transformed_Walls[i].from, horizon_factor);
HenryWTriff 25:31761087a83f 93 Transformed_Walls[i].to = Horizon_Point(Transformed_Walls[i].to, horizon_factor);
HenryWTriff 5:2d9f3c36bcb9 94 //Squish
HenryWTriff 5:2d9f3c36bcb9 95 Transformed_Walls[i].from = Squish_Point(Transformed_Walls[i].from, squish);
HenryWTriff 5:2d9f3c36bcb9 96 Transformed_Walls[i].to = Squish_Point(Transformed_Walls[i].to, squish);
HenryWTriff 5:2d9f3c36bcb9 97 }
HenryWTriff 17:4c5f25d5c4d5 98
HenryWTriff 10:29126a41b1da 99 //Transforms the points of the ghost position
HenryWTriff 6:5f76dd718dc3 100 Transformed_Ghost = ghost_position;
HenryWTriff 6:5f76dd718dc3 101 //Translation
HenryWTriff 6:5f76dd718dc3 102 Transformed_Ghost = Translate_Point(Transformed_Ghost, Round(translation.x), Round(translation.y));
HenryWTriff 6:5f76dd718dc3 103 //Rotation
HenryWTriff 6:5f76dd718dc3 104 Transformed_Ghost = Rotate_Point(Transformed_Ghost, angle);
HenryWTriff 25:31761087a83f 105 //Horizon
HenryWTriff 25:31761087a83f 106 Transformed_Ghost = Horizon_Point(Transformed_Ghost, horizon_factor);
HenryWTriff 6:5f76dd718dc3 107 //Squish
HenryWTriff 6:5f76dd718dc3 108 Transformed_Ghost = Squish_Point(Transformed_Ghost, squish);
HenryWTriff 9:7b1093d3f03a 109 Transformed_Ghost.x = Transformed_Ghost.x + 4;
HenryWTriff 17:4c5f25d5c4d5 110
HenryWTriff 10:29126a41b1da 111 //Draws the track lines
HenryWTriff 10:29126a41b1da 112 for(int i = 0; i < map_info.number_of_track_lines; i++) { //Iterates through each line
HenryWTriff 2:d08b6a1eaf2b 113 Graphics_Draw_Line(Track_Lines_Transformed[i].from, Track_Lines_Transformed[i].to, true, LCD);
HenryWTriff 2:d08b6a1eaf2b 114 }
HenryWTriff 17:4c5f25d5c4d5 115
HenryWTriff 10:29126a41b1da 116 //Draws the dotted lines
HenryWTriff 10:29126a41b1da 117 for(int i = 0; i < map_info.number_of_dotted_lines; i++) { //Iterates through each line
HenryWTriff 3:ceed6d026b8b 118 Graphics_Draw_Line(Track_Dotted_Lines_Transformed[i].from, Track_Dotted_Lines_Transformed[i].to,false, LCD);
HenryWTriff 3:ceed6d026b8b 119 }
HenryWTriff 17:4c5f25d5c4d5 120
HenryWTriff 10:29126a41b1da 121 //Draws the boost plates
HenryWTriff 10:29126a41b1da 122 for(int i = 0; i < map_info.number_of_boost_plates; i++) { //Iterates through each plate
HenryWTriff 6:5f76dd718dc3 123 Graphics_Draw_Boost_Plate(Track_Boost_Plates[i], translation, angle, squish, LCD);
HenryWTriff 6:5f76dd718dc3 124 }
HenryWTriff 17:4c5f25d5c4d5 125
HenryWTriff 10:29126a41b1da 126 //Draws the ghost
HenryWTriff 17:4c5f25d5c4d5 127 Graphics_Draw_Sprite(Transformed_Ghost, 8, 8, (int *)ghost, LCD);
HenryWTriff 17:4c5f25d5c4d5 128
HenryWTriff 10:29126a41b1da 129 //Draws the walls
HenryWTriff 5:2d9f3c36bcb9 130 for(int i = 0; i < map_info.number_of_walls; i++) {
HenryWTriff 5:2d9f3c36bcb9 131 Graphics_Draw_Wall(Transformed_Walls[i].from, Transformed_Walls[i].to,4, LCD);
HenryWTriff 5:2d9f3c36bcb9 132 }
HenryWTriff 26:f1d3b7e31091 133
HenryWTriff 26:f1d3b7e31091 134 //Draws the flags
HenryWTriff 26:f1d3b7e31091 135 for(int i = 0; i < map_info.number_of_sprites; i++) { //Iterates through each flag
HenryWTriff 26:f1d3b7e31091 136 if(Track_Sprites[i].type == Flag) {
HenryWTriff 26:f1d3b7e31091 137 Graphics_Draw_Sprite(Transformed_Sprites[i], 8, 8, (int *)flag, LCD);
HenryWTriff 26:f1d3b7e31091 138 } else if(Track_Sprites[i].type == Helicopter) {
HenryWTriff 26:f1d3b7e31091 139 Graphics_Draw_Sprite(Transformed_Sprites[i], 48, 24, (int *)helicopter, LCD);
HenryWTriff 26:f1d3b7e31091 140 } else if(Track_Sprites[i].type == People_Cheering) {
HenryWTriff 26:f1d3b7e31091 141 Graphics_Draw_Sprite(Transformed_Sprites[i], 8, 12, (int *)person_cheering, LCD);
HenryWTriff 26:f1d3b7e31091 142 } else if(Track_Sprites[i].type == People_Standing_1) {
HenryWTriff 26:f1d3b7e31091 143 Graphics_Draw_Sprite(Transformed_Sprites[i], 6, 12, (int *)person_standing_1, LCD);
HenryWTriff 26:f1d3b7e31091 144 } else if(Track_Sprites[i].type == People_Standing_2) {
HenryWTriff 26:f1d3b7e31091 145 Graphics_Draw_Sprite(Transformed_Sprites[i], 6, 12, (int *)person_standing_2, LCD);
HenryWTriff 26:f1d3b7e31091 146 }
HenryWTriff 26:f1d3b7e31091 147 }
HenryWTriff 17:4c5f25d5c4d5 148
HenryWTriff 10:29126a41b1da 149 //Draws the players car
HenryWTriff 26:f1d3b7e31091 150 Point_2D car_position = {0,0};
HenryWTriff 7:2ce6e90f6d47 151 if(car_type == Stupid) {
HenryWTriff 7:2ce6e90f6d47 152 Graphics_Draw_Sprite(car_position,8,8,(int *)stupid_car, LCD);
HenryWTriff 7:2ce6e90f6d47 153 } else if(car_type == Racecar) {
HenryWTriff 7:2ce6e90f6d47 154 Graphics_Draw_Sprite(car_position,8,8,(int *)race_car, LCD);
HenryWTriff 7:2ce6e90f6d47 155 } else if(car_type == Sportscar) {
HenryWTriff 7:2ce6e90f6d47 156 Graphics_Draw_Sprite(car_position,8,8,(int *)sports_car, LCD);
HenryWTriff 7:2ce6e90f6d47 157 } else if(car_type == Drifter) {
HenryWTriff 7:2ce6e90f6d47 158 Graphics_Draw_Sprite(car_position,8,8,(int *)drifter_car, LCD);
HenryWTriff 7:2ce6e90f6d47 159 } else if(car_type == Offroad) {
HenryWTriff 7:2ce6e90f6d47 160 Graphics_Draw_Sprite(car_position,8,8,(int *)offroad_car, LCD);
HenryWTriff 7:2ce6e90f6d47 161 } else {
HenryWTriff 7:2ce6e90f6d47 162 Graphics_Draw_Sprite(car_position,8,8,(int *)basic_car, LCD);
HenryWTriff 7:2ce6e90f6d47 163 }
HenryWTriff 2:d08b6a1eaf2b 164 }
HenryWTriff 2:d08b6a1eaf2b 165
HenryWTriff 18:5fcb0514fb70 166 //--------------
HenryWTriff 10:29126a41b1da 167 // START LOGO
HenryWTriff 18:5fcb0514fb70 168 //--------------
HenryWTriff 10:29126a41b1da 169
HenryWTriff 10:29126a41b1da 170 void Graphics::Draw_Logo(N5110 &LCD) //Draw the game logo
HenryWTriff 10:29126a41b1da 171 {
HenryWTriff 10:29126a41b1da 172 LCD.clear();
HenryWTriff 10:29126a41b1da 173 LCD.drawSprite(0,0,48,84,(int *)logo);
HenryWTriff 10:29126a41b1da 174 LCD.refresh();
HenryWTriff 10:29126a41b1da 175 wait(1);
HenryWTriff 10:29126a41b1da 176 }
HenryWTriff 10:29126a41b1da 177
HenryWTriff 18:5fcb0514fb70 178 //-------------------
HenryWTriff 2:d08b6a1eaf2b 179 // SCREEN SETTINGS
HenryWTriff 18:5fcb0514fb70 180 //-------------------
HenryWTriff 2:d08b6a1eaf2b 181
HenryWTriff 10:29126a41b1da 182 void Graphics::Change_Contrast(N5110 &LCD, Gamepad &Device) //Sets the contrast with a greater level of precision
HenryWTriff 2:d08b6a1eaf2b 183 {
HenryWTriff 2:d08b6a1eaf2b 184 LCD.setContrast((0.35 + 0.2 * Device.read_pot1()));
HenryWTriff 2:d08b6a1eaf2b 185 }
HenryWTriff 2:d08b6a1eaf2b 186
HenryWTriff 18:5fcb0514fb70 187 //---------------------------
HenryWTriff 18:5fcb0514fb70 188 // RACE START / END SCREEN
HenryWTriff 18:5fcb0514fb70 189 //---------------------------
HenryWTriff 6:5f76dd718dc3 190
HenryWTriff 18:5fcb0514fb70 191 //START
HenryWTriff 10:29126a41b1da 192 void Graphics::Start_Sequence(int state, N5110 &LCD) //Display the countdown numbers before the race starts
HenryWTriff 6:5f76dd718dc3 193 {
HenryWTriff 26:f1d3b7e31091 194 Point_2D count_down_position = {0,8};
HenryWTriff 6:5f76dd718dc3 195 if(state == 3) {
HenryWTriff 6:5f76dd718dc3 196 Graphics_Draw_Sprite(count_down_position, 17, 17, (int *) count_down_3, LCD);
HenryWTriff 6:5f76dd718dc3 197 } else if(state == 2) {
HenryWTriff 6:5f76dd718dc3 198 Graphics_Draw_Sprite(count_down_position, 17, 17, (int *) count_down_2, LCD);
HenryWTriff 6:5f76dd718dc3 199 } else if(state == 1) {
HenryWTriff 6:5f76dd718dc3 200 Graphics_Draw_Sprite(count_down_position, 17, 17, (int *) count_down_1, LCD);
HenryWTriff 6:5f76dd718dc3 201 } else if(state == 0) {
HenryWTriff 6:5f76dd718dc3 202 Graphics_Draw_Sprite(count_down_position, 18, 18, (int *) count_down_0, LCD);
HenryWTriff 6:5f76dd718dc3 203 }
HenryWTriff 6:5f76dd718dc3 204
HenryWTriff 6:5f76dd718dc3 205 }
HenryWTriff 6:5f76dd718dc3 206
HenryWTriff 18:5fcb0514fb70 207 //FINISH
HenryWTriff 10:29126a41b1da 208 void Graphics::Finish(N5110 &LCD) //When the race is over draw the cup symbol
HenryWTriff 6:5f76dd718dc3 209 {
HenryWTriff 26:f1d3b7e31091 210 Point_2D finish_position = {0,8};
HenryWTriff 6:5f76dd718dc3 211 Graphics_Draw_Sprite(finish_position, 16, 16, (int *) cup, LCD);
HenryWTriff 6:5f76dd718dc3 212 }
HenryWTriff 6:5f76dd718dc3 213
HenryWTriff 18:5fcb0514fb70 214 //--------------------
HenryWTriff 5:2d9f3c36bcb9 215 // DRAW LAP COUNTER
HenryWTriff 18:5fcb0514fb70 216 //--------------------
HenryWTriff 5:2d9f3c36bcb9 217
HenryWTriff 10:29126a41b1da 218 void Graphics::Draw_Laps(int laps, N5110 &LCD) //Prints the lab counter in the bottom left corner
HenryWTriff 5:2d9f3c36bcb9 219 {
HenryWTriff 5:2d9f3c36bcb9 220 if(laps == 1) {
HenryWTriff 5:2d9f3c36bcb9 221 LCD.printString("1",0,5);
HenryWTriff 5:2d9f3c36bcb9 222 } else if (laps == 2) {
HenryWTriff 5:2d9f3c36bcb9 223 LCD.printString("2",0,5);
HenryWTriff 5:2d9f3c36bcb9 224 } else if (laps == 3) {
HenryWTriff 5:2d9f3c36bcb9 225 LCD.printString("3",0,5);
HenryWTriff 5:2d9f3c36bcb9 226 } else if (laps == 4) {
HenryWTriff 5:2d9f3c36bcb9 227 LCD.printString("4",0,5);
HenryWTriff 5:2d9f3c36bcb9 228 } else if (laps == 5) {
HenryWTriff 5:2d9f3c36bcb9 229 LCD.printString("5",0,5);
HenryWTriff 5:2d9f3c36bcb9 230 }
HenryWTriff 5:2d9f3c36bcb9 231 }
HenryWTriff 2:d08b6a1eaf2b 232
HenryWTriff 18:5fcb0514fb70 233 //-------------------
HenryWTriff 18:5fcb0514fb70 234 // DRAW RACE CLOCK
HenryWTriff 18:5fcb0514fb70 235 //-------------------
HenryWTriff 18:5fcb0514fb70 236
HenryWTriff 10:29126a41b1da 237 void Graphics::Draw_Time(bool finished, Time time, N5110 &LCD) //Displays the lap time
HenryWTriff 6:5f76dd718dc3 238 {
HenryWTriff 10:29126a41b1da 239 if(finished == false) { //If the race is over, Print the time in the middle of the screen
HenryWTriff 6:5f76dd718dc3 240 char min[1];
HenryWTriff 10:29126a41b1da 241 sprintf(min, "%i", time.mins); //Converts min integer to char
HenryWTriff 10:29126a41b1da 242 LCD.printString(min,47,5); //Prints the min char on the screen
HenryWTriff 6:5f76dd718dc3 243
HenryWTriff 6:5f76dd718dc3 244 LCD.printString(":",52,5);
HenryWTriff 6:5f76dd718dc3 245
HenryWTriff 10:29126a41b1da 246 if(time.secs < 10) { //If the number of seconds is less than 10
HenryWTriff 10:29126a41b1da 247 LCD.printString("0",57,5); //Draw a 0
HenryWTriff 6:5f76dd718dc3 248 char sec[1];
HenryWTriff 10:29126a41b1da 249 sprintf(sec, "%i", time.secs); //Converts sec integer to char
HenryWTriff 10:29126a41b1da 250 LCD.printString(sec,63,5); //Prints the sec char on the screen
HenryWTriff 6:5f76dd718dc3 251 } else {
HenryWTriff 6:5f76dd718dc3 252 char sec[2];
HenryWTriff 10:29126a41b1da 253 sprintf(sec, "%i", time.secs); //Converts sec integer to char
HenryWTriff 10:29126a41b1da 254 LCD.printString(sec,57,5); //Prints the sec char on the screen
HenryWTriff 6:5f76dd718dc3 255 }
HenryWTriff 6:5f76dd718dc3 256
HenryWTriff 6:5f76dd718dc3 257 LCD.printString(".",68,5);
HenryWTriff 6:5f76dd718dc3 258
HenryWTriff 6:5f76dd718dc3 259 char mili[2];
HenryWTriff 10:29126a41b1da 260 sprintf(mili, "%i", time.milis); //Convers mili integer to char
HenryWTriff 10:29126a41b1da 261 LCD.printString(mili,73,5); //Prints the mili char on the screen
HenryWTriff 10:29126a41b1da 262 } else { //If the race is still ongoing
HenryWTriff 6:5f76dd718dc3 263 char min[1];
HenryWTriff 10:29126a41b1da 264 sprintf(min, "%i", time.mins); //Convers min integer to char
HenryWTriff 10:29126a41b1da 265 LCD.printString(min,23,0); //Prints the min char on the screen
HenryWTriff 6:5f76dd718dc3 266
HenryWTriff 6:5f76dd718dc3 267 LCD.printString(":",28,0);
HenryWTriff 6:5f76dd718dc3 268
HenryWTriff 10:29126a41b1da 269 if(time.secs < 10) { //If the number of seconds is less than 10
HenryWTriff 10:29126a41b1da 270 LCD.printString("0",33,0); //Draw a 0
HenryWTriff 6:5f76dd718dc3 271 char sec[1];
HenryWTriff 10:29126a41b1da 272 sprintf(sec, "%i", time.secs); //Convers sec integer to char
HenryWTriff 10:29126a41b1da 273 LCD.printString(sec,39,0); //Prints the sec char on the screen
HenryWTriff 6:5f76dd718dc3 274 } else {
HenryWTriff 6:5f76dd718dc3 275 char sec[2];
HenryWTriff 10:29126a41b1da 276 sprintf(sec, "%i", time.secs); //Convers sec integer to char
HenryWTriff 10:29126a41b1da 277 LCD.printString(sec,33,0); //Prints the sec char on the screen
HenryWTriff 6:5f76dd718dc3 278 }
HenryWTriff 6:5f76dd718dc3 279
HenryWTriff 6:5f76dd718dc3 280 LCD.printString(".",44,0);
HenryWTriff 6:5f76dd718dc3 281
HenryWTriff 6:5f76dd718dc3 282 char mili[2];
HenryWTriff 10:29126a41b1da 283 sprintf(mili, "%i", time.milis); //Convers mili integer to char
HenryWTriff 10:29126a41b1da 284 LCD.printString(mili,49,0); //Prints the mili char on the screen
HenryWTriff 6:5f76dd718dc3 285 }
HenryWTriff 6:5f76dd718dc3 286 }
HenryWTriff 6:5f76dd718dc3 287
HenryWTriff 18:5fcb0514fb70 288 //-------------------
HenryWTriff 18:5fcb0514fb70 289 // TRANSFORMATIONS
HenryWTriff 18:5fcb0514fb70 290 //-------------------
HenryWTriff 2:d08b6a1eaf2b 291
HenryWTriff 2:d08b6a1eaf2b 292 //ROTATE
HenryWTriff 10:29126a41b1da 293 Point_2D Graphics::Rotate_Point(Point_2D point, float angle) //Rotates all points around the origin
HenryWTriff 2:d08b6a1eaf2b 294 {
HenryWTriff 10:29126a41b1da 295 angle = (angle * PI)/180; //The angle given in degrees is changed into radian form
HenryWTriff 10:29126a41b1da 296 float x_rotated = point.x * cos(angle) - point.y * sin(angle); //X-points are rotated about the origin
HenryWTriff 10:29126a41b1da 297 float y_rotated = point.y * cos(angle) + point.x * sin(angle); //Y-points are rotated about the origin
HenryWTriff 2:d08b6a1eaf2b 298 return {x_rotated, y_rotated};
HenryWTriff 2:d08b6a1eaf2b 299 }
HenryWTriff 2:d08b6a1eaf2b 300 //TRANSLATE
HenryWTriff 10:29126a41b1da 301 Point_2D Graphics::Translate_Point(Point_2D point, int translate_x, int translate_y) //Translates all point by a given translationg
HenryWTriff 2:d08b6a1eaf2b 302 {
HenryWTriff 10:29126a41b1da 303 float x_translated = point.x - translate_x; //Translates the x-points
HenryWTriff 10:29126a41b1da 304 float y_translated = point.y - translate_y; //Translates the y-points
HenryWTriff 2:d08b6a1eaf2b 305 return {x_translated, y_translated};
HenryWTriff 2:d08b6a1eaf2b 306 }
HenryWTriff 25:31761087a83f 307
HenryWTriff 25:31761087a83f 308 //HORIZONED
HenryWTriff 25:31761087a83f 309 Point_2D Graphics::Horizon_Point(Point_2D point, float horizon_factor) //Makes all points look like they are following the horizon
HenryWTriff 25:31761087a83f 310 {
HenryWTriff 25:31761087a83f 311 float x_horizoned;
HenryWTriff 25:31761087a83f 312 if(point.x > 0) {
HenryWTriff 25:31761087a83f 313 x_horizoned = point.x - point.y * horizon_factor;
HenryWTriff 25:31761087a83f 314 } else {
HenryWTriff 25:31761087a83f 315 x_horizoned = point.x + point.y * horizon_factor;
HenryWTriff 25:31761087a83f 316 }
HenryWTriff 25:31761087a83f 317 return {x_horizoned, point.y};
HenryWTriff 25:31761087a83f 318 }
HenryWTriff 25:31761087a83f 319
HenryWTriff 2:d08b6a1eaf2b 320 //SQUISH
HenryWTriff 10:29126a41b1da 321 //To get the 3D perspective, the y-coordinate is reduced by a factor
HenryWTriff 10:29126a41b1da 322 Point_2D Graphics::Squish_Point(Point_2D point, float squish) //Squishes all y-coordinates by a factor
HenryWTriff 2:d08b6a1eaf2b 323 {
HenryWTriff 10:29126a41b1da 324 float x_squish = point.x; //The x-coordinate remains the same
HenryWTriff 10:29126a41b1da 325 float y_squish = point.y * squish; //The y-coordinate is reduced
HenryWTriff 2:d08b6a1eaf2b 326 return {x_squish, y_squish};
HenryWTriff 2:d08b6a1eaf2b 327 }
HenryWTriff 2:d08b6a1eaf2b 328
HenryWTriff 18:5fcb0514fb70 329 //--------
HenryWTriff 2:d08b6a1eaf2b 330 // MATH
HenryWTriff 18:5fcb0514fb70 331 //--------
HenryWTriff 2:d08b6a1eaf2b 332
HenryWTriff 18:5fcb0514fb70 333 //ROUNDING FLOATING POINT NUMBERS
HenryWTriff 10:29126a41b1da 334 int Graphics::Round(float number) //Simple function that rounds floating point numbers
HenryWTriff 2:d08b6a1eaf2b 335 {
HenryWTriff 10:29126a41b1da 336 int number_int = (number * 10); //The number is multiplied by 10
HenryWTriff 10:29126a41b1da 337 int remainder = number_int % 10; //The remainder when dividing by 10 is calculated
HenryWTriff 10:29126a41b1da 338 if(remainder < 5) { //If the number is less than 5
HenryWTriff 10:29126a41b1da 339 return ((number_int - remainder) / 10); //Round down
HenryWTriff 2:d08b6a1eaf2b 340 } else {
HenryWTriff 10:29126a41b1da 341 return ((number_int + (10 - remainder)) / 10); //Round up
HenryWTriff 2:d08b6a1eaf2b 342 }
HenryWTriff 2:d08b6a1eaf2b 343 }
HenryWTriff 2:d08b6a1eaf2b 344
HenryWTriff 18:5fcb0514fb70 345 //CALCULATING THE GRADIENT OF A LINE
HenryWTriff 10:29126a41b1da 346 float Graphics::Gradient(Point_2D from, Point_2D to) //Calculates the gradient of the line between two points
HenryWTriff 2:d08b6a1eaf2b 347 {
HenryWTriff 10:29126a41b1da 348 float change_in_y = to.y - from.y; //Calculates the change in the y-values of the two points
HenryWTriff 10:29126a41b1da 349 float change_in_x = to.x - from.x; //Calculates the change in the x-values of the two points
HenryWTriff 10:29126a41b1da 350 float gradient = change_in_y / change_in_x; //Calculates the gradient by doing dy/dx
HenryWTriff 10:29126a41b1da 351 if(gradient < 0.001 && gradient > -0.001) { //If the gradient is aproximately 0
HenryWTriff 10:29126a41b1da 352 return 0; //Return zero
HenryWTriff 2:d08b6a1eaf2b 353 } else {
HenryWTriff 10:29126a41b1da 354 return gradient; //Else return the gradient
HenryWTriff 2:d08b6a1eaf2b 355 }
HenryWTriff 2:d08b6a1eaf2b 356 }
HenryWTriff 2:d08b6a1eaf2b 357
HenryWTriff 18:5fcb0514fb70 358 //CHECKING IF A GRADIENT IS VERY LARGE
HenryWTriff 10:29126a41b1da 359 bool Graphics::Gradient_Check_Infinate(Point_2D from, Point_2D to) //Checks to see if the gradient is vertical
HenryWTriff 2:d08b6a1eaf2b 360 {
HenryWTriff 10:29126a41b1da 361 float change_in_x = to.x - from.x; //Calculates the change in the x-value
HenryWTriff 10:29126a41b1da 362 if(change_in_x < 0.001 && change_in_x > -0.001) { //If the change in x value is very small
HenryWTriff 2:d08b6a1eaf2b 363 return true;
HenryWTriff 2:d08b6a1eaf2b 364 } else {
HenryWTriff 2:d08b6a1eaf2b 365 return false;
HenryWTriff 2:d08b6a1eaf2b 366 }
HenryWTriff 2:d08b6a1eaf2b 367 }
HenryWTriff 2:d08b6a1eaf2b 368
HenryWTriff 18:5fcb0514fb70 369 //-------------
HenryWTriff 3:ceed6d026b8b 370 // DRAW LINE
HenryWTriff 18:5fcb0514fb70 371 //-------------
HenryWTriff 2:d08b6a1eaf2b 372
HenryWTriff 2:d08b6a1eaf2b 373 void Graphics::Graphics_Draw_Line(Point_2D from, Point_2D to, bool solid, N5110 &LCD) //Draw a line between two points on only the portion of the screen
HenryWTriff 2:d08b6a1eaf2b 374 {
HenryWTriff 2:d08b6a1eaf2b 375
HenryWTriff 2:d08b6a1eaf2b 376 if( Gradient_Check_Infinate(from, to) == false ) { //Checking to see if the line is vertical
HenryWTriff 10:29126a41b1da 377 //Initialising variables
HenryWTriff 2:d08b6a1eaf2b 378 Point_2D plot_x_from = {0,0};
HenryWTriff 2:d08b6a1eaf2b 379 Point_2D plot_x_to = {0,0};
HenryWTriff 2:d08b6a1eaf2b 380 Point_2D plot_y_from = {0,0};
HenryWTriff 2:d08b6a1eaf2b 381 Point_2D plot_y_to = {0,0};
HenryWTriff 2:d08b6a1eaf2b 382
HenryWTriff 2:d08b6a1eaf2b 383 float gradient = Gradient(from, to); // Calculating the gradient
HenryWTriff 2:d08b6a1eaf2b 384 float y_intercept = (from.y - gradient * from.x); // Calulating the y intercept y - mx = c
HenryWTriff 2:d08b6a1eaf2b 385 float x_intercept = (from.x - from.y / gradient); // Calculating the x intercept x - y/m = d
HenryWTriff 2:d08b6a1eaf2b 386
HenryWTriff 2:d08b6a1eaf2b 387 if(gradient <= 1 && gradient >= -1) {
HenryWTriff 10:29126a41b1da 388 //Reordering 'from' and 'to' so that the for loops below can use ++
HenryWTriff 2:d08b6a1eaf2b 389 if(to.x < from.x) {
HenryWTriff 2:d08b6a1eaf2b 390 plot_x_from = to;
HenryWTriff 2:d08b6a1eaf2b 391 plot_x_to = from;
HenryWTriff 2:d08b6a1eaf2b 392 } else {
HenryWTriff 2:d08b6a1eaf2b 393 plot_x_from = from;
HenryWTriff 2:d08b6a1eaf2b 394 plot_x_to = to;
HenryWTriff 2:d08b6a1eaf2b 395 }
HenryWTriff 10:29126a41b1da 396 if(solid == true) { //If the line is solid or dotted
HenryWTriff 2:d08b6a1eaf2b 397 for(int x = Round(plot_x_from.x); x <= Round(plot_x_to.x); x++) { //Iterating through the x points
HenryWTriff 2:d08b6a1eaf2b 398 int y = -(Round((gradient * x) + y_intercept)); //Calculating the value of y for each x point
HenryWTriff 2:d08b6a1eaf2b 399 Point_2D plot_x = {x+42, y+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
HenryWTriff 10:29126a41b1da 400 if(plot_x.x <= 84 && plot_x.x >= 0 && plot_x.y <=48 && plot_x.y >= 0) { //Checking to see if the point is on the screen
HenryWTriff 2:d08b6a1eaf2b 401 LCD.setPixel(plot_x.x, plot_x.y, true); //Plotting the points
HenryWTriff 2:d08b6a1eaf2b 402 }
HenryWTriff 2:d08b6a1eaf2b 403 }
HenryWTriff 2:d08b6a1eaf2b 404 } else {
HenryWTriff 2:d08b6a1eaf2b 405 for(int x = Round(plot_x_from.x); x <= Round(plot_x_to.x); x+=2) { //Iterating through the x points
HenryWTriff 2:d08b6a1eaf2b 406 int y = -(Round((gradient * x) + y_intercept)); //Calculating the value of y for each x point
HenryWTriff 2:d08b6a1eaf2b 407 Point_2D plot_x = {x+42, y+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
HenryWTriff 10:29126a41b1da 408 if(plot_x.x <= 84 && plot_x.x >= 0 && plot_x.y <=48 && plot_x.y >= 0) { //Checking to see if the point is on the screen
HenryWTriff 2:d08b6a1eaf2b 409 LCD.setPixel(plot_x.x, plot_x.y, true); //Plotting the points
HenryWTriff 2:d08b6a1eaf2b 410 }
HenryWTriff 2:d08b6a1eaf2b 411 }
HenryWTriff 2:d08b6a1eaf2b 412 }
HenryWTriff 2:d08b6a1eaf2b 413 } else {
HenryWTriff 10:29126a41b1da 414 //Reordering 'from' and 'to' so that the for loops below can use ++
HenryWTriff 2:d08b6a1eaf2b 415 if(to.y < from.y) {
HenryWTriff 2:d08b6a1eaf2b 416 plot_y_from = to;
HenryWTriff 2:d08b6a1eaf2b 417 plot_y_to = from;
HenryWTriff 2:d08b6a1eaf2b 418 } else {
HenryWTriff 2:d08b6a1eaf2b 419 plot_y_from = from;
HenryWTriff 2:d08b6a1eaf2b 420 plot_y_to = to;
HenryWTriff 2:d08b6a1eaf2b 421 }
HenryWTriff 10:29126a41b1da 422 if(solid == true) { //If the line is solid or dotted
HenryWTriff 2:d08b6a1eaf2b 423 for(int y = Round(plot_y_from.y); y <= Round(plot_y_to.y); y++) { //Iterating through the Y points
HenryWTriff 2:d08b6a1eaf2b 424 int x = Round((y / gradient) + x_intercept); //Calculating the value of x for every y point
HenryWTriff 2:d08b6a1eaf2b 425 Point_2D plot_y = {x+42, (-y)+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
HenryWTriff 10:29126a41b1da 426 if(plot_y.x <= 84 && plot_y.x >= 0 && plot_y.y <=48 && plot_y.y >= 0) { //Checking to see if the point is on the screen
HenryWTriff 2:d08b6a1eaf2b 427 LCD.setPixel(plot_y.x, plot_y.y, true); //Plotting the points
HenryWTriff 2:d08b6a1eaf2b 428 }
HenryWTriff 2:d08b6a1eaf2b 429 }
HenryWTriff 2:d08b6a1eaf2b 430 } else {
HenryWTriff 2:d08b6a1eaf2b 431 for(int y = Round(plot_y_from.y); y <= Round(plot_y_to.y); y+=2) { //Iterating through the Y points
HenryWTriff 2:d08b6a1eaf2b 432 int x = Round((y / gradient) + x_intercept); //Calculating the value of x for every y point
HenryWTriff 2:d08b6a1eaf2b 433 Point_2D plot_y = {x+42, (-y)+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
HenryWTriff 10:29126a41b1da 434 if(plot_y.x <= 84 && plot_y.x >= 0 && plot_y.y <=48 && plot_y.y >= 0) { //Checking to see if the point is on the screen
HenryWTriff 2:d08b6a1eaf2b 435 LCD.setPixel(plot_y.x, plot_y.y, true); //Plotting the points
HenryWTriff 2:d08b6a1eaf2b 436 }
HenryWTriff 2:d08b6a1eaf2b 437 }
HenryWTriff 2:d08b6a1eaf2b 438 }
HenryWTriff 2:d08b6a1eaf2b 439 }
HenryWTriff 2:d08b6a1eaf2b 440
HenryWTriff 2:d08b6a1eaf2b 441 } else {
HenryWTriff 2:d08b6a1eaf2b 442 Point_2D plot_y_from = {0,0};
HenryWTriff 2:d08b6a1eaf2b 443 Point_2D plot_y_to = {0,0};
HenryWTriff 2:d08b6a1eaf2b 444 //Reordering from and to so that the for loops below can use ++
HenryWTriff 2:d08b6a1eaf2b 445 if(to.y < from.y) {
HenryWTriff 2:d08b6a1eaf2b 446 plot_y_from = to;
HenryWTriff 2:d08b6a1eaf2b 447 plot_y_to = from;
HenryWTriff 2:d08b6a1eaf2b 448 } else {
HenryWTriff 2:d08b6a1eaf2b 449 plot_y_from = from;
HenryWTriff 2:d08b6a1eaf2b 450 plot_y_to = to;
HenryWTriff 2:d08b6a1eaf2b 451 }
HenryWTriff 2:d08b6a1eaf2b 452 float x_intercept = from.x; //Calculating the x_intercept
HenryWTriff 10:29126a41b1da 453 if(solid == true) { //If the line is solid or dotted
HenryWTriff 2:d08b6a1eaf2b 454 for(int y = plot_y_from.y; y <= plot_y_to.y; y++) {
HenryWTriff 2:d08b6a1eaf2b 455 Point_2D plot_y = {x_intercept+42, (-y)+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
HenryWTriff 2:d08b6a1eaf2b 456 LCD.setPixel(plot_y.x, plot_y.y, true); //Plotting the points
HenryWTriff 2:d08b6a1eaf2b 457 }
HenryWTriff 2:d08b6a1eaf2b 458 } else {
HenryWTriff 2:d08b6a1eaf2b 459 for(int y = plot_y_from.y; y <= plot_y_to.y; y+=2) {
HenryWTriff 2:d08b6a1eaf2b 460 Point_2D plot_y = {x_intercept+42, (-y)+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
HenryWTriff 2:d08b6a1eaf2b 461 LCD.setPixel(plot_y.x, plot_y.y, true); //Plotting the points
HenryWTriff 2:d08b6a1eaf2b 462 }
HenryWTriff 2:d08b6a1eaf2b 463 }
HenryWTriff 2:d08b6a1eaf2b 464 }
HenryWTriff 3:ceed6d026b8b 465 }
HenryWTriff 3:ceed6d026b8b 466
HenryWTriff 18:5fcb0514fb70 467 //---------------
HenryWTriff 3:ceed6d026b8b 468 // DRAW SPRITE
HenryWTriff 18:5fcb0514fb70 469 //---------------
HenryWTriff 3:ceed6d026b8b 470
HenryWTriff 10:29126a41b1da 471 void Graphics::Graphics_Draw_Sprite(Point_2D point, int x_size, int y_size, int *sprite, N5110 &LCD) //Similar to N5110 drawSprite function but mine uses the origin as poin 0,0 and allows for transparent pixels
HenryWTriff 3:ceed6d026b8b 472 {
HenryWTriff 26:f1d3b7e31091 473 Point_2D zeroed_point = {Round(point.x) + 42 - (x_size / 2), Round(-point.y) + 36 - y_size};
HenryWTriff 26:f1d3b7e31091 474 for (int i = 0; i < y_size; i++) {
HenryWTriff 26:f1d3b7e31091 475 for (int j = 0 ; j < x_size ; j++) {
HenryWTriff 26:f1d3b7e31091 476 int pixel = *((sprite+i*x_size)+j);
HenryWTriff 26:f1d3b7e31091 477 if(pixel == 1 || pixel == 0) {
HenryWTriff 26:f1d3b7e31091 478 int x = zeroed_point.x+j;
HenryWTriff 26:f1d3b7e31091 479 int y = zeroed_point.y+i;
HenryWTriff 26:f1d3b7e31091 480 if(x <= 84 && x >= 0 && y <= 48 && y >= 0) { //If the point is on the screen
HenryWTriff 26:f1d3b7e31091 481 LCD.setPixel(x,y, pixel);
HenryWTriff 3:ceed6d026b8b 482 }
HenryWTriff 3:ceed6d026b8b 483 }
HenryWTriff 3:ceed6d026b8b 484 }
HenryWTriff 3:ceed6d026b8b 485 }
HenryWTriff 5:2d9f3c36bcb9 486 }
HenryWTriff 5:2d9f3c36bcb9 487
HenryWTriff 18:5fcb0514fb70 488 //-------------
HenryWTriff 5:2d9f3c36bcb9 489 // DRAW WALL
HenryWTriff 18:5fcb0514fb70 490 //-------------
HenryWTriff 5:2d9f3c36bcb9 491
HenryWTriff 5:2d9f3c36bcb9 492 void Graphics::Graphics_Draw_Wall(Point_2D from, Point_2D to, int height, N5110 &LCD) //Draw a line between two points on only the portion of the screen
HenryWTriff 5:2d9f3c36bcb9 493 {
HenryWTriff 5:2d9f3c36bcb9 494 if(Gradient_Check_Infinate(from, to) == 0) { //Checking to see if the line is vertical
HenryWTriff 10:29126a41b1da 495 //Declaring points
HenryWTriff 5:2d9f3c36bcb9 496 Point_2D plot_x_from = {0,0};
HenryWTriff 5:2d9f3c36bcb9 497 Point_2D plot_x_to = {0,0};
HenryWTriff 5:2d9f3c36bcb9 498 Point_2D plot_y_from = {0,0};
HenryWTriff 5:2d9f3c36bcb9 499 Point_2D plot_y_to = {0,0};
HenryWTriff 5:2d9f3c36bcb9 500
HenryWTriff 5:2d9f3c36bcb9 501 float gradient = Gradient(from, to); // Calculating the gradient
HenryWTriff 5:2d9f3c36bcb9 502 float y_intercept = (from.y - gradient * from.x); // Calulating the y intercept y - mx = c
HenryWTriff 5:2d9f3c36bcb9 503 float x_intercept = (from.x - from.y / gradient); // Calculating the x intercept x - y/m = d
HenryWTriff 5:2d9f3c36bcb9 504
HenryWTriff 5:2d9f3c36bcb9 505 if(gradient <= 1 && gradient >= -1) {
HenryWTriff 10:29126a41b1da 506 //Reordering 'from' and 'to' so that the for loops below can use ++
HenryWTriff 5:2d9f3c36bcb9 507 if(to.x < from.x) {
HenryWTriff 5:2d9f3c36bcb9 508 plot_x_from = to;
HenryWTriff 5:2d9f3c36bcb9 509 plot_x_to = from;
HenryWTriff 5:2d9f3c36bcb9 510 } else {
HenryWTriff 5:2d9f3c36bcb9 511 plot_x_from = from;
HenryWTriff 5:2d9f3c36bcb9 512 plot_x_to = to;
HenryWTriff 5:2d9f3c36bcb9 513 }
HenryWTriff 5:2d9f3c36bcb9 514 for(int x = Round(plot_x_from.x); x <= Round(plot_x_to.x); x++) { //Iterating through the x points
HenryWTriff 5:2d9f3c36bcb9 515 int y = -(Round((gradient * x) + y_intercept)); //Calculating the value of y for each x point
HenryWTriff 5:2d9f3c36bcb9 516 Point_2D plot_x = {x+42, y+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
HenryWTriff 10:29126a41b1da 517 if(plot_x.x <= 84 && plot_x.x >= 0 && plot_x.y <=48 && plot_x.y >= 0) { //Checking to see if the point is on the screen
HenryWTriff 10:29126a41b1da 518 for(int y_add = 0; y_add < height; y_add++) { //To differentiate it from a line, 'heigh' number of pixels above the line are also drawn. This gives the impression of height
HenryWTriff 10:29126a41b1da 519 LCD.setPixel(plot_x.x, plot_x.y - y_add, true); //Plotting the point
HenryWTriff 5:2d9f3c36bcb9 520 }
HenryWTriff 5:2d9f3c36bcb9 521 }
HenryWTriff 5:2d9f3c36bcb9 522 }
HenryWTriff 5:2d9f3c36bcb9 523 } else {
HenryWTriff 10:29126a41b1da 524 //Reordering 'from' and 'to' so that the for loops can use ++
HenryWTriff 5:2d9f3c36bcb9 525 if(to.y < from.y) {
HenryWTriff 5:2d9f3c36bcb9 526 plot_y_from = to;
HenryWTriff 5:2d9f3c36bcb9 527 plot_y_to = from;
HenryWTriff 5:2d9f3c36bcb9 528 } else {
HenryWTriff 5:2d9f3c36bcb9 529 plot_y_from = from;
HenryWTriff 5:2d9f3c36bcb9 530 plot_y_to = to;
HenryWTriff 5:2d9f3c36bcb9 531 }
HenryWTriff 5:2d9f3c36bcb9 532 for(int y = Round(plot_y_from.y); y <= Round(plot_y_to.y); y++) { //Iterating through the Y points
HenryWTriff 5:2d9f3c36bcb9 533 int x = Round((y / gradient) + x_intercept); //Calculating the value of x for every y point
HenryWTriff 5:2d9f3c36bcb9 534 Point_2D plot_y = {x+42, (-y)+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
HenryWTriff 10:29126a41b1da 535 if(plot_y.x <= 84 && plot_y.x >= 0 && plot_y.y <=48 && plot_y.y >= 0) { //Checking to see if the point is on the screen
HenryWTriff 10:29126a41b1da 536 for(int y_add = 0; y_add < height; y_add++) { //To differentiate it from a line, 'heigh' number of pixels above the line are also drawn. This gives the impression of height
HenryWTriff 10:29126a41b1da 537 LCD.setPixel(plot_y.x, plot_y.y - y_add, true); //Plotting the point
HenryWTriff 5:2d9f3c36bcb9 538 }
HenryWTriff 5:2d9f3c36bcb9 539 }
HenryWTriff 5:2d9f3c36bcb9 540 }
HenryWTriff 5:2d9f3c36bcb9 541 }
HenryWTriff 5:2d9f3c36bcb9 542
HenryWTriff 5:2d9f3c36bcb9 543 } else {
HenryWTriff 10:29126a41b1da 544 //Declaring and initialising points
HenryWTriff 5:2d9f3c36bcb9 545 Point_2D plot_y_from = {0,0};
HenryWTriff 5:2d9f3c36bcb9 546 Point_2D plot_y_to = {0,0};
HenryWTriff 5:2d9f3c36bcb9 547 //Reordering from and to so that the for loops below can use ++
HenryWTriff 5:2d9f3c36bcb9 548 if(to.y < from.y) {
HenryWTriff 5:2d9f3c36bcb9 549 plot_y_from = to;
HenryWTriff 5:2d9f3c36bcb9 550 plot_y_to = from;
HenryWTriff 5:2d9f3c36bcb9 551 } else {
HenryWTriff 5:2d9f3c36bcb9 552 plot_y_from = from;
HenryWTriff 5:2d9f3c36bcb9 553 plot_y_to = to;
HenryWTriff 5:2d9f3c36bcb9 554 }
HenryWTriff 5:2d9f3c36bcb9 555 float x_intercept = from.x; //Calculating the x_intercept
HenryWTriff 5:2d9f3c36bcb9 556 for(int y = plot_y_from.y; y <= plot_y_to.y; y++) {
HenryWTriff 5:2d9f3c36bcb9 557 Point_2D plot_y = {x_intercept+42, (-y)+36}; //Assigning them to a Plot_2D variable and transforming so the centre of the screen is 0,0
HenryWTriff 10:29126a41b1da 558 for(int y_add = 0; y_add < height; y_add++) { //To differentiate it from a line, 'heigh' number of pixels above the line are also drawn. This gives the impression of height
HenryWTriff 10:29126a41b1da 559 LCD.setPixel(plot_y.x, plot_y.y - y_add, true); //Plotting the point
HenryWTriff 5:2d9f3c36bcb9 560 }
HenryWTriff 5:2d9f3c36bcb9 561 }
HenryWTriff 5:2d9f3c36bcb9 562
HenryWTriff 5:2d9f3c36bcb9 563 }
HenryWTriff 6:5f76dd718dc3 564 }
HenryWTriff 6:5f76dd718dc3 565
HenryWTriff 18:5fcb0514fb70 566 //--------------------
HenryWTriff 6:5f76dd718dc3 567 // DRAW BOOST PLATE
HenryWTriff 18:5fcb0514fb70 568 //--------------------
HenryWTriff 6:5f76dd718dc3 569
HenryWTriff 10:29126a41b1da 570 void Graphics::Graphics_Draw_Boost_Plate(Triangle_2D boost_plate, Point_2D translation, int angle, float squish, N5110 &LCD) //Draws a boost plate on the track
HenryWTriff 6:5f76dd718dc3 571 {
HenryWTriff 10:29126a41b1da 572 //Using the given coordinates, calculates the points of all corners of the boost plate
HenryWTriff 6:5f76dd718dc3 573 Point_2D boost_plate_TL = boost_plate.TL;
HenryWTriff 6:5f76dd718dc3 574 Point_2D boost_plate_TR = {boost_plate.BR.x, boost_plate.TL.y};
HenryWTriff 6:5f76dd718dc3 575 Point_2D boost_plate_BL = {boost_plate.TL.x, boost_plate.BR.y};
HenryWTriff 6:5f76dd718dc3 576 Point_2D boost_plate_BR = boost_plate.BR;
HenryWTriff 10:29126a41b1da 577 Point_2D boost_plate_mid; //The coordinate of the point of the arrow of the boost plate
HenryWTriff 10:29126a41b1da 578 //The type defines the direction the plate is facing, e.g. Noth, South, East, West
HenryWTriff 6:5f76dd718dc3 579 if(boost_plate.Type == 1) {
HenryWTriff 10:29126a41b1da 580 //Calculates the coordinate of the point of the arrow drawn on the boost plate
HenryWTriff 6:5f76dd718dc3 581 boost_plate_mid.x = boost_plate_BR.x;
HenryWTriff 6:5f76dd718dc3 582 boost_plate_mid.y = (((boost_plate_TL.y - boost_plate_BR.y) / 2) + boost_plate_BR.y);
HenryWTriff 6:5f76dd718dc3 583 } else if(boost_plate.Type == 2) {
HenryWTriff 10:29126a41b1da 584 //Calculates the coordinate of the point of the arrow drawn on the boost plate
HenryWTriff 6:5f76dd718dc3 585 boost_plate_mid.x = (((boost_plate_BR.x - boost_plate_TL.x) / 2) + boost_plate_TL.x);
HenryWTriff 6:5f76dd718dc3 586 boost_plate_mid.y = boost_plate_BR.y;
HenryWTriff 6:5f76dd718dc3 587 } else if(boost_plate.Type == 3) {
HenryWTriff 10:29126a41b1da 588 //Calculates the coordinate of the point of the arrow drawn on the boost plate
HenryWTriff 6:5f76dd718dc3 589 boost_plate_mid.x = boost_plate_TL.x;
HenryWTriff 6:5f76dd718dc3 590 boost_plate_mid.y = (((boost_plate_TL.y - boost_plate_BR.y) / 2) + boost_plate_BR.y);
HenryWTriff 6:5f76dd718dc3 591 } else if(boost_plate.Type == 4) {
HenryWTriff 10:29126a41b1da 592 //Calculates the coordinate of the point of the arrow drawn on the boost plate
HenryWTriff 6:5f76dd718dc3 593 boost_plate_mid.x = (((boost_plate_BR.x - boost_plate_TL.x) / 2) + boost_plate_TL.x);
HenryWTriff 6:5f76dd718dc3 594 boost_plate_mid.y = boost_plate_TL.y;
HenryWTriff 6:5f76dd718dc3 595 }
HenryWTriff 6:5f76dd718dc3 596
HenryWTriff 6:5f76dd718dc3 597 //Translation
HenryWTriff 6:5f76dd718dc3 598 boost_plate_TL = Translate_Point(boost_plate_TL, Round(translation.x), Round(translation.y));
HenryWTriff 6:5f76dd718dc3 599 boost_plate_TR = Translate_Point(boost_plate_TR, Round(translation.x), Round(translation.y));
HenryWTriff 6:5f76dd718dc3 600 boost_plate_BL = Translate_Point(boost_plate_BL, Round(translation.x), Round(translation.y));
HenryWTriff 6:5f76dd718dc3 601 boost_plate_BR = Translate_Point(boost_plate_BR, Round(translation.x), Round(translation.y));
HenryWTriff 6:5f76dd718dc3 602 boost_plate_mid = Translate_Point(boost_plate_mid, Round(translation.x), Round(translation.y));
HenryWTriff 6:5f76dd718dc3 603
HenryWTriff 6:5f76dd718dc3 604 //Rotation
HenryWTriff 6:5f76dd718dc3 605 boost_plate_TL = Rotate_Point(boost_plate_TL, angle);
HenryWTriff 6:5f76dd718dc3 606 boost_plate_TR = Rotate_Point(boost_plate_TR, angle);
HenryWTriff 6:5f76dd718dc3 607 boost_plate_BL = Rotate_Point(boost_plate_BL, angle);
HenryWTriff 6:5f76dd718dc3 608 boost_plate_BR = Rotate_Point(boost_plate_BR, angle);
HenryWTriff 6:5f76dd718dc3 609 boost_plate_mid = Rotate_Point(boost_plate_mid, angle);
HenryWTriff 6:5f76dd718dc3 610
HenryWTriff 6:5f76dd718dc3 611 //Squish
HenryWTriff 6:5f76dd718dc3 612 boost_plate_TL = Squish_Point(boost_plate_TL, squish);
HenryWTriff 6:5f76dd718dc3 613 boost_plate_TR = Squish_Point(boost_plate_TR, squish);
HenryWTriff 6:5f76dd718dc3 614 boost_plate_BL = Squish_Point(boost_plate_BL, squish);
HenryWTriff 6:5f76dd718dc3 615 boost_plate_BR = Squish_Point(boost_plate_BR, squish);
HenryWTriff 6:5f76dd718dc3 616 boost_plate_mid = Squish_Point(boost_plate_mid, squish);
HenryWTriff 17:4c5f25d5c4d5 617
HenryWTriff 10:29126a41b1da 618 //Draws the square edge of the boost plate
HenryWTriff 6:5f76dd718dc3 619 Graphics_Draw_Line(boost_plate_TL, boost_plate_TR, true, LCD);
HenryWTriff 6:5f76dd718dc3 620 Graphics_Draw_Line(boost_plate_TR, boost_plate_BR, true, LCD);
HenryWTriff 6:5f76dd718dc3 621 Graphics_Draw_Line(boost_plate_BR, boost_plate_BL, true, LCD);
HenryWTriff 6:5f76dd718dc3 622 Graphics_Draw_Line(boost_plate_BL, boost_plate_TL, true, LCD);
HenryWTriff 17:4c5f25d5c4d5 623
HenryWTriff 10:29126a41b1da 624 //For each type of boost plate draw the arrow
HenryWTriff 6:5f76dd718dc3 625 if(boost_plate.Type == 1) {
HenryWTriff 6:5f76dd718dc3 626 Graphics_Draw_Line(boost_plate_TL, boost_plate_mid, true, LCD);
HenryWTriff 6:5f76dd718dc3 627 Graphics_Draw_Line(boost_plate_BL, boost_plate_mid, true, LCD);
HenryWTriff 6:5f76dd718dc3 628 } else if(boost_plate.Type == 2) {
HenryWTriff 6:5f76dd718dc3 629 Graphics_Draw_Line(boost_plate_TL, boost_plate_mid, true, LCD);
HenryWTriff 6:5f76dd718dc3 630 Graphics_Draw_Line(boost_plate_BL, boost_plate_mid, true, LCD);
HenryWTriff 6:5f76dd718dc3 631 } else if(boost_plate.Type == 3) {
HenryWTriff 6:5f76dd718dc3 632 Graphics_Draw_Line(boost_plate_TR, boost_plate_mid, true, LCD);
HenryWTriff 6:5f76dd718dc3 633 Graphics_Draw_Line(boost_plate_BR, boost_plate_mid, true, LCD);
HenryWTriff 6:5f76dd718dc3 634 } else if(boost_plate.Type == 4) {
HenryWTriff 6:5f76dd718dc3 635 Graphics_Draw_Line(boost_plate_TR, boost_plate_mid, true, LCD);
HenryWTriff 6:5f76dd718dc3 636 Graphics_Draw_Line(boost_plate_BR, boost_plate_mid, true, LCD);
HenryWTriff 6:5f76dd718dc3 637 }
HenryWTriff 6:5f76dd718dc3 638
HenryWTriff 25:31761087a83f 639 }