Public Group

# Confused on DrawMap function?

This topic is 4761 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In the article Tile Based Gold, it has a function in it called DrawMap(int x, int y). I am confused as to just what parts of this function do. The function as a whole I get what it does, it draws the map in columns and rows, but I was wondering if someone could maybe break down each piece so that I might be able to understand it better. If anyone could do this it would be very very much appreciated. :)
void DrawMap(int x, int y)
{
register int i, j, MX, MY;

/* assigning map coord for base level*/
MX = x;
MY = y;

/* base level blitting */
for (i = 0; i < 17; i++)
{
for (j = 0; j < 17; j++)
{
blit((MX * 32) + xoff, (MY * 32) + yoff, image, screen);
MX++;
}
MX = x;
MY++;
}


##### Share on other sites
That is some odd code IMO, this is quite simpler and should make more sense.
First off we will say we are using a screen resolution of 640x480 and a tile size of 32x32. This means there will be 20 tiles accross and 15 tiles down. But I will make the map bigger, so the map can scroll, like in the tutorial.
const int MAP_WIDTH = 40;const int MAP_HEIGHT = 30;int g_map[WIDTH][HEIGHT];int g_pixelOffsetX;int g_pixelOffsetY;int g_blockOffsetX;int g_blockOffsetY;// we will say that our map can only have 3 differnt types of tilesBITMAP *g_tiles[3];// all the other functionsvoid drawMap(){  // these two for loops will move x all the way across the screen  // then move y down, until the whole screen has been covered.  // you do not want to use WIDTH and HEIGHT here becasue you only  // want to draw on the area visible.  for (int y = 0; y < 15; y++)  {    for (int x = 0; x < 20; x++)    {      // this gets the current tile. notice how the block offsets are      // added to x and y. if x = 0 in the for loop but the map has scrolled      // to the right one then the far left blocks (the blocks that appear when      // x = 0) needs to be like the blocks when x = 1. this is accomplished      // by using offsets. the tutorial explains this i beleive.      tile = map[x + g_blockOffsetX][y + g_blockOffsetY];      // you need to multiply x and y by the tile size to move them to screen      // co-ordinates. the pixel offsets are used to increment the map by single      // pixels to give a smooth scrolling effect.      blit(x * 32 + g_pixelOffsetX, y * 32 + g_pixelOffsetY, g_tiles[tile], screen);    }  }}

OK there we go. Hope that helped. (And I hope that code is correct to)

##### Share on other sites
Quote:
 // these two for loops will move x all the way across the screen // then move y down, until the whole screen has been covered.

When you say this you mean that it will fill the screen right. Like for example I have one tile for the map, then I blit the map within these two for loops it will fill the whole map with that tile. I must also say that I do not quite understand the blockOffsets.

##### Share on other sites
The offsets are used for the scrolling. Say you had a map, like this:

(* = tile, 0 = side of screen)0000000000000000*A***********00*************00*************00*************0000000000000000

Now, if we increment the x offset, the entire map moves to the right, like so:

0000000000000000 *A**********00 ************00 ************00 ************0000000000000000

Same thing for y, only it moves down. Now, if the offset is negative, then the map moves to the left or up. You can use the arrow keys or whatever to change this.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
17
5. 5

• 14
• 29
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631773
• Total Posts
3002263
×