I wrote
>Oh, I read your message again and understood, that you got me wrong :>
I understood what you're doing ;P
Let me explain in more detail.
If you want to avoid else if chains like that, you need to make things nameless. I mean if you have a sprite named "buliding1dspr" then that's pretty rigid and there isn't really a way around unwieldly else if chains.
To remove those names, you need to make everything dynamic. So what does that mean?
It means putting more things into files, and loading them into generic resource managers. For example, you could have a tileset file that looks something like this:
1 2 3
|
d someimage.png foo bar baz
b anotherimage.png foo bar baz
...
|
Each line opens up with the tile ID (I used d and b per your example), followed with information about that tile, such as the image used to draw it, whether or not that tile is walkable, how to draw it (like src x,y coords, whether or not it's flipped, that kind of thing), etc, etc. Everything the tile would need in order to operate.
When the tileset is loaded, you would load that info into objects of your Tile class:
1 2 3 4 5 6 7 8 9 10 11 12
|
// something like this
void Tile::Load(std::istream& file)
{
while(!eof)
{
file >> IDchar;
file >> ImageName;
file >> foo;
//...
// where IDchar, ImageName, foo, etc are all members of 'Tile'
}
}
|
Loading the tileset is as simple as opening the file, loading each tile, and sticking all the tiles into a container.
Now since your tiles are recognized by a special character (d, b) instead of a 0-based index, a std::map for holding the tiles might be more appropriate:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// Tileset class has the below member:
// std::map<char,Tile> tileset;
void Tileset::Load(std::istream& file)
{
tileset.clear();
while(!eof)
{
Tile t;
t.Load(file); // load the tile
tileset[ t.IDchar ] = t; // put it in our map, easily indexed by our ID char
}
}
|
Then when you load the map, you can use the ID char from the map to index your tileset and get the Tile*:
1 2 3 4 5 6 7 8 9 10 11
|
// Map class has members:
// array2d<Tile*> map;
// Tileset theTileset;
//
for( ...each tile... )
{
char t = __the_char_pulled_from_the_file__;
map(x,y) = &theTileset[t];
}
|
See how it works? You could also store the name of the tileset in your map file so that you can load the appropriate tileset when you load the map, rather than having to hardcode those.
But notice that everything is nameless. There's no hardcoded data that has to be accessed by a specific name in your source. Everything is fluid and dynamic. This is less restrictive, easier to code (after you get the hang of it), and much easier to change maps around if you want to later, or add new tiles, etc.
PS: I normally don't make long posts like these in PMs. This info could be really useful to other people too. Would you object if I posted these PM exchanges on the forums so other people could benefit from it?