I have a menu system with four screens and six buttons. I have created a button class and it works great. I even have events for the buttons working. The problem is that I have to run the events inside the game loop. This causes me to wait on a menu screen. Is there a way with SDL and c++ to wait until an even fires aside from setting a variable on button click and waiting in a while loop?
1 Answer
SDL allows you to wait for an event with... SDL_WaitEvent. (That's one of the first results if you search for "SDL wait event".) However, it's not exactly clear what your problem is, since running inside a while loop is the standard procedure for games. So it's also not clear that SDL_WaitEvent is the solution to your issue.
When implementing a GUI in a game the usual method is to have the main loop collect the events, and then you send the events to the GUI to be handled. So for example, you might wait for a SDL_MOUSEBUTTONDOWN
event, collect the click position from the event, and then pass that to each button in turn to see if the click was on that button - and if so, execute the associated code. You wouldn't generally need to wait at all - the event is read by the main game loop and then handed off to the GUI for potential processing.