Sol's Graphics for Beginners

(ch23src.zip)

(prebuilt win32 exe)

23 - Background Tunnel

Feel free to make a copy of the project, or continue from the last one.

Building the tunnel-rendering code was one of the longest chapters so far. To balance things off, integrating it with the game is one of the shortest. We'll mostly be copying stuff from chapter 17 - Variations on 2d Bitmap Tunnels.

First, copy the texture from chapter 17 to the current chapter's directory.

Start by copying the definitions of gLut and gMask to main.cpp and gp.h (with the extern-prefixes again). Do the same for gTexture.

Copy the contents of the init() function from chapter 17 to the beginning of the init() function in our main.cpp file. Again, move the declaration of the i, j and k integers over the declaration of i and j integers you just copied to the init() function.

Still in init(), find the line where we're loading tiles.bmp, and replace it with:

temp = SDL_LoadBMP("tiles.bmp");

This way we won't be defining the 'temp' variable several times.

Next, add a new file to the project, called 'background.cpp'. Start it off with the same 'include' block as all the other cpp files, and then copy the render() function from the old chapter into the file.

Rename the render() function to:

void drawbackground(int tick, int posx, int posy)

Remove all the lines between the beginning of the function and the declaration of the integers i and j.

Simplify the function so that we're only drawing one tunnel. That is, remove the second lookup, second mask, and the outer call to the blend_mul function. Remove all the lines after the rendering loop.

Since the effect is a bit too fast-paced right now, you might want to double the zooming 'tick' divider in the inner loop (from 8 to 16).

Next, add the background-drawing function as an extern to the end of the gp.h file:

extern void drawbackground(int tick, int posx, int posy);

Finally, we need to draw the background. Go to game.cpp, and find the comment that says 'fill background', and add, between the comment and the for-loop:

drawbackground(tick, (int)gXPos, (int)gYPos);

Compile and run.

You're not seeing anything new since we're filling the non-used areas in black. Remove the drawrect() call which you can find a few lines down, inside a couple of for-loops, so that "LEVEL_DROP" case doesn't do anything.

Compile and run again. You'll see the tunnel effect behind the maze, and the tunnel will move around when you move around on the map. The effect will make much more sense when we have bigger levels.

Before we can create levels that are bigger than the screen, we need a 24 - Moving Camera..

Having problems? Improvement ideas? Just want to discuss this tutorial? Try the forums!

Any comments etc. can be emailed to me.