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

HighScoreEntry.cpp

Committer:
RichardE
Date:
2013-06-11
Revision:
14:46a353b2a8e8
Parent:
4:673eb9735d44
Child:
15:d8ea0c7b7e64

File content as of revision 14:46a353b2a8e8:

/*
 * SOURCE FILE : HighScoreEntry.cpp
 *
 * Definition of class HighScoreEntry.
 * Routine to allow player to enter their name using joysticks.
 *
 */

#include "HighScoreEntry.h"
#include "Gameduino.h"       // Gameduino stuff
#include "GDExtra.h"         // more Gameduino stuff
#include "GDConst.h"         // Gameduino constants
#include "FrameCounter.h"    // counter updated every vertical flyback
#include "CharBlocks.h"      // blocks of characters in program memory
#include "CharFrame.h"       // for drawing frames made of characters
#include "CharCodes.h"       // character codes

/***************/
/* CONSTRUCTOR */
/***************/
HighScoreEntry::HighScoreEntry() :
  cursorPos( 0 )
{
}

/**************/
/* DESTRUCTOR */
/**************/
HighScoreEntry::~HighScoreEntry() {
}

/*********************/
/* GET A PLAYER NAME */
/*********************/
// Pass pointer to place to store name in name.
// Pass pointer to controls to read in controls.
// Pass pointer to Gameduino to display on in gd.
void HighScoreEntry::GetName( PlayerName *name, PanelControls *controls, Gameduino *gd ) {
  UInt16 inputs;
  UInt8 countdown = 0;
  char *curPtr;
  // Initialise name to all 'A' characters.
  for( UInt8 i = 0; i < PlayerName::Length; ++i ) {
    name->Name[ i ] = 'A';
  }
  // Draw screen.
  DrawScreen( gd );
  // Wait until player releases all controls.
  WaitControls( gd, controls, false );
  // Start at leftmost character.
  cursorPos = 0;
  // Loop until cursor moves beyond rightmost character.
  while( cursorPos < PlayerName::Length ) {
    // Read panel controls.
    controls->Read();
    inputs = controls->GetInputs();
    // Point to character under cursor.
    curPtr = name->Name + cursorPos;
    // Only react to controls every so often to slow things down.
    if( countdown == 0 ) {
      countdown = 10;
      if( inputs & PanelControls::Up1 ) {
        // Joystick up selects next character up.
        if( *curPtr >= PlayerName::MaxChar ) {
          *curPtr = PlayerName::MinChar;
        }
        else {
          (*curPtr)++;
        }
      }
      else if( inputs & PanelControls::Down1 ) {
        // Joystick down selects previous character down.
        if( *curPtr <= PlayerName::MinChar ) {
          *curPtr = PlayerName::MaxChar;
        }
        else {
          (*curPtr)--;
        }
      }
      else if( inputs & PanelControls::Left1 ) {
        // Joystick left moves cursor back.
        if( cursorPos > 0 ) {
          cursorPos--;
          // Wait until all controls released.
          WaitControls( gd, controls, false );
        }
      }
      else if( inputs & PanelControls::Right1 ) {
        // Joystick right moves cursor forwards.
        cursorPos++;
        // Wait until all controls released.
        WaitControls( gd, controls, false );
      }
    }
    else {
      countdown--;
    }
    // Wait for vertical flyback. Then draw name and do animation.
    gd->waitvblank();
    FrameCounter++;
    DrawName( gd, name );
    Animate( gd );
  }
  // Wait until player releases all controls before returning.
  WaitControls( gd, controls, false );
}

/*********************/
/* WAIT FOR CONTROLS */
/*********************/
// Pass pointer to Gameduino to display on in gd.
// Pass pointer to controls to read in controls.
// Pass true in waitActivate to wait for a control to be used.
// Pass false to wait for release.
void HighScoreEntry::WaitControls( Gameduino *gd, PanelControls *controls, bool waitActivate ) {
  bool released = false;
  UInt16 inputs;
  while( ! released ) {
    controls->Read();
    inputs = controls->GetInputs();
    released = ( waitActivate ? ( inputs != 0 ) : ( inputs == 0 ) );
    if( ! released ) {
      gd->waitvblank();
      FrameCounter++;
      Animate( gd );
    }
  }
}

/*******************/
/* DRAW THE SCREEN */
/*******************/
// Pass pointer to Gameduino to draw on in gd.
void HighScoreEntry::DrawScreen( Gameduino *gd ) {
  gd->waitvblank();
  // Clear the screen to zero characters.
  gd->fill( Gameduino::RAM_PIC, 0, RAM_PIC_SIZE );
  // Turn off all the sprites.
  for( UInt16 s = 0; s < SPRITE_COUNT; ++s ) {
    gd->sprite( s, 0, 400, 0, 0 );
  }
  // Draw border around screen.
  CharFrame::Draw( gd, 0, 0, VISIBLE_CHAR_WIDTH, VISIBLE_CHAR_HEIGHT );
  // Draw instructions.
  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." );
  // Draw character grid.
  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 */
/***************************/
// Pass pointer to Gameduino to draw on in gd.
void HighScoreEntry::DrawGrid( Gameduino *gd ) {
    UInt8 code = PlayerName::MinChar;
    UInt8 x = GRIDX, y = GRIDY;
    UInt8 columnNumber = 0;
    char str [] = "X";
    while( code <= PlayerName::MaxChar ) {
        str[ 0 ] = (char)code++;
        gd->putstr( x, y, str );
        columnNumber++;
        if( columnNumber >= GRIDCOLUMNS ) {
            columnNumber = 0;
            x = GRIDX;
            y += GRIDROWSPACING;
        }
        else {
            x += GRIDCOLUMNSPACING;
        }
    }
}

/********************************/
/* DRAW THE NAME AND THE CURSOR */
/********************************/
// Pass pointer to Gameduino to draw on in gd.
// Pass player name in name.
void HighScoreEntry::DrawName( Gameduino *gd, PlayerName *name ) {
  gd->putstr( 21, 11, name->Name );
  UInt16 address = Gameduino::RAM_PIC + 12 * SCREEN_CHAR_WIDTH + 21;
  for( UInt8 i = 0; i < PlayerName::Length; ++i ) {
    gd->wr( address, ( i == cursorPos ) ? ArrowUp : ' ' );
    address++;
  }
}

/********************/
/* UPDATE ANIMATION */
/********************/
// Pass pointer to Gameduino to display on in gd.
void HighScoreEntry::Animate( Gameduino *gd ) {
}