SFML community forums

Help => General => Topic started by: teunissenstefan on August 17, 2014, 11:56:09 pm

Title: Switching 'scenes'
Post by: teunissenstefan on August 17, 2014, 11:56:09 pm
How do you switch 'scenes' in SFML? Example: From the menu to the game.
This is something that I have not seen anywhere :/
Title: Re: Switching 'scenes'
Post by: eXpl0it3r on August 17, 2014, 11:59:36 pm
It's not a feature of SFML and thus you have to actually program it yourself.
Also if you "have not seen [it] anywhere" then you didn't even try really looking around. Google will give you all kinds of results for "scene/state/screen management" and given the amounts of links I've already posted, you'd certainly have found one to my SmallGameEngine (https://github.com/eXpl0it3r/SmallGameEngine) which was based on a state machine tutorial and essentially demonstrates that exactly.
Title: Re: Switching 'scenes'
Post by: teunissenstefan on August 18, 2014, 12:02:02 am
I have searched for hours and did not find anything useful. Will check 'SmallGameEngine'.
Title: Re: Switching 'scenes'
Post by: Ixrec on August 18, 2014, 12:13:50 am
This is probably one of those things that people don't know the programmer jargon for and thus can't search for unless they happen to have read about it at least once in the past.

The keyword is "state."  Switching from a menu to another menu to the actual game is an example of changing the state of your program.  You should have no difficulty whatsoever finding zillions of google hits about state machines and correctly switching state and the like.  Just for good measure, here's a specific link that discusses managing states in games: http://gameprogrammingpatterns.com/state.html
Title: Re: Switching 'scenes'
Post by: teunissenstefan on August 18, 2014, 12:35:39 am
Indeed, but I DID hear about "statemanager". I was just wondering if there was an easier way. :)
Title: Re: Switching 'scenes'
Post by: Ixrec on August 18, 2014, 12:42:23 am
The simplest version of a state manager is very easy.  The link I gave goes through quite a few options, so you can easily pick the one that's appropriate for your program.
Title: Re: Switching 'scenes'
Post by: teunissenstefan on August 18, 2014, 12:45:21 am
I tried eXpl0it3rs link and that works perfectly fine!
Thanks you guys :)
Title: Re: Switching 'scenes'
Post by: teunissenstefan on August 18, 2014, 06:57:07 pm
I do have 2 problems with it:
1) I want to make a new 'state' but I get the error: "OptionsMenuState" : undeclared identifier

2) m_next = StateMachine::build<PlayState>(m_machine, m_window, false);
works from IntroState.cpp, but not from another script.
Title: AW: Switching 'scenes'
Post by: eXpl0it3r on August 18, 2014, 07:23:35 pm
Well getting some example code and using a library with a simple API doesn't replace the fact that you first need to learn the language in question, meaning C++. ;)

If it's undeclared then the compiler couldn't find the declaration and if it works in one but not the other file you're probably not including the right headers... Basic C++...
Title: Re: Switching 'scenes'
Post by: teunissenstefan on August 18, 2014, 08:06:10 pm
Okay, let me tell you this. I know HTML, CSS, PHP, Java, JavaScript, Python. I know how programming languages work. And it is obvious that if you put "1" in "1.hpp" and if you need it, you need to include "1.hpp". That's obvious. In many languages. But I have looked through every file, but I cannot find it. At all.
Because there is nothing where a state such as "IntroState" is made.
Title: Re: Switching 'scenes'
Post by: eXpl0it3r on August 18, 2014, 08:33:54 pm
Well the header inclusion is a bit more complex than as in PHP, Java, JavaScript and Python, but you don't have to believe me, instead you should look it up, for example in a good C++ book (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).

Not fully sure what you can't find. The OptionsMenuState is a state of your own, so you'll have to create the definition and deceleration on your own and make sure that the right headers get included at the right place.

The state factory function is a a template which is defined in the StateMachine header (https://github.com/eXpl0it3r/SmallGameEngine/blob/master/src/StateMachine.hpp#L43). To be able to use it from a specific file, you need to include the right headers.

Also don't forget to forward declare things whenever possible.
Title: Re: Switching 'scenes'
Post by: teunissenstefan on August 18, 2014, 10:24:30 pm
 I don't read books.

What I did was I basically copied everything from PlayState.cpp and .hpp but renamed everything that had to be renamed.
So I really don't know where I went wrong.
Title: Re: Switching 'scenes'
Post by: eXpl0it3r on August 18, 2014, 11:47:20 pm
I don't read books.
Good luck then! ::)

So I really don't know where I went wrong.
Me neither. ;D
Title: Re: Switching 'scenes'
Post by: Stauricus on August 19, 2014, 12:02:46 am
there are so many things that could be wrong for you to receive that error...
including you not correctly renaming everything that needed to be renamed. that's a real problem when copy/pasting.

