gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Game data saving with libfat

#156992 - mca - Sat May 17, 2008 5:08 pm

Hi,

I have a problem saving game data with libfat.

I use
Code:
   /* Initialize libfat */
   fatInitDefault();


and later on in the game
Code:
/*Save GAME Data */
void saveGame(){
   FILE* datei = fopen("./game.sav", "w");
   fwrite(&game_save_data, sizeof(game_save_data), 1, datei);
   fclose(datei);
};


where game_save_data is a structure holding profile information about 4 players.

When I inspect the SDHC card afterwards, there is no file created on it in the directory where the game.nds file resides.

What went wrong? Is there a code example of saving data with libfat?

Thanks...
mca

#157005 - tepples - Sat May 17, 2008 7:12 pm

On the libfat page, chishm wrote:
Both fatInit and fatInitDefault return true if the library was successfully initialised and false otherwise.

Have you made sure that fatInitDefault() returns true (that is, not 0) and that 'datei' is not a null pointer?

(For those reading at home, 'Datei' is German for 'file', unrelated to the maker of GnM.)
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#157006 - yellowstar - Sat May 17, 2008 7:21 pm

Try removing that forward-slash. You can also try removing both the slash and the period.

#157016 - mca - Sat May 17, 2008 8:22 pm

fatInitDefault returns true. So it is initializing as it should.

Removing both the period and the slash is working fine.
.sav file is saved to root directory of the card.
Is there a way of storing it in the same directory as the game.nds file?

Would a '\' (Backslash) work?

Thanks...
mca

#157019 - Dwedit - Sat May 17, 2008 8:33 pm

Currently there's no reliable way to tell what directory a program was launched from. Often people just hardcode a directory name into their program, but that's an ugly solution.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#157021 - tepples - Sat May 17, 2008 8:46 pm

It's ugly, but it's similar to what Windows does. Windows provides a function called SHGetFolderPath() that exposes the paths of special folders for programs to store their data, such as "Application Data" and "My Documents". Likewise, DS homebrew has a de-facto standard, where programs store their data in subfolders of "/data". For example, a program called Lockjaw might store data in a file called "/data/lockjaw/lj-scores.txt".
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#157022 - mca - Sat May 17, 2008 8:48 pm

ok, going for the root directory...

My next problem is that saving doesn't work (NDS game crashes) while playing sound with playsound() from libnds.
is there an criteria to see, when an sound has ended to play.
Something like

Code:
while(unnamable_soundregister & stillPlays){}


as an pause loop.

Thanks...
mca

#157043 - Dwedit - Sun May 18, 2008 6:30 am

Don't stick stuff in the root directory, it has a limited number of files in FAT16. Long file names highly aggravate the problem.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#157048 - mca - Sun May 18, 2008 12:15 pm

ok, i got saving working occasionally.

In what state (display, sound, sprites, irq, timers...) should the Nintendo DS be, when saving with 'fopen' and 'fwrite'?

I can't figure out what crashes the machine sometimes. Are there any recommendations?


Code:
void saveGame(){
   FILE* file = fopen("fat1:game.sav", "wb");
   game_save_data.profile_container_save = p_container;
   fwrite(&game_save_data, sizeof(game_save_data), 1, file);
   fclose(file);
};


Thanks...
mca

#157050 - kusma - Sun May 18, 2008 3:26 pm

mca wrote:
I can't figure out what crashes the machine sometimes. Are there any recommendations?

Verify that fopen() doesn't return NULL.

On a more general note, fwrite'ing structs isn't recommended, as it makes the saved data extremely unportable (just changing some build-options might break compatibility). And just think about the pain that occurs every time you want to add a field to the structure :P