Public Group

# I really dum question

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

## Recommended Posts

Hello hoping you guys can help me out. I want to make a 2d tile map but I am lost on how to do the bitmap coding. Not sure what the technical name is called but it looks like "0000000000000000100", "1111111111011111111", and so on. I take it each digit represents a bitmap used in the map. If I understand it correctly you have to right some code to read this chart and insert the right bitmap based on the number? All the books I have looked at just clance over this and never go into detail. So How do you get the chart to match up with the bitmaps? I understand

##### Share on other sites
Well, if you want to do it with every bit, then you will need to use hexadecimal.

I wont go into detail, but you assign a variable hexadecimally like so:

unsigned int HexAssigned = 0xFFFF;

If you dont know how heexadecimal works, here is what you need to know.

##### Share on other sites
If you're a beginner, I'd just save your maps as plain text files for now. You should have learned how to save/load plain text while working with the console, but if you didn't it isn't particularly hard.

What you do then, is have a number respresent each type of tile. For example, you might have grass, stone, and brick, so a map could look like the following:
0000000000001111110000122221000012222100001222210000111111000000000000

You may have already guessed that 0 represents grass, 1 represents stone, and 2 represents brick, so this would represent a rectangle of grass, with a ractangle of stone inside it, and a rectangle of brick inside that.

When you're loading the map, you load these numbers into a 2d array, which happens to correspond very nicely to how the numbers are layed out - if you were to visualise a 2d array, it would look something like that. You then loop through the array, and draw the appropriate tile depending on what number you find; if you find a 1, you draw a stone tile.

Oh, and if you want to do additional research, what you want to look for is probably "tiling" and "tile maps".

##### Share on other sites
We can define the tilemap without using hexadecimal numbers. As in you original post, each position is taken by a character so we have 256 possible bitmaps (though we will use less probably). The map is defined as a list of strings of fixed size; let's assume each map is 16x16 tiles. Then our file would look like this:
11111111111111111000000000000001......1111111111111111

(16 lines of 16 characters each). You can read this in using anything you like such as fgets() or fgetch() (don't forget the newlines). The easiest data structure required is the following:
char pMap[16][16];

Now you must now which bitmap belongs to which map-character i.e. if the map above says '1' which bitmap must be shown. For this it may be useful to create a second array:
char pBitmapNames[] = {  "floor.bmp",  "wall.bmp",};

Now '0' means floor and '1' means wall. I assume you are able to load these bitmaps. Instead of or besides an array of bitmap filenames you might want to hold an array of the bitmaps itself for drawing.

We assume that each bitmap is of fixed size, lets say 16x16 pixels. For a character in pMap[j] the bitmap must then be drawn at (16*i, 16*j). So a simple drawing loop would look like:
for ( int i = 0; i < 16; i++ )  for ( int j = 0; j < 16; j++ )    DrawBitmap( pBitmapNames[pMap[j]], 16*i, 16*j );

I hope this clears up some things. It really isn't that difficult which is why it is never explained in detail. If you want to know more, please ask.

Greetz,

Illco

##### Share on other sites
When I get home ill post my tile editor code. It supports small maps, no scrolling, its just for a bomberman game im working on.

##### Share on other sites
oooh

LOL i feel real stupid now. I always thought there had to be some special conversion tool used. I got that idea fromTricks of Windows Game programming Gurus.

SO basicly if you have lets say 4 tiles for a 2D map. You draw the map out using numbers like so
1234
1234
3244
And you load this up into an Array and then when you draw the map you just point to the array which then points to the tile you should use.

So in this array is it where I should put tile info as well, like height or passible and stuff like that?

##### Share on other sites
Yes, that is what I would do,

I would create a struct like this
struct TileData {int TileNumber;int px_height;int px_width;int attribute;};

then would create an array of TileData Structures

TileData Map[4][4];

so now you would have a 4x4 map stored in one array..so if you wanted to know the size of tile in location (2,3)..you would just go

//Remember Arrays are 0-basedint tileHeight = Map[1][2].px_height;int tileWidth = Map[1][2].px_width;

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 14
• 10
• 11
• 13
• ### Forum Statistics

• Total Topics
634096
• Total Posts
3015488
×