developer's mag
main page
article
part 1
part 2
part 3
part 4
part 5
part 6
part 7
|
4 - Onward to Implementation
With the game details firmly in mind, we're
ready to go to implementation. Basically,
we need to monitor only three messages:
- MSG_KEY, to see if we need to display the
menu (the K_MENU key code).
- MSG_MENU_SELECT, to determine what menu item
was selected.
- MSG_PEN_DOWN, to handle X or O placement,
and check game status.
From these messages, we can create
the all-important
message loop:
S32 paTicTacToe::MsgHandler( MSG_TYPE type,
CViewable *from,
S32 data )
{
switch (type)
{
case MSG_KEY:
if (data == K_MENU)
m_menu->Show();
return 1; // ignore all other keys!
case MSG_MENU_SELECT:
HandleMenu(from,data);
return 1;
case MSG_PEN_DOWN:
HandlePen(from,data);
return 1;
default:
break;
}
return CWindow::MsgHandler(type, from, data);
}
Three items of note: we include a default
case handler to handle all other messages
(via falling through to CWindow::MsgHandler).
As well, we indicate that a message has been
processed by returning 1. This is especially
important to note in the case of MSG_KEY
- since we return 1, all other keys except
for menuing are ignored, including the keyboard
and cursor keys. If your program needs more
key control, remember to add more tests here.
Finally, notice how small the code
is - unlike
most of this site's tutorials, the
messages
are processed in other functions. For
anything
but tutorial code, this is what I recommend.
Filling up the message loop with handling
code can make debugging and analyzing
difficult,
while placing the code in subroutines
makes
everything easier to work with. In
this case,
we have a function to handle the menu
items,
and one to handle the pen message (the
MSG_KEY
processing is too trivial to move). |
|
Setting Up |
|
Remember when beginning any programming project:
- Create a new project directory.
- Copy in the basic files:
initial.mom, libraries.link, makefile,
os1.def,
os.def, os.link, <program>.atts,
- Rename the atts file to your program name,
and edit the _PUB, _NAME, and _ITXT entries.
- Edit the makefile to your project name and
add the code source you're including in the
APPNAME and SRCS_CPP entries.
- Create an icon by renaming and modifying
the default bmp.
- Include a default Forms Editor file to make
initial design easier.
Consider keeping this skeleton code package
handy to make new projects easier to
set
up.
|
Previous Section
Next Section
|