Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Software Development Languages and Environments. Programming languages High level languages are problem orientated contain many English words are easier.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of 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.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Exam Questions Chain of Responsibility & Singleton Patterns Game Design Experience Professor Jim Whitehead February 4, 2009 Creative Commons Attribution.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
McGraw-Hill© 2007 The McGraw-Hill Companies, Inc. All rights reserved. 1-1.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Object Oriented Analysis & Design Game Patterns 3.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Description, Classes, Interfaces, Hierarchy, Specifics George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
(c) University of Washington08-1 CSC 143 Models and Views Reading: Ch. 18.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
“The perfect project plan is possible if one first documents a list of all the unknowns.” Bill Langley.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Games Development Game Architecture: Entities CO2301 Games Development 1 Week 22.
2.3. A RCHITECTURAL D ESIGN I Example approach for game screen management.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
M1G Introduction to Programming 2 5. Completing the program.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
Visualization Four groups Design pattern for information visualization
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Chapter 9 Advanced Assembly Modeling Techniques. After completing this chapter, you will be able to perform the following: –Create design view representations.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Abstract Factory Pattern
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Design Patterns C++ Java C#.
Factory Patterns 1.
Design Patterns C++ Java C#.
Abstract Factory Pattern
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
SE-2811 Software Component Design
Singleton Pattern Pattern Name: Singleton Pattern Context
Week 6: Time and triggers!
Design pattern Lecture 6.
Games Development Game Architecture: Entities
Presentation transcript:

Object Oriented Analysis & Design Game Patterns

Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component  Object Pool  Singleton  Decorator  Observer  Factory Method  Decorator  Composite 2

What Patterns Are  Patterns are good solutions to common problems  Patterns are NOT code  Patterns are ideas about how to solve a problem  They are general solutions to a group of closely related problems  Patterns are well-known solutions to problems  You would discover most patterns yourself... eventually 3

