Game's MenuState and Game's Loop

Started by
4 comments, last by khoadd 10 years, 7 months ago

In normal application or non-animation application, we use event-driven. It means that just when there is at least one event inside event queue, the program will processes. If not, it will wait until at least one. During of that waiting time, the program does nothing.

For game, we use game loop (I saw others called it: loop-driven) because there are many things happening and need to be updated even if user did not input anything.

My question is:

With menu state (the menu for us to choose "Start new game" | "Load game" | "Continue Game" | "..." - not in-game menu/GUI), it is like normal app. It waits for user input. It does not need a real-time loop. So how we can implement it and attach it to our game?

Here is an overview of my code:

// class Game has a "StateManager" field and a loop like this

while(running)

{

-get Event

-process Event

-update

-draw

}

// inside update/draw, i simply call update/draw of "current_state" of StateManager

if the state is GamePlay, It is ok. If it is MenuState, it is my problem.

Advertisement

while( running )
{
while( running && in_menu_mode )
{
GetMessage(...)
}
while( running && in_game_mode )
{
PeekMessage(...)
}
}

Sorry - no idea how to format the code!

But essentially, just use whichever message loop is appropriate for what you're doing at the time.

@mark ds: wow it is really simple. Thank you so much.

@ActiveUnique: I still not figure out what you mean. Please explain! I really like to look for more ways.

Well, at the end of the day it's all about refresh, memory and the process per refresh. So personally, I don't see why you can't just use a big event-driven wrapper loop for all programs, regardless of them being games or not. It's just for human logic anyways, the computer doesn't care (afaik). Just adjust the currently running processes accordingly, thread or synchronize when needed and stop the loop children that aren't supposed to run at that time (and be sure to store relevant information for later, if needed).

A main menu is just another application that runs when the child gameLoop or any other loops (!running) and the parent mainLoop (running) or am I missing something here?

In pseudo-code:

mainLoop {

menuLoop ();

gameLoop ();

whateverOtherLoop ();

}

Throw an else if or more applicable statement (e.g. synchronized methods?) in there, so that only 1 loop is running at a time.

- Awl you're base are belong me! -

- I don't know, I'm just a noob -

@ActiveUnique: It is clear(more than the old). Thank you so much. :)

@Malabyte: Thanks for your advice. Maybe I just look one way. Your solution can be a good one. I am thinking about it. Thanks a lot :)

This topic is closed to new replies.

Advertisement