Draws grids using frames of characters on a Gameduino display. Requires Gameduino library.

CharacterNet.cpp

Committer:
RichardE
Date:
2012-11-21
Revision:
0:c7d5d58ef13a
Child:
1:b5d8f8deafa5

File content as of revision 0:c7d5d58ef13a:

/*
 * SOURCE FILE : CharacterNet.cpp
 *
 */
 
 #include "CharacterNet.h"
 
// An instance of this class.
CharacterNet CharacterNet::instance;

// Default character codes to use.
const UInt8 CharacterNet::defaultCodes[ CodePosCount ] = {
    148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158,
};
        
/** Constructor.
 */
CharacterNet::CharacterNet() :
    characterCodes( defaultCodes )
{
}

/** Draw a net of characters.
 * @param gd The Gameduino to draw on.
 * @param xco X coordinate for top left.
 * @param yco Y coordinate for top left.
 * @param columnCount The number of columns to draw.
 * @param rowCount The number of rows to draw.
 * @param cellWidth The width of each cell in characters.
 * @param cellHeight The height of each cell in characters.
 */
void CharacterNet::Draw( Gameduino *gd, UInt8 xco, UInt8 yco, UInt8 columnCount, UInt8 rowCount, UInt8 cellWidth, UInt8 cellHeight ) {
    UInt8 x = xco;
    for( UInt8 column = 0; column < columnCount; ++column ) {
        UInt8 y = yco;
        for( UInt8 row = 0; row < rowCount; ++row ) {
            DrawCell( gd, x, y, cellWidth, cellHeight, ( row == 0 ), ( column == 0 ) );
            y += cellHeight;
        }
        x += cellWidth;
    }
}

/** Draw a single cell.
 * Note that only the upper and left hand edges are drawn.
 * @param gd Gameduino to draw on.
 * @param xco X coordinate for top left.
 * @param yco Y coordinate for top left.
 * @param cellWidth Width of cell in characters.
 * @param cellHeight Height of cell in characters.
 * @param topRow Set to true if this is a cell in the top row of the net.
 * @param leftColumn Set to true if this is a cell in the leftmost column of the net.
 */
void CharacterNet::DrawCell( Gameduino *gd, UInt8 xco, UInt8 yco, UInt8 cellWidth, UInt8 cellHeight, bool topRow, bool leftColumn ) {
    // Draw character at top left corner of cell.
    char text[] = "X";
    if( topRow ) {
        if( leftColumn ) {
            text[ 0 ] = characterCodes[ TopLeftCorner ];
        }
        else {
            text[ 0 ] = characterCodes[ TDown ];
        }
    }
    else if( leftColumn ) {
        text[ 0 ] = characterCodes[ TRight ];
    }
    else {
        text[ 0 ] = characterCodes[ Cross ];
    }
    gd->putstr( xco, yco, text );
}