23
Chapter 3.3 Programming Fundamentals

3.3 programming fundamentals

Embed Size (px)

Citation preview

Page 1: 3.3 programming fundamentals

Chapter 3.3Programming Fundamentals

Page 2: 3.3 programming fundamentals

2

Data Structures Arrays

Elements are adjacent in memory (great cache consistency)

They never grow or get reallocated In C++ there's no check for going out

of bounds Inserting and deleting elements in the

middle is expensive

Page 3: 3.3 programming fundamentals

3

Data Structures Linked lists

Very cheap to add/remove elements. Available in the STL (std::list) Every element is allocated separately

Lots of little allocations Not placed contiguously in memory

Page 4: 3.3 programming fundamentals

4

Data Structures 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

Page 5: 3.3 programming fundamentals

5

Data Structures Stacks

First in, last out std::stack adaptor in STL

Queues First in, first out std::deque

Page 6: 3.3 programming fundamentals

6

Data Structures Bit packing

Fold all necessary data into a smaller number of bits

Very useful for storing boolean flags (pack 32 in a double word)

Possible to apply to numerical values if we can give up range or accuracy

Very low level trick Only use when absolutely necessary

Page 7: 3.3 programming fundamentals

7

Object Oriented Design Concepts

Class Abstract specification of a data type

Instance A region of memory with associated

semantics to store all the data members of a class

Object Another name for an instance of a class

Page 8: 3.3 programming fundamentals

8

Object Oriented Design Inheritance

Models “is-a” relationship Extends behavior of existing classes by

making minor changes

Page 9: 3.3 programming fundamentals

9

Object Oriented Design Inheritance

UML diagram representing inheritance

E ne m y B o s s Supe rD upe rB o s s

Page 10: 3.3 programming fundamentals

10

Object Oriented Design 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

Page 11: 3.3 programming fundamentals

11

Object Oriented Design 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, etc)

Multiple inheritance of abstract interfaces is much less error prone

Page 12: 3.3 programming fundamentals

12

Component Systems Limitations of inheritance

Tight coupling Unclear flow of control Not flexible enough Static hierarchy

Page 13: 3.3 programming fundamentals

13

Component Systems Component system organization

Use aggregation (composition) instead of inheritance

A game entity can “own” multiple components that determine its behavior

Each component can execute whenever the entity is updated

Messages can be passed between components and to other entities

Page 14: 3.3 programming fundamentals

14

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 15: 3.3 programming fundamentals

15

Component Systems Data-Driven Composition

The structure of the game entities can be specified in data

Components are created and loaded at runtime

Very easy to change (which is very important in game development)

Page 16: 3.3 programming fundamentals

16

Component Systems Analysis

Very hard to debug Performance can be a bottleneck Keeping code and data synchronized can

be a challenge Extremely flexible

Great for experimentation and varied gameplay Not very useful if problem/game is very

well known ahead of time

Page 17: 3.3 programming fundamentals

17

Design Patterns Singleton

Implements a single instance of a class with global point of creation and access

Don't overuse it!!!Single to n

s 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 tic S in g le to n u n iq u eI n s tan c e;

Page 18: 3.3 programming fundamentals

18

Design Patterns Object Factory

Creates objects by name Pluggable factory allows for new object

types to be registered at runtime Extremely useful in game development

for creating new objects, loading games, or instantiating new content after game ships

Page 19: 3.3 programming fundamentals

19

Design Patterns Object factory

O bje c tFac to ryP r o d u c t * C r eateO b jec t (P r o d u c tT y p e ty p e) ;

C re ate P ro duc t

P ro duc t

Page 20: 3.3 programming fundamentals

20

Design Patterns Observer

Allows objects to be notified of specific events with minimal coupling to the source of the event

Two parts subject and observer

Page 21: 3.3 programming fundamentals

21

Design Patterns Observer

Subje c tAttac h ( O b s erv er * ) ;D etac h (O b s er v er * ) ;N o tif y ( ) ;

O bs e rve rUp d ate( ) ;

C o nc re te O bs e rve rUp d ate( ) ;

C o nc re te Subje c t

Page 22: 3.3 programming fundamentals

22

Design Patterns Composite

Allow a group of objects to be treated as a single object

Very useful for GUI elements, hierarchical objects, inventory systems, etc

Page 23: 3.3 programming fundamentals

23

Design Patterns Composite

C o m po s i teO p era tio n ( ) ;

lis t< C o m p o n en t* > c h ild r en

Spe c if ic C o m po ne ntO p eratio n ( ) ;

C o m po ne ntO p er a tio n ( ) ;