Sign in to follow this  
kelcharge

Tiling a map?(allegro)

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 this post


Link to post
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 this post


Link to post
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 this post


Link to 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 this post


Link to post
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[i].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 this post


Link to post
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this