Chapter 2: Fundamentals of Game Programming

image from book Download CD Content

In modern game development it is common to hear the designers and developers talking about the all-inclusive term game engine. What exactly is a game engine? Most people mistakenly think it relates only to the graphical rendering capabilities of a particular game. In truth, the game engine is really the game’s guts, which are leveraged to complete a host of tasks in unison, thereby creating the interactive content most commonly viewed as a game. Thinking about a game as a machine is an excellent metaphor. A game has a set of specific tasks repeated in a dataset to deliver interactive entertainment.

This chapter is set up to provide programmers who are relatively new to Java or game programming a chance to get information regarding some of the most fundamental aspects of game programming across numerous topics. If you have programmed games in the past or feel that you have an understanding of how the basic components of a game work, you can probably skim this material or skip to the other portions of the book that fall in your domain of work. Most of the solutions presented here are organized for clear understanding and not necessarily for optimization.

Basic Game Structure

One of the most frustrating tasks for novice game programmers is organizing the execution of the tasks involved in programming a game. Sure, you can read all the tutorials on a particular API and then modify code snips and functional examples, but tutorials usually don’t emphasize where to put those tools so they work inside of the guts of the game. The basic idea seems pretty straightforward from the start, but it continues to increase in difficulty as the game develops. In truth most games are programmed in a similar manner at the beginning. They all have the same fundamental jobs to complete to display the visual representation of the game world on the screen. At the highest level of organization, a game can be represented by the three main components required during its operation. These aspects can be called Game Initialization, Game Playing, and Game Shutdown. The individual responsibilities of these components are key to understanding the underlying layout of a game.

During Game Initialization, most of the required elements for game operation are loaded into memory. These elements include all images, sounds, and data files, as well as anything else the game specifically requires to operate. Any classes used for the operation of the game should be instantiated and prepared for usage. This method is called caching and is particularly common for games built with a narrow or limiting system bus, such as the PC. Loading the data from disk during the actual gameplay generates performance problems, so it’s better to cache the needed components into memory for later consumption. Some systems, such as the Sony Playstation 2, offer programmers a much wider bus that reduces the amount of information that requires caching.

Game Shutdown is designed to do the exact opposite of Game Initialization. Game Shutdown is responsible for releasing all information and memory to the operating system and making sure that the game is exited properly. On the Java platform, memory management is generally handled for the programmer, but a number of operations may be handled in this component, including writing out log files regarding game performance or verifying that the user’s preferences have been stored for the next time the game is played.

Game Playing is a bit more complex, so it is listed last in the series. Game Playing internalizes a fundamental concept in game programming called the game loop. A game loop is nothing more than a standard looping mechanism that contains the tasks the game normally processes repeatedly until the game ends or the user indicates that he wants to quit playing. The programmers code the actual definitions of these tasks in the game as defined by the game documentation. Figure 2.1 shows the process of a single pass through the game loop.

image from book
Figure 2.1: A single pass through the game loop.

The main tasks the game loop is required to code against are: getting the user’s input, applying game logic, updating the game information, and rendering to the screen. The following code shows this process at the high level.

while(true) {  GetUserInput();  ApplyGameLogic();  Update();  Render(); }

Each of these tasks involves many more steps to process and will be reviewed individually.



Practical Java Game Programming
Practical Java Game Programming (Charles River Media Game Development)
ISBN: 1584503262
EAN: 2147483647
Year: 2003
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net