Frame Update, Level Representation & Graphics

Slides:



Advertisements
Similar presentations
CSE 380 – Computer Game Programming Tile Based Graphics Legend of Zelda, by Nintendo, released 1987.
Advertisements

Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
Game Programming Patterns Game Loop
CAP4730: Computational Structures in Computer Graphics Visible Surface Determination.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
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.
Computer Science 1620 Programming & Problem Solving.
Guide to Programming with Python
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
MOMA Display Screens K u r t R a l s k e.
IT253: Computer Organization
Tennis for Two, 1958, by William Higinbotham, Brookhaven National Lab CSE 380 – Computer Game Programming Graphics Device Management.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
IT253: Computer Organization
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Computer Organization and Assembly Language Bitwise Operators.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
Game Maker Terminology
Looping and Counting Lecture 3 Hartmut Kaiser
“The perfect project plan is possible if one first documents a list of all the unknowns.” Bill Langley.
Reference: The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)
Lecture 7: Intro to Computer Graphics. Remember…… DIGITAL - Digital means discrete. DIGITAL - Digital means discrete. Digital representation is comprised.
CSE 380 – Computer Game Programming Player Controls & Scrolling Mega Man, by Capcom, released 1988.
IT253: Computer Organization Lecture 9: Making a Processor: Single-Cycle Processor Design Tonga Institute of Higher Education.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
Game Programming Patterns Game Loop From the book by Robert Nystrom
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
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 –
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
I/O Software CS 537 – Introduction to Operating Systems.
Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.
Reference: What is it? A multimedia python library – Window Management – Graphics geometric shapes bitmaps (sprites) – Input Mouse Keyboard.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Behavior trees & The C# programming language for Java programmers
Flash Session 4: Importing Assets
2D Graphics Optimizations
Sprites (Images) and Sounds
Sound and more Animations
MORE Genre Specific Physics
CS 134 Alternate Inputs.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Intro CS – Screens and Variables
Binary Notation and Intro to Computer Graphics
CS 134 Video Game “AI”.
Animations.
Week 9 - Monday CS 113.
PYGAME.
Let’s Learn 2. Installing Pygame
Week 13: Searching and Sorting
8. Installing Pygame
Background Shapes & Collision Resolution (Top-down and Side-scrolling)
Game Loops + Part II Reference:
CS 134 More Graphics.
Huffman Coding Based on slides by Ethan Apter & Marty Stepp
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Computation as an Expressive Medium
Functions Inputs Output
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
CISC101 Reminders Slides have changed from those posted last night…
Chapter 10 Algorithms.
Game Loop Update & Draw.
Player preferences, Loading Scenes, Activating and Enabling
Guest Lecture by David Johnston
CENG 351 Data Management and File Structures
Game Programming Algorithms and Techniques
Game Programming Algorithms and Techniques
Presentation transcript:

Frame Update, Level Representation & Graphics

Today in Video Games

Homework Questions?

Homework A common problem I’ve seen is that on SOME computers, the player moves slowly and on other computers, the player moves fast. // In game logic update: spritePos[0] += 2;

Timing A common problem I’ve seen is that on SOME computers, the player moves slowly and on other computers, the player moves fast. // In game logic update: spritePos[0] += 2;

Timing // The game loop long lastFrameNS; long curFrameNS = System.nanoTime(); while (!shouldExit) { System.arraycopy(kbState, 0, kbPrevState, 0, kbState.length); lastFrameNS = curFrameNS; curFrameNS = System.nanoTime(); long deltaTimeMS = (curFrameNS - lastFrameNS) / 1000000; // Actually, this runs the entire OS message pump. window.display(); if (!window.isVisible()) { shouldExit = true; break; } // How often is this called? spritePos[0] += 2; gl.glClearColor(0, 0, 0, 1); gl.glClear(GL2.GL_COLOR_BUFFER_BIT); glDrawSprite(spriteTex, spritePos[0], spritePos[1], spriteSize[0], spriteSize[1]);

Timing Remember, the game loop is limited by graphics, so different computers will go through the loop at different speeds. Graphics prowess Vsync We need to make sure the sprite moves at a constant pixels / sec We wanted 2 pixels at 60 fps, so 120 pixels / sec Pixels/sec * sec/frame = pixels/frame

Timing System.nanoTime() Returns time as a nanosecond count Subtract the value since the last frame to figure out how much time has passed since the last frame Needs to be consistent across entire frame. Historically, games measure time in milliseconds, so I am used to converting nanoseconds to milliseconds ms = ns / 1,000,000

