GEOS SDK TechDocs
|
|
5.3 The Structure of TicTac
|
5.5 TicTacPiece Specifics
The TicTacBoard object, being of a subclass of
VisContentClass
, handles many messages specific to content objects. However, only three messages are handled specifically by
TicTacBoardClass
. These messages are
MSG_VIS_DRAW
MSG_VIS_DRAW
on to all of its children.
MSG_TICTAC_NEW_GAME
MSG_PIECE_NEW_GAME
, to each of its children. Each child will position itself and draw itself properly; TicTacBoard will then redraw the game board by sending itself a
MSG_VIS_DRAW
.
MSG_TICTAC_VALIDATE_BOUNDSEach of the methods for the above messages is shown in Methods of TicTacBoardClass . Each is heavily commented and explains the theory behind the method.
Code Display 6-5 Methods of TicTacBoardClass
/*********************************************************************** * * MESSAGE: MSG_TICTAC_NEW_GAME for TicTacBoardClass * * DESCRIPTION: This method notifies each of the visible children that * a new game is beginning; they should take their places, * and then the board object will redraw itself * * PARAMETERS: * void () * ***********************************************************************/
@method TicTacBoardClass, MSG_TICTAC_NEW_GAME {
WindowHandle win; /* the window to draw to */
GStateHandle gstate; /* the gstate of the window */
/* First notify all the children (game pieces) * that a new game is beginning. */
@send @visChildren::MSG_PIECE_NEW_GAME();
/* Now initiate a new gstate for the view window. * Get the window handle from the view, and then * create a new gstate for it. */
win = @call TicTacView::MSG_GEN_VIEW_GET_WINDOW();
gstate = GrCreateState(win);
/* Invalidate the game board rectangle in the document. * This will cause the view object to generate a * MSG_META_EXPOSED for the rectangle, causing MSG_VIS_DRAW * to be sent to this object (TicTacBoard). */
GrInvalRect(gstate, 0, 0, BOARD_WIDTH, BOARD_HEIGHT);
/* Now destroy the temporary gstate. This is important * to keep too many gstate handles from being locked * and slowing down the system. */
GrDestroyState(gstate); }
/*********************************************************************** * * MESSAGE: MSG_VIS_DRAW for TicTacBoardClass * * DESCRIPTION: This method draws the board's outline and the * lines of the playing field. It is sent each time * a portion of the view window becomes invalid (such * as when the primary is moved). * PARAMETERS: * void (word drawFlags, GStateHandle gstate) * gstate is the handle of the graphics state associated * with the exposed portion of the view window ***********************************************************************/
@method TicTacBoardClass, MSG_VIS_DRAW {
/* Set up the graphic state properly. The board
* lines are to be white and three points thick. */
GrSetLineColor(gstate, CF_INDEX, C_WHITE, 0, 0);
GrSetLineWidth(gstate, 3);
/* Now draw the border of the game board. It is a * rectangle that outlines the entire board. */
GrDrawRect(gstate, 0, 0, BOARD_WIDTH, BOARD_HEIGHT);
/* Set and draw the Tic Tac Toe playing field. The * lines are now set to 4 points thickness, and the * lines are drawn with HLine and VLine graphics * commands. Ideally, preset constants would be used.*/
GrSetLineWidth(gstate, 4);
GrDrawHLine(gstate, 5, 60, 175);
GrDrawHLine(gstate, 5, 120, 175);
GrDrawVLine(gstate, 60, 5, 175);
GrDrawVLine(gstate, 120, 5, 175);
/* When the MSG_VIS_DRAW is received by the game board, * it must pass it on to its visible children. It must * also pass on the parameters of the message as passed * to ensure all drawing is done properly. */
@send @visChildren::MSG_VIS_DRAW(drawFlags, gstate); }
/*********************************************************************** * * MESSAGE: MSG_TICTAC_VALIDATE_BOUNDS for TicTacBoardClass * * DESCRIPTION: This method checks to see if the bounds passed * are on the game board. This is invoked when a game * piece is in motion and receives an END_SELECT message * indicating it is being put down. The piece must * determine whether the suggested bounds are on the * game board; the piece should always query the board * object rather than check directly; if the board were * resizable, the piece could be incorrect sometimes. * * STRATEGY: Check the four bounds against the board's edges. If * all four are on the board, return TRUE. If any one * of the four is off the board, return FALSE. * PARAMETERS: * void (word bottom, word right, word top, word left) ***********************************************************************/
@method TicTacBoardClass, MSG_TICTAC_VALIDATE_BOUNDS {
if (((bottom < BOARD_HEIGHT) && (top > 0))
&& ((right < BOARD_WIDTH) && (left >= 0))) {
return(TRUE);
} else {
return(FALSE);
}
}
GEOS SDK TechDocs
|
|
5.3 The Structure of TicTac
|
5.5 TicTacPiece Specifics