Now that we have our variable in gamestate.message, we need to tell the game what to do with it.
First, at the top of WL_DRAW you'll need a new variable with which to store a timer for our messages, so they don't display indefinitely.
If you need more of an explanation of the use of timers in Wolf3D, check out the Timer Mechanics guide, which takes you through the steps of adding a timed bonus in more detail.
In the GLOBAL VARIABLES section at the top of the file, add these lines:
unsigned screenloc[3]= {PAGE1START,PAGE2START,PAGE3START};
#endif
unsigned freelatch = FREESTART;
long lasttimecount;
long frameon;
int messagetime=0;
unsigned wallheight[MAXVIEWWIDTH];
We create messagetime, and set it an initial value of 0. Now go to the bottom of WL_DRAW, where we'll want to place these two new functions:
/*
========================
=
= GetMessage
=
= gets ingame messages
=
========================
*/
void GetMessage (char *lastmessage)
{
messagetime = 140; // time for message to display
strcpy(gamestate.message, lastmessage);
}
/*
========================
=
= DrawMessage
=
= displays ingame messages
=
========================
*/
void DrawMessage (void)
{
messagetime-=tics;
fontnumber = 0;
SETFONTCOLOR(0xf,0x0); // set the color
PrintX=8;
PrintY=2; // position the message
US_Print(gamestate.message); // print message
if (messagetime <= 0)
DrawPlayBorderSides ();
}
These functions contain everything needed for In-game Messages to work.
GetMessage() is the function that we will call to tell the game what message we want displayed. It will take whatever value we assign to lastmessage, and copy it into our gamestate.message variable we created earlier. It also sets our timer to 140 tics (2 seconds).
Then, DrawMessage() handles the heavy-lifting. It specifies what font to use when printing a message (Wolf3D by default comes with two fonts - one small and one large), and what colour the font is, then displays the actual message. When the function is active, it also starts deducting tics from our timer, turning it into a countdown.
After that, it selects the coordinates for where the text will start from. For this guide, we'll be having it placed just off from the top left corner of the screen. As the very corner coordinate is (0,0), we specify x and y coordinates of (8,2) in the PrintX and PrintY variables.
Depending on your game, those coordinates can be altered to place the text anywhere on the screen you want!
Finally, USPrint() is called to print the value of gamestate.message to the screen.
Of course, creating a function to draw the message is meaningless unless we also call the function so it triggers during the game.
We will want to move back up in WL_DRAW until we find the ThreeDRefresh() function. Inside of it, we can find calls to functions that draw elements like the Player's weapon (Which is always drawn on top).
//
// draw all the scaled images
//
DrawScaleds(); // draw scaled stuff
DrawPlayerWeapon (); // draw player's hands
Inside of there, we will make our call to DrawMessage().
//
// draw all the scaled images
//
DrawScaleds(); // draw scaled stuff
DrawPlayerWeapon (); // draw player's hands
if (messagetime > 0)
DrawMessage();
ThreeDRefresh() is involved in the raycasting and visual aspects of the game and is practically always running, so we can rest assured knowing that DrawMessages() will be seen. It is a function that should be altered with great care though, being a crucial element of the game.
We have DrawMessage() display on the condition that our messagetime timer is active. So, messages will display for two seconds, then stop displaying.