Henry Triff
/
ELEC2645_Project_el18ht
Diff: Graphics/Graphics.cpp
- Revision:
- 5:2d9f3c36bcb9
- Parent:
- 4:9f41fc1c2ad1
- Child:
- 6:5f76dd718dc3
--- a/Graphics/Graphics.cpp Tue Feb 11 13:36:11 2020 +0000 +++ b/Graphics/Graphics.cpp Tue Feb 11 17:28:56 2020 +0000 @@ -30,12 +30,13 @@ // MAIN GRAPHICS FUNCTION //************************** -void Graphics::Draw_Map(int game_fps, Point_2D translation, int angle, float squish, Line_2D *Track_Lines, Line_2D *Track_Dotted_Lines, Point_2D *Track_Flags, Map_Data map_info, N5110 &LCD) +void Graphics::Draw_Map(Point_2D translation, int angle, float squish, Line_2D *Track_Lines, Line_2D *Track_Dotted_Lines, Line_2D *Track_Walls, Point_2D *Track_Flags, Map_Data map_info, N5110 &LCD) { Line_2D Track_Lines_Transformed[map_info.number_of_track_lines]; Line_2D Track_Dotted_Lines_Transformed[map_info.number_of_dotted_lines]; Point_2D Transformed_Flags[map_info.number_of_flags]; + Line_2D Transformed_Walls[map_info.number_of_walls]; for(int i = 0; i < map_info.number_of_track_lines; i++) { Track_Lines_Transformed[i] = Track_Lines[i]; @@ -73,7 +74,19 @@ Transformed_Flags[i] = Squish_Point(Transformed_Flags[i], squish); } - LCD.clear(); + for(int i = 0; i < map_info.number_of_walls; i++) { + Transformed_Walls[i] = Track_Walls[i]; + //Translation + Transformed_Walls[i].from = Translate_Point(Transformed_Walls[i].from, Round(translation.x), Round(translation.y)); + Transformed_Walls[i].to = Translate_Point(Transformed_Walls[i].to, Round(translation.x), Round(translation.y)); + //Rotation + Transformed_Walls[i].from = Rotate_Point(Transformed_Walls[i].from, angle); + Transformed_Walls[i].to = Rotate_Point(Transformed_Walls[i].to, angle); + //Squish + Transformed_Walls[i].from = Squish_Point(Transformed_Walls[i].from, squish); + Transformed_Walls[i].to = Squish_Point(Transformed_Walls[i].to, squish); + } + for(int i = 0; i < map_info.number_of_track_lines; i++) { Graphics_Draw_Line(Track_Lines_Transformed[i].from, Track_Lines_Transformed[i].to, true, LCD); } @@ -85,15 +98,15 @@ for(int i = 0; i < map_info.number_of_flags; i++) { Graphics_Draw_Sprite(Transformed_Flags[i], 8, 8, (int *)flag, LCD); } - + + for(int i = 0; i < map_info.number_of_walls; i++) { + Graphics_Draw_Wall(Transformed_Walls[i].from, Transformed_Walls[i].to,4, LCD); + } + Point_2D car_position = {4,0}; Graphics_Draw_Sprite(car_position,8,8,(int *)car, LCD); - - LCD.refresh(); - wait((1 / float(game_fps))); } - //******************* // SCREEN SETTINGS //******************* @@ -103,6 +116,24 @@ LCD.setContrast((0.35 + 0.2 * Device.read_pot1())); } +//******************** +// DRAW LAP COUNTER +//******************** + +void Graphics::Draw_Laps(int laps, N5110 &LCD) +{ + if(laps == 1) { + LCD.printString("1",0,5); + } else if (laps == 2) { + LCD.printString("2",0,5); + } else if (laps == 3) { + LCD.printString("3",0,5); + } else if (laps == 4) { + LCD.printString("4",0,5); + } else if (laps == 5) { + LCD.printString("5",0,5); + } +} //************** // TRANSFORM @@ -282,4 +313,79 @@ } } } +} + +//*************** +// DRAW WALL +//*************** + +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 +{ + if(Gradient_Check_Infinate(from, to) == 0) { //Checking to see if the line is vertical + Point_2D plot_x_from = {0,0}; + Point_2D plot_x_to = {0,0}; + Point_2D plot_y_from = {0,0}; + Point_2D plot_y_to = {0,0}; + + float gradient = Gradient(from, to); // Calculating the gradient + float y_intercept = (from.y - gradient * from.x); // Calulating the y intercept y - mx = c + float x_intercept = (from.x - from.y / gradient); // Calculating the x intercept x - y/m = d + + if(gradient <= 1 && gradient >= -1) { + //Reordering from and to so that the for loops below can use ++ + if(to.x < from.x) { + plot_x_from = to; + plot_x_to = from; + } else { + plot_x_from = from; + plot_x_to = to; + } + for(int x = Round(plot_x_from.x); x <= Round(plot_x_to.x); x++) { //Iterating through the x points + int y = -(Round((gradient * x) + y_intercept)); //Calculating the value of y for each x point + 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 + if(plot_x.x <= 84 && plot_x.x >= 0 && plot_x.y <=48 && plot_x.y >= 0) { + for(int y_add = 0; y_add < height; y_add++) { + LCD.setPixel(plot_x.x, plot_x.y - y_add, true); + } + } + } + } else { + if(to.y < from.y) { + plot_y_from = to; + plot_y_to = from; + } else { + plot_y_from = from; + plot_y_to = to; + } + for(int y = Round(plot_y_from.y); y <= Round(plot_y_to.y); y++) { //Iterating through the Y points + int x = Round((y / gradient) + x_intercept); //Calculating the value of x for every y point + 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 + if(plot_y.x <= 84 && plot_y.x >= 0 && plot_y.y <=48 && plot_y.y >= 0) { + for(int y_add = 0; y_add < height; y_add++) { + LCD.setPixel(plot_y.x, plot_y.y - y_add, true); + } + } + } + } + + } else { + Point_2D plot_y_from = {0,0}; + Point_2D plot_y_to = {0,0}; + //Reordering from and to so that the for loops below can use ++ + if(to.y < from.y) { + plot_y_from = to; + plot_y_to = from; + } else { + plot_y_from = from; + plot_y_to = to; + } + float x_intercept = from.x; //Calculating the x_intercept + for(int y = plot_y_from.y; y <= plot_y_to.y; y++) { + 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 + for(int y_add = 0; y_add < height; y_add++) { + LCD.setPixel(plot_y.x, plot_y.y - y_add, true); + } + } + + } } \ No newline at end of file