Jump to content
  • Advertisement
Sign in to follow this  
khoadd

Game's MenuState and Game's Loop

This topic is 2124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
while( running )
{
while( running && in_menu_mode )
{
GetMessage(...)
}
while( running && in_game_mode )
{
PeekMessage(...)
}
}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

@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.

Edited by KhoaVN

Share this post


Link to post
Share on other sites

Ok so you stated there's two application types in our minds here:

 Always on -  one big loop

 and wait for events - technically it is still a big loop with a minimum of one sentinel value between two other loops.

 

Maybe that can be confusing. But I was really just iterating the concept of what mark ds already gave you. It's nice to be able to say it to yourself and understand why it is going to work so try understanding it.

Share this post


Link to post
Share on other sites

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.

Edited by Malabyte

Share this post


Link to post
Share on other sites

@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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!