ELEC2645 (2018/19) / Mbed 2 deprecated el17cd

Dependencies:   mbed

Revision:
26:8a85aede976d
Parent:
25:3995271e411c
Child:
27:e46af658c67a
--- a/Rasturizer/Rasturizer.cpp	Wed Apr 03 20:03:47 2019 +0000
+++ b/Rasturizer/Rasturizer.cpp	Thu Apr 04 19:03:06 2019 +0000
@@ -1,7 +1,5 @@
 #include "Rasturizer.h"
 
-Serial pc(USBTX, USBRX); // tx, rx
-
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 
 Rasturizer::Rasturizer(){
@@ -34,8 +32,7 @@
 void Rasturizer::drawFace(Face *face, float angle){
     float points[4][3];
     float y, x;
-    int diffX1, diffY1, diffX2, diffY2;
-    float step, stepSmall1, stepSmall2;
+
     
     for(int vertex = 0; vertex < 4; vertex++){
         for(int axis = 0; axis < 3; axis++){
@@ -47,49 +44,63 @@
         points[vertex][0] = x*cos(-angle)-y*sin(-angle);
         points[vertex][1] = y*cos(-angle)+x*sin(-angle);
     }
-
+    
     if (checkOnScreen(points)){
-        diffX1 = xTo2D(points[0][0], points[0][2])-xTo2D(points[1][0], points[1][2]);
-        diffY1 = yTo2D(points[0][1], points[0][2])-yTo2D(points[1][1], points[1][2]);
-        diffX2 = xTo2D(points[2][0], points[2][2])-xTo2D(points[3][0], points[3][2]);
-        diffY2 = yTo2D(points[2][1], points[2][2])-yTo2D(points[3][1], points[3][2]);
-        if(diffX2 != 0 && face->getVisible()){
-            step = (float)diffY2/(float)diffX2;
-            stepSmall1 = (float)diffX1/(float)diffX2;
-            stepSmall2 = (float)diffY1/(float)diffX2;
-            
-            for(int s = 0; s< abs(diffX2); s++){
-                if(diffX2 > 0){
-                    lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])-stepSmall1*s),//rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42,
-                        rint(yTo2D(points[0][1], points[0][2])-stepSmall2*s),
-                        rint(xTo2D(points[3][0], points[3][2])+s), 
-                        rint(yTo2D(points[3][1], points[3][2])+step*s),
-                        0);
-                }
-                else{
-                    lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])+stepSmall1*s),//rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42,
-                        rint(yTo2D(points[0][1], points[0][2])+stepSmall2*s),
-                        rint(xTo2D(points[3][0], points[3][2])-s), 
-                        rint(yTo2D(points[3][1], points[3][2])-step*s),
-                        0);
-                }
-            }
-        }
-        for (int i = 0; i < 3; i++){     
-            lcd.drawLine(rint(xTo2D(points[i][0], points[i][2])),
-                rint(yTo2D(points[i][1], points[i][2])),
-                rint(xTo2D(points[i+1][0], points[i+1][2])),
-                rint(yTo2D(points[i+1][1], points[i+1][2])),
-                1);
-        }
-        lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])),
-            rint(yTo2D(points[0][1], points[0][2])),
-            rint(xTo2D(points[3][0], points[3][2])),
-            rint(yTo2D(points[3][1], points[3][2])),
-            1);
+        rasterizeFace(points, face);
+        drawFaceOutline(points);
+    }
+}
 
+void Rasturizer::rasterizeFace(float (&points)[4][3], Face *face){
+    int diffX1, diffY1, diffX2, diffY2;
+    float stepBottomY, stepTopX, stepTopY;
+    diffX1 = xTo2D(points[0][0], points[0][2])-xTo2D(points[1][0], points[1][2]); //Calculate difference between top horizontal edge x coordnates
+    diffY1 = yTo2D(points[0][1], points[0][2])-yTo2D(points[1][1], points[1][2]); //Calculate difference between right vertical edge y coordinates
+    diffX2 = xTo2D(points[2][0], points[2][2])-xTo2D(points[3][0], points[3][2]); //Calculate difference between bottom horizontal edge x coordinates
+    diffY2 = yTo2D(points[2][1], points[2][2])-yTo2D(points[3][1], points[3][2]); //Calculate difference between left horizontal edge y coordinates
+    if(diffX2 != 0 && face->getVisible()){
+        stepBottomY = (float)diffY2/(float)diffX2; //increment multiplier for Y axis on bottom edge of face
+        stepTopX = (float)diffX1/(float)diffX2; //increment multiplier for X axis on top edge of face
+        stepTopY = (float)diffY1/(float)diffX2; //increment multiplier for Y axis on top edge of face
+        
+        drawFillLines(points, diffX2, stepBottomY, stepTopX, stepTopY); //fill the face with white lines
     }
 }
