Skip to content

Commit

Permalink
added save file data deallocation
Browse files Browse the repository at this point in the history
  • Loading branch information
savaughn committed Nov 16, 2023
1 parent bfbcaba commit 92cb120
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ PLATFORM ?= PLATFORM_DESKTOP

# Define project variables
PROJECT_NAME ?= pokeromtrader
PROJECT_VERSION := 0.6.2
PROJECT_VERSION := 0.7.0
# prerelease or release
PROJECT_VERSION_TYPE ?= prerelease
PROJECT_BUILD_PATH ?= .
Expand Down
7 changes: 5 additions & 2 deletions include/pksavfilehelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
static enum pksav_error SAVE_FILE_ERROR = PKSAV_ERROR_NONE;

enum pksav_error detect_savefile_generation(const char *path, SaveGenerationType *save_generation_type);
PokemonSave load_savefile_from_path(const char *path);
void load_savefile_from_path(const char *path, PokemonSave *pkmn_save);
pksavhelper_error save_savefile_to_path(PokemonSave *pkmn_save, char *path);
void load_display_files(const struct save_file_data *save_file_data, PokemonSave *pkmn_saves);
void load_display_files(const struct save_file_data *save_file_data, PokemonSave *pkmn_saves, uint8_t *num_saves);
void free_pkmn_saves(PokemonSave *pkmn_saves, uint8_t *save_file_count);
void free_trade_saves(void);
void free_evolve_saves(void);
4 changes: 2 additions & 2 deletions src/components/TrainerInfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void reset_details_panel(void)
scale_width[1] = 1.0f;
}

void animate_details_panel(int16_t *grow_x, float *scale_width, uint8_t current_trainer_index, bool tr1_active, bool tr2_active)
void animate_details_panel(uint8_t current_trainer_index, bool tr1_active, bool tr2_active)
{
const uint8_t min_grow_x = 0;
const uint8_t max_grow_x = 207;
Expand Down Expand Up @@ -63,7 +63,7 @@ void draw_trainer_info(struct trainer_info *trainer, int x, int y, struct Traine
bool tr1_active = trainer_selection[0].pkmn_party_index != -1 && current_trainer_index == 0;
bool tr2_active = trainer_selection[1].pkmn_party_index != -1 && current_trainer_index == 1;

animate_details_panel(grow_x, scale_width, current_trainer_index, tr1_active, tr2_active);
animate_details_panel(current_trainer_index, tr1_active, tr2_active);

// Details Panel Rectangle
Rectangle container_rec;
Expand Down
6 changes: 4 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "common.h"
#include "filehelper.h"
#include "raylibhelper.h"
#include "pksavfilehelper.h"

int main(int argc, char *argv[])
{
Expand All @@ -10,8 +11,6 @@ int main(int argc, char *argv[])
PokemonSave pkmn_save_player2;
struct save_file_data save_file_data = {
.num_saves = 0,
.save_dir = NULL,
.saves_file_path = { NULL }
};

init_settings_from_config(&save_file_data);
Expand Down Expand Up @@ -40,6 +39,9 @@ int main(int argc, char *argv[])
&pkmn_save_player2);

free_filehelper_pointers();
// free allocated save file data
free_trade_saves();
free_evolve_saves();

return 0;
}
76 changes: 64 additions & 12 deletions src/pksavfilehelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,19 @@ enum pksav_error detect_savefile_generation(const char *path, SaveGenerationType
* @param path the path to the save file
* @return a PokemonSave struct
*/
PokemonSave load_savefile_from_path(const char *path)
void load_savefile_from_path(const char *path, PokemonSave *pkmn_save)
{
enum pksav_error err = PKSAV_ERROR_NONE;
PokemonSave pkmn_save;
SaveGenerationType save_generation_type = SAVE_GENERATION_NONE;
// PokemonSave pkmn_save;
pkmn_save->save_generation_type = SAVE_GENERATION_NONE;

err = detect_savefile_generation(path, &save_generation_type);
err = detect_savefile_generation(path, &pkmn_save->save_generation_type);
if (err != PKSAV_ERROR_NONE)
{
error_handler(err, "Error detecting save file generation");
}

pkmn_save.save_generation_type = save_generation_type;

switch (save_generation_type)
switch (pkmn_save->save_generation_type)
{
case SAVE_GENERATION_1:
{
Expand All @@ -70,7 +68,7 @@ PokemonSave load_savefile_from_path(const char *path)
{
error_handler(err, "Error loading save");
}
pkmn_save.save.gen1_save = save;
pkmn_save->save.gen1_save = save;
break;
}
case SAVE_GENERATION_2:
Expand All @@ -87,13 +85,12 @@ PokemonSave load_savefile_from_path(const char *path)
{
error_handler(err, "Error loading save");
}
pkmn_save.save.gen2_save = save;
pkmn_save->save.gen2_save = save;
break;
}
default:
break;
}
return pkmn_save;
}
/**
* @brief saves the save buffer to a path
Expand Down Expand Up @@ -128,15 +125,70 @@ pksavhelper_error save_savefile_to_path(PokemonSave *pkmn_save, char *path)
return error_none;
}

void load_display_files(const struct save_file_data *save_file_data, PokemonSave *pkmn_saves)
void load_display_files(const struct save_file_data *save_file_data, PokemonSave *pkmn_saves, uint8_t *num_saves)
{
int allocated_saves = 0;
long save_file_size = 0;
// Load save files once
for (int i = 0; i < save_file_data->num_saves; i++)
{
// if not initialized
if (pkmn_saves[i].save_generation_type == SAVE_GENERATION_NONE)
{
pkmn_saves[i] = load_savefile_from_path(save_file_data->saves_file_path[i]);
load_savefile_from_path(save_file_data->saves_file_path[i], &pkmn_saves[i]);
allocated_saves++;
save_file_size += pkmn_saves[i].save_generation_type == SAVE_GENERATION_1 ? sizeof(struct pksav_gen1_save) : sizeof(struct pksav_gen2_save);
}
}
if (allocated_saves > 0)
{
printf("PokeromTrader: Allocated memory (%ld bytes) for %d save files\n", save_file_size, allocated_saves);
*num_saves = allocated_saves;
}
}

void free_pkmn_saves(PokemonSave *pkmn_saves, uint8_t *save_file_count)
{
if (*save_file_count == 0)
{
return;
}
uint8_t count = 0;
printf("PokeromTrader: %u save files had been allocated", *save_file_count);
for (int i = 0; i < *save_file_count; i++)
{
switch (pkmn_saves[i].save_generation_type)
{
case SAVE_GENERATION_1:
{
pksav_gen1_free_save(&pkmn_saves[i].save.gen1_save);
count++;
break;
}
case SAVE_GENERATION_2:
{
pksav_gen2_free_save(&pkmn_saves[i].save.gen2_save);
count++;
break;
}
default:
puts("PokeromTrader: No save file to free");
break;
}
}

// reset save files to uninitialized
for (int i = 0; i < MAX_FILE_PATH_COUNT; i++)
{
pkmn_saves[i].save_generation_type = SAVE_GENERATION_NONE;
}

*save_file_count -= count;

printf(" / %d save files deallocated\n", count);

if (*save_file_count != 0)
{
printf("PokeromTrader: %d save files were not deallocated\n", *save_file_count);
}
}
28 changes: 11 additions & 17 deletions src/screens/FileSelectScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ static PokemonSave pkmn_saves[MAX_FILE_PATH_COUNT] = {
[0 ... MAX_FILE_PATH_COUNT - 1] = {
.save_generation_type = SAVE_GENERATION_NONE,
}};
static uint8_t save_file_count = 0;

void free_trade_saves(void)
{
free_pkmn_saves(pkmn_saves, &save_file_count);
}

/**
* @brief Draw the file select screen
Expand Down Expand Up @@ -36,6 +42,7 @@ void draw_file_select(struct save_file_data *save_file_data, char *player1_save_
static int y_offset = 75;
static int banner_position_offset = 0;
static bool show_duplicate_toast = false;
save_file_count = save_file_data->num_saves;

BeginDrawing();
ClearBackground(RED);
Expand All @@ -52,7 +59,7 @@ void draw_file_select(struct save_file_data *save_file_data, char *player1_save_
static int mouses_down_index = -1;

// Load save files once
load_display_files(save_file_data, pkmn_saves);
load_display_files(save_file_data, pkmn_saves, &save_file_count);

// Update and draw save files
for (int i = 0; i < save_file_data->num_saves; i++)
Expand Down Expand Up @@ -165,15 +172,10 @@ void draw_file_select(struct save_file_data *save_file_data, char *player1_save_
*current_screen = SCREEN_TRADE;
selected_saves_index[0] = -1;
selected_saves_index[1] = -1;
// reset save files
for (int i = 0; i < MAX_FILE_PATH_COUNT; i++)
{
pkmn_saves[i].save_generation_type = SAVE_GENERATION_NONE;
}
free_trade_saves();
ui_selection = E_UI_NONE;
y_offset = 75;
banner_position_offset = 0;

reset_toast_message();
show_duplicate_toast = false;
}
Expand All @@ -199,11 +201,7 @@ void draw_file_select(struct save_file_data *save_file_data, char *player1_save_
reset_toast_message();
show_duplicate_toast = false;

// reset save files
for (int i = 0; i < MAX_FILE_PATH_COUNT; i++)
{
pkmn_saves[i].save_generation_type = SAVE_GENERATION_NONE;
}
free_trade_saves();
ui_selection = E_UI_NONE;
y_offset = 75;
banner_position_offset = 0;
Expand All @@ -222,11 +220,7 @@ void draw_file_select(struct save_file_data *save_file_data, char *player1_save_
trainer1->trainer_id = 0;
trainer2->trainer_id = 0;

// reset save files
for (int i = 0; i < MAX_FILE_PATH_COUNT; i++)
{
pkmn_saves[i].save_generation_type = SAVE_GENERATION_NONE;
}
free_trade_saves();
ui_selection = E_UI_NONE;
y_offset = 75;
banner_position_offset = 0;
Expand Down
16 changes: 10 additions & 6 deletions src/screens/SingleFileSelectScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ static PokemonSave pkmn_saves[MAX_FILE_PATH_COUNT] = {
[0 ... MAX_FILE_PATH_COUNT - 1] = {
.save_generation_type = SAVE_GENERATION_NONE,
}};
static uint8_t save_file_count = 0;

void free_evolve_saves(void)
{
free_pkmn_saves(pkmn_saves, &save_file_count);
}

void draw_file_select_single(struct save_file_data *save_file_data, PokemonSave *pkmn_save, char *player_save_path, struct trainer_info *trainer, struct TrainerSelection *trainerSelection, enum single_player_menu_types menu_type, GameScreen *current_screen)
{
Expand Down Expand Up @@ -37,7 +43,7 @@ void draw_file_select_single(struct save_file_data *save_file_data, PokemonSave
static int mouses_down_index = -1;

// Load save files for selection display
load_display_files(save_file_data, pkmn_saves);
load_display_files(save_file_data, pkmn_saves, &save_file_count);

// Update and draw save files
for (int i = 0; i < save_file_data->num_saves; i++)
Expand Down Expand Up @@ -137,6 +143,7 @@ void draw_file_select_single(struct save_file_data *save_file_data, PokemonSave
{
*current_screen = SCREEN_MAIN_MENU;
selected_saves_index = -1;
free_evolve_saves();
}
ui_selection = E_UI_NONE;
break;
Expand All @@ -147,6 +154,7 @@ void draw_file_select_single(struct save_file_data *save_file_data, PokemonSave
trainerSelection->pkmn_party_index = -1;
*current_screen = SCREEN_FILE_EDIT;
selected_saves_index = -1;
free_evolve_saves();
}
ui_selection = E_UI_NONE;
break;
Expand All @@ -167,11 +175,7 @@ void draw_file_select_single(struct save_file_data *save_file_data, PokemonSave
if (menu_type == SINGLE_PLAYER_MENU_TYPE_EVOLVE && pkmn_save->save_generation_type != SAVE_GENERATION_CORRUPTED)
*current_screen = SCREEN_EVOLVE;

// reset save files
for (int i = 0; i < MAX_FILE_PATH_COUNT; i++)
{
pkmn_saves[i].save_generation_type = SAVE_GENERATION_NONE;
}
free_evolve_saves();
ui_selection = E_UI_NONE;
y_offset = 75;
banner_position_offset = 0;
Expand Down

0 comments on commit 92cb120

Please sign in to comment.