Game Programming Patterns Event Queue From the book by Robert Nystrom

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Introduction to Macromedia Director 8.5 – Lingo
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Game Programming Patterns Game Loop
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
A Pipeline for Lockless Processing of Sound Data David Thall Insomniac Games.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Threading Part 3 CS221 – 4/24/09. Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The.
How do games work? Game Workshop July 4, Parts Sprites/pictures Map/background Music/sounds Player character Enemies Objects.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
GAME:IT Junior Bouncing Ball Objectives: Create Sprites Create Sounds Create Objects Create Room Program simple game.
Scratch the Cat. Object Oriented Programing Writing computer programs Based on Objects Instead of Actions Based on Data Instead of Logic.
CSE 380 – Computer Game Programming Render Threading Portal, by Valve,
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Nachos Phase 1 Code -Hints and Comments
Introduction to Processes CS Intoduction to Operating Systems.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Playing Music in Alice By David Yan Under the direction of Professor Susan Rodger July 2015.
Threaded Applications Introducing additional threads in a Delphi application is easy.
SE 320 – Introduction to Game Development Lecture 8: Animations, GUIs, Debugging and IDEs Lecturer: Gazihan Alankuş Please look at the last two slides.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Lab 6: event & input intro User Interface Lab: GUI Lab Oct. 2 nd, 2013.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
1 Web Based Programming Section 8 James King 12 August 2003.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Playing Music in Alice By David Yan Under the direction of Professor Susan Rodger July 2015.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
Concurrent Programming and Threads Threads Blocking a User Interface.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Sequence Diagrams And Collaboration Diagrams HungNM.
GAM 200 Club. How to Game Engine GAM 200 Club Zachary Nawar.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
Game Maker – Getting Started What is Game Maker?.
Lecture 20: Parallelism & Concurrency CS 62 Spring 2013 Kim Bruce & Kevin Coogan CS 62 Spring 2013 Kim Bruce & Kevin Coogan Some slides based on those.
Games Development 2 Overview & Entity IDs and Communication CO3301 Week 1.
Session 13 Pinball Game Construction Kit (Version 3):
Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Game Programming Patterns Game Loop From the book by Robert Nystrom
CRE Programming Club - Class 2 Robert Eckstein and Robert Heard.
How to write a MSGQ Transport (MQT) Overview Nov 29, 2005 Todd Mullanix.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
MULTIVIE W Slide 1 (of 21) Software Transactional Memory Should Not Be Obstruction Free Paper: Robert Ennals Presenter: Emerson Murphy-Hill.
CompSci 44.1 Game Package Introduction to Jam’s Video Game Package.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Vector program patterns Vectors contain many elements, so loops are common. Counted processing // Print the first 3 elements. for (int i = 0; i < 3; i++)
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Taverna allows you to automatically iterate through large data sets. This section introduces you to some of the more advanced configuration options for.
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.
Today Advanced JavaFX animation and 3D demos from Oracle. Threading. Winter 2016CMPE212 - Prof. McLeod1.
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Race Conditions & Synchronization
CSE 153 Design of Operating Systems Winter 19
CGHS HERITAGE QUEST PRESENTATION
January 15, 2004 Adrienne Noble
Processes Creation and Threads
Software Engineering and Architecture
Presentation transcript:

Game Programming Patterns Event Queue From the book by Robert Nystrom

Event Queue pattern The problem: how do game systems communicate, and yet stay decoupled? The pattern: A queue stores a series of notifications or requests in first-in, first-out order. The request processor later processes items from the queue.

The Event Loop Operating Systems and GUIs use events. We’ve seen mouse events, keyboard events, window closing events, etc. The events have been put into a queue by the OS. while (running) { Event event = getNextEvent(); // Handle event... }

An example Your game has a tutorial system to display help boxes after specific in-game events. The first time the player kills a monster, you want to show a “Press X to grab the loot!” message. But you don’t want the tutorial code mixed into the already complex gameplay code. So, combat code can add an “enemy died” event every time a monster is slayed. The tutorial code listens for “enemy died” events.

Another example - sound class Audio { public: static void playSound(SoundId id, int volume); }; void Audio::playSound(SoundId id, int volume) { ResourceId resource = loadSound(id); int channel = findOpenChannel(); if (channel == -1) return; startSound(resource, channel, volume); }

Suppose we use playSound like this, to play a bloop when the selected menu item changes: class Menu { public: void onSelect(int index) { Audio::playSound(SOUND_BLOOP, VOL_MAX); // Other stuff... } }; Problem 1: The API blocks the caller until the audio engine has completely processed the request. playSound is synchronous – it doesn’t return back to the caller until bloops are coming out of the speaker.

In the AI system, we use playSound() to let out a wail of anguish when an enemy takes damage from the player. But sometimes the mighty player hits two or more enemies at exactly the same time. Double playSound()s may be too loud, or jarring. Problem 2: Requests cannot be processed in aggregate. Hey – we’re running on a multicore processor. Audio should run in a separate thread. Problem 3: Requests are processed in the wrong thread. (The same one as the caller.)

Solution: decouple making a request for sound and processing the request. Problem 1: The API blocks the caller until the audio engine has completely processed the request. Let’s make playSound() defer the work and return almost immediately. void Audio::playSound(SoundId id, int volume) { pending_[numPending_] = {id, volume}; numPending_++; } And add to the Audio class’s private area: static PlayMessage pending_[MAX_PENDING]; static int numPending_; And define the struct PlayMessage: struct PlayMessage { SoundId id; int volume; }; The event queue!

Of course, the sound still has to be played somewhere – in an update() method: class Audio { public: static void update() { for (int i = 0; i < numPending_; i++) { ResourceId resource = loadSound(pending_[i].id); int channel = findOpenChannel(); if (channel == -1) return; startSound(resource, channel, pending_[i].volume); } numPending_ = 0; } }; Call update() from the main game loop, or from a dedicated audio thread.

Once we have the event queue – so far just a simple array – many improvements and variations are possible. 1.Use a ring buffer to efficiently remove single items from the beginning of the queue. (See the book for details.) 2.Aggregate multiple instances of the same request (problem 2). 3.Ensure that playSound() and update() are thread safe, so that multiple cores can be utilized (problem 3). 4.In this example, only the Audio class could read from the “single-cast” queue. A multi-reader “broadcast” queue is another option. 5.The items in the queue can be messages: requests for actions to be performed in the future events: notifications of something that already happened