In the previous lesson we did quite a lot! everything from changing animations and modifying the timer loop to changing animations. This lesson we will focus on adding the radar to the Car's control panel.


If you are manually creating the project make file then use the one from lesson 6, there are no changes.
The following files will be changed in this lesson:


radar In our game we will have two radar screens, as shown in the picture. The first will cover the entire map and will show the roads (the lines in orange), the petrol stations (the white dots) and the car (the red dot).
The local radar will show the car (in the centre) and the surround area (a few screens worth). On this map we will shown the enemy cars and the cups. As seen in the picture, there is a cup to the top right of the car.
Admittedly, the national map doesn't exactly look like North America, but that's because it was drawn in Mappy without actually thinking about it. The bitmap for this is stored in the transam.bmp file, alongside the little icons for the local radar.
The map was actually drawn from code, and can be seen commented out in game_game.cpp in the LoadLevel() function. It simply looped through every tile in the map file and if it was a 'sea' block a pixel was drawn (because the sea tiles are only drawn on the outside of the map). This was then reversed to produce the map.

Static Graphics

In the beginning we mentioned that there is a better way to load, store and use static graphics (i.e. those that do not have animations). A method of the Animation library, GetFirstGraphic(), takes an id (from the animations.xml file) as the parameter and returns back a BITMAP pointer. This is ideal for bitmaps that both have no animations and we will not be performing any Animation methods on it (e.g. countdown timers, faders, etc).


In the header file, we set up our new BITMAP variables to hold the radar. We also create a new array to hold the location of all the cups. We need this to determine whether to draw the cups on the local radar.
In the source file we update the SetupGame function to retrieve the static graphics using the GetFirstGraphic function, i.e.
LocalRadar =GameLibrary->GetFirstGraphic("localradar");
Where LocalRadar is of type BITMAP*, and we repeat the same for the other static graphics (e.g. the small graphics used for the local radar).

Code Walkthrough of game_car.cpp/h

In our car code we want to fix a few bugs and improve the way the handbrake works by making the car slide if the car is turning whilst holding down the handbrake.
To make the car turn when handbraking, we store the last angle of the car in a variable, lastangle, and update the car position using this angle. This is because when the car is turning we want to continue sliding in the same direction and not the one that the car is facing. Once the handbrake is released we revert to using the 'angle' variable.
Other changes are as follows: Note: You can find all these changes by using a visual differencing tool such as WinMerge.

Code Walthrough of game_game.cpp

The code is to be altered to draw the local and national radar maps, as well as a few tweaks here and there. First the tweaks: In the InitialiseLevel() function we need to update the local and national radar. The local radar is simply created as a new bitmap, cleared and drawn onto the Panel. When the bitmap is created we need to know if we are using memory, system or video bitmaps.
The national map is drawn as follows:
BLKSTR* tmp; for(int y=0;y<mapheight;y++) { for(int x=0;x<mapwidth;x++) { tmp=MapGetBlock(x,y); if(tmp->unused2) //road putpixel(localMap,x/LocalMapScale,y/LocalMapScale,makecol(229,167,1)); if(tmp->unused1) //petrol putpixel(localMap,x/LocalMapScale,y/LocalMapScale,makecol(255,255,255)); } }
This loops through the whole tile map and where a road or petrol tile is found, it is drawn on the bitmap we generated to show the map. LocalMapScale is a constant we found as the size ratio between the bitmap we are drawing to and the actual size of the tile map.
Within TransAmGameDrawing function we need to update the local and national radar.
Using the same LocalMapScale constant we draw the player on the national map. The mapx/mapy variables store the car's location in pixels so we translate that to tile location and divide by the scale, then use a rectangle fill to draw the player:
newx=(((int)Player->mapx/mapblockwidth)/LocalMapScale); newy=(((int)Player->mapy/mapblockheight)/LocalMapScale); rectfill(GameFramework->DrawingSurface,LocalMapX+newx,LocalMapY+newy,LocalMapX+newx+1,LocalMapY+newy+1,makecol(255,0,0));
The local radar is updated by looping through the Cup array and seeing if the player is within a set number of tiles of it. We are translating 1 pixel to 1 tile and so the local radar shows int offset=LocalRadar->w/2 tiles, which is approximately 40 pixels. Assuming there are about 15 tiles on screen this is about 3 screens worth of information. The code then checks each cup's location against the player's and draws the cup on the screen if within range:
if(!CupPositions[n][2] && abs(xoff)<offset && abs(yoff)<offset)
We are drawing these to temporary bitmaps so we can blank them every frame.


Just to recap, we've added the local and national radar using the data from our tile map, made a few tweaks and learnt how to retrieve static bitmaps without the overhead of the Animation class.
So, all that remains is to compile the code and try out the game.


The next lesson will be will be adding the enemy cars, but not moving them. The lesson after that we will be learning about the A* path finding algorithm so that we can make the enemy cars chase the player.