A Stationary Ball


To how see the LlamaWorks2D game engine draws things on the screen, we'll create a program that displays a ball. Then in the following section, we'll animate it by bouncing it around the screen.

The basic process for displaying a ball on a screen is straightforward. Your game class must create the ball as a sprite. A sprite is anything that moves on the screen. That includes monsters, people, oozing lava, or anything else. Even though the ball for this program won't move, we'll still create it as a sprite so that we can animate it easily in the next section.

After it creates the sprite, your game must display the sprite on the screen. Graphics programmers call this process rendering. For each frame, the function that does the rendering must clear the screen and draw the ball. The position and image of the ball are stored in the sprite object. Listing 4.2 shows how to make all of this happen.

Listing 4.2. Creating and rendering a stationary ball

 1    #include "LlamaWorks2d.h" 2 3    using namespace llamaworks2d; 4 5 6    class my_game : public game 7    { 8    public: 9      bool InitGame(); 10     bool RenderFrame(); 11 12     ATTACH_MESSAGE_MAP; 13   private: 14     sprite theBall; 15   }; 16 17 18   CREATE_GAME_OBJECT(my_game); 19 20   START_MESSAGE_MAP(my_game) 21   END_MESSAGE_MAP(my_game) 22 23   bool my_game::InitGame() 24   { 25     bool initOK; 26 27     theBall.BitmapTransparentColor(color_ rgb(0.0f,1.0f,0.0f)); 28     initOK = 29       theBall.LoadImage( 30         "ball.bmp", 31         image_file_base::LWIFF_WINDOWS_BMP); 32     if (initOK==true) 33     { 34       theBall.X(1); 35       theBall.Y(1); 36     } 37     else 38     { 39       ::MessageBox( 40           NULL, 41           theApp.AppError().ErrorMessage().c_str(), 42           NULL, 43           MB_OK | MB_ICONSTOP | MB_SYSTEMMODAL); 44     } 45     return (initOK); 46   } 47 48   bool my_game::RenderFrame() 49   { 50 51       theBall.Render(); 52       return true; 53   } 

As you might expect, the game class has changed a bit for this program. Lines 910 of Listing 4.2 show the prototypes for two new functions. The game engine requires these functions, but it provides them for you if you don't put them in.

LlamaWorks2D automatically calls the InitGame() function when your game starts up. Therefore, you should put all of the code that is required to get your game going into the InitGame() function. If your game doesn't use elaborate startup menus, this is not very much code.

Factoid

In case you're interested, the way that LlamaWorks2D provides the InitGame() and RenderFrame() functions when you don't write them is through inheritance. The inheritance happens on line 6. However, if you don't know what inheritance is yet and don't want to fuss with it, then just continue reading this chapter. As I mentioned previously, inheritance is explained in chapter 6, "Inheritance: Getting a Lot for a Little."


During every frame of animation, LlamaWorks2D calls the RenderFrame() function. All of the code that draws one animation frame goes here. At the beginning of each frame, LlamaWorks2D automatically clears the background. So your code should paint a background, if you need one, and then draw anything that goes in front of it. The code in the RenderFrame() function should work the same way people paint paintingsthat is, everything should be painted from back to front. That way, objects that are closer to the viewer will appear to be in front of objects that are farther away.

Right now, the RenderFrame() function is quite simple. It just calls the sprite class's Render() function and the ball renders itself. In general, you'll render your entire scene this way. It often makes rendering one of the easiest tasks in the game.

In addition to the two new member functions, the my_game class gained a private data member, as you can see on line 14. The data member is of type sprite. As Figure 4.1 showed, LlamaWorks2D provides the sprite class for your games. Sprites represent anything moving on the screen. When your program creates an object of type sprite, it gives itself a place to store information about the ball.

The code for the two new member functions appears on lines 2353. The first is the InitGame() function. After the InitGame() function declares a variable, it sets the ball's transparent color. The transparent color is the color that you don't want displayed. Figure 4.2 illustrates what the transparent color is used for.

Figure 4.2. The return of the Krelnorian.


