Monday, October 19, 2020

Computer Games

 Computer games are programs that enable a player to interact with a virtual gameenvironment for entertainment and fun. There are many types of computer gamesavailable, ranging from traditional card games to more advanced video games such asrole playing games and adventure games. In this chapter, we first discuss the differenttypes of computer games. The architecture of computer games is also described. Finally,the programming environment that is used to build the computer games is discussed.

 Types of Computer Games

Although computer games mainly provide entertainment and fun, it also improveshand/eye coordination and problem-solving skills. Each game has its own strategy, actionand fantasy that make each game unique and interesting. Generally, we can classifycomputer games into the following types: card games, board games, puzzles, maze,fighting, action, adventure, role playing, strategy, sports and simulation games. However,the classification is a fuzzy concept, as many games are hybrids that fall into more thanone class. For example, Doom can be classified either as a maze game or an action game,while Monopoly can be classified as a board game or strategy game. The different typesof computer games are briefly described as follows

Card GamesThey are computerized versions of traditional card games, or games which are essentiallylike card games in that they are primarily card-based (such as solitaire). Examples of cardgames include Blackjack, Bridge, Casino, Solitaire and Video Poker

Board GamesThey are adaptations of classic board games. Examples of board games include Chess,Checkers, Backgammon, Scrabble and Monopoly.

PuzzlesPuzzle games aim at figuring out of a solution, which often involves solving enigmas,navigation, learning how to use different tools, and the manipulating or reconfiguring ofobjects. Mastermind and Tetris are examples of puzzle games


MazeMaze games require the successful navigation of a maze. Mazes can be viewed indifferent ways. For example, they may appear in an overhead view (as in Pac-Man), orfirst-person perspective (as in Doom).


FightingFighting

 games involve characters who fight usually hand-to-hand, in one-to-one combatsituations. The fighters are usually represented as humans or animated characters.Fighting games include Street Fighter, Avengers and Body Slam     


ActionAction games 

 involve the human player shoots at a series of opponents or objects.Traditional action games include Space Invaders, Asteroids, etc. The recent popularaction games are Doom, Quake, Descent, Half-Life and Unreal that involve the humanplayer to control a character in a virtual environment to save the world from the forces ofevil by using deadly force 


AdventureAdventure games

 are different from action games. They emphasize more on the story,plot and puzzle solving rather than simply catching, shooting, capturing, or escaping. Thehuman player must solve puzzles while adventuring. Characters are usually able to carryobjects, such as weapons, keys, tools, etc. The settings of these games often evoke aparticular historical time period and place, such as the middle ages or Arthurian England,or are thematically related to content-based types such as Science Fiction, Fantasy, orEspionage. Examples of adventure games include Adventure, Zork, Haunted House,Raiders of the Lost Ark and Superman 


Role PlayingIn role playing games,

 players can take on different types of character. The character’sdescription may include specifics such as species, race, gender, and occupation, and mayalso include various abilities, such as strength and dexterity. In the virtual game world,the player goes on quests, fights monsters and improves the capability of the character onstrength and magic. Example games include Diablo, Dungeons & Dragons and Ultima.Many role playing games are also networked games that allow more than one player toplay and interact in the same game world over the network such as the Internet or LAN(Local Area Network). Everquest and Ultima Online are networked role playing games. 


StrategyStrategy games

 emphasize the use of strategy as opposed to fast action or the use of quickreflexes. Traditional strategy games include Chess, Monopoly, and Othello. In recentpopular strategy games such as Age of Empire, Warcraft and Close Combat, the playercan control many combat units to do battle against one or more opponents. In thesegames, the player needs to resolve the problem of resource allocation, and organization ofdefenses and attacks 


SportsSports games 

are adaptations of existing real-world sports or variations of them. Themost popular sports games include American Football, Baseball, Boxing, Fishing,Soccer, Tennis, Volleyball and Golf


SimulationThere are two types of simulation games: management simulation and trainingsimulation. Management simulation games refer to those games in which players mustmanage the use of limited resources to build or expand some kind of community,institution or empire. Example management simulation games include Railroad Tycoon

