Display/update loop
- As
much as possible, you want to separate your actual drawing (display) from
your game logic (update).
- You
want to update at a constant frame rate. If you don’t, as you add more
objects to your scene, the game will slow down.
A simple display/update loop:
while
(TRUE)
{
while (elapsed < frame_time);
DisplayScene();
UpdateScene();
}
- Another
option is to keep the update rate constant, but display as quickly as
possible. This will also handle crappy machines with slow displays.
A fancier display/update loop:
while
(TRUE)
{
while (elapsed < frame_time)
{
DisplayScene();
}
UpdateScene(elapsed);
}
Actors
- No
matter what your game involves, see if you can use actors to play the
parts
- Enumerated
types! Make an enumerated type showing all your actors
typedef
enum
{
ACT_RIGHTWING,
ACT_CENTER,
…
ACT_GOALIE,
ACT_WATERBOY,
ACT_CHEERLEADER1,
}
Actor*
gameActors[MAX_ACTORS];
- Then
you can access each individual member of the gameActors array be just
saying: gameActors[ACT_WATERBOY]
- And
now, of course, you can loop through your entire actors array to perform
operations (like drawing, updating animations, etc.) on them.
Tasks
- Actors
can perform tasks, that often cause specific animations to be played. Make
tasks an enumerated type, just like your actors.
if
(InGoal(ball, goal))
{
SetTask(ACT_GOALIE, TASK_COLLAPSE);
SetTask(GetGoalScorer(), TASK_CELEBRATE);
for (i=0; i<NUM_CHEERLEADERS; i++)
{
SetTask(ACT_CHEERLEADER1+i,
TASK_SHAKEIT);
}
}
More on displays
void DisplayActors()
{
for (actorNum=0; actorNum<NUM_ACTORS;
i++)
{
Actor* actor = gameActors[actorNum];
DisplayFrame(actorNum,
gameAnimations[actor->currTask][actor->currFrame];
}
}
State Machines
- Make a
state machine indicating which game state you’re currently in
(STATE_PREGAME, STATE_INGAME, STATE_AFTERGOAL, etc…), and make decisions
based on game events, user input, and your current state.
- You
can use state machines for buttloads of other stuff too. As an example,
for opponent AI, you can have actors be in either a (STATE_TRACK, STATE_RETREAT,
STATE_PASS, STATE_SHOOT, STATE_RUN_FOR_PASS) state, and have it change
depending on game conditions at the time.