Saving and loading from FAT

Today we will deal with basic saving and loading functions. It will provide some quick examples that can be implemented in almost any game. If you have any questions, comments, or suggestions, feel free to post them in the forum or email me at the address shown at the bottom of the page.

Initializing FAT

Initializing FAT for saving and loading is fairly simple. Call all of you basic initializations for PAlib, then call fatInitDefault() to initialize libfat (which is a devkitPro library).

Our code so far is:

#include <PA9.h>
 
int main(){
        PA_Init();
        PA_InitVBL();
 
	fatInitDefault();
 
	while(true){
		PA_WaitForVBL();
	}
}

Making save files

First we will go over how to write save data to a file.

Create the save structure

First you must create a structure for the save data. In it, you will put everything that you will be saving to the file. The structure for saving data in this example will use bool and int, but others should work too. Here is the structure we will be using for this example:

typedef struct
{
	bool character;
	int level;
} save_struct;
 
save_struct save_data;

Writing the data to a file

Now we will actually write the structure to a file use the structure above. To do this, we will need to have defined our structure, and initialized FAT. Here is a sample code for writing save_data to a file:

#include <PA9.h>
 
// Save structure
typedef struct
{
	bool character;
	int level;
}save_struct;
 
// Save data
save_struct save_data;
 
int main(){
	PA_Init();
	PA_InitVBL();
 
	// FAT initialization
	fatInitDefault();
 
	// Set the save_data values
	save_data.character = true;
	save_data.level = 2;
 
	// WB trunacates write and create. This will be used to create and write to the save file.
	FILE* save_file = fopen("save_file.sav", "wb");
 
	// Write save_data to save_file don't forget the & for save_data
	fwrite(&save_data, 1, sizeof(save_data), save_file);
 
	// Close the file
	fclose(save_file);
 
	while(true){
		PA_WaitForVBL();
	}
}

First we set the values here:

	save_data.character = true;
	save_data.level = 2;

Then we open the file that we want to save the data to.

	// WB trunacates write and create. This will be used to create and write to the save file
	FILE* save_file = fopen("save_file.sav", "wb");

After that, we write the data to the file with fwrite:

	// Write save_data to save_file don't forget the & for save_data
	fwrite(&save_data, 1, sizeof(save_data), save_file);

Then finally, we close the file.

	// Close the file
	fclose(save_file);

And that should work.

Reading the save data

Reading save data is pretty much the same as writing it, but instead we will load values into the structure. We will use the same info from Writing the save data in this too. Please note, that the values for the structure must be identical to the ones that you are loading.

In order to read save data from a file, you will need to change fwrite and wb. Change fwrite to fread and wb to rb to read the data instead. Pretty simple, right? That will replace the structures previous values with the ones that are loaded.

Conclusion

In conclusion there are a few things that you should know about this tutorial:

  1. This will write the data in binary, so don’t think you can read it without a hex editor.
  2. This code was written fairly fast, so some of it may or may not work.

As stated at the top, if you have any questions, comments, or concerns, contact me at:

GEMISIS 05/02/2009 21:54

 
day21.txt · Last modified: 17/09/2009 18:58 by fincs
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki