Flyweights, Prototypes, and Evil Space Monsters: What Conventional Software Design Can Learn From Ga - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Flyweights, Prototypes, and Evil Space Monsters: What Conventional Software Design Can Learn From Ga

Description:

From Game Design, and Vice Versa. Blair Schneider McKay. Clareos, Inc. ... naughty nice. instructive escapist. free cheap expensive . . . and, one hopes, 'fun' ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 38
Provided by: bmc94
Category:

less

Transcript and Presenter's Notes

Title: Flyweights, Prototypes, and Evil Space Monsters: What Conventional Software Design Can Learn From Ga


1
Flyweights, Prototypes, and Evil Space Monsters
What Conventional Software Design Can Learn
From Game Design, and Vice Versa.
Presented to Upsilon Pi Epsilon at The
University of Michigan Dearborn Monday, April
4th, 2005
  • Blair Schneider McKay
  • Clareos, Inc.
  • http//home.comcast.net/blairschneidermckay/
  • email blairschneidermckay_at_comcast.net

Scott Deming Clareos, Inc. http//www.makefile.com
/ email sdeming_at_makefile.com
2
  • You are in a maze of twisty passages, all alike.

_

3
Agenda
  • playing at workworking at play
  • whats in a game?
  • design parallels
  • design choices
  • resources for the game developer
  • q a

4
Agenda
  • playing at workworking at play
  • whats in a game?
  • design parallels
  • design choices
  • resources for the game developer
  • q a

5
Games come in all sorts of flavors.They can be
  • relaxing frustrating
  • educational mind-rotting
  • violent non-violent
  • graphical text-based
  • noisy peaceful
  • sophisticated simple
  • uplifting offensive
  • naughty nice
  • instructive escapist
  • free cheap expensive
  • . . . and, one hopes, fun!

6
For example
7
Developing games can be
  • relaxing frustrating
  • educational mind-rotting
  • violent non-violent
  • graphical text-based
  • noisy peaceful
  • sophisticated simple
  • uplifting offensive
  • naughty nice
  • instructive escapist
  • lucrative?
  • . . . and, one hopes, fun!

8
Getting a job programming games?
  • Finally, youll need a game demo. . . . It
    doesnt matter if the game is a pixelated mess
    just show that you made the effort and you have
    the passion.
  • Melanie Cambron, Game Recruiting Goddess
  • http//www.melaniecambron.com/
  • In Introduction to Computer Game Programming
  • with DirectX 8.0 by Ian Parberry, Ph.D.
  • Read Melanies interviews at
  • http//www.gignews.com/goddess/

9
Whats so great about game programming?
  • Developing games can give you a chance to enjoy
    the practice of your craft.

Conventional programming often doesnt.
10
Whats so great about game programming?
  • When developing a game you are in complete
    control of the design and implementation.
  • The choices are yours.

( Presuming you dont work for an evil game
company.)
In conventional programming, you are generally
constrained by the (often hideous) choices of
others.
11
Agenda
  • playing at workworking at play
  • whats in a game?
  • design parallels
  • design choices
  • resources for the game developer
  • q a

12
Whats in a game?
  • user-interface
  • data validation, parsing, and processing
  • flow-control
  • state machines
  • error handling
  • interface to third-party APIs
  • graphics, sounds, file-system, network
  • mouse, keyboard, joystick, light-saber
  • artificial intelligence
  • simulations of consciousness
  • simulations of environmental conditions

13
Agenda
  • playing at workworking at play
  • whats in a game?
  • design parallels
  • design choices
  • resources for the game developer
  • q a

14
Design Parallels using low-level APIs
  • Interfacing with low-level APIs generally means
    using
  • C-style function calls, e.g. int
    retcodefn(param1, param2..)

Game Conventional DirectX ODBC,
E/SQL Direct3D TCP/IP - Sockets OpenGL PLC
/ Sensor MIDI Both File System Network
I/O Math Libraries
15
Design Parallels modular architectures 1
  • OSI 7-Layer Networking Model

Layer 7 APPLICATION
telnet, ftp, pop3, smtp
Layer 6 PRESENTATION
conversions, encrypt/decrypt, parts of odbc / html
Layer 5 SESSION
cookies, shopping basket techniques, etc.
Layer 4 TRANSPORT
TCP, data reliability (re-transmission, etc.)
Layer 3 NETWORK
IP
Layer 2 DATA-LINK
ppp, slip, hdlc, token-ring, MAC addressing
Layer 1 PHYSICAL
NIC cards, RS-232, electrical characteristics
16
Design Parallels modular architectures 2
  • A decoupled model for a biological simulation

Mod USER INTERFACE
instrumentation to let the user manipulate
environment params
Mod ARTIFICIAL INTELLIGENCE
maybe we have smart fish, that detect and try
to avoid sharks
the environment itself might occasionally throw
your sprites a curve (perhaps, a fishing net).
Mod EVENTS
Mod MOVEMENT
wriggle, swim, or fly
Mod POPULATION MECHANICS
automatic management of birth/death,
production/consumption
Mod RENDERING
interface to low-level graphics APIs
Mod SERIALIZATION
saving the simulations current state to disk,
for later recall
17
Design Parallels Code clarity
  • An unfortunate truth in conventional programming
    many developers feel no responsibility to write
    clear, understandable, maintainable code.
  • There are many reasons for thisall of them bad.
  • In game programming, where (often) youre the
    only coder, you may be tempted to do the same
  • But remember you are also the poor soul who has
    to maintain the program!

18
Design Parallels Robustness
  • My chief complaint with the computer games Ive
    played over the years has been unexplained
    crashes and program errors.
  • My chief complaint with the conventional computer
    software Ive used over the years has been
    unexplained crashes and program errors.
  • E.g., no one will thank you if your RainForest
    Quest game dies just as Amazon Sally finds the
    Magic Ruby and is a the verge of rescuing her
    Prince Charming.

19
Agenda
  • playing at workworking at play
  • whats in a game?
  • design parallels
  • design choices
  • resources for the game developer

20
Design Choices how much to optimize?
  • Commonly Held Belief
  • The best way to achieve speed is by using
    low-level languages, like C, or even
    Assembly-Language.
  • Real-world findings
  • Modern compilers can sometimes beat hand-crafted
    assembler code.
  • Developers are terrible at guessing where code
    needs optimization.
  • Optimize late in the game, ideally only after
    profiling.
  • But, dont pessimize prematurely!

21
Design Choices using low-level APIs
  • If you are in C, you can use the Resource
    Acquisition Is Initialization (RAII) idiom

class ScreenManager public ScreenManager(
/construction params/ ) m_hwnd
CreateDefaultWindow( / / ) if (!m_hwnd)
/throw exception/ ScreenManager( )
DestroyWindow(m_hwnd) // . . .
void goToHelpScreen( ) // Obtain a DirectX
screen handle ScreenManager screen( FULL_SCRN,
PRIMARY) // Use the screen object. drawHelpGr
aphicsOn( screen.getHandle( ) ) // etc //
On exit, the screen object is automatically
destroyed, and the underlying resource handle is
freed.
22
Design Choices Polymorphism
Polymorphism is about handling different objects
as if they were all the same. (Access through a
common interface.) Consider example of an Ocean
simulation or screen-saver
A Movement sub-system iterates over a collection
of Sprites, telling each sprite when to move.
(Each sprite knows how to move itself wriggle,
swim, fly, etc.) Even though the collection
holds different types of objects, they can all be
handled as if they were the same type.
23
Design Choices the Prototype pattern
Its related to the Factory Method, but even the
factory doesnt need to know exactly how to build
each product sub-class. The sub-class itself
takes on that responsibility.
class Guppy public Sprite public virtual
Sprite clone() const // return copy of myself
virtual bool handles( SpriteType request ) //
...
SpriteFactorySpriteList my_prototypes_ Sprite
SpriteFactorygetSprite( SpriteType request )
for (/ ... each prototype in my list . .
./) if (prototype-handles(request))
return prototype-clone() // return
default type or throw exception.
24
Design Choices the Observer pattern
Here is a simplified Observer pattern
Observer
The goal of this pattern is for each (concrete)
observer object to receive updates on state
changes that occur within the subject object
without the subject object knowing any details
about each observer. This is the ultimate in
need-to-know programming.
(Diagram based on p. 294 in Design Patterns, by
Gamma et al. Addison Wesley, 1995.)
25
Observer Pattern Event Sequence
Setup
1. Observer calls attach() on Subject, passing
this pointer.
Use
2. Client of Subject (or Subject itself) calls
notify() at appropriate time. 3. In notify(),
Subject iterates through list of observers and
calls update() on each one, possibly passing
state-data.
Tear-down
4. Observer calls detach().
26
Observer Pattern Sample Implementation
Fish.cpp include Ocean.h // . .
. Fishupdate(Ocean ocean) if
(ocean-getWaterTemp() s_max) die()

