Game Programming Patterns Game Loop From the book by Robert Nystrom

Slides:



Advertisements
Similar presentations
Computer Organization. The Nature of Data Spreadsheets give us a way to create processes that manipulate data We can think of a piece of data as a quantity.
Advertisements

Game Programming Patterns Game Loop
IS660Z Programming Games Using Visual Basic Overview of Cannonball.
Slide 2-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 2 Using the Operating System 2.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Microsoft® Small Basic
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
How do games work? Game Workshop July 4, Parts Sprites/pictures Map/background Music/sounds Player character Enemies Objects.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 5 Interactive Programs.
Latency Compensating Methods in Client/Server In-game Protocol Design and Optimization I’m Curtis.
How to Debug VB .NET Code.
CS320n –Visual Programming Interactive Programs Mike Scott (Slides 5-1)
Games Development Practices Semester 2 Overview CO2301 Games Development 1 Week 14.
AGD: 5. Game Arch.1 Objective o to discuss some of the main game architecture elements, rendering, and the game loop Animation and Games Development.
Graphing Linear Equations From the beginning. All the slides in this presentation are timed. You do not need to click the mouse or press any keys on the.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
Every week: Sign in at the door If you are new: Fill in Registration Form Ask a Mentor how to get started Make sure you are on the Athenry Parents/Kids.
PacMan by Midway, released 1980 CSE 380 – Computer Game Programming Real-Time Game Architecture.
CS 0004 –Lecture 1 Wednesday, Jan 5 th, 2011 Roxana Gheorghiu.
.:::The EA FEAR Division Presents:. When you download FEAR you should be able to load it pretty quickly, so if it’s taking long consider the problems.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Introduction to Networked Graphics Part 3 of 5: Latency.
Lecture 5: Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
Learning the skills for programming Advanced Visual Programming.
IT253: Computer Organization
Moving Around in Scratch The Basics… -You do want to have Scratch open as you will be creating a program. -Follow the instructions and if you have questions.
4.1 Advanced Operating Systems Desktop Scheduling You are running some long simulations. In the mean time, why not watch an illegally downloaded Simpsons.
CS 4730 Game Architecture CS 4730 – Computer Game Design Credit: Some slide material courtesy Walker White (Cornell)
Concurrent Programming and Threads Threads Blocking a User Interface.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
2.1. T HE G AME L OOP Central game update and render processes.
Game Programming Patterns Event Queue From the book by Robert Nystrom
Game Maker – Getting Started What is Game Maker?.
Flash Another Look 21-Nov-15 Computing Engineering and Technology 1 Flash.
Reference: The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)
What is Programming? Computer programming is about telling the computer what it is we want it to do We tell the computer what we want it to do by sending.
11 General Game Programming Approach. The program is event-driven The program is event-driven –Messages = events –So as all windows system (for example.
An operating system is the software that makes everything in the computer work together smoothly and efficiently. What is an Operating System?
Welcome to Physics Main Principal #1: Physics is the study of moving objects! We want to be able to predict how objects will move in all situations.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
UW EXTENSION CERTIFICATE PROGRAM IN GAME DEVELOPMENT 2 ND QUARTER: ADVANCED GRAPHICS Game program main loop.
High Level Architecture Time Management. Time management is a difficult subject There is no real time management in DIS (usually); things happen as packets.
CRE Programming Club Class 8 Robert Eckstein and Robert Heard.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
T HE G AME L OOP. A simple model How simply could we model a computer game? By separating the game in two parts: – the data inside the computer, and –
5 Event Handling Interactive Programming Suggested Reading Interaction: Events and Event Handling, Supplemental Text for CPSC 203 Distributed this term.
Repetition everywhere – comparing while in a method and as an event Susan Rodger Duke University July 2010.
Today's Ninja Challenge: Write Your First Computer Game!
Introduction to: Python and OpenSesame FOR PROS. OpenSesame In OpenSesame you can add Python in-line codes which enables complex experiment. We will go.
Speedway Schools. A computer has many parts that make it work. Some of those parts we can see. Others are hidden inside where we can't see them. In this.
(More) Event handling. Input and Windowed-OS’s Windowed OS’s (Windows, OSX, GUI-linux’s) send messages (events) when the user does something “on” the.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
Lecture 5 Page 1 CS 111 Summer 2013 Bounded Buffers A higher level abstraction than shared domains or simple messages But not quite as high level as RPC.
Computer Control and Monitoring Today we will look at: What we mean by computer control Examples of computer control Sensors – analogue and digital Sampling.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Reference: What is it? A multimedia python library – Window Management – Graphics geometric shapes bitmaps (sprites) – Input Mouse Keyboard.
Information Technology (IT). Information Technology – technology used to create, store, exchange, and use information in its various forms (business data,
PYGAME.
Game Loops + Part II Reference:
Motion: Speed & Acceleration
Frame Update, Level Representation & Graphics
Motion: Speed & Acceleration
Week 6: Time and triggers!
Tonga Institute of Higher Education IT 141: Information Systems
Motion: Speed & Acceleration
Tonga Institute of Higher Education IT 141: Information Systems
Year 10 Computer Science Hardware - CPU and RAM.
Presentation transcript:

Game Programming Patterns Game Loop From the book by Robert Nystrom

A simple main loop for an interactive program: while (true) { char* command = readCommand(); // read text from keyboard handleCommand(command); }

A modern event-driven program such as your word processor is similar: while (true) { Event* event = waitForEvent(); dispatchEvent(event); } Events could be key presses, mouse clicks, system shutdown, window minimization, timer firing, or many other notifications.

A game keeps moving even when the user isn’t providing input. This is the first, key part of a real game loop: it processes user input, but doesn’t wait for it. while (true) { processInput(); // doesn’t wait update(); // advances game simulation one step render(); // draws a frame on the screen } What do each of these functions do?

A game keeps moving even when the user isn’t providing input. This is the first, key part of a real game loop: it processes user input, but doesn’t wait for it. while (true) { processInput(); // doesn’t wait update(); // advances game simulation one step render(); // draws a frame on the screen } We want this to run at a fixed number of frames per second, no matter how many objects exist, no matter how complex the physics, and no matter what fast the CPU is. We don’t want the game to run too fast or too slow.

A simple fix: wait. If we want 60 FPS, then we have 16 milliseconds per frame. while (true) { double start = getCurrentTime(); processInput(); update(); render(); sleep(start + MS_PER_FRAME - getCurrentTime()); }

A simple fix: wait. If we want 60 FPS, then we have 16 milliseconds per frame. while (true) { double start = getCurrentTime(); processInput(); update(); render(); sleep(start + MS_PER_FRAME - getCurrentTime()); } Calling sleep() makes sure the game doesn’t run too fast. But it doesn’t help if the game runs too slowly.

Let’s try something a bit more sophisticated. The problem we have basically boils down to: 1.Each update advances game time by a certain amount. 2.It takes a certain amount of real time to process that. If step two takes longer than step one, the game slows down. But if we can advance the game by more than 16ms of game time in a single step, then we can update the game less frequently and still keep up.

Choose a time step to advance based on how much real time passed since the last frame. double lastTime = getCurrentTime(); while (true) { double current = getCurrentTime(); double elapsed = current - lastTime; processInput(); update(elapsed); // here is the magic render(); lastTime = current; } Say you’ve got a bullet shooting across the screen. With a fixed time step, in each frame, you’ll move it according to its velocity. With a variable time step, you scale that velocity by the elapsed time.

Looks good: The game plays at a consistent rate on different hardware. Players with faster machines are rewarded with smoother gameplay. But, alas, there’s a serious problem lurking ahead: we’ve made the game non-deterministic and unstable. Consider a two-player networked game, where one computer runs at 100 FPS and one runs at 50 FPS. A bullet travels across each screen for one second, at 100 meters (in game space) per second. Computer one: 100 meters/second * (100 *.01 seconds) = Computer two: 100 meters/second * (50 *.02 seconds) = Floating point rounding error! Also, many physics engines don’t handle changing time steps.

Better idea: fixed time steps and flexibility as to when we render: double previous = getCurrentTime(); double lag = 0.0; while (true) { double current = getCurrentTime(); double elapsed = current - previous; previous = current; lag += elapsed; processInput(); while (lag >= MS_PER_UPDATE) // not MS_PER_FRAME { update(); // our old friend fixed update lag -= MS_PER_UPDATE; } render(); }

One issue is left, “residual lag”. Update happens at a fixed interval, but rendering is less frequent than updating, and not as steady. Potentially, motion looks jagged or stuttery. Conveniently, we actually know exactly how far between update frames we are when we render: it’s stored in lag. We bail out of the update loop when it’s less than the update time step, not when it’s zero. That leftover amount? That’s how far into the next frame we are. When we go to render, we’ll pass that in: render(lag / MS_PER_UPDATE);

Some additional thoughts. Who owns the game loop – the engine/platform or the game programmer? Is power consumption important? In particular, with mobile devices you might sacrifice some frame rate and “sleep” between frames. There are some trade-offs between more complex coding and greater adaptability to a range of CPU speeds.

Unity’s Mono Engine: