Skip to content

Commit

Permalink
init memcards only when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfdsfgs committed Aug 21, 2021
1 parent ad31704 commit 2819ae7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/engine/mcrd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <libetc.h>

#include "engine/common.h"
#include "engine/timer.h"
#include "engine/mcrd.h"

#define MCRD_SAVE_MAGIC "CSS\x01"
Expand Down Expand Up @@ -45,6 +44,8 @@ static u8 save_icon_data[] = {
#include "saveicon.inc"
};

static bool mcrd_init_done = FALSE;

static inline char *mcrd_devname(const int chan) {
static char buf[6] = "bu00:";
buf[2] = '0' + (chan / 16); // port
Expand Down Expand Up @@ -118,12 +119,15 @@ void mcrd_init(void) {

ExitCriticalSection();

mcrd_init_done = TRUE;

printf("mcrd_init(): memcard initialized\n");
}

void mcrd_start(void) {
// tick music in the timer callback because all memcard processing is synchronous
timer_set_callback(timer_cb_music);
// call InitCARD and open events if we haven't done it already
if (!mcrd_init_done)
mcrd_init();

mcrd_clear_events(MCRD_EV_HW);
mcrd_clear_events(MCRD_EV_SW);
Expand Down Expand Up @@ -155,9 +159,6 @@ void mcrd_stop(void) {
}
ExitCriticalSection();

// reset timer back to normal callback
timer_set_callback(timer_cb_ticker);

printf("mcrd_stop(): memcard processing stopped\n");
}

Expand Down
14 changes: 14 additions & 0 deletions src/game/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "engine/memory.h"
#include "engine/sound.h"
#include "engine/input.h"
#include "engine/timer.h"
#include "engine/org.h"
#include "engine/mcrd.h"

Expand Down Expand Up @@ -750,6 +751,13 @@ static inline void menu_saveload_set_state(const int state, const char *title, c
}

static void menu_saveload_open(void) {
// tick music in the timer callback because all memcard processing is synchronous
timer_set_callback(timer_cb_music);

// clear screen to show that something is actually happening
gfx_draw_clear_immediate(main_bg_rgb);

// ...since this takes a while the first time
mcrd_start();

saveload.num_cards = mcrd_cards_available(saveload.cards);
Expand All @@ -765,7 +773,13 @@ static void menu_saveload_open(void) {

static inline void menu_saveload_close(const bool success) {
mcrd_stop();

// reset timer back to normal callback
timer_set_callback(timer_cb_ticker);

// close menu
menu_id = 0;

// if we're in the intro stage, re-open the main menu
if (!stage_data || stage_data->id == STAGE_OPENING_ID) {
menu_open(MENU_TITLE);
Expand Down
1 change: 0 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ int main(int argc, char **argv) {
snd_init(SFX_MAIN_BANK);
org_init();
in_init();
mcrd_init();

// load main graphics bank
gfx_load_gfx_bank(GFX_MAIN_BANK);
Expand Down

0 comments on commit 2819ae7

Please sign in to comment.