Timing long lastFrameNS; long curFrameNS = System.nanoTime(); while (!shouldExit) { System.arraycopy(kbState, 0, kbPrevState, 0, kbState.length); lastFrameNS = curFrameNS; // Actually, this runs the entire OS message pump. window.display(); if (!window.isVisible()) { shouldExit = true; break; } currentFrameNS = System.nanoTime(); int deltaTimeMS = (currentFrameNS - lastFrameNS) / 1000000; // Check keyboard input for player // Update positions and animations of all sprites gl.glClearColor(0, 0, 0, 1); gl.glClear(GL2.GL_COLOR_BUFFER_BIT); // Draw background(s) // Draw sprites // Draw more background(s) // Present to the player. window.swapBuffers();

Timing For C, use SDL_GetTicks() Returns time in milliseconds instead of nanoseconds. Otherwise, identical.

2D Drawing Look at any Super Nintendo era game. Lots of cool graphical effects How did they make these worlds and draw them?

2D Drawing Static ”backgrounds” Animated ”sprites” Overlayed HUD info No matter what, everything can be built off out of our single glDrawSprite function

2D Drawing

History Naive Implementation Apple II (1977) One giant array Each pixel is a byte

History Scroll BG w/ Sprites NES (1985) Separate layers BG, Sprite BG space is bigger than one screen

History Command Based PlayStation (1995) Redraw everything every frame Huge array of draw commands

Painter's Algorithm Still using Command Based hardware today. For us, there's just one command – draw sprite Everything is completely redrawn every frame. Need to make sure you draw things in the right order.

Painter's Algorithm Let's break this up into drawing order...

Backgrounds You might want to have a texture for whole background. This won't work.

Backgrounds Problems GL_MAX_TEXTURE_SIZE glGetIntegerv( GL_MAX_TEXTURE_SIZE, &val ); Often 8196 or higher on PC, 2048 on mobile Art Time Big textures take a lot of time to make You need an artist to draw every single level Solution: Tiles

Backgrounds

Backgrounds Level is 2D array of indexes Tile position: x*w y*h

Can you see the tiles here? Backgrounds Can you see the tiles here?

Backgrounds class BackgroundDef { int width; int height; int[] tiles; public int getTile(int x, int y) { return tiles[y * width + x]; } You could write a file loader, but for now just define this in code, it's easier!

Backgrounds Questions?

Sprites Unlike Backgrounds, sprites change what you see from frame to frame But each animation is unchanging.

Sprites Idea: have a ”def” for the animation.

Sprites List of frames Each ”frame” has a time and an image 1 2 1 3 4 3 100ms 100ms 100ms 100ms 100ms

Sprites class AnimationDef { public FrameDef[] frames; } class FrameDef { public int image; public float frameTimeSecs;

Sprites But wait, the AnimationDef alone is not enough to draw the current state of Ryu!

Sprites You also need to know: Where in the animation you are How much time until the next part of the animation 1 2 1 3 4 3 100ms 100ms 100ms 100ms 100ms

Sprites class AnimationData { AnimationDef def; int curFrame; float secsUntilNextFrame; public void update(float deltaTime); public void draw(int x, int y); } Every frame, the AnimationData for Ryu will change!

Level Representation Data / Defs Split up actor information into changing (Data), unchanging shared (Defs). Share Defs among all actors. Level data is Defs and per-actor data. Prototype based Combine Data and Defs. Let both change. Level data is Prototype and per-actor data.

Data / Defs class AnimationDef { String name; FrameDef[] frames; } class FrameDef { int image; float frameTimeSecs; class AnimationData { AnimationDef def; int curFrame; float secsUntilNextFrame; void update(float deltaTime); void draw(int x, int y);

Data / Defs class AnimationDef { String name; FrameDef[] frames; } class FrameDef { int image; float frameTimeSecs; class AnimationData { AnimationDef def; int curFrame; float secsUntilNextFrame; void update(float deltaTime); void draw(int x, int y); struct CharacterDef { String name; String walkAnimDef; String attackAnimDef; } class CharacterData { float x; float y; float health; bool isWalking; AnimationData curAnimation; void update(float deltaTime); void draw();

Data / Defs class AnimationDef { String name; FrameDef[] frames; } class FrameDef { int image; float frameTimeSecs; class AnimationData { AnimationDef def; int curFrame; float secsUntilNextFrame; void update(float deltaTime); void draw(int x, int y); struct CharacterDef { String name; String walkAnimDef; String attackAnimDef; } class CharacterData { float x; float y; float health; bool isWalking; AnimationData curAnimation; void update(float deltaTime); void draw(); class LevelCharacterDef { String actor; float intialX; float initialY; float initialHealth; }

Data / Defs Advantage: All Defs only exist once Easy to understand and reason about what data changes and doesn't change Avoids having ”bad to customize” fields be customizable Disadvantage: What can be customized is controlled by code

Prototype Based Rarely is EVERY system prototype based, it doesn't make much sense What would a prototype for Animation be? Choose key classes and make them prototype based

Prototype Based class AnimationDef { String name; FrameDef[] frames; } class FrameDef { int image; float frameTimeSecs; class AnimationData { AnimationDef def; int curFrame; float secsUntilNextFrame; struct CharacterDef { String name; String walkAnimDef; String attackAnimDef; } class CharacterData { float x; float y; float health; bool isWalking; AnimationData curAnimation; // No need for this, just store // CharacterData in your level directly! class LevelCharacterDef { String actor; float intialX; float initialY; float initialHealth; }

Prototype Based Advantage: Full flexibility, every single field can be customized Less classes to deal with Disadvantage: Full flexibility, every single field can be customized, including ones that could get out of sync Less data sharing can go on

Prototype Based Questions?

Summary Backgrounds are easy Simple for loop! Can have multiple backgrounds to have stuff in front of and behind sprites. Sprites are a bit harder They have state! But ultimately, you have a list of sprites and you call update() and draw() on each of them.

The Game Loop So Far while (!shouldExit) { System.arraycopy(kbState, 0, kbPrevState, 0, kbState.length); // Actually, this runs the entire OS message pump. window.display(); if (!window.isVisible()) { shouldExit = true; break; } // Check keyboard input for player // Update positions and animations of all sprites gl.glClearColor(0, 0, 0, 1); gl.glClear(GL2.GL_COLOR_BUFFER_BIT); // Draw background(s) // Draw sprites // Draw more background(s) // Present to the player. window.swapBuffers();

Next Class