Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop ...
-
Upload
mildred-cummings -
Category
Documents
-
view
228 -
download
0
Transcript of Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop ...
![Page 1: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/1.jpg)
Object Oriented Analysis & Design
Game Patterns
![Page 2: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/2.jpg)
2
Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component Object Pool Singleton Decorator Observer Factory Method Decorator Composite
![Page 3: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/3.jpg)
3
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
![Page 4: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/4.jpg)
4
Delegation 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();
}}
![Page 5: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/5.jpg)
5
Delegation 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
![Page 6: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/6.jpg)
6
Delegation 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
![Page 7: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/7.jpg)
7
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
![Page 8: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/8.jpg)
8
Game Loop Solution
Create a loop that repetitively performs the operations in rendering a frame in a game
while(keepRendering){
getUserInput();updateScene();renderFrame();
}
![Page 9: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/9.jpg)
9
Game Loop
![Page 10: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/10.jpg)
10
Scene Graph 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
![Page 11: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/11.jpg)
11
Scene Graph 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
![Page 12: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/12.jpg)
12
Scene Graph 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
![Page 13: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/13.jpg)
13
Scene Graph
![Page 14: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/14.jpg)
14
Double Buffering 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
![Page 15: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/15.jpg)
15
Double Buffering 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
![Page 16: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/16.jpg)
16
Implementation Most modern graphics systems have
automated support for double buffering Usually, you just configure your graphics
system to use double buffering
![Page 17: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/17.jpg)
17
Component 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
![Page 18: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/18.jpg)
18
Component 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
![Page 19: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/19.jpg)
19
Component
![Page 20: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/20.jpg)
20
Object Pool 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.
![Page 21: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/21.jpg)
21
Object Pool 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
![Page 22: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/22.jpg)
22
Object Pool
![Page 23: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/23.jpg)
23
Object Pool Implementation
![Page 24: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/24.jpg)
24
Singleton 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
![Page 25: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/25.jpg)
25
Singleton 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
![Page 26: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/26.jpg)
26
Singleton Implementation
class Singleton {
private:static Singleton *theInstance;Singleton() { ... }public:static Singleton* getInstance() {if(! theInstance) theInstance = new Singleton();return theInstance;}
}
![Page 27: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/27.jpg)
27
Decorator 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
![Page 28: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/28.jpg)
28
Decorator 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
![Page 29: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/29.jpg)
29
Decorator
![Page 30: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/30.jpg)
30
Decoratorclass 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; }
}
![Page 31: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/31.jpg)
31
Decoratorclass 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;}
}
![Page 32: Object Oriented Analysis & Design Game Patterns. Contents What patterns are Delegation Game Loop Scene Graph Double Buffering Component](https://reader036.fdocuments.in/reader036/viewer/2022081504/5697bf841a28abf838c86fc1/html5/thumbnails/32.jpg)
32
Decorator