83: Design Patterns: Game Loop.
Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Categorie:
What is the game loop design pattern? The game loop behavioral pattern is essential in games and simulations to make the actions proceed at the proper speed regardless of what the user is doing or how fast the computer is. A game loop will repeatedly do the following three things: Check for input and process it if available. Event-driven operating systems should provide some way to check for events without returning control back to the operating system. This might be called peeking at an event or something like that. With this, you have the ability to check if the user pressed any buttons or moved the mouse, and if nothing happened, then continue doing the work of the game. Update game objects. This means you can move things around, create or destroy things, and change any other game object properties. Render the display. This means that you clear what used to be drawn on the display and draw the game objects in their new positions or state. All games should have a loop like this. This episode explains how to avoid blocking progress while waiting for user input. And it also explains how to manage the game speed. If you’d like to read the book that describes this pattern along with diagrams and sample code, then you can find Game Programming Patterns at the Resources page. You can find all my favorite books and resources at this page to help you create better software designs. Listen to the full episode or read the full transcript below. Transcript The basic description says that this pattern decouples game time from user input and processor speed. We’ve actually finished going through all the patterns in the Gang of Four book. There’s a really good book called Game Programming Patterns by Robert Nystrom that I recommend that documents several more patterns applicable to game development. I remember working for a bank once helping to develop an electronic banking application and I was in charge of creating digital signatures for large customers. A digital signature is a long series of random bits that are generated through cryptography to relate to another series of random bits. This relation is usually based on large prime numbers and needs a lot of calculation. I ran a software application that created these signatures. This software was designed to run on Windows 3.1 which still had some issues running multiple applications. In particular, when an application was running, the operating system could not stop that application to let another application have a turn. Windows relied on cooperative multitasking. This meant that a well mannered application would voluntarily give up control of the computer back to Windows so that Windows could let another application have a turn. Well, this application I was using was a little too well mannered and it would perform a small portion of calculations and then return control back to Windows. It did this on purpose. Because it needed a true source of random numbers, it could not rely on anything the computer itself could generate. So it used the mouse coordinates. And by doing a little work and returning back to Windows, it would stop until I moved the mouse. I had to continuously move the mouse for two to three minutes while it gathered random mouse positions and did the work needed to calculate a digital signature. If I stopped moving the mouse, then it also stopped calculating. Imagine a game that played like that. If you stop typing or stop moving the mouse, then the game also stops. You don’t want your game progress to depend on user input. This pattern helps with one more aspect too and that’s processor speed. Imagine you buy a new game that was just released to take advantage of the latest processors and hardware available. The game plays perfect and becomes one of your favorites. So much so that you keep it. Now, many years pass and what used to be a fast computer is considered miserably slow. You buy a new computer that’s at least twice as f