Figure 4.2 shows the Krelnorian you saw in chapter 1. The Krelnorian's bitmap is rectangular, but the Krelnorian itself isn't. Most things aren't rectangular, but bitmaps are. That means there is essentially empty space in the bitmap that we don't want shown. In Figure 4.2, we don't want the game to show the dark area around the Krelnorian. To make part of a bitmap transparent, you first set the entire transparent area to the same color. Then you tell the game engine what the transparent color is, and it handles the rest. It displays the bitmap without displaying the transparent area.

Warning

Be careful that you do not accidentally use the transparent color in parts of the bitmap you do not want to be transparent.


When your game loads the ball's bitmap image, it tests every pixel against the transparent color. The sprite class works some special magic on all pixels that are of the transparent color to convert them into transparent pixels. On line 27, all pixels that are pure green (no red, no blue, and green set to maximum) are made transparent.

The files that go with Listing 4.2 are on the CD in the folder Source\Chapter04\Prog_04_02. The program file is Prog_04_02.cpp. The folder also contains Ball.bmp, which is the bitmap image of the ball. All of the transparent pixels in Ball.bmp are set to pure green. The floating-point color values for pure green are (0,0, 1.0, 0.0). The integer color values for pure green are (0,255,0).

Factoid

You can specify the transparent color as floating-point numbers or as integers. If you use floating-point numbers, the color values must be between 0.0 and 1.0, inclusive. If you use integers, valid color values are between 0 and 255, inclusive.


Next, the InitGame() function uses the initOK variable to store the results of the initialization. As lines 2831 show, the initialization is successful if the ball's image is loaded. The if statement on lines 3244 tests to see whether the initialization is successful. If it is, the statements on lines 3435 use the sprite class's X() and Y() functions to set the position of the upper-left corner of the ball's bitmap image. If the initialization is not successful, the statements on lines 3943 display an error message.

Warning

The source code for this program is on the CD in the folder Source\Chapter04\Prog_04_02. If you compile this program yourself, you'll need to copy both the .cpp and .bmp files in the folder into a folder on your computer's hard drive. If you forget to copy the file ball.bmp, the program will compile properly; however, it won't be able to load the ball's bitmap image because it's not in the same folder as the file Prog_04_02.cpp.


The RenderFrame() function, which begins on line 48, is where your game renders all of its sprites. In this case, there's only one sprite, so the function uses the sprite class's Render() function to display the ball's bitmap image.

It's important to note that both the InitGame() and Render() functions must return true if they succeed and false if they do not. This is how LlamaWorks2D knows whether or not there is an error.

BMP Files, Transparent Pixels, and Professional Game Art

The LlamaWorks2D sprite class makes it easy for you to use Windows BMP files with your games. It enables you create all the art for your games with nothing more complicated than Windows Paint.

You should be aware, however, that professional games rarely use BMP files for their game art. One of the primary reasons for this is that BMP files are not compressed. They are huge compared to other file formats. Common compressed file formats are JPEG, GIF, TIF, and TGA. To enable you to use these file formats, I've included a powerful free paint program on the CD that you can use in place of Windows Paint. The program is called Gnu Image Manipulation Program, or GIMP. I strongly encourage you to use it rather than Windows Paint. You'll find it in the \Tools\GIMP folder.

Another reason for not using BMP files is that BMP files are not designed for transparent pixels. As the program in Listing 4.2 shows, I have done some special things with the llamaworks2d::sprite class to add transparent pixels to BMP files. The disadvantage to the technique I use here is that all pixels that are pure green are converted to transparent pixels. You cannot use the transparent color for opaque pixels. Unlike BMP files, file formats such as TGA (Truevision's TARGA format) do support transparent pixels. You can use GIMP to mark any pixels in a TGA file as transparent. It doesn't matter what color the pixel is. Therefore, you don't have to use a specific color as a transparent color. Instead, you add an extra 8 bits of information called an alpha channel. The alpha channel specifies how transparent a pixel is. An alpha value of 0.0 means the pixel is opaque, and an alpha value of 1.0 means it is transparent.

The free version of LlamaWorks2D that you get with this book does not support TGA files or alpha channels.




Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

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