Tải bản đầy đủ - 0 (trang)
Chapter 1. What it Takes to be a Game Programmer

Chapter 1. What it Takes to be a Game Programmer

Tải bản đầy đủ - 0trang

handledonacomputer.Ifyou'veeverdrawnsomethingusing

thepaintprogramthatcomeswithWindows,youknowthe

basicsofcreatingartonacomputer.Ifyouknowhowtorip

MP3s,youknowhowtopreparemusicforgames.Butifyou've

neverdoneeitherofthese,don'tworry.I'llteachyouhowto

handlebothartandmusicforyourgames.

Thischapterpresentsabriefoverviewofthemostessential

skillsyou'llneed:programming,computergraphics,game

design,art,andtheabilitytousesoundandmusic.Becauseit's

anoverview,don'tworryifyourunacrosssomethingthatyou

don'tunderstand.Everythingdiscussedinthischapteris

presentedagainingreaterdetailinlaterchapters.







ProgrammingSkills

Peopleareoftenfearfuloflearningtoprogramcomputers.

Programmingcangetprettyhairyattimes,butlearningto

programproficientlyisnotasdifficultasitmayseem.Infact,if

youliketotinkerwiththingsandfindouthowtheywork,

programmingoftenseemslikeplay.

Thefirststepinlearningtoprogramistounderstandwhata

programis.



WhatisaComputerProgram?

Note

Computerprogramsarejustcomplexsetsof

instructions.Theinstructionstellthecomputerhow

toperformatask,suchasplayingagame.



Imagineyouwanttobakeacake.Ifyou'relikeme,youdon't

knowhowtodothat.Inthatcase,you'dprobablygotoa

cookbookandfindarecipe.Therecipeisasetofinstructions.If

youfollowtheinstructionsexactly,yougetacake.Ifnot,you

maywindupwithafoul-tastingmess.

Acomputerprogramislikearecipe.It'sasetofinstructions.

Theinstructionsinaprogramtellthecomputerhowtobea

gamemachine.Ifyouwritetheinstructionsproperly,yougeta

game.Ifnot,yougetamess.

Eachinstructioninacomputerprogramismadeupofoneor



morestatementsinaprogramminglanguage.

Awhat?

Computersdon'tunderstandhumanlanguageslikeEnglishor

Japanese.Infact,theydon'treally"understand"anythingatall.

Butthecomputer'smicroprocessor,whichisalsocalledits

centralprocessingunit(CPU)canexecutecommands.



Note

Everycomputerhasamicroprocessor.Youcanthink

ofthemicroprocessorasthecomputer's"brain."



Tobeexecutable,thecommandsinaprogrammustbein

binary.Ifthey'renot,themicroprocessorwon'tbeableto

executethem.

Sowhat'sbinary?

Binaryisanumbersystem.It'salsocalledthebase2number

system.Peoplenormallyusethedecimal,orbase10,number

system.Inotherwords,wecount0,1,2,3,4,5,6,7,8,9,10,

andsoon.Binaryisbase2,soyoucount0,1,10,11,100,

101,110,111,1000,andsoforth.Youreallydon'thaveto

knowthedetailsofusingbinaryinordertoprogramcomputers.

Allyouhavetoknowisthatabinarynumberisagroupof0s

and1s.Allinstructionsyougivetoamicroprocessormustbe0s

and1s,orthemicroprocessorwon'tbeabletoexecutethem.

Whenaprogramisrunning,allofitsbinaryinstructionsare

storedasbitsinyourcomputer'smemory.Abitissimplya

binary0or1.Therefore,thebinarynumber10is2bitslong.In

computers,thebitsinmemoryoronadiskaregrouped



together.Agroupof8bitsisabyte.Thebinarynumber

10011100is8bitslongsoitfitsin1byte.Agroupof1024

bytesisakilobyte;1024kilobytesisamegabyte;1024

megabytesisagigabyte;1024gigabytesisaterabyte.Thisis

illustratedinTable1.1.

Table1.1.GroupingsofBitsandBytesina

Computer

AGroupof...



Equals



8bits



1byte



1024bytes



1kilobyte



1024kilobytes



1megabyte



1024megabytes



1gigabyte



1024gigabytes



1terabyte



TheC++ProgrammingLanguage

WhenIstartedincomputerprogramming,youhadto

understandbinarytobeabletowriteprograms.Therewasn't

reallyanywaytoavoidit.Thesedays,that'snotnecessary.

Consideryourselflucky.Programminginbinaryisn'tmuchfun.

Instead,youcannowwriteprogramsinlanguagesthatare

similartothelanguagespeoplespeak.Mostgamesarewritten

inalanguagecalledC++(pronouncedsee-plus-plus).

TheC++programminglanguageenablesyoutowrite

statementsthatletyoucontrolallpartsofacomputer,suchas

thedisplay,soundcard,orjoystick.WithC++statements,you



candisplayaliensonthescreen,lettheplayercontrolaspace

fighter,andshootthealiensintospacedust.C++alsoletsyou

playmusicandaddsoundeffects.Itisafast,efficientlanguage

thatyoumustknowtobeaprofessionalgameprogrammer.

C++programsstartasC++instructionsthatwestoreintext

files.Atextfileisjustwhatitsoundslike:It'safileonthedisk

thatcontainstextlettersandnumbers.C++textfilesarealso

calledsourcefiles.TheentirecollectionofC++instructionsis

calledthesourcecodeoftheprogram.



ToolsoftheTrade

Recallthatmicroprocessorsonlyexecutecommandsthatare

binarynumbers.C++isnotbinary;it'smorelikeEnglish.How

doyoutranslateC++statementsintobinarycommands?

Figure1.1givestheanswer.



Figure1.1.TranslatingaC++sourcefileinto

binary.



[Viewfullsizeimage]



Figure1.1showsthatthere'safairamountofworkinvolvedin

gettingsourcecodetranslatedintoanexecutablebinary

program.Inthenextfewsections,I'llexplaineachstep.



Compilers

Figure1.1showsthatyouneedaspecialprogramcalleda

compilertotranslateyourC++sourcecodeintobinary.

Therefore,you'llneedacompilertowriteyourgames.I've

providedyouoneforfree.It'scalledDev-C++andyou'llfindit

ontheCDthatcomeswiththisbook.Theinstructionsfor

installingitareintheIntroduction.



Linkers



Compilerstranslatesourcecodeintoanintermediateform

calledobjectcode.Objectcodeisbinary,butitisnot

executable.Objectcodemustbeconvertedtoexecutablecode,

whichisanactualprogramthatyoucanrunonyourcomputer.

Thetoolthatconvertsobjectcodetoexecutablecodeisa

linker.

You'llneedalinkertocreateyourgames.Alinkerisincluded

withtheDev-C++compilerontheCDandit'sinstalledwhen

youinstallDev-C++.Wheneveryoucompileyourprogramwith

Dev-C++,thelinkerrunsautomatically,soyourprogramis

bothcompiledandlinkedinonestep.



Note

ToaccesstheDev-C++compiler,justopentheDVD

andgotothe\Tools\DevCPPfolder.Ifyouhavenot

installedDev-C++onyourcomputer,pleaseturnto

theIntroductionanddothatnow.



Warning

AlloftheC++sourcecodeyouwillencounterinthis

bookisdesignedtoworkwithDev-C++.Itmayor

maynotcompilewithothercompilers,suchas

Microsoft'sVisualC++.Ideally,C++sourcecode

shouldworkwithallcompilers,butinreality,subtle

differencesexistbetweencompilersproducedby

differentcompanies.Therefore,Istrongly

recommendthatyouuseDev-C++forallthe

programsinthisbook.Afteryoubecomean

experiencedC++programmer,youshouldhaveno

problemcompilingthisbook'sprogramswithVisual



C++withonlyminimalchanges.



WhatIsDev-C++?

Althoughyou'lloftenseemerefertoDev-C++asacompiler,itisreallyan

integrateddevelopmentenvironment(IDE).AnIDEcontainseverythingyou

needtocompileaprogram.ThisincludesaprogramtoedityourC++source

files,acompiler,alinker,andadebugger(linkersanddebuggerswillbe

explainedshortly).Thecompiler,linker,anddebuggerthattheDev-C++IDE

usesareWindowsversionsoftheGNUCompilerCollection(GCC).TheGCCisan

opensourceprojectthatiswrittenbyalargeandvibrantdevelopercommunity.

YoucanfindtheGCCathttp://gcc.gnu.org.TheWindowsversionoftheGCCis

