C - - Module I.pdf
(
1445 KB
)
Pobierz
C++ Programming for Games
C++ Programming for Games
Module I
e-Institute Publishing, Inc.
1
©Copyright 2004 e-Institute, Inc. All rights reserved. No part of this book may be reproduced or
transmitted in any form or by any means, electronic or mechanical, including photocopying, recording,
or by any information storage or retrieval system without prior written permission from e-Institute Inc.,
except for the inclusion of brief quotations in a review.
Editor:
Susan Nguyen
Cover Design:
Adam Hoult
E-INSTITUTE PUBLISHING INC
www.gameinstitute.com
Frank Luna, Game Institute Faculty,
C++ Programming for Games
All brand names and product names mentioned in this book are trademarks or service marks of their
respective companies. Any omission or misuse of any kind of service marks or trademarks should not be
regarded as intent to infringe on the property of others. The publisher recognizes and respects all marks
used by companies, manufacturers, and developers as a means to distinguish their products.
E-INSTITUTE PUBLISHING titles are available for site license or bulk purchase by institutions, user
groups, corporations, etc. For additional information, please contact the Sales Department at
sales@gameinstitute.com
2
Table of Contents
CHAPTER 1: INTRODUCING C++....................................................................................................................................... 9
I
NTRODUCTION
...................................................................................................................................................................... 10
C
HAPTER
O
BJECTIVES
........................................................................................................................................................... 10
1.1
G
ETTING
S
TARTED
—Y
OUR
F
IRST
C++
P
ROGRAM
.......................................................................................................... 10
1.1.1 Creating the Project................................................................................................................................................ 10
1.1.2 Adding A .CPP File to the Project .......................................................................................................................... 12
1.1.3 Writing the Code ..................................................................................................................................................... 13
1.1.4 Compiling, Linking, and Executing......................................................................................................................... 14
1.2
T
HE
“P
RINT
S
TRING
”
P
ROGRAM
E
XPLAINED
................................................................................................................... 16
1.2.1 Comments................................................................................................................................................................ 16
1.2.2 White Space............................................................................................................................................................. 17
1.2.2 Include Directives ................................................................................................................................................... 18
1.2.3 Namespaces............................................................................................................................................................. 18
1.2.4 The
main{...}
Function..................................................................................................................................... 20
1.2.5 std::string ................................................................................................................................................................ 20
1.2.6 Input and Output with
std::cin
and
std::cout
............................................................................................ 20
1.3
V
ARIABLES
...................................................................................................................................................................... 21
1.3.1 Variable Declarations and Definitions ................................................................................................................... 24
1.3.2 Variable Names....................................................................................................................................................... 25
1.3.3 The
sizeof
Operator ........................................................................................................................................... 25
1.3.4 The
unsigned
Keyword ....................................................................................................................................... 26
1.3.5 Literal Assignments................................................................................................................................................. 27
1.3.6 Type Conversions .................................................................................................................................................... 27
1.3.7
Typedef
s .............................................................................................................................................................. 30
1.3.8 Const Variables....................................................................................................................................................... 30
1.3.9 Macros .................................................................................................................................................................... 30
1.4
A
RITHMETIC
O
PERATIONS
............................................................................................................................................... 31
1.4.1 Unary Arithmetic Operations.................................................................................................................................. 32
1.4.2 Binary Arithmetic Operations ................................................................................................................................. 33
1.4.3 The Modulus Operator ............................................................................................................................................ 34
1.4.4 Compound Arithmetic Operations .......................................................................................................................... 35
1.4.5 Operator Precedence .............................................................................................................................................. 36
1.5
S
UMMARY
........................................................................................................................................................................ 37
1.6
E
XERCISES
....................................................................................................................................................................... 38
1.6.1 Arithmetic Operators .............................................................................................................................................. 38
1.6.2 Cin/Cout .................................................................................................................................................................. 38
1.6.3 Cube ........................................................................................................................................................................ 38
1.6.4 Area/Circumference ................................................................................................................................................ 39
1.6.5 Average ................................................................................................................................................................... 39
1.6.6 Bug Fixing............................................................................................................................................................... 39
CHAPTER 2: LOGIC, CONDITIONALS, LOOPS AND ARRAYS ................................................................................. 41
I
NTRODUCTION
...................................................................................................................................................................... 41
C
HAPTER
O
BJECTIVES
: .......................................................................................................................................................... 42
2.1
T
HE
R
ELATIONAL
O
PERATORS
........................................................................................................................................ 42
2.2
T
HE
L
OGICAL
O
PERATORS
............................................................................................................................................... 44
2.3
C
ONDITIONAL
S
TATEMENTS
:
I
F
,
I
F
…E
LSE
...................................................................................................................... 48
2.3.1 The If Statement ...................................................................................................................................................... 49
2.3.2 The Else Clause....................................................................................................................................................... 50
2.3.3 Nested If…Else Statements...................................................................................................................................... 51
2.3.4 The Switch Statement .............................................................................................................................................. 53
2.3.5 The Ternary Operator ............................................................................................................................................. 55
3
2.4
R
EPETITION
......................................................................................................................................................................56
2.4.1 The for-loop.............................................................................................................................................................56
2.4.2 The
while
Loop.....................................................................................................................................................58
2.4.3 The
do…while
Loop..............................................................................................................................................60
2.4.4 Nesting Loops ..........................................................................................................................................................61
2.4.5 Break and Continue Keywords ................................................................................................................................62
2.5
A
RRAYS
...........................................................................................................................................................................63
2.5.1 Array Initialization ..................................................................................................................................................64
2.5.2 Iterating Over an Array...........................................................................................................................................64
2.5.3 Multidimensional Arrays .........................................................................................................................................65
2.6
S
UMMARY
........................................................................................................................................................................67
2.7
E
XERCISES
.......................................................................................................................................................................68
2.7.1 Logical Operator Evaluation ..................................................................................................................................68
2.7.2 Navigator.................................................................................................................................................................68
2.7.3 Average....................................................................................................................................................................69
2.7.4 Factorial..................................................................................................................................................................69
2.7.5 Matrix Addition .......................................................................................................................................................70
2.7.6 ASCII .......................................................................................................................................................................71
2.7.7 Linear Search ..........................................................................................................................................................71
2.7.8 Selection Sort...........................................................................................................................................................73
CHAPTER 3: FUNCTIONS ...................................................................................................................................................75
I
NTRODUCTION
......................................................................................................................................................................75
3.1
U
SER
D
EFINED
F
UNCTIONS
..............................................................................................................................................78
3.1.2 Functions with One Parameter ...............................................................................................................................80
3.1.3 Functions with Several Parameters.........................................................................................................................82
3.2
V
ARIABLE
S
COPE
.............................................................................................................................................................83
3.2.1 Example 1 ................................................................................................................................................................83
3.2.2 Example 2 ................................................................................................................................................................85
3.2.3 Example 3 ................................................................................................................................................................86
3.3
M
ATH
L
IBRARY
F
UNCTIONS
............................................................................................................................................87
3.4
R
ANDOM
N
UMBER
L
IBRARY
F
UNCTIONS
.........................................................................................................................88
3.4.1 Specifying the Range ...............................................................................................................................................91
3.5
F
UNCTION
O
VERLOADING
................................................................................................................................................92
3.5.1 Default Parameters .................................................................................................................................................94
3.6
S
UMMARY
........................................................................................................................................................................96
3.7
E
XERCISES
.......................................................................................................................................................................97
3.7.1 Factorial..................................................................................................................................................................97
3.7.2 ToUpper; ToLower..................................................................................................................................................97
3.7.3 3D Distance.............................................................................................................................................................98
3.7.4 Arc Tangent 2 ..........................................................................................................................................................99
3.7.5 Calculator Program ..............................................................................................................................................100
3.7.6 Slot Machine..........................................................................................................................................................101
3.7.7 Binary Search ........................................................................................................................................................102
3.7.8 Bubble Sort ............................................................................................................................................................103
CHAPTER 4: REFERENCES AND POINTERS ...............................................................................................................107
I
NTRODUCTION
....................................................................................................................................................................108
C
HAPTER
O
BJECTIVES
.........................................................................................................................................................108
4.1
R
EFERENCES
..................................................................................................................................................................108
4.1.1 Constant References ..............................................................................................................................................110
4.2
P
OINTERS
.......................................................................................................................................................................111
4.2.1 Computer Memory Primer ....................................................................................................................................111
4.4.2 Pointer Initialization .............................................................................................................................................112
4.4.3 Dereferencing........................................................................................................................................................114
4.3
A
RRAYS
R
EVISITED
........................................................................................................................................................117
4
4.3.1 Pointer to the Beginning of an Array .................................................................................................................... 117
4.3.2 Pointer Arithmetic ................................................................................................................................................. 118
4.3.1 Passing Arrays into Functions .............................................................................................................................. 120
4.4
R
ETURNING
M
ULTIPLE
R
ETURN
V
ALUES
...................................................................................................................... 122
4.4.1 Returning Multiple Return Values with Pointers .................................................................................................. 122
4.4.2 Returning Multiple Return Values with References .............................................................................................. 124
4.5
D
YNAMIC
M
EMORY
....................................................................................................................................................... 125
4.5.1 Allocating Memory................................................................................................................................................ 126
4.5.2 Deleting Memory................................................................................................................................................... 127
4.5.3 Memory Leaks ....................................................................................................................................................... 127
4.5.4 Sample Program ................................................................................................................................................... 128
4.6
STD
::
VECTOR
.................................................................................................................................................................. 132
4.7
F
UNCTION
P
OINTERS
...................................................................................................................................................... 135
4.7.1 The Uses of Function Pointers .............................................................................................................................. 136
4.7.2 Function Pointer Syntax........................................................................................................................................ 137
4.8
S
UMMARY
...................................................................................................................................................................... 138
4.9
E
XERCISES
..................................................................................................................................................................... 139
4.9.1 Essay Questions .................................................................................................................................................... 139
4.9.2 Dice Function........................................................................................................................................................ 140
4.9.3 Array Fill............................................................................................................................................................... 140
4.9.4 Quadratic Equation............................................................................................................................................... 141
CHAPTER 5: CLASSES AND OBJECT ORIENTED PROGRAMMING..................................................................... 144
I
NTRODUCTION
.................................................................................................................................................................... 144
C
HAPTER
O
BJECTIVES
......................................................................................................................................................... 145
5.1
O
BJECT
O
RIENTED
P
ROGRAMMING
C
ONCEPTS
.............................................................................................................. 145
5.2
C
LASSES
........................................................................................................................................................................ 146
5.2.1 Syntax .................................................................................................................................................................... 146
5.2.2 Class Access: The Dot Operator ........................................................................................................................... 148
5.2.3 Header Files; Class Definitions; Class Implementations ..................................................................................... 150
5.2.2.1 Inclusion Guards................................................................................................................................................ 152
5.2.4
D
ATA
H
IDING
:
P
RIVATE VERSUS
P
UBLIC
.................................................................................................................... 153
5.2.5 Constructors and Destructors ............................................................................................................................... 155
5.2.6 Copy Constructors and the Assignment Operator................................................................................................. 157
5.3
RPG
G
AME
:
C
LASS
E
XAMPLES
...................................................................................................................................... 158
5.3.1 The Range Structure.............................................................................................................................................. 158
5.3.2 Random Functions ................................................................................................................................................ 159
5.3.3 Weapon Class........................................................................................................................................................ 159
5.3.4 Monster Class ....................................................................................................................................................... 160
5.3.5 Player Class .......................................................................................................................................................... 165
5.3.6 Map Class ............................................................................................................................................................. 173
5.4
T
HE
G
AME
..................................................................................................................................................................... 177
5.4.1 Segment 1 .............................................................................................................................................................. 179
5.4.2 Segment 2 .............................................................................................................................................................. 180
5.4.3 Segment 3 .............................................................................................................................................................. 180
5.4.4 Segment 4 .............................................................................................................................................................. 182
5.5
S
UMMARY
...................................................................................................................................................................... 183
5.6
E
XERCISES
..................................................................................................................................................................... 184
5.6.1 Gold Modification ................................................................................................................................................. 185
5.6.2 Character Races.................................................................................................................................................... 185
5.6.3 Leveling Up ........................................................................................................................................................... 185
5.6.4 Magic Points ......................................................................................................................................................... 185
5.6.5 Random Encounters During Rest .......................................................................................................................... 186
5.6.6 A Store................................................................................................................................................................... 186
5.6.7 Items ...................................................................................................................................................................... 187
5.6.8 Multiple Enemies................................................................................................................................................... 187
5
Plik z chomika:
remy74
Inne pliki z tego folderu:
3D Math Primer for Graphics and Game Development.pdf
(22680 KB)
3D Geometry Tuts.rar
(11543 KB)
AI Game Wisdom.pdf
(38666 KB)
3D Math Primer for Graphics and Game Development Souce Code.rar
(7434 KB)
Advanced 3D Game Programming with DirectX 9 0.pdf
(14364 KB)
Inne foldery tego chomika:
Pliki dostępne do 01.06.2025
Dokumenty
Galeria
Prywatne
zachomikowane
Zgłoś jeśli
naruszono regulamin