Understanding the Game Loop
Hands-on Rust — by Herbert Wolverson (33 / 120)
👈 Chapter 3 Build Your First Game with Rust | TOC | Creating Different Game Modes 👉
The terminal-based programs you wrote in Chapter 2, First Steps with Rust, operate and execute top to bottom through the main function, pausing for user input. Most games won’t stop or pause whenever the player wants to press a key. In Flappy Dragon, the player’s dragon continues to fall even if the player isn’t touching the keyboard. For games to operate smoothly, they instead run a game loop.
The game loop initializes windowing, graphics, and other resources. It then runs every time the screen is refreshed — often 30, 60, or more times per second. Each pass through the loop, it calls your game’s tick function. A closer look at the game loop and what each step does is shown.
- Configure App, Window, and Graphics: Configuring an application to work with your Operating System is no small undertaking and varies by platform. Displaying graphics can be an enormous endeavor — Vulkan or OpenGL can require hundreds of lines of code before the system is ready to draw a single triangle.
- Poll OS for Input State: Polling for input state is also platform specific. Most Operating Systems provide a series of events representing what the user did…