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

Committer:
RichardE
Date:
Sat Nov 24 17:18:47 2012 +0000
Revision:
2:fa0d977ce89f
Parent:
1:b5d8f8deafa5
Added GetCharacterCodes method.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RichardE 0:c7d5d58ef13a 1 /*
RichardE 0:c7d5d58ef13a 2 * SOURCE FILE : CharacterNet.cpp
RichardE 0:c7d5d58ef13a 3 *
RichardE 0:c7d5d58ef13a 4 */
RichardE 0:c7d5d58ef13a 5
RichardE 0:c7d5d58ef13a 6 #include "CharacterNet.h"
RichardE 0:c7d5d58ef13a 7
RichardE 0:c7d5d58ef13a 8 // An instance of this class.
RichardE 0:c7d5d58ef13a 9 CharacterNet CharacterNet::instance;
RichardE 0:c7d5d58ef13a 10
RichardE 0:c7d5d58ef13a 11 // Default character codes to use.
RichardE 0:c7d5d58ef13a 12 const UInt8 CharacterNet::defaultCodes[ CodePosCount ] = {
RichardE 0:c7d5d58ef13a 13 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158,
RichardE 0:c7d5d58ef13a 14 };
RichardE 0:c7d5d58ef13a 15
RichardE 0:c7d5d58ef13a 16 /** Constructor.
RichardE 0:c7d5d58ef13a 17 */
RichardE 0:c7d5d58ef13a 18 CharacterNet::CharacterNet() :
RichardE 0:c7d5d58ef13a 19 characterCodes( defaultCodes )
RichardE 0:c7d5d58ef13a 20 {
RichardE 0:c7d5d58ef13a 21 }
RichardE 0:c7d5d58ef13a 22
RichardE 0:c7d5d58ef13a 23 /** Draw a net of characters.
RichardE 0:c7d5d58ef13a 24 * @param gd The Gameduino to draw on.
RichardE 0:c7d5d58ef13a 25 * @param xco X coordinate for top left.
RichardE 0:c7d5d58ef13a 26 * @param yco Y coordinate for top left.
RichardE 0:c7d5d58ef13a 27 * @param columnCount The number of columns to draw.
RichardE 0:c7d5d58ef13a 28 * @param rowCount The number of rows to draw.
RichardE 0:c7d5d58ef13a 29 * @param cellWidth The width of each cell in characters.
RichardE 0:c7d5d58ef13a 30 * @param cellHeight The height of each cell in characters.
RichardE 0:c7d5d58ef13a 31 */
RichardE 0:c7d5d58ef13a 32 void CharacterNet::Draw( Gameduino *gd, UInt8 xco, UInt8 yco, UInt8 columnCount, UInt8 rowCount, UInt8 cellWidth, UInt8 cellHeight ) {
RichardE 0:c7d5d58ef13a 33 UInt8 x = xco;
RichardE 0:c7d5d58ef13a 34 for( UInt8 column = 0; column < columnCount; ++column ) {
RichardE 0:c7d5d58ef13a 35 UInt8 y = yco;
RichardE 0:c7d5d58ef13a 36 for( UInt8 row = 0; row < rowCount; ++row ) {
RichardE 1:b5d8f8deafa5 37 DrawCell( gd, x, y, cellWidth, cellHeight, ( row == 0 ), ( column == 0 ), ( row == rowCount - 1 ), ( column == columnCount - 1 ) );
RichardE 0:c7d5d58ef13a 38 y += cellHeight;
RichardE 0:c7d5d58ef13a 39 }
RichardE 0:c7d5d58ef13a 40 x += cellWidth;
RichardE 0:c7d5d58ef13a 41 }
RichardE 0:c7d5d58ef13a 42 }
RichardE 0:c7d5d58ef13a 43
RichardE 0:c7d5d58ef13a 44 /** Draw a single cell.
RichardE 0:c7d5d58ef13a 45 * Note that only the upper and left hand edges are drawn.
RichardE 0:c7d5d58ef13a 46 * @param gd Gameduino to draw on.
RichardE 0:c7d5d58ef13a 47 * @param xco X coordinate for top left.
RichardE 0:c7d5d58ef13a 48 * @param yco Y coordinate for top left.
RichardE 0:c7d5d58ef13a 49 * @param cellWidth Width of cell in characters.
RichardE 0:c7d5d58ef13a 50 * @param cellHeight Height of cell in characters.
RichardE 0:c7d5d58ef13a 51 * @param topRow Set to true if this is a cell in the top row of the net.
RichardE 0:c7d5d58ef13a 52 * @param leftColumn Set to true if this is a cell in the leftmost column of the net.
RichardE 1:b5d8f8deafa5 53 * @param bottomRow Set to true if this is a cell in the bottom row of the net.
RichardE 1:b5d8f8deafa5 54 * @param rightColumn Set to true if this is a cell in the rightmost column of the net.
RichardE 0:c7d5d58ef13a 55 */
RichardE 1:b5d8f8deafa5 56 void CharacterNet::DrawCell( Gameduino *gd, UInt8 xco, UInt8 yco, UInt8 cellWidth, UInt8 cellHeight, bool topRow, bool leftColumn, bool bottomRow, bool rightColumn ) {
RichardE 0:c7d5d58ef13a 57 // Draw character at top left corner of cell.
RichardE 1:b5d8f8deafa5 58 char c;
RichardE 0:c7d5d58ef13a 59 if( topRow ) {
RichardE 0:c7d5d58ef13a 60 if( leftColumn ) {
RichardE 1:b5d8f8deafa5 61 c = characterCodes[ TopLeftCorner ];
RichardE 0:c7d5d58ef13a 62 }
RichardE 0:c7d5d58ef13a 63 else {
RichardE 1:b5d8f8deafa5 64 c = characterCodes[ TDown ];
RichardE 0:c7d5d58ef13a 65 }
RichardE 0:c7d5d58ef13a 66 }
RichardE 0:c7d5d58ef13a 67 else if( leftColumn ) {
RichardE 1:b5d8f8deafa5 68 c = characterCodes[ TRight ];
RichardE 0:c7d5d58ef13a 69 }
RichardE 0:c7d5d58ef13a 70 else {
RichardE 1:b5d8f8deafa5 71 c = characterCodes[ Cross ];
RichardE 1:b5d8f8deafa5 72 }
RichardE 1:b5d8f8deafa5 73 gd->putchar( xco, yco, c );
RichardE 1:b5d8f8deafa5 74 // If in the rightmost column then draw character at top right of cell.
RichardE 1:b5d8f8deafa5 75 if( rightColumn ) {
RichardE 1:b5d8f8deafa5 76 c = ( topRow ? characterCodes[ TopRightCorner ] : characterCodes[ TLeft ] );
RichardE 1:b5d8f8deafa5 77 gd->putchar( xco + cellWidth, yco, c );
RichardE 1:b5d8f8deafa5 78 }
RichardE 1:b5d8f8deafa5 79 // If in the bottom row then draw character at bottom left of cell.
RichardE 1:b5d8f8deafa5 80 if( bottomRow ) {
RichardE 1:b5d8f8deafa5 81 c = ( leftColumn ? characterCodes[ BottomLeftCorner ] : characterCodes[ TUp ] );
RichardE 1:b5d8f8deafa5 82 gd->putchar( xco, yco + cellHeight, c );
RichardE 1:b5d8f8deafa5 83 }
RichardE 1:b5d8f8deafa5 84 // If in the rightmost column and the bottom row then draw character at bottom right of cell.
RichardE 1:b5d8f8deafa5 85 if( rightColumn && bottomRow ) {
RichardE 1:b5d8f8deafa5 86 gd->putchar( xco + cellWidth, yco + cellHeight, characterCodes[ BottomRightCorner ] );
RichardE 0:c7d5d58ef13a 87 }
RichardE 1:b5d8f8deafa5 88 // Draw top edge.
RichardE 1:b5d8f8deafa5 89 c = characterCodes[ Horizontal ];
RichardE 1:b5d8f8deafa5 90 for( UInt8 x = xco + 1; x < xco + cellWidth; ++x ) {
RichardE 1:b5d8f8deafa5 91 gd->putchar( x, yco, c );
RichardE 1:b5d8f8deafa5 92 }
RichardE 1:b5d8f8deafa5 93 // If in bottom row then draw bottom edge.
RichardE 1:b5d8f8deafa5 94 if( bottomRow ) {
RichardE 1:b5d8f8deafa5 95 for( UInt8 x = xco + 1; x < xco + cellWidth; ++x ) {
RichardE 1:b5d8f8deafa5 96 gd->putchar( x, yco + cellHeight, c );
RichardE 1:b5d8f8deafa5 97 }
RichardE 1:b5d8f8deafa5 98 }
RichardE 1:b5d8f8deafa5 99 // Draw left edge.
RichardE 1:b5d8f8deafa5 100 c = characterCodes[ Vertical ];
RichardE 1:b5d8f8deafa5 101 for( UInt8 y = yco + 1; y < yco + cellHeight; ++y ) {
RichardE 1:b5d8f8deafa5 102 gd->putchar( xco, y, c );
RichardE 1:b5d8f8deafa5 103 }
RichardE 1:b5d8f8deafa5 104 // If in rightmost column then draw right edge.
RichardE 1:b5d8f8deafa5 105 if( rightColumn ) {
RichardE 1:b5d8f8deafa5 106 for( UInt8 y = yco + 1; y < yco + cellHeight; ++y ) {
RichardE 1:b5d8f8deafa5 107 gd->putchar( xco + cellWidth, y, c );
RichardE 1:b5d8f8deafa5 108 }
RichardE 1:b5d8f8deafa5 109 }
RichardE 0:c7d5d58ef13a 110 }