ebook img

C# game programming : for serious game creation PDF

426 Pages·2011·4.336 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview C# game programming : for serious game creation

C# Game Programming: For Serious Game Creation Daniel Schuller CourseTechnologyPTR ApartofCengageLearning Australia•Brazil•Japan•Korea•Mexico•Singapore•Spain•UnitedKingdom•UnitedStates C#GameProgramming: †2011CourseTechnology,apartofCengageLearning. ForSeriousGameCreation ALLRIGHTSRESERVED.Nopartofthisworkcoveredbythecopyrighthereinmay DanielSchuller bereproduced,transmitted,stored,orusedinanyformorbyanymeansgraphic, electronic,ormechanical,includingbutnotlimitedtophotocopying,recording, PublisherandGeneralManager, scanning,digitizing,taping,Webdistribution,informationnetworks,or CourseTechnologyPTR: informationstorageandretrievalsystems,exceptaspermittedunderSection107 StacyL.Hiquet or108ofthe1976UnitedStatesCopyrightAct,withoutthepriorwritten AssociateDirectorofMarketing: permissionofthepublisher. SarahPanella ManagerofEditorialServices: Forproductinformationandtechnologyassistance,contactusat HeatherTalbot CengageLearningCustomer&SalesSupport,1-800-354-9706 MarketingManager:Jordan Forpermissiontousematerialfromthistextorproduct, Castellani submitallrequestsonlineatcengage.com/permissions Furtherpermissionsquestionscanbeemailedto SeniorAcquisitionsEditor:Emi [email protected] Smith ProjectEditor:JennyDavidson VisualStudio2010ExpressWebinstallerisaregisteredtrademarkof TechnicalReviewer:JamesE.Perry MicrosoftCorporationintheUnitedStatesandothercountries. InteriorLayoutTech:MPSLimited, TortoiseSVNisatrademarkofCollabNet,Inc.sfxrwascreatedby AMacmillanCompany DrPetter.NUnitisacopyrightofNUnit.org.BitmapFontCreatorisa CoverDesigner:MikeTanamachi copyrightofandreasjo¨nsson. CD-ROMProducerBrandonPenticuff Allothertrademarksarethepropertyoftheirrespectiveowners. Indexer:BroccoliIndexingServices Allimages†CengageLearningunlessotherwisenoted. Proofreader:SaraGullion LibraryofCongressControlNumber:2010922097 ISBN-13: 978-1-4354-5556-6 ISBN-10:1-4354-5556-8 eISBN-10:1-4354-5623-8 CourseTechnology,apartofCengageLearning 20ChannelCenterStreet Boston,MA 02210 USA CengageLearningisaleadingproviderofcustomizedlearningsolutions withofficelocationsaroundtheglobe,includingSingapore,theUnited Kingdom,Australia,Mexico,Brazil,andJapan.Locateyourlocalofficeat: international.cengage.com/region CengageLearningproductsarerepresentedinCanadabyNelson Education,Ltd. Foryourlifelonglearningsolutions,visitcourseptr.com Visitourcorporatewebsiteatcengage.com Printed in the United States of America 1 2 3 4 5 6 7 12 11 10 Contents Introduction ..................................................xii PARTI BACKGROUND ........................................ 1 Chapter1 TheHistoryofC# ...................................... 3 C#Basics...................................................... 3 VersionsofC# ............................................... 5 C#2.0 ....................................................5 Generics............................................... 5 AnonymousDelegates................................... 9 C#3.0 ...................................................10 LINQ ................................................. 10 LambdaFunctions...................................... 12 ObjectInitializers ...................................... 12 CollectionInitializers ................................... 12 LocalVariableTypeInferenceandAnonymousTypes........ 13 C#4.0 ...................................................14 DynamicObjects....................................... 14 OptionalParametersandNamedArguments............... 15 Summary .................................................... 17 Chapter2 Introducing OpenGL .................................. 19 ArchitectureofOpenGL........................................ 21 Vertices—TheBuildingBlocksof3DGraphics ...................21 vi Contents vii ThePipeline................................................22 OpenGLIsChanging........................................... 24 OpenGLES.................................................25 WebGL ....................................................25 OpenGLandtheGraphicsCard.................................. 25 Shaders—ProgramsontheGraphicsCard.......................27 TheTaoFramework ........................................... 28 Summary .................................................... 30 Chapter3 ModernMethods..................................... 31 PragmaticProgramming ....................................... 31 GameProgrammingTraps....................................32 KISS.......................................................32 DRY.......................................................32 C++.....................................................36 SourceControl..............................................37 UsingSourceControl ......................................39 UnitTesting................................................40 TestDrivenDevelopment ..................................44 UnitTestinginC#.........................................44 Summary .................................................... 44 PARTII IMPLEMENTATION.................................... 47 Chapter4 Setup ............................................... 49 IntroducingVisualStudioExpress—AFreeIDEforC#............... 49 AQuickHelloWorld.........................................50 VisualStudioExpressTips ....................................53 AutomaticRefactoringandCodeGeneration .................53 Renaming............................................. 54 CreatingFunctions ..................................... 56 SeparatingChunksofCode.............................. 57 Shortcuts ................................................60 Subversion,anEasySourceControlSolution ...................... 60 WheretoGetIt.............................................60 Installation.................................................60 CreatingtheRepository......................................60 AddingtotheRepository ....................................64 History ....................................................67 viii Contents ExtendingHelloWorld.......................................68 Tao ......................................................... 69 NUnit ....................................................... 71 UsingNUnitwithaProject ...................................72 RunningTests ..............................................75 AnExampleProject..........................................76 Summary .................................................... 80 Chapter5 TheGameLoopandGraphics........................... 81 HowDoGamesWork?......................................... 81 ACloserLookattheGameLoop ..............................82 ImplementingaFastGameLoopinC# ........................... 82 AddingHigh-PrecisionTiming ................................89 Graphics..................................................... 91 FullScreenMode............................................94 Rendering .................................................96 ClearingtheBackground...................................96 Vertices..................................................98 Triangles.................................................99 ColoringandSpinningtheTriangle.........................101 Summary ................................................... 103 Chapter6 GameStructure ..................................... 105 TheBasicPatternofaGameObject............................. 105 HandlingGameState......................................... 106 GameStateDemo............................................ 112 SettingtheScenewithProjections.............................. 114 FormSizeandOpenGLViewportSize .........................114 AspectRatio...............................................116 TheProjectionMatrix.......................................117 2DGraphics ...............................................117 Sprites...................................................... 120 PositioningtheSprite.......................................124 ManagingTextureswithDevIl ...............................125 TexturedSprites ...........................................131 AlphaBlendingSprites......................................134 ColorModulatingSprites....................................136 ASpriteandRendererClass..................................136 UsingtheSpriteClass.......................................143 Contents ix Chapter7 RenderingText...................................... 145 FontTextures................................................ 145 FontData................................................... 148 ParsingtheFontData.......................................149 UsingtheCharacterData....................................151 RenderingText .............................................. 154 CalculatingtheFPS.........................................155 V-SyncandFrame-Rate .....................................158 Profiling..................................................159 RefiningtheTextClass........................................ 159 FasterRenderingwithglDrawArrays............................ 165 ModifyingtheRenderer ....................................168 ProfilingtheBatchDrawMethod.............................169 Summary ................................................... 169 Chapter8 GameMath......................................... 171 TrigonometricFunctions ...................................... 171 PlottingaGraph...........................................171 TrigonometricFunctionsforSpecialEffects ....................176 Vectors..................................................... 179 WhatIsaVector? ..........................................179 TheLengthOperation ......................................181 VectorEquality ............................................182 VectorAddition,Subtraction,andMultiplication ...............184 NormalVectors............................................187 TheDotProductOperation..................................191 TheCross-ProductOperation ................................194 FinishingTouchestotheVectorStructure......................196 Two-DimensionalIntersection ................................. 197 Circles....................................................197 Rectangles................................................203 Tweens..................................................... 206 AnOverviewofTweening...................................206 TheTweenClass ...........................................208 UsingTweens..............................................211 Matrices.................................................... 213 WhatIsaMatrix? ..........................................214 TheIdentityMatrix.........................................215 Matrix-MatrixandVector-MatrixMultiplication ................217 x Contents TranslatingandScaling .....................................218 Rotation..................................................220 Inverse ...................................................221 MatrixOperationsonSprites ................................222 ModifyingtheSpritetoUseMatrices..........................225 Optimization..............................................226 Chapter9 MakingtheGameEngine............................. 229 ANewGameEngineProject................................... 229 ExtendingtheGameEngine................................... 233 UsingtheGameEngineinaProject...........................233 MultipleTextures ..........................................242 AddingSoundSupport ....................................... 246 CreatingSoundFiles........................................246 DevelopingaSoundManager................................247 ImprovingInput ............................................. 259 WrappingGameControllers.................................259 AddingBetterMouseSupport ...............................276 AddingKeyboardSupport...................................284 Chapter10 ASimpleSide-ScrollingShootingGame................. 289 ASimpleGame .............................................. 289 TheFirstImplementationPass.................................. 290 TheStartMenuState.......................................294 TheInnerGameState.......................................308 TheGameOverState.......................................311 DevelopingtheInnerGame ................................... 314 MovingthePlayerCharacter.................................315 FakingMovementwithaScrollingBackground.................320 AddingSomeSimpleEnemies................................323 IntroducingSimpleWeapons ................................334 DamageandExplosions.....................................343 ManagingExplosionsandEnemies ...........................353 LevelDefinitions...........................................359 EnemyMovement..........................................363 EnemyAttacks.............................................375 ContinuingIterations......................................... 380 Contents xi Chapter11 CreatingYourOwnGame............................. 385 ProjectManagement ......................................... 385 DisplayMethods............................................. 388 2DGames.................................................388 3DGames.................................................388 TypesofGames.............................................. 392 TextGames ...............................................392 PuzzleGames..............................................394 First-PersonShooterGames..................................397 StrategyGames............................................399 Role-PlayingGames ........................................401 RoguelikeGames ........................................401 Tile-BasedRole-PlayingGames.............................402 3DRole-PlayingGames ...................................405 PlatformGames............................................407 FinalWords ................................................. 410 AppendixA RecommendedReading .............................. 411 ThePracticeofProgramming .................................. 411 TheC#LanguageandSoftwareArchitecture..................... 412 MathandGraphicsProgramming .............................. 412 OpenGL .................................................... 413 Index........................................................... 415 Introduction I want to help you make your game. Everyonehasagreatgameidea,butthepathfrominitialideatofinishedproduct isnotaclearone.Thereareanintimidatingnumberofprogramminglanguages, libraries, and production methods. Evenexperienced game developers often fail torealizetheirvision.Withoutagoodsolidarchitecture,gamecodemaybecome so complicated that a developer drowns in the complexity. The more complex the code, the harderthe game becomes to change or continue developing. This book shows how to write simple, clean, reliable code by developing two basic games. These games are built using the C# programming language and OpenGL. C# is a modern, high-level programming language, so writing code is faster, with fewer programming warts to avoid. OpenGL is as close as the game industry has to a standard way to display graphics. When the book is finished, you’llhaveanexcellentcodebasetodevelopandgrow,pursuingyourownideas. In the first part of the book you’ll find a broad overview of the methodologies and libraries used to build great games. The second part introduces how to use these libraries and how to create your own reusable game library. You’ll also learn how to develop a simple scrolling shooter game, and then you’ll be pro- vided with some suggestions and tips for developing your own great game idea. The included CD has everything that you will need to start developing games. Everycodesnippetinthebookhasthefullsourcecodeandprogramincludedon xii CD-ROMDownloads xiii theCD.Therearealsosomesimplegameassetsandacollectionoflinkstouseful game development and graphics websites. CD-ROM Downloads Ifyou purchased an ebook version of this book, and the book had a companion CD-ROM, we will mail you a copy of the disc. Please send ptrsupplements@ cengage.com the title of the book, the ISBN, your name, address, and phone number.Thankyou.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.