PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

writing files!

Hi All, I'm trying to create a level editor for my new game, I can "edit" the levels etc, but not save the info in the format I require. I have a d-array [20][20] and would like the output to be easy to place into the game code....

anyhow, does anyone have any ideas how to do this? some sample code would be good....

data is in the following format

int level_data[20][20];

required file contents after editor output: (std text)

{
{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5}
};
etc....

help, please.... I looked at the file handling funcs, and it looks hell messy....

-thanks, Brendan
it may not be the most optimized, but something like this should get you started.

Code:

//A nice tidbit of code that I got somewhere (can't remember, too tired)
char* combine(char *format, ...)
{
va_list argptr;
static char string[2];

va_start (argptr, format);
vsprintf (string, format,argptr);
va_end (argptr);

return string;
}


//code for saving level data
FILE *fp=fopen("level.dat","ba+");//Opens at the end of the file to allow multiple levels in one file.
fwrite("{", sizeof(char), 1, fp);
for(int x=0; x<20;x++)
{
fwrite("{",sizeof(char), 1, fp);
for(int y=0; y<20;y++)
{
fwrite(va("%d,",level_data[x][y]), sizeof(char)+sizeof(int), 1, fp);
}
fwrite((")",sizeof(char), 1, fp);
}
fwrite("}",sizeof(char), 1, fp);
fclose(fp);



I think that should work, but, I am extreamly tired (3 hours of sleep last night, and working this morning) so I can not be held accountable for monday errors :D
what it the va func? does not work, not defined?

-Brendan
Just use fprintf instead of the fwrite / va combo at line 23:
Code:

fprintf(fp, "%d,", level_data[x][y]);


Rob, the length value passed to the fwrite at line 23 (sizeof(char)+sizeof(int)) is incorrect. Using fprintf negates the need to supply a length at all.

Dan East
have following code working!

Code:

FILE *fp=fopen("level.txt","w");
fprintf(fp,"{");
for(int x=0; x<20;x++)
{
fprintf(fp,"{");
for(int y=0; y<20;y++)
{
if (y != 19) fprintf(fp,"%d,",level_data[x][y]);
else fprintf(fp,"%d",level_data[x][y]);
}
fprintf(fp,"}");
if (x != 19) fprintf(fp,",");
fprintf(fp,"\n");
}
fprintf(fp,"}");
fclose(fp);



what about reading values back into the array?
Dan East wrote:
Just use fprintf instead of the fwrite / va combo at line 23:
Code:

fprintf(fp, "%d,", level_data[x][y]);


Rob, the length value passed to the fwrite at line 23 (sizeof(char)+sizeof(int)) is incorrect. Using fprintf negates the need to supply a length at all.

Dan East

You are correct. As I said, I was very tired when I wrote that. No idea what I was thinking. Now, you should just say that I wanted to write up to 5 possible characters into the file, and that was just a clever, hard to comprehend when reading the source a month later, way to do it, so there! jk :)

As for the "va thing", that's another error, it should read combine( instead of va(. Also, you will need to change string in function combine to how many chars total you think each entry will make (i.e. 1 for the comma plus the number of digits in the largest number. fprintf would eliminate the size thing. There is something else I was going to say, but forgot