HTF does the game loop work?

No code today, just a very basic question that I'm having trouble wrapping my mind around. "MakingGameWithBen" is a youtube channel thats dedicated to teaching how to make video games using c++. I've watched most of the basics and am on his advanced tutorials. I'm not that far in but so far he's been showing how to make a very basic engine which is very nice since after I'm done with the tutorial series, I'll have an engine to start practicing making games with that I understand completely. So far though, its been setting up the shaders and really basic stuff, not any real video game development. Which is fine, I understand the importance of everything he's been showing so far.

Very early on in the series, though, he shows us how to make the game loop with is really cut and dry: get input, draw. I'm tried to do some mild research but I can't seem to wrap my mind around how the game loop works. I understand the theory but as far as adding to it to actually make a game? I'm lost. And that's not surprising really since he hasn't gone over it yet. I'm hesitant looking ahead at other videos to try and find when he starts fucking with the game loop since i might come across other content that may confuse me.

What I'm asking for is some context to how a game engine usually looks. Maybe you have a pretty good snippet of code that would help me understand how the game loop works, maybe you know a simple open source game I can take a look at for code ideas.

Thanks guys, hope ya'll are having a good night.
For example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (true){
    //Process events.
    auto input = get_input();

    //Update state.
    if (input.left_pressed() && !input.right_pressed())
        character.move_left();
    else if (!input.left_pressed() && input.right_pressed())
        character.move_right();

    //Update display.
    screen.clear();
    screen.draw(character);
}
Every game loop is a more sophisticated version of this. Process events, update program state based on those events, draw things on the screen.
Last edited on
Helios: I guess this makes sense, it's pretty much every example I've seen and after thinking about, I realized that that example makes sense for a first person shooter, let's say: but right now the game I'm designing is a turn based strategy game (the most basic example I can think of is Pokemon). The only way I see a game loop working is avgameloop being used after every choice.

Game loop: awaiting input for which task to use.

Processes attack, picks target, weapon, finalizes.

Game loop: awaiting input.

I feel like I'm not explaining this very well but I'm not sure how a turn based game would use a game loop.
I feel like I'm not explaining this very well but I'm not sure how a turn based game would use a game loop.


Exactly the same.

1
2
3
4
5
6
7
8
9
while(true)
{
  cout << "What would you like to do this turn?";
  cin >> user_input;
  
  process_user_input();
  calculate_result_of_user_input();
  update_screen_with_all_changes();
}

In the case of a GUI-heavy game such as Pokemon what will happen is, the user will not directly control the character. They will control the GUI, and their actions with the GUI will give out control commands.
Additionally, the state update step will process animations, timers, and other things that are independent of the user, same as with a normal game loop.
In fact, this is exactly how GUIs are implemented at the lowest level. Here's a real example from a GUI I wrote for an audio player (irrelevant details have been redacted for clarity):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while (true){
    auto status = this->handle_in_events();
    if (check_flag(status, QUIT))
        break;
    this->handle_out_events();
    this->handle_finished_jobs();
    if (!this->redraw_needed()){
        delay_execution();
        continue;
    }

    this->screen.clear();
    this->current_element->update();
    this->screen.show();
}
You can see the original, if you'd like: https://github.com/Helios-vmg/CopperRat/blob/master/src/jni/src/SUI/SUI.cpp#L469
Last edited on
Topic archived. No new replies allowed.