Saving multiple tilemaps to one file

Talk about making games.

Saving multiple tilemaps to one file

Postby jimmynewguy » Tue Sep 18, 2012 3:07 am

I've made map editors in the past that saved a level to a file, but what would be the best way to save multiple levels to a single file? A 3d array? (I've never used one before, but I assume it wouldn't work well with this) Use an offset in the save and load loop? Ie:
Code: Select all
void save_level(char*name)
{
    FILE * map = fopen(name, "w");
    int i,j;
    for(j=0; j<10; j++)
    {
        for(i=0; i<8; i++)
        {
            fputc(MAP[j + 10 * levelnum][i], map);
        }
    }
    fclose(map);
}

I feel like neither of these would workout as well as planned (or maybe not even work at all), does anyone have any better ideas or any thoughts?
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: Saving multiple tilemaps to one file

Postby skydereign » Tue Sep 18, 2012 5:43 am

The best method really depends on the type of data your saving, so if you want a more specific response, we'll need to know more about what you are saving. If it is an array of integers that won't change in size, and you don't mind allocating all of it at once, you can just use a single fwrite/fread. But if you need to load one level at a time, and be able to search through the file for the next level, that won't work. A simple way of adding more levels to a file is through the append mode of fopen. With this you can save out as many levels as you want by calling the save function. When you edit a map and change its size, you'll need to be careful and usually rewrite the entire file. It is also harder to find maps to load.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Saving multiple tilemaps to one file

Postby jimmynewguy » Wed Sep 26, 2012 3:12 am

Yea I figured they would rewrite like that, thanks for clearing that up. I'm going to do this the way I originally planned. Just declaring the levels as arrays in global code and loading them. if I use this Global Code:

Code: Select all
int level1[8][10] = {
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,5,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,5,0,4,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
                   };
 
int level2[8][10] = {
{1,1,1,1,1,1,1,1,1,1},
{1,0,5,0,5,0,5,0,5,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,4,0,4,0,4,0,4,0,1},
{1,1,1,1,1,1,1,1,1,1},
                   };
//
void load_level(int lev[][]){
int i, j;
int MAP[8][10];

memcpy(MAP, lev, sizeof(MAP));

for(i = 0; i < 8; i ++)
{
for(j = 0; j < 10; j++)
{
char * anim = getAnimName(MAP[i][j]-1);
CreateActor("tile", anim, "(none)", "(none)", 30*j, 30*i, true);
}
}
and then load_level(level1);
Do you see any problems I could run into along the way?
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: Saving multiple tilemaps to one file

Postby skydereign » Wed Sep 26, 2012 5:55 am

Well, since you are just storing the maps in global space, I'd recommend using just one large 3d array. You are storing the same number of integers, and that way you can use the array directly. Even if you were to use that system, I'd still recommend using an int to hold the level. That way you can use a switch to determine what level to load, instead of using the memcpy. Only other thing to watch out for is your use of getAnimName. For that to work, the actor calling the load_level must either be the tile actor, or have the same animation names as it. Both seem a bit weird, so I'd suggest using an array that holds all the tile names instead.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest