2.3. Architectural Design I

Slides:



Advertisements
Similar presentations
2000 Prentice Hall, Inc. All rights reserved. 1 Outline 3.1Introduction 3.2Game Loop Components 3.3How to Implement in C# 3.4Adding Image to XNA Project.
Advertisements

Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
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.
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.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to XNA Game.
Developing the Game User Interface (UI) Lesson 5.
Microsoft Tech Days 2012 Cheezia: Developing a Windows Phone XNA Game Rodrigo Barretto Software Engineer - MCPD on Windows Phone
9.3. P ARTICLE S YSTEMS Development of a particle system.
Rob Miles. Creating a Working MoodLight We know that colours in XNA are stored as values which represent the amount of red, blue, green and transparency.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
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.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.
GAM 200 Club. How to Game Engine GAM 200 Club Zachary Nawar.
2.1. T HE G AME L OOP Central game update and render processes.
2.3. A RCHITECTURAL D ESIGN I Example approach for game screen management.
Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
M1G Introduction to Programming 2 5. Completing the program.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
XNA Tutorial 1 For CS134 Lecture. Overview Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager.
Rob Miles. Using data in an XNA game program An XNA game program Draw and Update methods that are called to run the game Colours are held in XNA as four.
5.3. S ECTION R OUNDUP Exploration of project hand-in and section roundup.
Rob Miles. Creating a Broken MoodLight An XNA game contains game data which is used by the Draw and Update methods – Update updates the game data – Draw.
HTBN Batches These slides are intended as a starting point for further discussion of how eTime might be extended to allow easier processing of HTBN data.
Exploring Taverna 2 Katy Wolstencroft myGrid University of Manchester.
ENGINEERING 1D04 Tutorial 4. What are we doing today? Focus Functions Scope of Variables Returning Values Objects Graphics library Aliasing Events Mouse.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Greenfoot.
Eighth Lecture Exception Handling in Java
Introducing Instructions
Recursive Exploration II
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Eclipse Navigation & Usage.
Lecture 27 Creating Custom GUIs
CSE 143 Introduction to C++ [Appendix B] 4/11/98.
CS1371 Introduction to Computing for Engineers
Java Programming: Guided Learning with Early Objects
Using a Stack Chapter 6 introduces the stack data type.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Project Snake. Project Snake Snake For this project, we’re going to make Snake This includes Making the overall game logic (using a 2D array) Handling.
Recursion Copyright (c) Pearson All rights reserved.
File Handling Programming Guides.
Stacks.
Algorithms & Pseudocode
Using a Stack Chapter 6 introduces the stack data type.
Lesson 16: Functions with Return Values
int [] scores = new int [10];
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
2.2. Architectural Design I
Do While (condition is true) … Loop
Stacks Abstract Data Types (ADTs) Stacks
Stacks.
Using a Stack Chapter 6 introduces the stack data type.
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
Using a Stack Chapter 6 introduces the stack data type.
Flowcharts and Pseudo Code
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Engineering Quality Software
Development of a particle system
True / False Variables.
CS210- Lecture 3 Jun 6, 2005 Announcements
Presentation transcript:

2.3. Architectural Design I Example approach for game screen management

Question Clinic: FAQ In lecture exploration of answers to frequently asked student questions

Game Screen Management Exploration of an example game screen manager

The need for screen management The front-end to most games will feature a series of navigatable screens (e.g. configuration, high- score, etc.). Additionally, whilst being played, many games will permit other screens (e.g. options screen) to sometimes appear on top of the main game screen. Also, the different levels, etc. within a game can also be viewed as a set of screens. It is desirable to have some means of easily controlling the management (adding, removing, displaying, etc.) of screens.