Delegation 4  A delegate is someone who you ask to perform a job on your behalf  In programming, a delegate is an object that performs the work of another object class Delegator { private: Delegate *delegate; public: void doSomething() { delegate->doSomething(); }

Delegation 5  Delegation allows you to change what doSomething() does by  Changing the delegator object  You could create a subclass of Delagator and override doSomething() but  You have to create an instance of Degator or Delegate  In order to change the behaviour, you have to destroy one object and create a new one  If you want to add new behaviour, you must derive a new subclass and code the logic to use this into your game

Delegation 6  By using delegation  You can alter behaviour any time by changing the delegate  You can create and use a new delegate any time without changing the code in the delegator  Delegation is more flexible than subclassing  Subclassing has early binding  The functionality is bound to the class at compile time  Delegation allows for later binding time  The functionality is bound to the delegator at run time  Late binding allows for far more flexibility

Game Loop  Description  A way to structure games that render frame-by-frame with movement calculated between frames.  Motivation  Games involve  handling user input  Performing the game logic  Rendering the game  It was recognized that this was common to all games and that there was a common way to architect all games 7

Game Loop  Solution  Create a loop that repetitively performs the operations in rendering a frame in a game while(keepRendering) { getUserInput(); updateScene(); renderFrame(); } 8

Game Loop 9

Scene Graph 10  Description  An organized technique to represent all objects in a scene to make it easier to render the scene  Motivation  As games become more complex, the number of objects grows  This makes the rendering of the scene more difficult  It makes sense to automate the rendering of the scene

Scene Graph 11  Solution  Create a data structure which will store  A reference to every object in the scene  The position, orientation and scale factor of every object  The game loop will now update the position and orientation of objects in the scene graph  The game engine can render the scene by  Traversing the scene graph  Rendering every object at the indicated position, orientation and scale

Scene Graph 12  Implementation  For a tile-based game  The graph could be stored as a 2D array of objects  The engine would traverse the array, drawing objects in the correct position, orientation, and scale  For a non-tile based game or one with complex objects  Use a graph structure  Each node has  A position, orientation and scale  A list of objects at that position  A list of child nodes whose position, orientation and scale are relative to that of the parent node

Scene Graph 13

Double Buffering 14  Description  Smooth graphics can be displayed by drawing the frame into an off-screen buffer and swapping it with the frame buffer when the drawing is complete.  Motivation  When graphics are drawn directly to the screen buffer  The screen is refreshed before the frame is completely drawn  The screen appears to flicked to the user  This is because the user is seeing part of one frame and part of the next

Double Buffering 15  Solution  The solution is to use 2 buffers  1 that is rendered onto the screen  1 into which the next scene is rendered  When the scene is rendered  The pointers to the 2 buffers are swapped  The next screen refresh will show the new scene  The old screen buffer is ready for the next scene to be rendered into it

Implementation 16  Most modern graphics systems have automated support for double buffering  Usually, you just configure your graphics system to use double buffering

Component 17  Description  Complex behaviour of an object is spread over several classes so that a change in one behaviour does not require a change in other behaviour. This also allows common behaviour to be shared among objects.  Motivation  When creating a game object it is common to  Place AI logic in the class  Place rendering logic in the class  Place physics calculations in the class  Place update logic in the class  This results in  Overly complex classes  The combination of separate logic

Component 18  Solution  Separate each responsibility into a separate class  Build the game object so it refers to the classes representing each responsibility  This has the benefits  The logic for each responsibility is separated  Several game objects can now share common behaviour by referencing the same behaviour class  You can change the logic in one behaviour class without affecting the other behaviour classes

Component 19

Object Pool 20  Description  An object pool holds a collection of objects which are expensive to create and destroy.  The pool allows objects to be used and then returned to the pool when they are no longer required. The next time an object is needed, it is take from the pool for use.  Motivation  The constant creation and destruction of objects is a very costly series of operations.  When this involves large numbers of objects, as in a particle system, this can waste a lot of time.

Object Pool 21  Solution  Create a pool which will create a large number of objects  Track the objects which are in use and the ones not in use  Provide operations  To retrieve an object not in use from the pool  To return an object to the pool and mark it available  One of the design goals will be to manage the retrieval and return of objects from and to the pool as efficient as possible  We also need the class stored in the pool to provide a method to allow the objects to be initialized before use

Object Pool 22

Object Pool Implementation 23

Singleton 24  Description  Restricting a class so that only one instance of the class can be created  Motivation  Many classes only require one instance and more than that will cause problems for the game. Consider:  One game engine  One scene graph  We need a way to ensure that only one instance of these classes can be created

Singleton 25  Solution  Make the class constructor private  Declare a static pointer to the single instance initialized to NULL.  Declare a static method getInstance() which will build an instance if there is none or return the existing instance if there is one

Singleton 26  Implementation class Singleton { private: static Singleton *theInstance; Singleton() {... } public: static Singleton* getInstance() { if(! theInstance) theInstance = new Singleton(); return theInstance; }

Decorator 27  Description  Allows visual decorations to be added or removed from a game object without deleting and recreating the game object  Motivation  During a game, the visual appearance of objects will change  A character might appear different due to a power-up  A character might glow as a result of a spell  We want to be able to add / remove these decorations  Without destroying the original object  Without changing the type of the reference to the original object

Decorator 28  Solution  The solution is to  Create the GameObject class  Make GameDecorator a subclass of GameObject  Add a pointer to GameObject within GameDecorator  Override the methods in GameDecorator to call the same methods on the GameObject the decorator has a pointer to

Decorator 29

Decorator 30 class GameObject { public: void doSomething() {// code to do something } void render() { // code to render game object } GameObject& decorate(GameObject decoratee&) { return *this;} GameObject& undecorate() { return *this; } }

Decorator 31 class GameDecorator: public GameObject { private: GameObject *decoratedObject; public: void doSomething() {decoratedObject->doSomething(); } void render() { decoratedObject->render(); // code to render game object } GameObject& decorate(GameObject decoratee&) { decoratedObject = &decoratee; return *this; } GameObject& undecorate() { return *decoratedObject; }

Decorator 32