SimAnt, and SimCity. For training simulation games, it refers to games that attempt tosimulate a realistic situation, for the purpose of training. Through the game simulation, ithelps the player to develop some physical skills, such as steering as in driving and flightsimulation games. Example training simulation games include Police Trainer, Gunshipand Flight Unlimited.1.2 Game DesignA computer game can be just a C application program. Figure 1.1 shows the architectureof a typical computer game. It consists of the following components: Input, Game Logic,Graphics/Sound Support, Game Output and Networking. They are briefly described asfollows:Input – Users interact with the game program through input devices. Common inputdevices include keyboard, mouse or joystick.Game Logic – It implements the game logic or game code that handles most of thebasic mechanics of game. Generally, before the game logic is developed, the storyline on how the game is played and how the players should interact should bedesigned. Simple physics, networking support and animations should be planned. Insome advanced games, artificial intelligence (AI) and collision detection are alsoimplemented in this step.Graphics Rendering Engine - It has complicated code to efficiently identify andrender the game objects and background from a two-dimensional (3-D) model of theenvironment. It supports transformation of objects that are moved, rotated and scaledwhen required.Graphics/Sound Drivers – The graphics drivers receive requests from the renderingengine to the graphics library using APIs. Windows APIs and Microsoft FoundationClasses (MFC) provide two-dimensional (2-D) graphics support for PCs. Forsupporting both 2-D and 3-D graphics, OpenGL and DirectX are the two mostpopular graphics libraries. DirectX also provides libraries for music and soundsupport.Game Output – The generated 2-D or 3-D graphics is output to the display. Thegenerated sound effect or music is output to the sound card.Networking - It provides networking protocol support that allows several users inremote locations to play and interact in the same game environment. In a networkedgame environment, a server is needed to maintain information on which the virtualgame world is supporting, communicates with game clients that are used by players toprovide them with information about the shared environment. The server also needsto synchronize the information, and maintain the consistent scenes of the virtual gameworld among the networked clients.When a game program begins execution, it should first initialize the memory, loadsimages and sound files, starts the graphics and set up variables such as scores. Afterinitialization, the game logic then starts. When the game session ends, housekeeping isalso needed to update sound effects, update and display scores, update data structures,etc. The user can end this session or go back to the game logic to start the game again

 Story Line

To design a successful computer game, it is important to have a good story line, togetherwith good 2-D and 3-D graphics, and sound effects to make the computer game seemrealistic. The story line should be first developed before we start developing a game. Todevelop a story line, we need to determine the following:The type of the game – We need to classify the nature of the game into one of thecategories that we have discussed in Section 1.1. Different types of game havedifferent requirements, for example, role playing games requires realism in graphicsand sound effects, whereas some strategy games emphasize on strategy rather thanrealism, and simplified display would be enough for such games.The goal of the game – We need to tell the player what to do to succeed in the game.In a space war game, the goal is to shoot down as many space invaders as possible. Ina fighting game, the goal is to defeat or kill the opponent.The player’s performance in the game – We need to decide how to rate the players fortheir performance. This should indicate how close they have achieved the goal of thegame. In general, we use a total score as a player’s performance indicator. In addition,when the player has achieved the goal, we also need to determine how to reward theplayer.The rules of the game – We need to tell the players how to play the game. Instructionson how to operate the keyboard or mouse in order to play the game should beexplained

Computer Graphics and Sound Effects

Visual effects of graphics and special sound effects are important to help establish thestory line and provide the illustrations that make the player to feel part of the game. Manysimple games rely only on 2-D graphics, which draws points, lines, and filled shapes suchas rectangle and polygons in a plane. Graphics functions provided from Windows API(application Programming Interface) or Microsoft Foundation Class (MFC) library onWindows environment support 2-D graphics. Advanced computer games often require tohandle 3-D objects. Microsoft DirectX supports advanced 3-D graphics on Windowsplatform. Instead of just supporting the Windows platform, OpenGL supports 2-D and 3-D graphics on both Unix and Windows environment.Apart from 2-D and 3-D graphics, image display and manipulation is another importanttechnique for game development. In many games, we can use a scanner to digitizepictures into images and used them as graphical elements in the display. We can alsomanipulate the images through scaling and rotation. Animation of images is another

5popular technique employed in many games. Windows API and MFC library providesfunctions for image display and manipulation.Sound is another important element that can make a computer game look realistic.Windows and MFC library only support the generation of only one tone at a time.However, with an optional sound card, most computer games can generate more complexsound effects. To support game development, Microsoft DirectX provides the DirectXAudio subsystem that supports music and stereo sound effects. OpenGL library does notprovide any support for sound effects.In this section, we briefly review some of the graphics libraries including Windows API,MFC library, DirectX and OpenGL.

Windows API and MFC Library

