Ah, thank you. I've had it drilled into me to decouple everything whenever possible. I think I understand. So, right now my game loop is:
1. process input event handler, which links button presses to actions including gui state init
2. enum state switch
2x. render enum state x background and buttons
What else do I need to nest within the enum state switch to couple the UI rendering and processing? Do I need to run my event handler multiple times?
I've had it drilled into me to decouple everything whenever possible.
Sure, but the state is dependent so it doesn't make sense.
The game can be thought of as a filter whose input is the state of the user input devices (mouse, keyboard, controllers, etc.) and whose output is a sequence of audio and video frames.
The relevant part of your game loop should have access to the current state of user input and its own internal state. If the game state requires you to draw a menu, you can draw (or queue the drawing) of the menu exactly when you check its state.
For example in the main game loop you could have something along the lines of:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// - `ctx` is the I/O context, a handle to the resources needed to produce
// graphical and auditory output.
// - `user_input_state` is the state of the virtual pointer at least.
// - `text_label` draws the button.
// - `button` both draws the button and returns whether `user_input_state`
// indicates the button is clicked.
// Additional arguments to `text_label` and `button` that I didn't show might
// include position, size, style, and so on.
if (current_state == game_state::in_main_menu)
text_label(ctx, "My Title");
if (button(ctx, user_input_state, "Play"))
current_state = game_state::gameplay_loop;
if (button(ctx, user_input_state, "Quit"))
current_state = game_state::prompt_to_save_and_quit;
Thank you. So, I should nest the input, or functions that call up the input, or pointers to the input inside their respective game states like I do for the rendering. Time to tweak some more.
Oh, thanks! By the Group of Four? Yes, I've found game programming patterns very helpful. I didn't even know most of the concepts in there were even a thing before I ran across them in that ebook. Command patterns, state machines, and decoupling were definitely three eye openers.