C++ Module II.pdf

(2788 KB) Pobierz
C++ Module II
C++ Programming for
Game Developers
Module II
e-Institute Publishing, Inc.
87275036.001.png
©Copyright 2005 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, C++ Programming for Games II
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
Table of Contents
MODULE II OVERVIEW........................................................................................................................................................1
CHAPTER 10: INTRODUCTION TO TEMPLATES ..........................................................................................................2
I NTRODUCTION ........................................................................................................................................................................3
C HAPTER O BJECTIVES .............................................................................................................................................................3
10.1 C LASS T EMPLATES ...........................................................................................................................................................4
10.1.1 Class Template Definition .......................................................................................................................................7
10.1.2 Class Template Implementation ..............................................................................................................................7
10.1.3 Class Template Instantiation...................................................................................................................................8
10.2 E XAMPLE : A T ABLE T EMPLATE C LASS .............................................................................................................................9
10.2.1 Table Data...............................................................................................................................................................9
10.2.2 Class Interface.......................................................................................................................................................10
10.2.3 The destroy Method ..........................................................................................................................................11
10.2.4 The resize Method ............................................................................................................................................11
10.2.5 The Overloaded Parenthesis Operator .................................................................................................................13
10.2.6 The Table Class.....................................................................................................................................................13
10.3 F UNCTION T EMPLATES ..................................................................................................................................................17
10.3.1 Example Program .................................................................................................................................................18
10.4 S UMMARY ......................................................................................................................................................................21
10.5 E XERCISES .....................................................................................................................................................................21
10.5.1 Template Array Class............................................................................................................................................21
10.5.2 Template Bubble Sort Function.............................................................................................................................22
10.5.3 Table Driver ..........................................................................................................................................................22
CHAPTER 11: ERRORS AND EXCEPTION HANDLING ...............................................................................................23
I NTRODUCTION ......................................................................................................................................................................24
C HAPTER O BJECTIVES ...........................................................................................................................................................24
11.1 E RROR C ODES ................................................................................................................................................................24
11.2 E XCEPTION H ANDLING B ASICS ......................................................................................................................................26
11.3 A SSERT ..........................................................................................................................................................................29
11.4 S UMMARY ......................................................................................................................................................................31
11.5 E XERCISES .....................................................................................................................................................................31
11.5.1 Exception Handling...............................................................................................................................................31
CHAPTER 12: NUMBER SYSTEMS ...................................................................................................................................33
I NTRODUCTION ......................................................................................................................................................................34
C HAPTER O BJECTIVES ...........................................................................................................................................................34
12.1 N UMBER S YSTEMS .........................................................................................................................................................34
12.1.1 The Windows Calculator .......................................................................................................................................35
12.2 T HE B INARY N UMBER S YSTEM ......................................................................................................................................37
12.2.1 Counting in Binary ................................................................................................................................................37
12.2.2 Binary and Powers of 2 .........................................................................................................................................38
12.2.3 Binary Arithmetic ..................................................................................................................................................39
Addition .......................................................................................................................................................................................... 39
Subtraction...................................................................................................................................................................................... 41
Multiplication ................................................................................................................................................................................. 43
12.2.4 Converting Binary to Decimal ..............................................................................................................................43
12.2.5 Converting Decimal to Binary ..............................................................................................................................44
12.3 T HE H EXADECIMAL N UMBER S YSTEM ...........................................................................................................................45
12.3.1 Counting in Hexadecimal......................................................................................................................................45
12.3.2 Hexadecimal Arithmetic ........................................................................................................................................46
Addition .......................................................................................................................................................................................... 46
i
Subtraction...................................................................................................................................................................................... 47
Multiplication ................................................................................................................................................................................. 48
12.3.3 Converting Hexadecimal to Binary .......................................................................................................................48
12.3.4 Converting Binary to Hexadecimal .......................................................................................................................49
12.4 B ITS AND M EMORY ........................................................................................................................................................50
12.5 B IT O PERATIONS ............................................................................................................................................................51
12.5.1 AND.......................................................................................................................................................................51
12.5.2 Inclusive OR ..........................................................................................................................................................52
12.5.3 NOT .......................................................................................................................................................................52
12.5.4 Exclusive OR .........................................................................................................................................................53
12.5.5 Shifting ..................................................................................................................................................................53
12.5.6 Compound Bit Operators ......................................................................................................................................54
12.6 F LOATING -P OINT N UMBERS ..........................................................................................................................................54
12.7 S UMMARY ......................................................................................................................................................................56
12.8 E XERCISES .....................................................................................................................................................................57
12.8.1 Binary Arithmetic ..................................................................................................................................................57
12.8.2 Hex Arithmetic.......................................................................................................................................................57
12.8.3 Base Conversions ..................................................................................................................................................58
12.8.4 Bit Operations .......................................................................................................................................................59
12.8.5 Binary to Decimal .................................................................................................................................................61
12.8.6 Decimal to Binary .................................................................................................................................................61
12.8.7 Bit Operation Calculator.......................................................................................................................................61
12.9 R EFERENCES ..................................................................................................................................................................62
CHAPTER 13: STL PRIMER................................................................................................................................................63
I NTRODUCTION ......................................................................................................................................................................64
C HAPTER O BJECTIVES ...........................................................................................................................................................64
13.1 P ROBLEMS WITH A RRAYS ..............................................................................................................................................64
13.2 L INKED L ISTS .................................................................................................................................................................66
13.2.1 Theory ...................................................................................................................................................................66
13.2.2 Traversing .............................................................................................................................................................71
13.2.3 Insertion ................................................................................................................................................................72
13.2.4 Deletion .................................................................................................................................................................73
13.3 S TACKS ..........................................................................................................................................................................74
13.3.1 Theory ...................................................................................................................................................................74
13.3.2 Stack Operations ...................................................................................................................................................76
13.4 Q UEUES .........................................................................................................................................................................78
13.4.1 Theory ...................................................................................................................................................................78
13.4.2 Queue Operations .................................................................................................................................................78
13.5 D EQUES .........................................................................................................................................................................80
13.5.1 Theory ...................................................................................................................................................................80
13.5.2 Deque Operations .................................................................................................................................................81
13.6 M APS .............................................................................................................................................................................81
13.6.1 Theory ...................................................................................................................................................................81
13.6.2 Insertion ................................................................................................................................................................81
13.6.3 Deletion .................................................................................................................................................................82
13.6.4 Traversal ...............................................................................................................................................................82
13.6.5 Searching...............................................................................................................................................................83
13.7 S OME A LGORITHMS .......................................................................................................................................................84
13.7.1 Functors ................................................................................................................................................................84
13.7.2 Some More Algorithms..........................................................................................................................................88
13.7.3 Predicates..............................................................................................................................................................90
13.8 S UMMARY ......................................................................................................................................................................91
13.9 E XERCISES .....................................................................................................................................................................93
13.9.1 Linked List .............................................................................................................................................................93
13.9.2 Stack ......................................................................................................................................................................93
ii
13.9.3 Queue ....................................................................................................................................................................94
13.9.4 Algorithms .............................................................................................................................................................94
CHAPTER 14: INTRODUCTION TO WINDOWS PROGRAMMING ...........................................................................95
I NTRODUCTION ......................................................................................................................................................................96
C HAPTER O BJECTIVES ...........................................................................................................................................................97
14.1 Y OUR F IRST W INDOWS P ROGRAM .................................................................................................................................97
14.2 T HE E VENT D RIVEN P ROGRAMMING M ODEL ...............................................................................................................103
14.2.1 Theory .................................................................................................................................................................103
14.2.2 The MSG Structure...............................................................................................................................................103
14.3 O VERVIEW OF C REATING A W INDOWS A PPLICATION ..................................................................................................104
14.3.1 Defining the Window Procedure .........................................................................................................................105
14.3.2 The WNDCLASS Structure ...................................................................................................................................108
14.3.3 WNDCLASS Registration .....................................................................................................................................110
14.3.4 CreateWindow.....................................................................................................................................................110
14.3.5 Showing and Updating the Window ....................................................................................................................112
14.3.6 The Message Loop...............................................................................................................................................113
14.4 Y OUR S ECOND W INDOWS P ROGRAM ...........................................................................................................................113
14.5 S UMMARY ....................................................................................................................................................................116
14.6 E XERCISES ...................................................................................................................................................................117
14.6.1 Exit Message .......................................................................................................................................................117
14.6.2 Horizontal and Vertical Scroll Bars....................................................................................................................117
14.6.3 Multiple Windows................................................................................................................................................117
14.6.4 Change the Cursor ..............................................................................................................................................117
14.6.5 Blue Background .................................................................................................................................................118
14.6.6 Custom Icon ........................................................................................................................................................119
CHAPTER 15: INTRODUCTION TO GDI AND MENUS...............................................................................................122
I NTRODUCTION ....................................................................................................................................................................123
C HAPTER O BJECTIVES .........................................................................................................................................................123
15.1 T EXT O UTPUT ..............................................................................................................................................................124
15.1.1 The WM_PAINT Message....................................................................................................................................124
15.1.2 The Device Context .............................................................................................................................................124
15.1.3 TextOut ............................................................................................................................................................125
15.1.3 Example Program ...............................................................................................................................................126
15.2 S HAPE P RIMITIVES .......................................................................................................................................................131
15.2.1 Drawing Lines .....................................................................................................................................................131
15.2.2 Drawing Rectangles ............................................................................................................................................137
15.2.3 Drawing Ellipses .................................................................................................................................................141
15.3 L OADING AND D RAWING B ITMAPS ..............................................................................................................................142
15.3.1 Loading ...............................................................................................................................................................142
15.3.2 Rendering ............................................................................................................................................................145
15.3.3 Deleting ...............................................................................................................................................................146
15.3.4 Sample Program..................................................................................................................................................146
15.4 P ENS AND B RUSHES .....................................................................................................................................................150
15.4.1 Pens .....................................................................................................................................................................150
15.4.2 Brushes................................................................................................................................................................151
15.5 S HAPE C LASSES ...........................................................................................................................................................152
15.5.1 Class Definitions .................................................................................................................................................152
15.5.2 Class Implementations ........................................................................................................................................154
15.6 M ENUS .........................................................................................................................................................................157
15.6.1 Creating a Menu Resource..................................................................................................................................157
15.6.2 Loading a Menu and Attaching it to a Window...................................................................................................160
15.6.3 Checking Menu Items ..........................................................................................................................................160
15.6.4 Selecting Menu Items ..........................................................................................................................................161
iii
Zgłoś jeśli naruszono regulamin