alsoanopensourceprojectwhosehomepageiswww.mingw.org.



Debuggers

It'snotpossibleforustowriteperfectgamesinjustone

trywe'llmakealotofmistakes.Inprogramming,mistakesare

calledbugs.It'snormalforprogrammerstoaccidentallycreate

thousandsofbugsineachgametheywrite.Yes,Ididsay

thousands.

Asaresultofthemistakeswemake,youandIneedatoolto

helpusfindandfixbugs.Appropriatelyenough,thattoolis

calledadebugger.YougetadebuggerforfreewithDev-C++.

Likethelinker,it'sinstalledautomatically.



GraphicsLibraries

Manyofthetasksinvolvedingamesareuniversalingame

programming.Forexample,severaltimesasecond,everytype

ofgamegetsuserinput,reactstoit,andupdatesthecontents

ofthescreen.Nomatterwhattypeofgameyou'rewriting,your

gamefollowsthisdesign.



Thebasictasksinallgamesarebyfarsomeofthehardest.It's

noteasytodrawcircles,lines,andsoforthonthescreen.Alot

ofverysmartpeoplehadtothinkforalongtimetolearnhow

todothesetasksefficiently.Themethodstheyusearevery

advancedandveryinvolved.Thisstuffisnoteasy,anditused

tobethecasethateverygameprogrammerhadtoknowhow

todoitall.

Fortunately,younolongerhavetobotherwithlearninghowto

dothesebasictasks.Whenyouwriteyourgames,you'llusea

toolcalledagraphicslibrary.Agraphicslibrarycontainscode

thatdoesallthebasicgraphicstaskssuchasdrawinglinesand

circles,ordisplayingpicturesonascreen.Thelibraryusesall

thebestmethodsandtechniquesthatweredevelopedbyallthe

smartpeopleImentionedintheprecedingparagraph.Asa

result,youalreadyhavecodeforthebasictasksyou'lldowhen

youwritegames.Youdon'thavetowriteityourself.

Bytheway,thegraphicslibraryyou'lluseiscalledOpenGL.

OpenGLisusedinhigh-poweredgamessuchasQuakeand

DoomfromidSoftware.ThereisaversionoftheOpenGL

graphicslibraryforDev-C++.Theinstructionsforinstallingit

areintheIntroduction.

MicrosoftalsoprovidesagraphicslibrarycalledDirectX

Graphics(morecommonlycalledDirect3D).LikeOpenGL,

Direct3Disusedforprofessionalgames.Direct3Dispartof

DirectX,whichcontainslibrariesforaddingsound,music,and

networkingtogames.

WhetheryouuseOpenGLorDirect3Dislargelyamatterof

preference.Theybothwillgetyouwhereyouneedtogo.

AlthoughthisbookprimarilyusesOpenGL,I'vealsoprovided

theMicrosoftDirectXSoftwareDevelopmentKit(SDK)onthe

DVDthatcomeswiththisbook.It'sinthefolder\Tools\Microsoft

DirectXSDK.



Tip



Ifyou'replanningonbecomingaprofessionalgame

programmer,it'sbesttobefamiliarwithboth

OpenGLandDirectX.



IfyouhavenotinstalledDev-C++andOpenGLonyour

computer,pleaseturntotheIntroductionanddothatnow.



GameEngines

Gamesthatareofthesametypeprettymuchallworkthesame

way.Forexample,allfirst-personshooterslikeDoomneedcode

thatdoesessentiallythesamesetoftasks.Allside-scrolling

games,suchastheoldSuperMarioBrothersorSonicthe

Hedgehog,alsousecodethatdoesbasicallythesamethings.

Tomakegameprogrammers'liveseasier,otherprogrammers

oftendevelopgameengines.Agameengineperformsthemost

commontasksthataparticulartypeofgamedoes.For

instance,agameengineforfirst-personshootershandlesallof

thetasksassociatedwithdisplayingscenery,drawingbadguys,

andsoforth.Youhavetoinsertyourownscenery,codeforyour

weapons,andanyothercodethatisspecifictoyourgame.The

gameenginehandlesdisplayingbuildings,trees,andthingslike

that.Figure1.2showshowgameenginesareused.



Figure1.2.Gamesarebuiltinlayersofcode.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 1. What it Takes to be a Game Programmer

Tải bản đầy đủ ngay(0 tr)

×