Home > Article > Anatomy of a Game Engine

Anatomy of a Game Engine

Anatomy of a Game EngineBⅴ: Richard bald,Online

org/licenses/by/30Collection structure revised3/02/7and attribution information for the modules contained in this collection see the Attributions" section at the end of the

Compile and run the programCreate a batch filee Execute the batch fileKun the programSummarye What's next· Miscellaneous· Download source cod1 2 Preface· Viewing tipo magesTurning the crankAs a professor of Computer Information Technology at Austin Community College, I teachcourses in game programming using both C++ and C#/XNA

I have long had a concern thatstudents enter my courses expecting to simply turn the crank"on a game engine such as DarkGDK or XNA and have great games emerge from the other end of the process UnfortunatAnatomy of a game engineGiven time limitations and other restrictions, it is not practical to teach those students much aboutsuch game engines Therefore, I have decided to publish a series of moduleson the anatomy of a game engine that my students, (and other interested parties can read to learntbout those inner workingsFirst in a collectionTherefore, this module is the first in a collection of modules designed to teach you about theanatomy of a typical game engine (sometimes called a game framework)

The Slick2D libraryave chosen to concentrate on a free game library named slick2D, (which is written in Java)foeveral reasonsIding the followingJava is the language with which i am the most comfortable hence, i can probably do a betterob of explaining the anatomy of a game engine that uses Slicked than would be the case for agame engine written in C++, C#, Python, or some other programming languageJava has proven in recent years to be a commercially successful game programming languageFor example, I cite the commercial game named Minecraft, written in Java, for whichapparently millions of copies have been sold Also, knowing Java is very beneficial for thosewho might want to develop apps for AndroidSlick2D is free and the source code for Slick2D is readily available

The overall structure of a basic SlickeD game engine is very similar to Dark gDK and XNAand is probably similar to other game engines as weJava is platform independentApplicable to other environments as welAlthough the modules in this collection will concentrate on the Java game library named Slick2Dhe concepts involved and the knowledge that you will gain is applicable to other game engineswritten in different programming languagesPurposeThe purpose of this module is to get you started, including showing you how to download andtall Slick2D, and how to compile and execute your first Slick2D program Future modulesstart digging into and explaining the inner workings of a basic Slick2D game engineWhat you should knowThis series of modules is not intended for beginning programmers As a minimum, you shouldtalprogramming conceptsatements, for loops, whileloops, method or function calls, parameter passing, etc Ideally, you will have some objectoriented programming knowledge in a modern programming language such as Java, C#, C++, orpossibly python or JavaScripttrees batch or script files etcFinally, you should also be comfortable downloading and installing software on the machine and

