# Tiling a map?(allegro)

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

## Recommended Posts

I have classes that define the texture of a tile, then one that defines an array of the tiles, and then a map class that draws it to the screen. I am having trouble getting the right tile to go to the right place on the screen. I have for loops that draw tiles in rows and columns. I just need to know how to put what tile where.

##### Share on other sites
Do something like this:

offsetx = 0
offsety = 0

for (y = player.y -5 to player.y +5)

for (x = player.x -5 to player.y +5)

Draw Tile from the map(x,y), at location (offsetx, offsety)

xoffset += tile_width

next

xoffset = 0
yoffset += tile_height

next

yeah, crappy pseudocode, but you should get the idea. The most important thing to do is to make sure that your offset coordinates are not dependant on your map coordinates. You could also easily add bounds checking.

##### Share on other sites
Hmmmm....maybe you did not understand, I have made a map engine before, but it used just one big bitmap for the maps. I wish now to draw the map to the screen tile by tile. So instead of being one big image, its just a bunch of tiles. What I need to know is how I would get, lets say, the sky to be drawn to the screen so far down, then below that on the screen I draw the ground....e.g.

SKY = S;
GROUND = G;

SSSSSSSSSSSSS
SSSSSSSSSSSSS
SSSSSSSSSSSSS
SSSSSSSSSSSSS
GGGGGGGGGGGGG

Now that is how I want the map to be drawn, but I am confused on how to get the ground tiles to be drawn below the sky. I can fill the map with all one tile, but I would like it to be more than one.

I hope that helps you understand my problem more and thanx for your first post....
:)

##### Share on other sites
I don't really understand what you're asking, but I'll give it a shot.

For simplicity's sake, we assume we have 2 tiles, a sky and a ground one, both the same size. If our map is stored in a 2d array, with 'S' denoting sky and 'G' ground (just like your example), then the following pseudocode will draw a portion of the map.

for (int y = 0; y < num_vertical_tiles_in_screen; y++)    for (int x = 0; x < num_horizontal_tiles_in_screen; x++)        if (map[x][y] == 'S')            draw_sprite(backbuffer, sky, x*sky->width, y*sky->height);        else            draw_sprite(backbuffer, ground, x*ground->width, y*ground->height);

It is obvious (I hope) that this is not production code, so do not use it as is. At the very least you should design your map and tile structures in such a way that you do not need to use an if-clause inside the loop. Also keep in mind that this assumes your screen is not scrolling (note the constant bounds in the loops).

##### Share on other sites
That helps me a lot, I just did not know how to tell what tile goes where. This gives me an idea of where to start. :)

##### Share on other sites
If anyone has any suggestions as how I should store the layout of the map, I would love to hear them.

##### Share on other sites
As for storing the map, I'd have to say some sort of array or vector object:

- Array of arrays - Disadvantage: no bounds checking and you have to "new" each row individually as well as the master array
- Vector of vectors - Better, as you don't need to "new" things yourself.

So basically something like:

const int MapWidth 40;const int MapHeight 25;std::vector < std::vector <MyTileType> > map;void initMap() {  map.resize(MapWidth);  for (int i=0; j<MapWidth; i++) {   map.resize(MapHeight, MyBlankTile);  }}

Easy, isn't it?

Then you can just say map[x][y] which will give you a MyTileType for that part of the map.

Mark

##### Share on other sites
Quote:
 Original post by markrSTL Goodness
The STL is your friend, use it. I made up two class wrappers to allow the use of Dynamic 2D and 3D Vectors. They will probably be of interest for those who are looking for a resizable 2D vector map.

##### Share on other sites
Ok, I get and like how you set up your 2d vector class. I would like to use it if that is okay. Also, is there anything that needs to be done for me to use it, or should I just copy and paste the code?

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 10
• 27
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633405
• Total Posts
3011681
• ### Who's Online (See full list)

There are no registered users currently online

×