Windows is a very important subsystem in Microsoft's operating system. It makesMicrosoft's 32 bit Windows API available to application programs. Windows APIcontains functions for Graphics Device Interface (GDI) that allows users to draw andwrite in a window. The GDI functions let users display graphics in Windows using adevice context object such as a display or a printer. Device context is designed to isolatea Windows program from the physical output device, so that when you call GDIfunctions for all graphics output, it accesses the specific device driver. GDI providesvector drawing functions that can draw graphical objects such as lines, rectangles,ellipses and polygons; text output functions to display text in a window; bitmapmanipulation functions to display and manipulate images; and palette managementfunctions to exploit the colors.MFC library provides a set of functions to control text and graphics output. It is object-oriented and the key class is the CDC class that defines a class of device context objects.The CDC object provides member functions for working with a device context. Similar toWindows GDI, the member functions provide operations to support drawing of lines,simple shapes, ellipses and polygons, drawing of text and working with fonts, colors andpalettes. In addition, member functions are also provided for working with viewport,working with regions, mapping and clipping.

DirectX 

The goal of DirectX is to make Microsoft Windows a desirable platform for gamedevelopment. It aims at shifting the burden of hardware support from the gamedevelopers to the hardware manufacturers, who are more qualified to write drivers fortheir products than the game developers. However, DirectX is not a game-creationpackage. It only aids in the development of applications through the use of APIs designedto interface directly with the computer’s hardware. If the hardware is equipped withDirectX drivers, access can be granted to the accelerated functions, which that deviceprovides. If no accelerated functions exist, DirectX will emulate them. Hence, theprogrammer can continue to work on a consistent interface without worrying about thingssuch as hardware features. If a feature does not exist on the card, it is likely that thefeature will work through DirectX’s emulation functions.      

Thus, DirectX is a set of low-level application programming interfaces for creatinggames and other high-performance multimedia applications. It includes support for 2-Dand 3-D graphics, sound effects and music, input devices, and support for networkedapplications such as multiplayer games. DirectX 8 has the following major components:DirectX Graphics - It is a complete 3-D graphics system.DirectX Audio - It includes sound and music systems that provides a complete systemfor implementing a dynamic soundtrack.DirectPlay - It is a set of tools that simplify communications across the networks, theInternet or modems. The tools allow game players to find game sessions easily tohelp manage the flow between servers and players.DirectInput - It provides the game developer with an interface to myriad inputdevices, such as keyboards, mouse and joysticks.OpenGLOpenGL was developed by Silicon Graphics Inc. (SGI) as a multi-purpose, platform-independent graphics API. The development of OpenGL has been overseen by theOpenGL Architecture Review Board (ARB), which is made up of major graphics vendorsand manuafacturers since 1992. ARB is responsible for establishing and maintaining theOpenGL specification. The current release of OpenGL is at version 1.3. Unlike DirectXwhich is now at its eighth version, the OpenGL specification is quite stable which doesnot get updated often.OpenGL is a collection of several hundred functions providing access to all the featuresoffered by the graphics hardware. This includes 2-D image scaling, rendering 3-D objectsincluding spheres, cylinders, and disks, coloring, lighting, blending, and so on. The APIis a powerful, low-level rendering and modeling software library available on all majorplatforms. It is designed for use in any graphics applications, from games to modeling toComputer Aided Design (CAD). Many computer games such as Quake 3 have usedOpenGL for their core graphics-rendering engines.Under Windows environment, OpenGL provides an alternative to using the GraphicsDevice Interface (GDI). GDI is designed to make the graphics hardware entirely invisibleto Windows programmers. However, the layers of abstraction that help programmersavoid dealing with device-specific issues have caused applications lacking the speedrequired for games. In OpenGL, GDI can be bypassed entirely. OpenGL API can accessdirectly with graphics hardware.OpenGL does not directly support any form of windowing menus or input. The OpenGLUtility Toolkit (GLUT) is a set of support libraries available to provide basicfunctionality in many areas, while remaining platform independent. For instance, GLUT-based applications can be easily ported from Windows to Unix. GLUT is easy to use andlearn. Although it does not provide all the functionality that the operating system offers, itworks well for simple applications

Programming Environment

Her, we intend to use computer games as programming examples to illustrate thedifferent concepts in C such as branching, looping, functions, arrays, strings, structuresand file I/O. Advanced computer games such as role playing games, adventure games andsimulation games require complex 3-D graphics to make the virtual game world realistic.As such, only traditional, simple games that only require simple 2-D graphics such asdrawing lines, rectangles and polygons are discussed.Windows API, MFC library and DirectX are only available in Windows platform, whileOpenGL is an open source that can be available in both Windows and Unix platforms.Here, we have chosen OpenGL and GLUT as the graphics driver for supporting different2-D and 3-D graphics API for the developed game programs.Microsoft’s Visual C/C++ is used for the development of the game programs in theWindows environment. OpenGL and GLUT are required to be installed within theMicrosoft’s Visual C/C++ environment. We will also discuss the installation of OpenGLand GLUT for Microsoft's Visual C/C++