| . |
| Palm Programming |
|
|
| Subroutines and Snippets |
Preferences are more permanent and should be used for storing things such as volume level, unlock codes, and any important game options.
You can keep important working variables in your data structures along with the actual game data, allowing you to quickly save and restore your game state.
The Palm API call PrefSetAppPreferences() allows us to do this. The last argument of the call is true-false. Set it to true to save and load from the preferences area, and set it false to work with the data area. The preferences area will be backed up automatically by the Palm Desktop software, and should be as small as possible to avoid increasing the time required for the user to sync the pilot.
subroutines that simplify saving and loading
The color coding used:
light blue = variables, constants, and subroutines names chosen by the programmer
light green = C defined words, Palm API calls, and all else NOT chosen by the programmer
First we use a define to set our creator id - this should be done in a header file.
Be sure to use a unique creator id for each of your programs, and register them at www.palmos.com if you are going to make your program available to the public.
#define CREATOR 'AbCd'
#define VERSION 1
We define two structures to store our preferences and our data (also done in a header file)
struct
GamePrefsType {
Int RegCode;
int Volume;
};
struct GameDataType
{
Boolean
GameOver;
Boolean
TwoPlayer;
int Score;
};
Then we define our global variables
struct
GamePrefsType GamePrefs;
struct GameDataType
GameData;
Finally we have four simple subroutines to save and load them
void SaveGameData(int gameID)
{
PrefSetAppPreferences (
CREATOR, gameID, VERSION,&GameStatus, sizeof (GameStatus), false);}
int
LoadGameData(int gameID){
Word
size, result;int i;
size = sizeof(GameStatus);
result = PrefGetAppPreferences(
CREATOR, gameID, &GameStatus, &size, false);return
result;}
void
SaveGamePrefs(){
PrefSetAppPreferences (
CREATOR, 0, VERSION,&GamePrefs, sizeof (GamePrefs), true);}
int
LoadGamePrefs(){
Word
size, result;int i;
size = sizeof(GamePrefs);
result = PrefGetAppPreferences(
CREATOR, 0, &GamePrefs, &size, true);return
result;}
tech info
If the size of the structures you are loading can vary, you may have to load the data twice, once to get an accurate size reading of the stored data, and a second time to load it correctly.
| Saving and loading data structures |