Version of Robotron arcade game using LPC1768, a Gameduino shield, a serial EEPROM (for high scores), two microswitch joysticks and two buttons plus a box to put it in. 20 levels of mayhem.

Dependencies:   25LCxxx_SPI CommonTypes Gameduino mbed

Revision:
17:194789db2215
Parent:
16:d0b142ba4362
Child:
18:70190f956a24
diff -r d0b142ba4362 -r 194789db2215 HighScoreEntry.cpp
--- a/HighScoreEntry.cpp	Sat Jun 15 19:27:36 2013 +0000
+++ b/HighScoreEntry.cpp	Sun Jun 16 08:54:57 2013 +0000
@@ -27,10 +27,14 @@
 #define GRIDX 5
 #define GRIDY 16
 #define GRIDCOLUMNS 20
-#define GRIDROWS 3
+#define GRIDROWS 4                  // 3 rows for characters, 1 row for special functions
 #define GRIDCOLUMNSPACING 2
 #define GRIDROWSPACING 2
 
+// Some text.
+const char HighScoreEntry::textDelete[] = "DELETE";
+const char HighScoreEntry::textEnter[] = "ENTER";
+
 /***************/
 /* CONSTRUCTOR */
 /***************/
@@ -72,6 +76,7 @@
 /* INITIALISE GRID */
 /*******************/
 void HighScoreEntry::InitialiseGrid( void ) {
+    // First initialise rows used for characters.
     UInt8 code = PlayerName::MinChar;
     UInt8 x = GRIDX, y = GRIDY;
     UInt8 columnNumber = 0, rowNumber = 0;;
@@ -97,8 +102,34 @@
         }
         code++;
     }
+    // Add another row for special functions.
+    columnNumber = 0;
+    x = GRIDX;
+    y += GRIDROWSPACING;
+    row = grid.GetRow( rowNumber++ );
+    if( row != (FieldRow*)NULL ) {
+        AddTextCell( x, y, row, textDelete );
+        AddTextCell( x, y, row, textEnter );
+    }
 }
 
+/***************************************/
+/* ADD A CELL CONTAINING TEXT TO A ROW */
+/***************************************/
+// Pass character coordinates in x and y. These will be updated.
+// Pass row to which cells should be added in row.
+// Pass text which is to be contained in cell in text.
+void HighScoreEntry::AddTextCell( UInt8 &x, UInt8 &y, FieldRow *row, const char *text ) {
+    int len = strlen( text );
+    FieldCell *cell = new FieldCell();
+    cell->Rect.X1 = x - 1;
+    cell->Rect.Y1 = y - 1;
+    cell->Rect.X2 = x + len;
+    cell->Rect.Y2 = y + 1;
+    row->AddCell( cell );
+    x += ( len + 1 );
+ }
+
 /*********************/
 /* GET A PLAYER NAME */
 /*********************/
@@ -161,7 +192,7 @@
       }
       if( inputs & PanelControls::Button1 ) {
           // Button 1 adds a character or possibly does something special.
-          if( cursorRow < grid.GetRowCount() ) {
+          if( cursorRow < grid.GetRowCount() - 1 ) {
               char newChar = (char)( PlayerName::MinChar + cursorRow * GRIDCOLUMNS + cursorColumn );
               name->Name[ charIndex ] = newChar;
               if( charIndex < PlayerName::Length - 1 ) {
@@ -169,7 +200,8 @@
               }
           }
           else {
-              // TODO. Special functions.
+              // Last row contains special functions.
+              SpecialFunction( gd, cursorColumn, name, done );
           }
           // Draw modified name.
           gd->waitvblank();
@@ -195,6 +227,29 @@
   WaitControls( gd, controls, false );
 }
 
+/************************************************************************/
+/* PERFORM A SPECIAL FUNCTION TRIGGERED FROM A CELL ON LAST ROW OF GRID */
+/************************************************************************/
+// Pass pointer to Gameduino to draw on in gd.
+// Pass function number in funcNum (this is the cursor column number).
+// Pass player name in name.
+// Pass reference to done flag in done.
+void HighScoreEntry::SpecialFunction( Gameduino *gd, UInt8 funcNum, PlayerName *name, bool &done ) {
+    switch( funcNum ) {
+    
+    case 0 :        // DELETE function
+        if( charIndex > 0 ) {
+            name->Name[ charIndex-- ] = ' ';
+        }
+        break;
+        
+    case 1 :        // ENTER function
+        done = true;
+        break;
+        
+    }
+}
+
 /*********************/
 /* WAIT FOR CONTROLS */
 /*********************/
@@ -236,8 +291,8 @@
   gd->putstr( 2, 2,  "   CONGRATULATIONS : YOU HAVE A HIGH SCORE!" );
   gd->putstr( 2, 4,  "PLEASE ENTER YOUR NAME USING THE LEFT JOYSTICK" );
   gd->putstr( 2, 6,  " TO MOVE THE CURSOR. PRESS THE LEFT BUTTON TO" );
-  gd->putstr( 2, 8,  "  ENTER EACH LETTER. MOVE CURSOR TO FOOT OF" );
-  gd->putstr( 2, 10, " SCREEN AND PRESS LEFT BUTTON WHEN FINISHED." );
+  gd->putstr( 2, 8,  "  ENTER EACH LETTER. MOVE CURSOR TO \"ENTER\"" );
+  gd->putstr( 2, 10, "     AND PRESS LEFT BUTTON WHEN FINISHED." );
   // Draw player's name.
   DrawName( gd, name );
   // Draw character grid.
@@ -251,6 +306,7 @@
 /***************************/
 // Pass pointer to Gameduino to draw on in gd.
 void HighScoreEntry::DrawGrid( Gameduino *gd ) {
+    // Draw rows containing characters.
     UInt8 code = PlayerName::MinChar;
     UInt8 x = GRIDX, y = GRIDY;
     UInt8 columnNumber = 0;
@@ -268,6 +324,12 @@
             x += GRIDCOLUMNSPACING;
         }
     }
+    // Draw row containing special functions.
+    x = GRIDX;
+    y += GRIDROWSPACING;
+    gd->putstr( x, y, textDelete );
+    x += ( strlen( textDelete ) + 1 );
+    gd->putstr( x, y, textEnter );
 }
 
 /********************************/