file based save states, and improved file structure
This commit is contained in:
41
lib/state.c
41
lib/state.c
@ -1,4 +1,5 @@
|
||||
#include <state.h>
|
||||
#include <windows.h>
|
||||
|
||||
void state_save(save_state* state) {
|
||||
printf("Saving state\n");
|
||||
@ -24,4 +25,44 @@ void state_load(const save_state* state) {
|
||||
audio_load_state(&state->audio);
|
||||
gamepad_load_state(&state->ctlr);
|
||||
cart_load_state(&state->cart);
|
||||
}
|
||||
|
||||
void state_save_file(u8 slot) {
|
||||
save_state* state = malloc(sizeof(*state));
|
||||
state_save(state);
|
||||
char *filename = strrchr(cart_get_context()->filename, '\\');
|
||||
filename++;
|
||||
char fn[1048];
|
||||
char *profile = getenv("USERPROFILE");
|
||||
sprintf(fn, "%s/Documents/gbemu/states/%s", profile, filename);
|
||||
CreateDirectory(fn, NULL);
|
||||
sprintf(fn, "%s/Documents/gbemu/states/%s/%d.state", profile, filename, slot);
|
||||
FILE *fp = fopen(fn, "wb");
|
||||
|
||||
if(!fp) {
|
||||
fprintf(stderr, "unable to save state %d\n", slot);
|
||||
return;
|
||||
}
|
||||
|
||||
fwrite(state, sizeof(*state), 1, fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
void state_load_file(u8 slot) {
|
||||
save_state* state = malloc(sizeof(*state));
|
||||
char *filename = strrchr(cart_get_context()->filename, '\\');
|
||||
filename++;
|
||||
char fn[1048];
|
||||
char *profile = getenv("USERPROFILE");
|
||||
sprintf(fn, "%s/Documents/gbemu/states/%s/%d.state", profile, filename, slot);
|
||||
FILE *fp = fopen(fn, "rb");
|
||||
|
||||
if(!fp) {
|
||||
printf("Save slot %d doesnt exist.\n", slot);
|
||||
return;
|
||||
}
|
||||
|
||||
fread(state, sizeof(*state), 1, fp);
|
||||
fclose(fp);
|
||||
state_load(state);
|
||||
}
|
Reference in New Issue
Block a user