Archived

This topic is now archived and is closed to further replies.

Mosselmaniac

Tile problem

Recommended Posts

Hello all. I''m working on a tile based race game. The map is 40 x 40 tiles (32 pixels each tile). This works great. I created my own Map Editor. So i was able to create a map of 200x200 tiles. This works, but it takes > 15 minutes to load the map, because my code isn''t good enough. I''m doing it like this: For Row = 0 to RowCount For Column = 0 to ColumnCount Tile(Column,Row) loadTile next column next Row I hope you understand what i mean. I have a bitmap (tiles.bmp) which consist out of 5 different tiles. I''m loading this bitmap 200x200=40000 times!!! This is not efficient I think there has to be a way to load the bitmap once. and then??! Help please thanks

Share this post


Link to post
Share on other sites
I'm not sure about the structure of your loading, neither about what language you are using. Nevertheless, I still got an idea about how to do it.

I assume you have an array for your map which contains all your tile indexes. What I don't understand is why you need to load your tile bitmap one time for every tile...

At start up: Load "Tiles.bmp" (or whatever it's called) into a global surface/texture structure (are you using OGL, SDL, DD or D3D?). You only need one copy of this bitmap.

Your tile structure/type should look something like this:

      
struct TILE
{
int offset; //offset into the bitmap

//other data members here

};


Then your array might look like this:

       
TILE map[200][200];


When you load the map (or save it), you can do it once in one call:

  
fwrite(&map, sizeof(map), 1, file); //this saves the whole map

//use fread for loading



When you are to draw the map with your drawing loop, you just need to use the correct part of the Tiles.bmp bitmap. That's what the offset variable in the TILE structure is for. This code example assumes all your tiles are placed after each other on the X-axis (5 tiles * 40 pixels = 200 pixels = total width of bitmap):

  
for(int i = something; i < something; i++)
{
for(int j = something; j < somthing; j++)
{
int left_src = 40 * map[i][j].offset;
int right_src = left_src + 40;
int top_src = 0;
int bottom_src = 40;
Blitting_Function(&bmpTiles, dest_x, dest_y, left_src,
top_src, right_src, bottom_src);
}
}


Since I haven't compiled this code , I can't guarantee everything would work exactly as I did it. However I hope you understand the concept of storing the offset into the tile bitmap, instead of storing the bitmap itself in the tile structure.

[edited by - Unwise owl on January 19, 2003 10:17:30 AM]

Share this post


Link to post
Share on other sites
Since you appear to be using Visual Basic (based on your pseudo-code) perhaps you would find the graphics engine used by the Scrolling Game Development Kit useful. It's a COM component designed for use with VB to draw maps of tiles. It makes it very easy to load and save maps because there is a property MapData that returns or sets the whole map as a byte array. That array can be easily written to or read from a file in one statement. The VC++ source code for the component is available here and to get the compiled binary component, you'll either have to compile that or download the Scrolling Game Development Kit from the GD Showcase or the project file listing for the Scrolling Game Development Kit.

"All you need to do to learn circular logic is learn circular logic"

[edited by - bluemonk on January 22, 2003 9:05:37 AM]

Share this post


Link to post
Share on other sites