Fish.h class Ocean // forward decl. class
Fish public Fish() virtual Fish()
void update(Ocean ocean) // . . .
Ocean.h include class Fish //
forward decl. class Ocean public Ocean()
virtual Ocean() void attach( Fish fish )
void detach( Fish fish ) void notify( )
double getWaterTemp() const // . . . public
stdlist fishes_
Ocean.cpp include Observer.h LevelReaderat
tach(Observer obs) observers_.push_back(obs)
LevelReaderdetach(Observer obs)
observers_.erase(obs) LevelReadernotify( )
for ( / each fish in my list / )
fish-update(this)
27
Game Uses of Observer Pattern
  • Collecting statistics and tracking history.
  • Feed data to the brain engine for AI.
  • Feed data to the rendering engine to add residual
    effects (disrupting smoke, fog.)
  • Collect/transmit data in multi-player game.
    Observer feeds separate thread to avoid
    sputtering/lag.
  • Input observer capture user input before sending
    to game engine. Pause or mute the audio without
    the game itself having to worry about it.

28
The Decorator Pattern
  • Attach additional responsibilities to an object
    dynamically.
  • Alternative to sub-classing.
  • How it works
  • The Decorator has the same interface as the type
    being decorated and contains an object of that
    type.
  • It accepts requests on behalf of the decorated
    object. It forwards them, as needed, to the
    object, and adds its own flourish.
  • Key point adding additional responsibilities to
    a specific object, not to a whole class.
  • http//www.dofactory.com/Patterns/PatternDecorator
    .aspx

29
Game Uses of the Decorator Pattern
  • Alter the visual environment.
  • Weather conditions (fog, rain, etc.)
  • Night-vision goggles
  • Line of sight
  • Alter the musical environment.
  • Increase tempo during exciting moments without
    changing the musical score
  • Add special effects based on power-ups.
  • Make player sprite transparent to indicate
    invisibility
  • Lighten an entire area to indicate an
    illumination spell
  • Alter physical features
  • Change the magnetic properties of certain objects
    and watch the effect on their forces of
    attraction
  • Add density to an object in water and watch it
    sink like a rock

30
Demos of Observer and Decorator
  • DemoObs.exe dynamic registration
  • Ex2.exe transparency and searchlight
  • Ex5.exe line-of-sight decoration

31
Other Patterns
  • Flyweight
  • Support a large number of find-grained objects
    through sharing.
  • Use in games to efficiently model vast numbers of
    sprites that have only minor differences in
    state. (Make the state extrinsic to the object.)
  • Façade
  • Encapsulate all calls to a third-party API or
    low-level library within one class or set of
    classes.
  • For example, put a façade between your game logic
    and DirectX, OpenGL, etc.
  • Memento
  • Serialize an objects state for later recall.
  • Use for save game and for multi-player network
    traffic.

32
Agenda
  • playing at workworking at play
  • whats in a game?
  • design parallels
  • design choices
  • resources for the game developer
  • q a

33
Resources Clanlib
  • http//www.clanlib.org/
  • Cross-platform C framework / Game SDK
  • Released under LGPL 2.1. Next release under
    Mozilla Public License 1.1.
  • DEMO cl_test.exe

34
Resources DirectX
  • http//www.microsoft.com/windows/directx/
  • SDK enables high-performance graphics and sound,
    without requiring hardware-specific code.
  • Faster, richer than Windows GDI.
  • Drawback Windows-specific.

35
Resources OpenGL
  • http//www.opengl.org/
  • High-performance. (Quake III, Doom III, etc.)
  • Portable.

36
Final Thoughts What matters to us.
  • Scott
  • Focus on GAME PLAY, please!
  • (Too many bloody shooters out there.)
  • Blair
  • There is always a need for new and better
    educational games.
  • The standards logic puzzles, math, spelling,
  • Simulation technology can now teach all sorts of
    things
  • cooperation / conflict resolution
  • problem solving
  • the scientific method

37
Questions, Answers, and Cowardly
Evasions.
Also, please feel free to email questions
Blair Schneider McKay http//home.comcast.net/bl
airschneidermckay/ email BlairSchneiderMcKay_at_co
mcast.net Scott Deming http//www.makefile.com/
email sdeming_at_makefile.com
Write a Comment
User Comments (0)
About PowerShow.com