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

Author Topic: Quick question about game states.  (Read 2199 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Quick question about game states.
« on: September 18, 2012, 12:09:06 pm »
Hey guys,

I'm just enquiring what you guys would do, I'm making a small game but I'm not 100% sure on how I should code the states (main menu, new game, choose puzzle, puzzle level), I have a gState which will have the sf::View and sf::RenderWindow screen, gState will also check what is in use and return what sprites needs to be return't. But when a state is changed, should gState do all of the work, or should i make a cpp and h file for each state.
like so
mainMenu.cpp/.h
newGame.cpp/.h
choosePuzzle.cpp/.h
puzzleLevel.cpp/.h
or should gState have a enum which will deal with the different states?

I hope that is enough information.

Canvas
« Last Edit: September 18, 2012, 12:25:48 pm by Canvas »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10844
    • View Profile
    • development blog
    • Email
Re: Quick question about game states.
« Reply #1 on: September 18, 2012, 04:05:12 pm »
An often seen version is to have a base class State and then as many states as you need deriving from the base class (e.g. SplashState, MenuState, etc.). The usual and advised handling of classes is to have the deceleration in the header file (.hpp) and the definition in the source file (.cpp). For the base class one does often make an exception and make the deceleration also the definition, that's because usually a base class shouldn't have much actual code.

If you want you can take a look at my adaption of a tutorial: SmallGameEngine
It under the MIT license so feel free to use it or parts of it, if you find the useful. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

plakada3

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Quick question about game states.
« Reply #2 on: September 18, 2012, 05:05:31 pm »
SmallGameEngine

Could you please fix the indentation, and the line length in order to make it readable through the web browsing?

Indeed when I'm loading into my text editor I can correct the size of the tabs to match the size of the space indentation next to it, but through the web browsing it is not possible. There are also long lines that I can not see the end.

Thanks

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10844
    • View Profile
    • development blog
    • Email
Re: Quick question about game states.
« Reply #3 on: September 18, 2012, 05:19:19 pm »
Could you please fix the indentation, and the line length in order to make it readable through the web browsing?
I don't have any issues in my browser, if the line is too long you (should) get a horizontal scroll bar. We don't live in the time where monitors where so small that a 80 char count was a good measurement. I know that the one line is quite long and still I don't really like to break it either. ;)
For the indentation, I can't find any problems. Since I'm not mixing tabs and spaces it should be fine. It would only look bad when I'd try to match up some lines with a specific position, which I don't do...

So with what exactly do you have problems with? Have you tried fixing things on your end by using a proper font/changing browser settings?

Additionally you can always just pull it and look at it from your HDD. :P
I might change the indentation the next time I'll touch it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Quick question about game states.
« Reply #4 on: September 18, 2012, 08:33:22 pm »
Cheers eXpl0it3r, i will take a look, but yea im most probably going to use my gState as the state handler, as it wont be doing much, but for more complex states like Battle and Network Battle i will make its own class file, but also I would just like to ask another question,

Now i have a image manager and I have it in my gState class, now any other class that needs to load textures and stuff, takes a reference from gState, but I have hit a problem. I have created a class called gEntity, these are used to display text, graphics, sounds bla bla bla, anything small, now I want to declare the variable
std::vector<gEntity> gEnts;
but i have a constructor for gEntity which is
gEntity::gEntity(gImageManager& imgMan)
now this vector will work, but they dont have the reference held, how can I give them the reference in the helper and initialize my vector? If the worst comes I don't mind making a vector of gEnts and just passing the reference when needed.

Canvas

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10844
    • View Profile
    • development blog
    • Email
Re: Quick question about game states.
« Reply #5 on: September 18, 2012, 10:01:41 pm »
You could look at a C++/STL reference to find out that the vector takes a initialization in the second parameter, the first is the number of elements:
std::vector<T> vec(num, T(init));
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything