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 –

Slides:



Advertisements
Similar presentations
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Advertisements

SCRATCH Lesson Two – Interactive Concepts Using Sensing, Operators, and Variables.
Game Programming Patterns Game Loop
Section 3-1 to 3-2, 3-5 Drawing Lines Some of the material in these slides may have been adapted from university of Virginia, MIT, and Åbo Akademi University.
Programming Types of Testing.
TU/e Processor Design 5Z032 1 Processor Design 5Z032 The role of Performance Henk Corporaal Eindhoven University of Technology 2009.
THQ/Gas Powered Games Supreme Commander and Supreme Commander: Forged Alliance Thread for Performance.
3.3. G AME I NPUT Handling input within games. In lecture exploration of answers to frequently asked student questions.
How do games work? Game Workshop July 4, Parts Sprites/pictures Map/background Music/sounds Player character Enemies Objects.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
1 Lecture 6 Performance Measurement and Improvement.
Traffic Light Behavior IF A=1 AND B=0 Car Sensors B A.
Game Design and Programming. Objectives Classify the games How games are design How games are implemented What are the main components of a game engine.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
ART: Augmented Reality Table for Interactive Trading Card Game Albert H.T. Lam, Kevin C. H. Chow, Edward H. H. Yau and Michael R. Lyu Department of Computer.
Chapter 1 and 2 Computer System and Operating System Overview
Computer Science 1620 Programming & Problem Solving.
Chapter 4 Assessing and Understanding Performance
1 Chapter 4. 2 Measure, Report, and Summarize Make intelligent choices See through the marketing hype Key to understanding underlying organizational motivation.
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.
CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA.
Gearbox Software PRODUCTION PIPELINE – JOBS TITLES – JOB DESCRIPTIONS.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
Chapter 1: Introduction To Computer | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 1 Introduction To Computers.
1 CS101 Introduction to Computing Lecture 19 Programming Languages.
PacMan by Midway, released 1980 CSE 380 – Computer Game Programming Real-Time Game Architecture.
The Need for Speed. The PDF-4+ database is designed to handle very large amounts of data and provide the user with an ability to perform extensive data.
Games Development 2 Entity / Architecture Review CO3301 Week
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSE 381 – Advanced Game Programming 3D Game Architecture.
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
London April 2005 London April 2005 Creating Eyeblaster Ads The Rich Media Platform The Rich Media Platform Eyeblaster.
CS101 Introduction to Computing Lecture Programming Languages.
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Objects and Methods in Scratch 1. 2 Scratch environment Stage is at upper right (where actors act and information is displayed) Sprite is another name.
1 CS/EE 362 Hardware Fundamentals Lecture 9 (Chapter 2: Hennessy and Patterson) Winter Quarter 1998 Chris Myers.
4.1 Advanced Operating Systems Desktop Scheduling You are running some long simulations. In the mean time, why not watch an illegally downloaded Simpsons.
Software Development Process.  You should already know that any computer system is made up of hardware and software.  The term hardware is fairly easy.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Scratch Programming Lesson 4 Question asking and answering.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Game Maker Terminology
2.1. T HE G AME L OOP Central game update and render processes.
Reference: The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)
Games Development Game Architecture: Entities CO2301 Games Development 1 Week 22.
Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1.
Introduction to Interactive Media Interactive Media Tools: Authoring Applications.
Game Programming Patterns Game Loop From the book by Robert Nystrom
Intermediate 2 Computing Unit 2 - Software Development.
SCRIPT PROGRAMMING WITH FLASH Introductory Level 1.
Maths & Technologies for Games Graphics Optimisation - Batching CO3303 Week 5.
Marble Racer. The screen where you can see what happens when you play your game is called the STAGE. The SCRIPT BANK is where the types of instructions.
CRE Programming Club Class 8 Robert Eckstein and Robert Heard.
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Image Processing A Study in Pixel Averaging Building a Resolution Pyramid With Parallel Computing Denise Runnels and Farnaz Zand.
Unit 1: Computing Fundamentals. Computer Tour-There are 7 major components inside a computer  Write down each major component as it is discussed.  Watch.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Starting Out with Games & Graphics in C++ Tony Gaddis Chapter 8 The.
Fall UI Design and Implementation1 Lecture 13: Animation.
Interactive computability Episode 13 0 Hard-play machines (HPMs) Easy-play machines (EPMs) Definition of interactive computability The interactive version.
CS F EBRUARY 9, 2016 P ARTS G AME A RCHITECTURE, M EMORY, AND I/O S YSTEMS G AME T IMING G AME T IMING G AME P ARALLELISM G AME P ARALLELISM.
Project Information Abstract Project Objectives The objective of this project is to: Create a visual designer that will allow inexperienced end- users.
Reference: What is it? A multimedia python library – Window Management – Graphics geometric shapes bitmaps (sprites) – Input Mouse Keyboard.
CS101 Introduction to Computing Lecture 19 Programming Languages
Game Engines By James Tedder.
3D Game Development Time and game loop Jernej Vičič.
Central game update and render processes
Title of Project Joseph Hallahan Computer Systems Lab
Games Development Game Architecture: Entities
Games Development 2 Entity / Architecture Review
Presentation transcript:

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 – everything outside the computer. So, outside the computer, we have (at the very least) the player. Inside the computer we have all sorts of data, but the most important is what we call the game state: it contains all the dynamic information of the game: the position of the player, monsters and camera; the current time; the score – in essence, everything that changes with time.

A simple model A computer game can then be understood as a system in which those two entities – player and game state – interact with each other according to a specific pattern of rules. The player inputs data into the system, and the system uses the game state to generate output to display to the player. The difference is how that behaves in respect with time.

The Game Loop Applications are developed in a reactive way. With games, your code is constantly being invoked, even in the absence of user input. void GameClass::main() { init(); runMainLoop(); deInit(); } while (running) { processInput(); updateGameState(); drawScreen(); } Main Loop

A More Sophisticated Main Loop The loop pseudo-code above suffers from a major flaw: it will run at unpredictable speeds – on fast machines it might run thousands of times per second (which would be a waste), and on slow machines it might run on unacceptably slow speeds – perhaps only five updates per second or so. Besides the obvious issues, this makes the game much harder to program – it’s much easier to write your game logic when you know that you’ll have a fixed number of steps per second.

A More Sophisticated Main Loop Consider this: the player presses the right arrow, and you store a flag containing that information. Now, on every step, regardless of any player action, you’ll move the player to the right by 1 unit in the game state (which, let’s say, corresponds to 1 pixel on the screen). If the game runs at 5 Hz (that is, 5 updates per second), the player will move VERY slowly. If it runs at 5000 Hz, he’ll be out of the screen before you can even see it!

Select one of the shown genre of game (assuming a 2D game). Think about the game in terms of how it looks, the features that it has, the things that it does. Try to identify common aspects that most games in the genre will share in terms of features and behaviour. Easy: Hard: Puzzle game Adventure game Racing game Platform game Rhythm game

Things most 2D game have... Front-end (titles, menus) Assets Graphical assets (animations, backgrounds) Sound assets (sfx, background music) Objects In-game objects (sprites, platforms, etc.) HUD objects (score, lives, time, etc.) Object Containers Levels, Areas, Maps Input Events {other things as needed} Things most 2D games do... Once per game/per level Load assets Construct objects Populate containers Lots of times / second Consider input events Update objects Draw graphics Consider sounds {other things as needed} Note: There is no correct answer. Each model will impose a certain set of assumptions, suiting some types of game but not others.

Game Container construct { Build objects() } update { Update objects() } draw { Draw objects() } object Game Object construct { Load assets() Build containers() } Game Engine run { loop { Update active layers() Draw visible layers() } Game consists of objects (player, collectables, score, etc.). Assets are loaded at runtime and can be managed via an asset manager Input events may be ‘consumed’ by the main game loop, layers and/or objects Objects reside within a container (front-end, game level, config screen, etc.) Asset Manager Input Events There may be separate update/draw management for game containers

T HE U PDATE /D RAW L OOP The central mechanism to evolve and render game objects

Timers ● Most games require a timer to manage the game loop ● Early games used the machine as the timer ○ the update/render loop would run as fast as possible (tied to processor speed) ○ used as typically no spare CPU resource and common platform hardware ○ Approach does not scale to varied platforms or increased hardware capabilities

Early Approaches ● Early solution was to use a fixed frame rate as the timer (with one update/render tick every frame) set up fixed rate ticker ( 30/s ) when( tick event ) { update() render() } while( running ) { record tStart update() render() record tEnd if( tEnd-tStart < tickPeriod ) wait( tickPeriod – (tEnd-tStart)) } Why is this problematic? ● UPS reduced if update and draw take too long ● game runs slower, not just displayed with lower FPS

Assume you have access to the following methods ● update() – update all game objects ● render() – draw all game objects ● time() – get the current time (ms) ● sleep(ms) – sleep for specified number of ms And the following constraint and target: ● ups –number of update/s (constraint) ● fps – equal to ups (target) Develop an algorithm that adapts to different update and render loads. Hint: The fps should be reduced to ensure the ups remain on target. Optional: Produce an algorithm that compensates for timing/sleep inaccuracies (see SleepTimerTest in the Java Code Repository)

while( running ) { tBefore = time() update() render() tAfter = time() sleepTime = updatePeriod - (tAfter - tBefore) - overTime if( sleepTime > 0 ) { sleep( sleepTime) overTime = time() - tAfter - sleepTime } else { timeBeyondPeriod = timeBeyondPeriod - sleepTime overTime = 0 while( timeBeyondPeriod > updatePeriod ) { update() timeBeyondPeriod = - updatePeriod }}}}}} updatePeriod = 1000 / UPS timeBeyondPeriod = 0 overTime = 0 Period Loop 1 Loop 2 s o r u Period Loop n Loop m

● Previous slide can be viewed as a form of semi-decoupling ● The AI (update) runs at a fixed rate as before, but the frame (draw) rate just runs as fast as it possibly can (however, no real point running faster than the update loop).

● “Full” decoupling runs the update loop as fast as possible ● A variable interval duration and delta (change) value are used: ○ on faster machines more AI ticks/s with a smaller delta value ○ on slower machines ticks take longer and the delta value will be larger

When designing game objects decouple the update and render behaviours. Ask two separate questions: ‘how will I update this’ and ‘how will I draw this’ e.g. for an animation, select the animation to be display in the update phase and render it in the draw phase The draw phase should not change the game state (i.e. properties, etc. of objects, it should simply visualise the game state)