I'm currently working on a project where I've decided to redraw the screen with every OnGameLoop().
I've declared a variable that is used to indicate where in the game the user is:
- Code:
enum tGameState
{
MAINMENU,
PLAY,
SCORES,
OPTIONS,
GAMESTYLE_OPTIONS
};
and the OnGameLoop() looks like this...
- Code:
bool Project003::OnGameLoop()
{
Display *display = GetDisplay();
if (display->BeginScene())
{
Surface *backBuf = display->GetBackBuffer();
//DRAW MAIN MENU
if (GameState == MAINMENU)
{
DrawMainMenu( backBuf );
}
//DRAW OPTIONS MENU
else if (GameState == OPTIONS)
{
DrawOptionsMenu( backBuf );
}
//DRAW SCORES SCREEN
else if (GameState == SCORES)
{
DrawScoresScreen( backBuf );
}
.
.etc
.
display->Swap(SWAP_VSYNC);
}
return ContinueGame;
}
ContinueGame is set to false when the user quits the game.
The GameStates are changed within OnStylusUp() when the user presses on graphics with the stylus.
The overall animation is smooth on my iPaq 4155 and the code is easy to read... but it seems that redrawing the entire screen all the time is a bit excessive.
What solution have other people implemented ?
Since nobody has replied, I'll give it a shot...
The overall animation is smooth on my iPaq 4155 and the code is easy to read... but it seems that redrawing the entire screen all the time is a bit excessive.
It could also be called excessive to code everything so that nothing get's re-drawn if it doesn't need it.
Anyway, I think you may be redrawing more than needed just because you're running as fast as possible. I would limit the max frame rate with something like this:
- Code:
bool Project003::OnGameLoop()
{
static unsigned long lastframetime = GetTickCount();
unsigned long thisframetime = GetTickCount();
const int minimumframemilliseconds = 20; //MAX frame rate = 50
if((thisframetime - lastframetime) < minimumframemilliseconds)
{
int sleeptime = (minimumframemilliseconds - (thisframetime - lastframetime));
if(sleeptime > 5) Sleep(sleeptime);
return; //don't draw
}
lastframetime = thisframetime;
// continue with OnGameLoop...
Also, since you've already got the enum{} in place, use an array of function pointers... so that this:
- Code:
//DRAW MAIN MENU
if (GameState == MAINMENU)
{
DrawMainMenu( backBuf );
}
//DRAW OPTIONS MENU
else if (GameState == OPTIONS)
{
DrawOptionsMenu( backBuf );
}
//DRAW SCORES SCREEN
else if (GameState == SCORES)
{
DrawScoresScreen( backBuf );
}
would become
- Code:
(DrawFunction[GameState])(backBuf);
If your gane's performance is good redrawing the whole screen, I can't see any good reason for not doing that except for one: battery life. The more work the device has to do per frame, the more of an impact on battery life there will be. How big a deal this really is I'm not sure, but it's certainly true.
Myself, I've only worried about not redrawing the entire screen in one instance, and that's because the nature of the game was such that only a small portion of the screen would change each frame, the rest was relatively static. In that case, the difference in code complexity between redrawing everything and conditionally drawing portions was negligible, so it made sense, and plus it did help performance, which was a consideration.
As for program structure, what your doing is basically what I do as well. Some people have classes for each screen and call a method of it based on a switch like you have (you can actually get fancy and through the magic of points you can actually eliminate the need for that switch at all). Some people prefer a more "C-like" structure in that classes are not used, instead you just call either methods of your one game class or separate functions outside. Either approach is modular, which I think is the key point. I personally opt for the C approach with the functions I call actually methods of the game class.
So, I'll have all the code for a given screen in it's own .cpp file, in that file will be methods of the main game class like PerformNextFrame, KeyDown, KeyUp, etc. Then I'll have that big switch statement in each of the handlers (the main ones that get called during execution) and they call the "delegate" versions of those functions for the current screen. For me, this code is easier to follow and maintain than having a pointer to a given object and then calling a method off that. The code for my approach is more volumous, but I think also a bit less error-prone. It's a matter of taste though, certainly it works either way.