Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: This Game code organization, is good ??  (Read 2098 times)

0 Members and 1 Guest are viewing this topic.

sggocu88

  • Newbie
  • *
  • Posts: 13
    • View Profile
This Game code organization, is good ??
« 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
« Last Edit: February 15, 2017, 11:11:28 pm by sggocu88 »

Turbine

  • Full Member
  • ***
  • Posts: 100
    • View Profile
Re: This Game code organization, is good ??
« Reply #1 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.

JayhawkZombie

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: This Game code organization, is good ??
« Reply #2 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.

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: This Game code organization, is good ??
« Reply #3 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, it should give you idea where to start. You should probably read the book as well, it explains a lot of concepts quite nicely.
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler