Skip to content

Commit

Permalink
Improve persistent data handling
Browse files Browse the repository at this point in the history
  • Loading branch information
theDrake committed Aug 16, 2015
1 parent 7b89a33 commit b46524f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/space_merc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3394,7 +3394,8 @@ void init_mission_location(void)
Function: deinit_mission
Description: Deinitializes the global mission struct, freeing associated
memory.
memory. Also deletes mission data (if any) from persistent
storage.
Inputs: None.
Expand All @@ -3406,6 +3407,7 @@ void deinit_mission(void)
{
free(g_mission);
g_mission = NULL;
persist_delete(MISSION_STORAGE_KEY);
}
}

Expand Down Expand Up @@ -3721,13 +3723,13 @@ void init(void)

// Check for saved data and initialize the player struct, etc.:
g_player = malloc(sizeof(player_t));
if (persist_exists(STORAGE_KEY))
if (persist_exists(PLAYER_STORAGE_KEY))
{
persist_read_data(STORAGE_KEY, g_player, sizeof(player_t));
if (persist_exists(STORAGE_KEY + 1))
persist_read_data(PLAYER_STORAGE_KEY, g_player, sizeof(player_t));
if (persist_exists(MISSION_STORAGE_KEY))
{
g_mission = malloc(sizeof(mission_t));
persist_read_data(STORAGE_KEY + 1, g_mission, sizeof(mission_t));
persist_read_data(MISSION_STORAGE_KEY, g_mission, sizeof(mission_t));
set_player_direction(g_player->direction); // To update compass.
}
}
Expand All @@ -3750,10 +3752,10 @@ Description: Deinitializes the SpaceMerc app.
******************************************************************************/
void deinit(void)
{
persist_write_data(STORAGE_KEY, g_player, sizeof(player_t));
persist_write_data(PLAYER_STORAGE_KEY, g_player, sizeof(player_t));
if (g_mission != NULL)
{
persist_write_data(STORAGE_KEY + 1, g_mission, sizeof(mission_t));
persist_write_data(MISSION_STORAGE_KEY, g_mission, sizeof(mission_t));
}
app_focus_service_unsubscribe();
#ifdef PBL_COLOR
Expand Down
3 changes: 2 additions & 1 deletion src/space_merc.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ enum {
#define ENERGY_RECOVERY_RATE 1 // Energy (ammo) per second.
#define MIN_DAMAGE (HP_RECOVERY_RATE + 1)
#define ENERGY_LOSS_PER_SHOT (ENERGY_RECOVERY_RATE + 1)
#define STORAGE_KEY 417
#define PLAYER_STORAGE_KEY 417
#define MISSION_STORAGE_KEY (PLAYER_STORAGE_KEY + 1)
#define MAX_NPCS_AT_ONE_TIME 2
#define ANIMATED true
#define NOT_ANIMATED false
Expand Down

0 comments on commit b46524f

Please sign in to comment.