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

Author Topic: How to switch between Menu and Game screens efficiently?  (Read 8141 times)

0 Members and 1 Guest are viewing this topic.

kepler0

  • Newbie
  • *
  • Posts: 23
    • View Profile
How to switch between Menu and Game screens efficiently?
« on: January 01, 2017, 04:19:56 pm »
Hello.

I'm not sure if I'm posting on the right forums or not but I'm having trouble understanding how to switch between game and menu screens.

I have already created my main menu, and it is not a small amount of code. But now I need a way to move between the other screens such as settings and the game itself.

I've already looked at https://github.com/SFML/SFML/wiki/Tutorial:-Manage-different-Screens but I found it very confusing. I'm not very familiar with using different files to get things done and have only stuck to main.cpp when learning C++.

Is there a built in function that will manage this for me, and do I have to edit parts of my code that reference the RenderWindow?
« Last Edit: January 01, 2017, 05:22:18 pm by kepler0 »
Zzz

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 878
    • View Profile
Re: How to switch between Menu and Game screens efficiently?
« Reply #1 on: January 01, 2017, 07:26:33 pm »
Look up finite state machines. There's no real way around that. But before doing so, try to get yourself familiar with how you can split your code into multiple files, because in the end your code will become a lot more readable and maintainable that way. One (IMO) excellent source regarding design patterns in general would be Game Programming Patterns, in this case the chapter about the State Pattern.

Tigre Pablito

  • Full Member
  • ***
  • Posts: 225
    • View Profile
    • Email
Re: How to switch between Menu and Game screens efficiently?
« Reply #2 on: January 01, 2017, 09:56:25 pm »
Hello kepler0

to switch to a menu screen when you are in the game, just add in your "key pressed" event handler method something like this

if (e.Code == Keyboard.Key.Escape) {
    Options option = EscPressedScreen()
    if (option == Options.Exit)
        ExitGame();  // exits the application
    else if (option == Options.Reset)
        .........          // resets the game
    ..............
}
 

your EscPressedScreen() method would be something like this

private Options EscPressedScreen() { // Options is an enum, but you may use an int
    Options option;
    while (!Keyboard.IsKeyPressed(Keyboard.Key.Return)) { // when you press Enter the selected
        // here you select your option                           // option is returned
        // commonly with the arrow keys
        ..............
    }
    return option;
}
 

if you select "continue game", the method just returns (does nothing) and the game main loop continues.

hope this helps
i wrote in C# cos i don't know too much C++


Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: How to switch between Menu and Game screens efficiently?
« Reply #3 on: January 02, 2017, 01:10:31 am »
You can see an example of using States in the code from the SFML Game Dev Book, here:
https://github.com/SFML/SFML-Game-Development-Book/tree/master/05_States
Pay special attention to the classes State and StateStack

 

anything