# 14.3 Graphics

## 14.3 Graphics

Understandably, one of the first questions any game programmer asks is how to show graphics on the screen. When people refer to graphics in this general sense they typically mean any kind of image that can be shown on the screen: colors, shapes, pictures, etc. In practice there are various kinds of graphics, such as 2D graphics and 3D graphics, and if one is so inclined these categories can be further divided into smaller categories of increasingly specific types. But generally speaking, it is enough to define graphics as being either 2D or 3D.

2D graphics are those that are seen from one perspective, and include things like photos, paintings, sketches, and the kinds of doodles you can do in photo programs like GIMP or Photoshop. The original Disney cartoons were also 2D. Although there are no real limitations on the kinds of images that can be classified as 2D, all 2D images share a common property that classifies them as 2D: 2D images cannot be seen from other perspectives. You can draw a happy face on a sheet of paper, but you cannot turn the paper at different angles and observe the face from other sides. You cannot, for example, walk around to see the back of the face if you have only drawn the front. Nor can you see either profile of the face. In short, it doesn't exist as a 3D head, but rather appears exactly how it is drawn.

3D graphics are analogous to sculptures and are made in 3D graphics programs like 3ds Max, Maya, and Blender 3D. Unlike 2D graphics, 3D graphics can be rotated, moved, transformed, and scaled, and it's possible to see the models from any angle. If you make a 3D teapot you can zoom in or zoom out, turn the teapot around to see it from the other side, and you can even zoom down the spout to see it from the inside.

So to summarize, graphics come in two forms: 2D and 3D, and computer games feature both.

The next step is to find out how graphics can be drawn on the screen. Thus far, beyond using std::cout to show text on the screen, there appears to be no class or function in C++ to show a picture. You can't write std::cout <<“MyImage.bmp” and expect it to show the image. Instead, it will simply print “MyImage.bmp”, which is not especially useful to a game programmer. Instead, there are a number of libraries available to programmers-extra classes and functions-that can be added to C++ for making games. These libraries offer a wide range of features for drawing graphics, from simple 2D images to cutting-edge 3D graphics.

### 14.3.1 DirectX

DirectX is a well-known free library maintained by Microsoft. Most people will probably have seen this library installed alongside their game on Windows systems. It is intended to be used primarily for game development, although many other kinds of software can be made using it too, ranging from museum kiosks to bowling alley displays, and the list goes on. DirectX as a library provides a set of classes and functions that abstract the graphics hardware and allow developers to draw fast 2D and 3D graphics, as well as play media files such as MP3s and AVIs. It can also abstract the input hardware and accept data from the keyboard, mouse, and even plug-and-play joysticks and other peripherals. From a graphics perspective, DirectX offers a range of functions to draw images to the screen in coordinate space. The top-left corner of the screen is the origin (0,0), and by using such a coordinate space, programmers can draw animations, videos, and static images at specific screen locations. DirectX can also represent 3D graphics in a 3D coordinate space, where coordinates are defined as x, y, z. Naturally, DirectX does not provide features to organize a game in the sense of storing lists of data, such as units of an army, nor does it include functions for artificial intelligence, or making the computer think and respond to the player. These kinds of features are either made by the programmers themselves or can be provided by a separate library. The purpose of DirectX is to draw graphics-either 2D images loaded from files such as BMPs, JPEGs, and PNGs, or 3D models exported using 3D software.

Although DirectX has become quite large and popular, there are a number of aspects programmers should be aware of when using this library. First, DirectX is platform specific. This means it can only run on Windows. And second, it is not open source. This means programmers cannot actually see the source code for the DirectX library; it is maintained by Microsoft. So though programmers can use the DirectX functions and classes in their software, they cannot adjust DirectX's source code. Therefore, if bugs are found, developers rely on Microsoft to find and repair them.

Where to get it: http://www.msdn.microsoft.com/directx/

### 14.3.2 OpenGL

OpenGL (Open Graphics Library) is like a kind of cross-platform DirectX, and is also open source. Cross-platform means it can run on more than one operating system, including Mac OS, OS/2, UNIX, Windows Vista, Windows XP, Windows 95/98, Windows 2000, Windows NT, Linux, OPENSTEP, and BeOS. Open source means the source code for the library can be seen and changed. OpenGL is not related to Microsoft, however, and is developed independently. Like DirectX, OpenGL allows developers to draw fast-paced 2D and 3D graphics for games and other multimedia software, and is also free to use for commercial and non-commercial products. However, unlike DirectX, OpenGL does not provide support for areas other than graphics. It does not offer classes for programming sound or input. This book does not explore OpenGL further, but there are many other books devoted to OpenGL that explain the subject well.

Where to get it: http://www.opengl.org

### 14.3.3 Other Libraries

DirectX and OpenGL are the main graphics libraries used in the game industry, and enjoy wide hardware support. However, programmers often complain that these libraries are complicated to use and that it takes a lot of work just to achieve simple things, such as creating a small 3D world or drawing animated 2D images to the screen. For this reason, a number of extension libraries are available that add new features to DirectX and OpenGL, and they also include new features of their own. In short, these are themselves separate libraries, but they extend upon the features and functions offered by OpenGL or DirectX. They are therefore dependent upon OpenGL and DirectX, but they offer extra features and extra classes to make drawing graphics even simpler. Since these libraries tend to be based on DirectX, OpenGL, or both, they often call themselves middleware libraries. The extent to which developers use these middleware libraries varies, as some companies prefer to develop their own libraries and use no existing libraries at all, some purchase commercial middleware libraries, and others use the wide assortment of open-source libraries freely available, some of which this book examines in more detail later. A few of the middleware libraries that can be downloaded and used for games are discussed in the following sections.

#### 14.3.3.1 SDL

One of the most prevalent libraries for making cross-platform games is SDL, which stands for Simple DirectMedia Layer. SDL sits on top of both OpenGL and DirectX and makes many of their classes and functions easier to use. SDL can be used to create any kind of game, and is a general-purpose library. Like OpenGL and DirectX, SDL can be used for both commercial and non-commercial projects free of charge.

Where to get it: http://www.libsdl.org

#### 14.3.3.2 ClanLib

ClanLib is an open-source, cross-platform 2D graphics library designed for making 2D games. This library uses OpenGL as its base. ClanLib is an extension of OpenGL and offers a simple and easy-to-learn design structure, has many classes for all kinds of 2D game objects, is stable, and is free for commercial and non-commercial use. Furthermore, ClanLib is more than simply a graphics library; it also contains other features for input, sound, and creating games with multi-player modes. ClanLib is covered in more detail in Chapters 15, 16, and 17.

Where to get it: http://www.clanlib.org

#### 14.3.3.3 OGRE 3D (Object-Oriented Graphics Rendering Engine)

In the realm of 3D graphics there aren't many free, open-source libraries quite like OGRE. OGRE is a powerful cross-platform 3D graphics engine that uses both DirectX and OpenGL at its base, and can be switched to use either one. In short, OGRE is a vast library for rendering fast-paced 3D graphics in an effective and versatile way that is particularly ideal for gaming. OGRE works by allowing developers to position and arrange their graphics and objects in a single integrated 3D world. It also features classes and functions to create effects like rain, snow, and fog. Chapters 18 and 19 examine OGRE 3D in detail.

Where to get it: http://www.ogre3d.org

Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

Similar book on Amazon