by the way eXpl0it3r, thanks for the SmallGameEngine. it's a great starting point for beginners, and gave me a really better idea on how to make a more functional and clean code.
Title: AW: Re: Switching 'scenes'
Post by: eXpl0it3r on August 19, 2014, 07:58:36 am
by the way eXpl0it3r, thanks for the SmallGameEngine. it's a great starting point for beginners, and gave me a really better idea on how to make a more functional and clean code.
I very glad to hear that it was useful for you. :)
Title: Re: Switching 'scenes'
Post by: teunissenstefan on August 19, 2014, 02:44:59 pm
It was also useful for me and I also thanked you. Yet you're not glad it was useful for me  >:(

And there are many more ways to learn programming than reading books.
And I renamed everything correctly, 100% sure.
Title: Re: Switching 'scenes'
Post by: teunissenstefan on August 20, 2014, 07:34:46 pm
Okay fine, thanks for not even helping me anymore. Jesus, what a community...
Title: Re: Switching 'scenes'
Post by: Jesper Juhl on August 20, 2014, 07:43:15 pm
Okay fine, thanks for not even helping me anymore. Jesus, what a community...
Well. Your "I don't read books" and "I know how programming languages work." comments display a rather arrogant attitude which is not really conductive towards getting help.

C++ is a very large and very complex language. It takes years to get to even an intermediate level and to master it you can easily spend many more years. I personally treat anyone who claim 5 years or less experience with C++ as still being a relative newbie (maybe intermediate).

In any case. You could try to post the exact compiler output you get (along with compiler version and other relevant details).
You could also post a complete and minimal version of your code so we could take a look, run on our own machines, and help spot the problem.
You don't do any of those things.

Also remember that this is a web forum, not a telephone call. People are not sitting ready refreshing their browsers constantly, waiting to answer your questions. It may be days between when people check-in, so have some patience.
Title: Re: Switching 'scenes'
Post by: MadMartin on August 20, 2014, 07:46:08 pm
Okay fine, thanks for not even helping me anymore. Jesus, what a community...
What goes around comes around. You come here and demand help without giving necessary information. That is very bold, to say the least.
Title: Re: Switching 'scenes'
Post by: teunissenstefan on August 20, 2014, 07:49:53 pm
How about asking for information?
And at least now I got you people's attention again.

-
Displaying arrogant? That's an opinion because I don't think it displays arrogant at all, I'm just saying what my brain wants me to.
And what parts of the code should I upload? Because I have no idea what you want to look at then.

And the reason that I didn't give any info was because I'm fairly new to C++, and I thought that I might give you guys the wrong information. For example if I give you A, you need to see B
Title: Re: Switching 'scenes'
Post by: Jesper Juhl on August 20, 2014, 07:57:26 pm
Read these (don't worry, they are not books):

http://sscce.org/

https://github.com/SFML/SFML/wiki/FAQ#tr-grl-minimal

http://www.catb.org/esr/faqs/smart-questions.html

http://en.sfml-dev.org/forums/index.php?topic=5559.0

Regarding asking for information: you are the one who wants help. It's your responsibility to provide all details needed to help you and ask a good question. We are all volunteers and none of us have any obligation to help you. If you want help you need to help us help you.
Title: Re: Switching 'scenes'
Post by: teunissenstefan on August 20, 2014, 08:03:49 pm
I know what SSCCE is, I just can't do it.
And uhm they are going to be a bit more than 40 lines, so I put them on pastebin. (I hope it is allowed here)
OptionsMenuState.hpp http://pastebin.com/Lxt7BHdG
OptionsMenuState.cpp http://pastebin.com/JdN7iwCM
I removed everything that I thought was unnecessary.
Title: Re: Switching 'scenes'
Post by: teunissenstefan on August 20, 2014, 08:12:24 pm
Uhm I really have NO IDEA what i did, but it works now :|
Thanks for your help though.
Title: Re: Switching 'scenes'
Post by: firet on September 03, 2021, 06:31:59 pm
How do you switch 'scenes' in SFML? Example: From the menu to the game.
This is something that I have not seen anywhere :/
This is pretty simple, as long as the game/app isn't too insanely complex. Essentially the state of a program is stored in variables. So use a few to keep track of the scene:
int scene = 1; //Scene 1 is menu, 2 is game, etc. Only render certain things or run certain code/inputs if the scene is a certain integer. Example: only check for clicks on menu buttons if scene = 1.
bool running = false; //Only run the code for the primary game functions when running = true. This allows you to freeze the program.
bool reset = false; //You will see this later
 
In your main loop, on the game end, set running to false.
When the game starts, set running to true and scene to 2.
When you want to reset the game, set reset to true. Then you'll need an if statement, IN THE MAIN LOOP, like this:
if(reset == true)
{
    //Reset the game variables to their original values
    reset = false; //Make sure it doesn't reset every frame
}
 
Sorry if it's hard to understand, if you need help, just DM me and I can set up a tutorial.