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

Files at this revision

API Documentation at this revision

Comitter:
RichardE
Date:
Sat Jun 15 15:05:19 2013 +0000
Parent:
14:46a353b2a8e8
Child:
16:d0b142ba4362
Commit message:
Started adding code to represent a grid of rectangles which is used on high score entry screen. See FieldGrid, FieldRow and FieldCell classes and use in HighScoreEntry.cpp.

Changed in this revision

FieldCell.cpp Show annotated file Show diff for this revision Revisions of this file
FieldCell.h Show annotated file Show diff for this revision Revisions of this file
FieldGrid.cpp Show annotated file Show diff for this revision Revisions of this file
FieldGrid.h Show annotated file Show diff for this revision Revisions of this file
FieldRow.cpp Show annotated file Show diff for this revision Revisions of this file
FieldRow.h Show annotated file Show diff for this revision Revisions of this file
HighScoreEntry.cpp Show annotated file Show diff for this revision Revisions of this file
HighScoreEntry.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FieldCell.cpp	Sat Jun 15 15:05:19 2013 +0000
@@ -0,0 +1,24 @@
+/*
+ * SOURCE FILE : FieldCell.cpp
+ *
+ * Definition of class FieldCell.
+ *
+ */
+
+#include "FieldCell.h"
+
+/***************/
+/* CONSTRUCTOR */
+/***************/
+FieldCell::FieldCell() :
+    nextCell( (FieldCell*)NULL )
+{
+}
+
+/**************/
+/* DESTRUCTOR */
+/**************/
+FieldCell::~FieldCell() {
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FieldCell.h	Sat Jun 15 15:05:19 2013 +0000
@@ -0,0 +1,59 @@
+/*
+ * SOURCE FILE : FieldCell.h
+ *
+ * Definition of class FieldCell.
+ *
+ */
+
+#ifndef FieldCellDefined
+
+  #define FieldCellDefined
+
+  #include <stdlib.h>           // for NULL
+  #include "Rectangle.h"
+  
+  class FieldCell {
+
+  public :
+
+    // Rectangle which cell covers.
+    // Specified in pixel coordinates.
+    Rectangle Rect;
+    
+    /***************/
+    /* CONSTRUCTOR */
+    /***************/
+    FieldCell();
+
+    /**************/
+    /* DESTRUCTOR */
+    /**************/
+    virtual ~FieldCell();
+
+    /************************/
+    /* GET NEXT CELL IN ROW */
+    /************************/
+    FieldCell *GetNext( void ) const {
+        return nextCell;
+    }
+    
+    /************************/
+    /* SET NEXT CELL IN ROW */
+    /************************/
+    // Pass pointer to next cell in cell.
+    void SetNext( FieldCell *cell ) {
+        nextCell = cell;
+    }
+    
+  private :
+
+    // Pointer to next cell in row. NULL if last cell in row.
+    FieldCell *nextCell;
+      
+  };
+
+#endif
+
+/* END of FieldCell.h */
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FieldGrid.cpp	Sat Jun 15 15:05:19 2013 +0000
@@ -0,0 +1,41 @@
+/*
+ * SOURCE FILE : FieldGrid.cpp
+ *
+ * Definition of class FieldGrid.
+ * This is a grid of rectangles used for fields on a form or whatever.
+ *
+ */
+
+#include "FieldGrid.h"
+
+/***************/
+/* CONSTRUCTOR */
+/***************/
+// Pass number of rows in grid in rc.
+FieldGrid::FieldGrid( UInt8 rc ) :
+    rows( new FieldRow[ rc ] ),
+    rowCount( rc )
+{
+}
+
+/**************/
+/* DESTRUCTOR */
+/**************/
+FieldGrid::~FieldGrid() {
+    delete [] rows;
+}
+
+/*************/
+/* GET A ROW */
+/*************/
+// Pass row number in rowNum.
+// Returns pointer to row or NULL if no such row.
+FieldRow *FieldGrid::GetRow( UInt8 rowNum ) {
+    if( rowNum < rowCount ) {
+        return rows + rowNum;
+    }
+    else {
+        return (FieldRow*)NULL;
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FieldGrid.h	Sat Jun 15 15:05:19 2013 +0000
@@ -0,0 +1,61 @@
+/*
+ * SOURCE FILE : FieldGrid.h
+ *
+ * Definition of class FieldGrid.
+ * This is a grid of rectangles used for fields on a form or whatever.
+ *
+ */
+
+#ifndef FieldGridDefined
+
+  #define FieldGridDefined
+
+  #include <stdlib.h>           // for NULL
+  #include "Types.h"
+  #include "FieldRow.h"
+  
+  class FieldGrid {
+
+  public :
+
+    /***************/
+    /* CONSTRUCTOR */
+    /***************/
+    // Pass number of rows in grid in rc.
+    FieldGrid( UInt8 rc );
+
+    /**************/
+    /* DESTRUCTOR */
+    /**************/
+    virtual ~FieldGrid();
+
+    /*************/
+    /* GET A ROW */
+    /*************/
+    // Pass row number in rowNum.
+    // Returns pointer to row or NULL if no such row.
+    FieldRow *GetRow( UInt8 rowNum );
+
+    /*****************/
+    /* GET ROW COUNT */
+    /*****************/
+    // Returns number of rows.
+    UInt8 GetRowCount( void ) const {
+        return rowCount;
+    }
+
+  private :
+  
+    // Pointer to array of FieldRow objects, one for each row in the grid.
+    FieldRow *rows;
+    
+    // Number of rows.
+    UInt8 rowCount;
+    
+  };
+
+#endif
+
+/* END of FieldGrid.h */
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FieldRow.cpp	Sat Jun 15 15:05:19 2013 +0000
@@ -0,0 +1,50 @@
+/*
+ * SOURCE FILE : FieldRow.cpp
+ *
+ * Definition of class FieldRow.
+ *
+ */
+
+#include "FieldRow.h"
+
+/***************/
+/* CONSTRUCTOR */
+/***************/
+FieldRow::FieldRow() :
+    nextCell( (FieldCell*)NULL )
+{
+}
+
+/**************/
+/* DESTRUCTOR */
+/**************/
+FieldRow::~FieldRow()
+{
+    // Free all the cells in the row starting at the one after the root cell.
+    FieldCell *ptr = root.GetNext();
+    FieldCell *next;
+    while( ptr != (FieldCell*)NULL ) {
+        next = ptr->GetNext();
+        delete ptr;
+        ptr = next;
+    }
+}
+
+/*************************/
+/* ADD A CELL TO THE ROW */
+/*************************/
+// Pass cell to add in cell.
+// The cell being added must have been dynamically allocated!
+void FieldRow::AddCell( FieldCell *cell ) {
+    // Start at the root cell.
+    FieldCell *ptr = &root;
+    // Follow chain until you find a cell who has no next cell.
+    FieldCell *next;
+    while( ( next = ptr->GetNext() ) != (FieldCell*)NULL ) {
+        ptr = next;
+    }
+    // Make last cell point to new cell.
+    ptr->SetNext( cell );
+    // Make new cell point to nothing since it is the last cell.
+    cell->SetNext( (FieldCell*)NULL );
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FieldRow.h	Sat Jun 15 15:05:19 2013 +0000
@@ -0,0 +1,91 @@
+/*
+ * SOURCE FILE : FieldRow.h
+ *
+ * Definition of class FieldRow.
+ *
+ */
+
+#ifndef FieldRowDefined
+
+  #define FieldRowDefined
+
+  #include "FieldCell.h"
+  
+  class FieldRow {
+
+  public :
+
+    /***************/
+    /* CONSTRUCTOR */
+    /***************/
+    FieldRow();
+
+    /**************/
+    /* DESTRUCTOR */
+    /**************/
+    virtual ~FieldRow();
+
+    /*************************/
+    /* ADD A CELL TO THE ROW */
+    /*************************/
+    // Pass cell to add in cell.
+    // The cell being added must have been dynamically allocated!
+    void AddCell( FieldCell *cell );
+
+    /*************************/
+    /* GET FIRST CELL IN ROW */
+    /*************************/
+    // Returns pointer to first cell or NULL if no cells in row.
+    FieldCell *GetFirstCell( void ) {
+        FieldCell *result = root.GetNext();
+        if( result == (FieldCell*)NULL ) {
+            nextCell = (FieldCell*)NULL;
+        }
+        else {
+            nextCell = result->GetNext();
+        }
+        return result;
+    }
+    
+    /************************/
+    /* GET NEXT CELL IN ROW */
+    /************************/
+    // Returns pointer to NEXT cell or NULL if no MORE cells in row.
+    FieldCell *GetNextCell( void ) {
+        FieldCell *result = nextCell;
+        if( result != (FieldCell*)NULL ) {
+            nextCell = result->GetNext();
+        }
+        return result;
+    }
+    
+    /**********************************************/
+    /* GET CELL AT PARTICULAR POSITION IN THE ROW */
+    /**********************************************/
+    // Pass index of cell in pos.
+    // Returns pointer to cell or NULL if no cell at given index.
+    FieldCell *GetCellAt( UInt8 pos ) {
+        FieldCell *result = GetFirstCell();
+        while( ( pos > 0 ) && ( result != (FieldCell*)NULL ) ) {
+            result = GetNextCell();
+            pos--;
+        }
+        return result;
+    }
+    
+  private :
+  
+    // The root cell of the row.
+    // Does not contain any useful data and never gets deleted.
+    FieldCell root;
+    
+    // Pointer used when scanning through row cells.
+    FieldCell *nextCell;
+            
+  };
+
+#endif
+
+/* END of FieldRow.h */
+
+
--- a/HighScoreEntry.cpp	Tue Jun 11 21:04:48 2013 +0000
+++ b/HighScoreEntry.cpp	Sat Jun 15 15:05:19 2013 +0000
@@ -15,12 +15,48 @@
 #include "CharFrame.h"       // for drawing frames made of characters
 #include "CharCodes.h"       // character codes
 
+// Define this for debugging messages on serial port.
+#define CHATTY
+
+#ifdef CHATTY
+    extern Serial pc;
+#endif
+
+// Grid constants.
+#define GRIDX 5
+#define GRIDY 14
+#define GRIDCOLUMNS 20
+#define GRIDROWS 3
+#define GRIDCOLUMNSPACING 2
+#define GRIDROWSPACING 2
+
 /***************/
 /* CONSTRUCTOR */
 /***************/
 HighScoreEntry::HighScoreEntry() :
-  cursorPos( 0 )
+  cursorPos( 0 ),
+  grid( GRIDROWS )
 {
+  // Initialise grid around which cursor moves.
+  InitialiseGrid();
+  #ifdef CHATTY
+  for( UInt8 rowNum = 0; rowNum < grid.GetRowCount(); ++rowNum ) {
+    FieldRow *row = grid.GetRow( rowNum );
+    if( row != (FieldRow*)NULL ) {
+        FieldCell *cell = row->GetFirstCell();
+        UInt8 columnNum = 0;
+        while( cell != (FieldCell*)NULL ) {
+            pc.printf(
+                "Row %d column %d -> %d,%d,%d,%d\r\n",
+                (int)rowNum, (int)columnNum,
+                (int)cell->Rect.X1, (int)cell->Rect.Y1, (int)cell->Rect.X2, (int)cell->Rect.Y2
+            );
+            cell = row->GetNextCell();
+            columnNum++;
+        }
+    }
+  }
+  #endif
 }
 
 /**************/
@@ -29,6 +65,29 @@
 HighScoreEntry::~HighScoreEntry() {
 }
 
+/*******************/
+/* INITIALISE GRID */
+/*******************/
+void HighScoreEntry::InitialiseGrid( void ) {
+    UInt8 x, y = GRIDY;
+    for( UInt8 rowNumber = 0; rowNumber < GRIDROWS; ++rowNumber ) {
+        FieldRow *row = grid.GetRow( rowNumber );
+        if( row != (FieldRow*)NULL ) {
+            x = GRIDX;
+            for( UInt8 columnNumber = 0; columnNumber < GRIDCOLUMNS; ++columnNumber ) {
+                FieldCell *cell = new FieldCell();
+                cell->Rect.X1 = ( x - 1 ) << 3;
+                cell->Rect.Y1 = ( y - 1 ) << 3;
+                cell->Rect.X2 = ( ( x + 1 ) << 3 ) + 7;
+                cell->Rect.Y2 = ( ( y + 1 ) << 3 ) + 7;
+                row->AddCell( cell );
+                x += GRIDCOLUMNSPACING;
+            }
+        }
+        y += GRIDROWSPACING;
+    }
+}
+
 /*********************/
 /* GET A PLAYER NAME */
 /*********************/
@@ -151,14 +210,6 @@
   DrawGrid( gd );
 }
 
-// Grid constants.
-#define GRIDX 5
-#define GRIDY 14
-#define GRIDCOLUMNS 20
-#define GRIDROWS 3
-#define GRIDCOLUMNSPACING 2
-#define GRIDROWSPACING 2
-
 /***************************/
 /* DRAW THE CHARACTER GRID */
 /***************************/
--- a/HighScoreEntry.h	Tue Jun 11 21:04:48 2013 +0000
+++ b/HighScoreEntry.h	Sat Jun 15 15:05:19 2013 +0000
@@ -14,6 +14,7 @@
   #include "PanelControls.h"   // for reading panel controls.
   #include "PlayerName.h"
   #include "Gameduino.h"
+  #include "FieldGrid.h"       // grid around which cursor moves
   
   class HighScoreEntry {
 
@@ -42,6 +43,14 @@
     // Position of cursor (zero for first character).
     UInt8 cursorPos;
 
+    // Grid around which cursor moves.
+    FieldGrid grid;
+    
+    /*******************/
+    /* INITIALISE GRID */
+    /*******************/
+    void InitialiseGrid( void );
+    
     /*********************/
     /* WAIT FOR CONTROLS */
     /*********************/