The following draws upon the Game State Management XNA tutorial Management Overview Game Screen Update { Update objects() } Draw { Draw objects() Game Engine loop { ScreenManager.Update() ScreenManager.Draw() } Screen Manager Update() { Update screens } Draw() { Consider screen rendering The core game engine makes use of a screen manager that is responsible for updating and rendering game screens. The screen manager contains a list of screens to be managed. The following draws upon the Game State Management XNA tutorial

The Screen Manager Design/implementation highlights of a screen manager class

The Screen Manager The ScreenManager contains a list of managed GameScreens. A stack of GameScreens to be updated is formed each update. Input (InputState) and output (SpriteBatch) setup is handled within the Screen Manager and made available to each screen. public class ScreenManager { List<GameScreen> screens = new List<GameScreen>(); Stack<GameScreen> screensToUpdate = new Stack<GameScreen>(); InputState input = new InputState(); SpriteBatch spriteBatch; } The Game State Management XNA tutorial uses a second list to hold the screens to be updated – but uses the list as an effective stack. I’m unsure why a Stack instance was not used – maybe it was felt that some readers would not be that familiar with a stack – although the code is arguably more straightforward and simple using a Stack instance. ScreenManager screenManager = new ScreenManager(this); Components.Add(screenManager); Aside: The ScreenManager is added to the Game instance as a drawable game component

Screen Manager (Update() method) The input state is evolved for the update tick. GameScreens are pushed onto the update stack (screens added last will be updated first) Flags are assigned initial values. public override void Update(GameTime gameTime) { input.Update(); foreach (GameScreen screen in screens) screensToUpdate.Push(screen); bool otherScreenHasFocus = !Game.IsActive; bool coveredByOtherScreen = false; // Update screens... } One good reason to make a copy of the screens list into screensToUpdate is that it avoid the horror of iterating over a collection that could change (i.e. if a GameScreen decides to add an extra GameScreen to the screens collection, then it needs to do so in a manner that does not clash with the iteration). Personally, I would prefer to maintain an addList which would be merged with the screens list at the end of the update. The Game.IsActive will return false (on Windows) if the game is minimised or something else has the input focus. On an XBox360, it will return false if the user is playing with the Dashboard.

The flags permit context specific screen update Each screen is updated in reverse add order (those added last are updated first) The flags permit context specific screen update Only the topmost ‘active’ screen is permitted to handle user input. while (screensToUpdate.Count > 0) { GameScreen screen = screensToUpdate.Pop(); screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen); if (screen.ScreenState == ScreenState.TransitionOn || screen.ScreenState == ScreenState.Active) if (!otherScreenHasFocus) screen.HandleInput(input); otherScreenHasFocus = true; } if (!screen.IsPopup) coveredByOtherScreen = true;

Screen Manager (Draw / Add / Remove) public override void Draw(GameTime gameTime) { foreach (GameScreen screen in screens) if (screen.ScreenState != ScreenState.Hidden) screen.Draw(gameTime); } All non-hidden screens are asked to draw themselves. GameScreens can be added to the manager public void AddScreen(GameScreen screen) { screen.ScreenManager = this; screens.Add(screen); } Aside: It is intended that the GameScreen instance will decide when to remove itself by calling a RemoveScreen method. Think about the usage assumptions this design decision introduces.

The Game Screen Design/implementation highlights of a game screen class

The GameScreen An enumerated type of the valid screen states is defined. The GameScreen holds flags defining if it is a pop-up screen (assumed persistent), or if it is currently exiting (valid over several frames), or another screen has the input focus (valid for this update only). A fade transition time for the screen appearing and disappearing is also defined. public enum ScreenState { TransitionOn, Active, TransitionOff, Hidden } bool isPopup = false; bool isExiting = false; bool otherScreenHasFocus; ScreenState screenState = ScreenState.TransitionOn; TimeSpan transitionOnTime = TimeSpan.Zero; TimeSpan transitionOffTime = TimeSpan.Zero;

GameScreen (Update() method) The update firstly checks to see if the GameScreen is exiting. If not, it then checks to see if it is a covered screen If not, it finally checks to see if the screen needs to transition on and become active Why is it good to declare this as virtual? public virtual void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { if (isExiting) // Deal with exit } else if (coveredByOtherScreen) // Deal with covered screen else // Check for transition on It is good to declare this as virtual as it communicate to the user of the class that the class designer expects them to provide a use specific implementation, i.e. it communicates to the user that this method is partial in its implementation. It is introduces an assumption that the user will ‘remember’ to call the base update method (problems will arise if this is not the case) Extending classes will extend Update() to provide screen specific behaviour

If exiting, wait until the fade is complete and then remove the screen If covered, fade screen out and enter hidden state Otherwise, if screen is not already active, fade it in and make active if (isExiting) { screenState = ScreenState.TransitionOff; if (!UpdateTransition(gameTime, transitionOffTime, 1)) ScreenManager.RemoveScreen(this); } else if (coveredByOtherScreen) { if (UpdateTransition(gameTime, transitionOffTime, 1)) screenState = ScreenState.TransitionOff; else screenState = ScreenState.Hidden; } else { if (UpdateTransition(gameTime, transitionOnTime, -1)) screenState = ScreenState.TransitionOn; screenState = ScreenState.Active; } The UpdateTransition method fades the screen in / out, returning true whilst doing this and false when finished

GameScreen (Draw() method) The Draw method is declared virtual, with no inheritable implementation – i.e. it’s up to each game screen how it will be drawn public virtual void Draw(GameTime gameTime) { } To explore this tutorial in greater depth, see the Game State Management tutorial from: http://creators.xna.com/education/

Summary Today we explored: An example implemen- tation of a game screen manager To do: Submit Project Development Report with details of game idea, team, development language Complete setup of development environment (XNA / Java) and play about with some tutorials, etc.