csce 552 spring 2009 programming fundamentals by jijun tang
TRANSCRIPT
![Page 1: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/1.jpg)
CSCE 552 Spring 2009
Programming Fundamentals
By Jijun Tang
![Page 2: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/2.jpg)
Homework #1
6 points, due on next Wednesday Turn in hard copy before the class Expected about 1 page Question: Review your popular games
(2-3), including game description, features, strength and weakness, etc.
![Page 3: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/3.jpg)
C++: Strengths
Performance High-level, object-oriented C Heritage Libraries
![Page 4: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/4.jpg)
C++: Weaknesses
Too low-level Slow iteration Too complicated Lacking features
![Page 5: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/5.jpg)
Java Performance
Has typically been Java's weak point Has improved in the last few years: still
not up to C++ level, but very close Uses Just-In-Time compiling and
HotSpot optimizations Now has high-performance libraries Also has access to native functionality
![Page 6: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/6.jpg)
Popular scripting languages
Python Lua Other off-the-shelf options such as
Ruby, Perl, Javascript Custom scripting languages
UnrealScript, QuakeC, NWNScript
![Page 7: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/7.jpg)
Data Structures: Array
Elements are adjacent in memory (great cache consistency) Requires continuous memory space
They never grow or get reallocated Use dynamic incremental array concept GCC has a remalloc function
In C++ there's no check for going out of bounds Use vector if possible Keep in mind of checking boundaries
Inserting and deleting elements in the middle is expensive
![Page 8: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/8.jpg)
List
Very cheap to add/remove elements. Available in the STL (std::list) Every element is allocated separately,
not placed contiguously in memory Lots of little allocations Bad cache awareness, but can use arrays
to hold pre-allocated items Single/Double linked list
![Page 9: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/9.jpg)
Lists
![Page 10: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/10.jpg)
Dictionaries
Maps a set of keys to some data. std::map, std::hash, etc Very fast access to data Perfect for mapping IDs to pointers, or
resource handles to objects May waste space, need to design
comparison operators
![Page 11: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/11.jpg)
Hash Table
![Page 12: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/12.jpg)
Others
Stacks First in, last out std::stack adaptor in STL
Queues First in, first out std::deque Priority queue is useful in game to schedule
events
![Page 13: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/13.jpg)
Stack/Queue/Priority Queue
![Page 14: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/14.jpg)
Bit packing
Fold all necessary data into a smaller number of bits
Bool in C++ may use up to 4 bytes, thus is very expensive
Very useful for storing boolean flags: pack 32 in an integer
Possible to apply to numerical values if we can give up range or accuracy
Very low level trick Use shifts to handle the operation or use assembly Only use when absolutely necessary
![Page 15: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/15.jpg)
Bits
![Page 16: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/16.jpg)
Game Architecture and Math
![Page 17: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/17.jpg)
Inheritance
Models “is-a” relationship Extends behavior of existing classes by
making minor changes Do not overuse, if possible, use component
systerm UML diagram representing inheritance
E ne m y B o s s Supe rD upe rB o s s
![Page 18: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/18.jpg)
Polymorphism
The ability to refer to an object through a reference (or pointer) of the type of a parent class
Key concept of object oriented design C++ implements it using virtual functions
![Page 19: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/19.jpg)
Multiple Inheritance
Allows a class to have more than one base class
Derived class adopts characteristics of all parent classes
Huge potential for problems (clashes, casting, dreaded diamond, etc)
Multiple inheritance of abstract interfaces is much less error prone (virtual inheritance)
Java has no multiple inheritance
![Page 20: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/20.jpg)
Dreaded Diamond
It is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C.
If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?
![Page 21: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/21.jpg)
Component Systems
Component system organization
G am e E nti ty
N am e = s w o r d
R e nde rC o m p C o ll is io nC o m p D am age C o m p P ic kupC o m p W ie ldC o m p
![Page 22: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/22.jpg)
Object Factory
Creates objects by name Pluggable factory allows for new object
types to be registered at runtime Extremely useful in game development
for passing messages, creating new objects, loading games, or instantiating new content after game ships
![Page 23: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/23.jpg)
Factory Pattern
![Page 24: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/24.jpg)
Simple Sample Factory - I
![Page 25: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/25.jpg)
Simple Sample Factory - II
![Page 26: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/26.jpg)
Singleton
Implements a single instance of a class with global point of creation and access
For example, GUI Don't overuse it!!!
Single to ns ta tic S in g le to n & G etI n s tan c e ( ) ;/ / R eg u lar m em b er f u n c tio n s . . .
s ta t ic S in g le to n u n iq u eI n s tan c e ;
![Page 27: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/27.jpg)
Singleton Example
![Page 28: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/28.jpg)
Adapter
Convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn't otherwise because of incompatible interfaces
Real interface
![Page 29: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/29.jpg)
Adapter Pattern
![Page 30: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/30.jpg)
Adapter Example - I
![Page 31: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/31.jpg)
Adapter Example - II
![Page 32: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/32.jpg)
Observer
Allows objects to be notified of specific events with minimal coupling to the source of the event
Two parts subject and observer
![Page 33: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/33.jpg)
Observer Pattern
![Page 34: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/34.jpg)
Composite
Allow a group of objects to be treated as a single object
Very useful for GUI elements, hierarchical objects, inventory systems, etc
![Page 35: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/35.jpg)
Composite Pattern
![Page 36: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/36.jpg)
Composite Pattern Example - I
Add many more inherited classes
![Page 37: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/37.jpg)
The Five StepDebugging Process
1. Reproduce the problem consistently
2. Collect clues
3. Pinpoint the error
4. Repair the problem
5. Test the solution
![Page 38: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/38.jpg)
Expert Debugging Tips
Question assumptions Minimize interactions and interference Minimize randomness Break complex calculations into steps Check boundary conditions, use assertions Disrupt parallel computations Exploit tools in the debugger (VC is good, purify) Check code that has recently changed Explain the bug to someone else Debug with a partner (A second pair of eyes) Take a break from the problem Get outside help (call people)
![Page 39: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/39.jpg)
Game Architecture
![Page 40: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/40.jpg)
Overall Architecture
The code for modern games is highly complex The Sims: 3 million lines of code Xbox HD DVD player: 4.7 million lines MS Train Simulator has 1GB installed, with only
10MB executable With code bases exceeding a million lines of
code, a well-defined architecture is essential
![Page 41: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/41.jpg)
Overall Architecture
Main structure Game-specific code Game-engine code
Both types of code are often split into modules, which can be static libraries, DLLs, or just subdirectories
![Page 42: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/42.jpg)
Overall Architecture
Architecture types Ad-hoc (everything accesses everything) Modular DAG (directed acyclic graph) Layered
Options for integrating tools into the architecture Separate code bases (if there's no need to share
functionality) Partial use of game-engine functionality Full integration
![Page 43: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/43.jpg)
Ad-hoc
![Page 44: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/44.jpg)
Modular
![Page 45: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/45.jpg)
DAG
![Page 46: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/46.jpg)
Layered
![Page 47: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/47.jpg)
Overview: Initialization/Shutdown
The initialization step prepares everything that is necessary to start a part of the game
The shutdown step undoes everything the initialization step did, but in reverse order
![Page 48: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/48.jpg)
Initialization/Shutdown
Resource Acquisition Is Initialization A useful rule to minimalize mismatch errors in the
initialization and shutdown steps Means that creating an object acquires and
initializes all the necessary resources, and destroying it destroys and shuts down all those resources
Optimizations Fast shutdown Warm reboot
![Page 49: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/49.jpg)
Overview:Main Game Loop
Games are driven by a game loop that performs a series of tasks every frame
Some games have separate loops for the front and the game itself
Other games have a unified main loop Must finish a loop within 0.017 second
![Page 50: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/50.jpg)
Tasks of Main Game Loop
Handling time Gathering player input Networking Simulation Collision detection and response Object updates Rendering Other miscellaneous tasks
![Page 51: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/51.jpg)
![Page 52: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/52.jpg)
Sample Game Loop
![Page 53: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/53.jpg)
Main Game Loop
Structure Hard-coded loops Multiple game loops: for each major game state Consider steps as tasks to be iterated through
Coupling Can decouple the rendering step from simulation
and update steps Results in higher frame rate, smoother animation,
and greater responsiveness Implementation is tricky and can be error-prone
![Page 54: CSCE 552 Spring 2009 Programming Fundamentals By Jijun Tang](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649eb05503460f94bb59d1/html5/thumbnails/54.jpg)
Execution Order of Main Loop
Most of the time it doesn't matter In some situations, execution order is
important Can help keep player interaction
seamless Can maximize parallelism Exact ordering depends on hardware