+
+void Rasturizer::drawFillLines(float (&points)[4][3], int diffX2, int stepBottomY, int stepTopX, int stepTopY){
+    for(int step = 0; step< abs(diffX2); step++){
+        if(diffX2 > 0){
+            lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])-stepTopX*step),
+                rint(yTo2D(points[0][1], points[0][2])-stepTopY*step),
+                rint(xTo2D(points[3][0], points[3][2])+step), 
+                rint(yTo2D(points[3][1], points[3][2])+stepBottomY*step),
+                0);
+        }
+        else{
+            lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])+stepTopX*step),
+                rint(yTo2D(points[0][1], points[0][2])+stepTopY*step),
+                rint(xTo2D(points[3][0], points[3][2])-step), 
+                rint(yTo2D(points[3][1], points[3][2])-stepBottomY*step),
+                0);
+        }
+    }
+}
+
+void Rasturizer::drawFaceOutline(float(&points)[4][3]){
+    for (int i = 0; i < 3; i++){     
+        lcd.drawLine(rint(xTo2D(points[i][0], points[i][2])),
+            rint(yTo2D(points[i][1], points[i][2])),
+            rint(xTo2D(points[i+1][0], points[i+1][2])),
+            rint(yTo2D(points[i+1][1], points[i+1][2])),
+            1);
+    }
+    lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])),
+        rint(yTo2D(points[0][1], points[0][2])),
+        rint(xTo2D(points[3][0], points[3][2])),
+        rint(yTo2D(points[3][1], points[3][2])),
+        1);
+}
+
 void Rasturizer::drawAllFaces(Face *faceArray, int noOfCubes, float angle){
     Face temp;
     for (int f = 0; f< (noOfCubes*6)-1; f++){
@@ -139,6 +150,10 @@
     wait_ms(1000/60);
 }
 
+void Rasturizer::turnOff(){
+    lcd.turnOff();
+}
+
 void Rasturizer::drawDeathScreen(bool selection){
     Face faces[6];
     int x;
@@ -156,29 +171,21 @@
         z = 50;
     }
     
+    drawDeathButtons();
+    drawSelectionCube(x, y, z, 2);
+}
+
+void Rasturizer::drawDeathButtons(){
     lcd.drawRect(24, 14, 45, 11, FILL_WHITE);
     lcd.drawRect(24, 14, 45, 11, FILL_TRANSPARENT);
     lcd.printString("Restart",26,2);
     lcd.drawRect(24, 30, 45, 11, FILL_WHITE);
     lcd.drawRect(24, 30, 45, 11, FILL_TRANSPARENT);
     lcd.printString("Menu",35,4);
-    selectionCube.translate(x, y, z);
-    for(int i = 0; i < 6; i++){
-        faces[i] = selectionCube.getFace(i);
-    }
-    drawAllFaces(faces, 1, 0);
-    selectionCube.rotateX(0.1);
-    selectionCube.rotateY(0.05);
-    selectionCube.rotateZ(-0.08);
-    selectionCube.translate(-x, -y, -z);
 }
 
 void Rasturizer::drawHomeScreen(int selection){
-    Face faces[6];
-    int x;
-    int y;
-    int z;
-    
+    int x, y, z;
     if(selection == 0){
         x = -30;
         y = -12;
@@ -194,7 +201,11 @@
         y = 22;
         z = 50;
     }
-    
+    drawHomeButtons();
+    drawSelectionCube(x, y, z, -1);
+}
+
+void Rasturizer::drawHomeButtons(){
     lcd.drawRect(24, 6, 45, 10, FILL_WHITE);
     lcd.printString("Play",35,1);
     lcd.drawRect(24, 6, 45, 10, FILL_TRANSPARENT);
@@ -206,14 +217,46 @@
     lcd.drawRect(24, 38, 45, 10, FILL_WHITE);
     lcd.printString("Quit",35,5);
     lcd.drawRect(24, 38, 45, 10, FILL_TRANSPARENT);
+}
+
+void Rasturizer::drawSelectionCube(int x, int y, int z, int rotationSpeed){
+    Face faces[6];
     selectionCube.translate(x, y, z);
     
     for(int i = 0; i < 6; i++){
         faces[i] = selectionCube.getFace(i);
     }
     drawAllFaces(faces, 1, 0);
-    selectionCube.rotateX(-0.05);
-    selectionCube.rotateY(-0.025);
-    selectionCube.rotateZ(0.04);
+    selectionCube.rotateX(-0.05*rotationSpeed);
+    selectionCube.rotateY(-0.025*rotationSpeed);
+    selectionCube.rotateZ(0.04*rotationSpeed);
     selectionCube.translate(-x, -y, -z);
+}
+
+void Rasturizer::drawHelp(){
+    clear();
+    lcd.printString("Use the",17,1);
+    refresh();
+    wait(0.2);
+    lcd.printString("joystick",15,2);
+    refresh();
+    wait(0.2);
+    lcd.printString("to move",17,3);
+    refresh();
+    wait(0.2);
+    lcd.printString("left and right",0,4);
+    refresh();
+    wait(3);
+    clear();
+    refresh();
+    lcd.printString("Dodge all",15,1);
+    wait(0.2);
+    refresh();
+    lcd.printString("the cubes!",15,2);
+    refresh();
+    wait(1);
+    lcd.printString("Good Luck!",15,4);
+    refresh();
+    wait(3);
+    clear();
 }
\ No newline at end of file