operating system of your choiceWhat you will learnIn thisearn how to download and install slicked on a windows xP vista orWindows 7 machine and how to compile and execute a very simple slicked program (If you areusing a different operating system, you will need to translate this information to your system ofchoice However, since Java is platform independent, the code details that / will discuss will applyto all or most platformsViewing tipcommend that you open another copy of this module in a separate browser window and use thefollowing links to easily find and view the figures and listings while you are reading about themmageImage 1

Output from Slicked during program startupImage 2 a default Slick2D game windonListingsListing 1 Slick2D program named Slicko100a javaListing 2 The file named CompileAndrun bat13 PreviewMost of the slick2D tutorials that you will find on the Internet will begin by telling you todownload and install a high-level IDE such as Eclipse or NetBeans I won't do thatWhile high-level IDEs are great for improving productivity for experienced programmers, Iconsider them to be overkill for students just learning how to program Not only are they overkithey also hide many details that beginning programmers need to understandTherefore, I will show you how to install Slick2D in such a way that you can easily compile andexecute Slicked programs from the command line with no need for a highIDE All you will

need is a text editor(preferably color coded for Java syntax), the free Slick2d distribution, andthe free Java development kit from Oracle1 4 Download the required softwareText editorsThe slicked distributionThe Java Development KitText editorsThere are numerous free text editors available on the internet some with and some without javasyntax color coding (In a pinch, even windows Notepad will suffice

)Here are links to a few ofGRASP· DrAwaArachnophiliaThe slicked distributionwill be using this material in some of the Java OoP programming courses that I teach I expectthat changes and improvements will be made to the slick2D library over time However, it can bevery confusing when different students in the same programming course are using differentversions of software, particularly if changes to the software are made that are not backwardTherefore, I will make a copy of the slicked distribution available by clicking here so that mystudents can all download and use the same version in my coursesIf you are not one of my students, you may prefer to go to the slickeD main page and select theload the latest version of the distribution save that file beve more toay about it later

USing whatever program you can find to open a zip file( use a program named winZip), extractand save the following files from the slick2d distribution file that you downloaded earlierck jnatives-win 32 iarThere are many other files in the slicked distribution file, but we dont need them just yet If weneed them in a future module, I will tell youThese three files are needed to satisfy the classpath and java library path requirements that i wdescribe laterThe first two jar filesCopy the first two jar files from the above list into the folder named jarss rese need to execute the following command in order to set theclasspath whenever you compile or execute a Slick2D programcp ijars/slick

jar; /jars/wjgljarThe third jar fileshould find a similar file in the Slick2D distribution that applies to your system ent system, yoThe third file in the above list applies to windows only If you are using a diffExtract contents of the jar fileUSing whatever program you can find to open a jar file (l use a program named winzip), extractthe following files from the file named natives-win32jarjinput-dx8dIljinput-rawdI· lwjgl dllOpenal32 dllCopy these four files into the folder named lwjglbinAs you will see later, this results in the need to execute the following command in order to set thejavalibrary path sys

tem property

Djava library path=lwjgnbin(These files can also be stored in the folder from which the program is being run and this willeliminate the requirement to set the java library path if you prefer that approach16 Create, compile, and execute your first Slick2DprogramCreate a source-code fileCompile and run the programo Create a batch fileo Execute the batch fileCreate a source-code fileUse your text editor to create a text file named slick0100a java and store it in the folder namedSlicko100a(Be careful to ensure that the file has the correct extension, particularly if you create it withWindows notepad An extension of txt wont work)Carefully copy the code from Listing linto the text file This is the file that you will attempt tocompile and run to confirm correct operation of your system/*Slick0100a javaCRG BaldwinA simple program that shows the method definitrequired by the slick frameworkTested using JDK 1

7 under WinXPmport org newdawn slick AppGame Containermport org newdawn slick BasicGamenewdawn slick GameContainermport org newdawn slick Graphicsmport orgck SIickExceptio

Anatomy of a Game EngineTable of contentsChapter l slicko100: Getting started with the slickeD game libralo 11 Table of contentso 1 2, PrefaceViewing tipListio13 PreviewDownload the required softwareText editoThe slickeD distributiona The Java Development Kito 16 Create, compile, and execute your first SlickeD programCreate a source-code fileCompile and run the programCreate a batch fileExecute the batch fileo lZ Run the programSummaryo19 What's nexto111 Download source code· Chapter2, SlickerI0overvIewo

Table of contentso 2 2 PrefaceViewing tipListingso 23 The bottom line at the topo 2 4 Preview25, What is a game engine?A software framework7 Discussion and sample codeA service provider program■ Two primary objectsBehavior of an obiect of the App Game Container class

Behavior of an object that implements the Game interfaceed Slick011oaThe game interfaceun the programo 29 Summaryo 210 What's next?o 211, MiscellaneousChapter 3 slicko120: Starting your programo 3 1, Table of contentso2 Preface■ lewing tipListingDiscussion and sample code■ Two primary objectsa Behavior of an object of the App Game Container classa Behavior of an object that implements the game interfaceThe constructors for the App Game Container classThe setup method of the App game Container classThe get Delta method of the Game Container classThe gameLoop method of the Appgame Container classo 36 Run the programo 3

7 Summaryo 3 8 What's next?3 10 Complete program listingChapter 4 slicko130: The game loopo 41 Table of contentso PrefaceViewing tipImagesListingso 43 PreviewThe property named runningThe gameLoop methodder methoda Calls to the update methoda Calls to the render methoda Overall structure of a game programDiscussion and sample code

The program named slicko130■ The screen outputBegimed slicko130aThe main methodThe overridden init methodThe overridden update methodn The overridden render method■ End of discussionRun the program0 47 Summaryo 8 What's next?o 4 10 Complete program listingChapter 5 slicko140: A first look at SlickeD bitmap graphicso 51 Table of Contentso 52 PrefaceViewing tiListings53 Preview5 4

General background informati55 Discussion and sample codeBeginning of the class named Slick0140aThe main methodThe overridden init methodAn empty update methodn The overridden render metho56 Run the programO 5Suro 58 What's next?o Miscellaneouso 510 Complete program listingChapter 6 Slicko150: A first look at sprite motion, collision de■ lewing tipo 63 Preview6 4 General background informati65 Discussion and sample codeA program with a relatively constant frame rate- Slick0150aThe screen output for Slicko150a

Beginning of the class named Slick015(The constructor and the main methodThe init method■ The render methodA program with a highly variable frame rate- Slicko150bThe screen output for Slicko150bThe render methodThe update methoo 66 Run the programs67 Summaryo 6 8 What,s next69 Miscellaneouso 6 10 Complete program listingsChapter 1 slicko160: Using the draw and draw Flash methodso 7 1

Table of Contents07 2 PrefaceViewing tip■ Listingso 73 Preview74background informatio 75 Discussion and sample codeThe program named Slick0160aa Beginning of the Slick0160a class■ The main methodThe init methodThe update methodThe render methoda The program named Slick0160bBeginning of the class named Slicko160bThe update methodThe render methodRun the programs0 17 SummaryMiscellaneous710 Complete program listing8So81 Table of contentso82 PrefaceViewing tip

o83 Previewo 84 Genero 85 Discussion and sample code6 Run the programmo8 What's next?o89 Miscellaneouso 8 10 Complete program listingChapter 9 Slicko180: Sprite sheet animation, part Io 91 Table of ContentsPrefaceViewing tip■ Listingso 3 Previewo 94 Generkground informationThe spritesheet classThe Animation class95 Discussion and sample codeThe class named slicko180The init methodThe update methodThe render methodo 26

Run the programaSummaryo 8 What's next?99 Miscellaneouso 9 10 Complete program listingChapter 10 Slick0190: Sprite sheet animation, part 2o 101 Table of Contentso 10 2 PrefaceViewing tip03 Preview10 4 General baThe SpritesheetThe Animation class105 Discussion and sample codeThe class named slicko190The init methodThe update method

The render methodo 106 Run the programl summary0108, What's next?o109 Miscellaneouso 1010 Complete program listingChapter ll slick0200: Developing a sprite classo1 Table of Contento 1 2, Preface■ Viewing tipListingo 115 Discussion and sample codeThe class named spritedThe class named slick0200The init methodThe update methodThe render methodo 116 Run the programo IL7 Summaryoil

8 What's next?o 119, Miscellaneous1110CChapter 12 slick0210: Collision dond soundo121Tontentso2 PrefaceViewing tipLio 12,3 Previewo 124 General background informationo 12,5 Discussion and sample codeThe class named SpritedThe class nam■ The init methodThe update methodollision method of the Spriteol classThe render method12 6 Run the programo12 8 What's next?

o12 9 Miscellaneouso 1210 Complete program listingsChapter 13 Slick0220: Simulating a pandemico 131, Table of contentso 2 PrefaceViewing tip■ Listings0133, Previewo 13 4

General background informationo 135 Discussion and sample codeThe class named spriteolThe class namk0220The update methodThe render methodo 136 Run the program013 8 Conclusiono9 Miscellaneouso 13 10 Complete program listil

Chapter 1 Slick0100: Getting started with the slick2Dgame libraryIt is licensed under the creative commons attribution licensehttp://creativecommons

org/licenses/b2013/02/0708:38:05-0600SummaryLearn how to install Slick2D in such a way that you can easily compile and execute Slick2Dprograms from the command line with no need for a high level IDE11 Table of contents· PrefaceViewing tipImagesListingse PreviewDownload the required software· Text editorsThe slick 2D distributionThe ava Development KInstall the required softwareCreate, compile, and execute your first Slick2D programo Create a source-code file