SFML community forums

Help => General => Topic started by: sggocu88 on February 15, 2017, 11:54:42 am

Title: This Game code organization, is good ??
Post by: sggocu88 on February 15, 2017, 11:54:42 am
Hi. It's bad if my game organization  wil be something like this ? :

while (window.isOpen()
{
if (user press new game button) go to room newgame();
}

void newgame()
{
while (1)
{
//Some code there

//and if user want to go main menu:
return;

}

}

Is bad to do this ? i want create very much rooms with infinite loop and if user want to exit  function will return and go back to main. Every room will have they own pollevent .
IS BAD ?
SRY FOR MY ENGLISH
I AM BEGGINER
Title: Re: This Game code organization, is good ??
Post by: Turbine on February 15, 2017, 12:16:49 pm
It's bad in a lot of ways. It might help if you learn good structured programming before complicating it with SFML.
Title: Re: This Game code organization, is good ??
Post by: JayhawkZombie on February 15, 2017, 03:38:45 pm
It's bad in a lot of ways. It might help if you learn good structured programming before complicating it with SFML.
You should provide some sources OP could use to improve.  OP can't improve if we don't tell them how or direct them.

OP, you could use a class to handle levels, and then switch between them in main whenever the user wanted to switch levels. Then you could avoid having a separate event loop in each of them.
Title: Re: This Game code organization, is good ??
Post by: Elias Daler on February 15, 2017, 06:25:02 pm
Yes, this won't work well. You should only have 1 main loop in your game.

The basic idea is to have some state manager which will have a pointer to current GameState. You'll have a bunch of classes deriving from GameState and having virtual functions like handleEvent, update, etc. Once user presses the "new game" button, you go to InGame state and change pointer to a current state, so that currentState->handleEvent(event) calls InGame::handleEvent

Check out this example from SFML Game Programming book (https://github.com/SFML/SFML-Game-Development-Book/tree/master/05_States), it should give you idea where to start. You should probably read the book as well, it explains a lot of concepts quite nicely.