better savestate code

This commit is contained in:
radius 2018-02-03 11:28:35 -05:00
parent 50fd7f1140
commit a8741674ce

View File

@ -735,7 +735,10 @@ bool retro_load_game_special(unsigned type, const struct retro_game_info *info,
size_t retro_serialize_size(void) size_t retro_serialize_size(void)
{ {
return 2 * GB_get_save_state_size(&gameboy[0]); if (emulated_devices == 2)
return GB_get_save_state_size(&gameboy[0]) + GB_get_save_state_size(&gameboy[1]);
else
return GB_get_save_state_size(&gameboy[0]);
} }
bool retro_serialize(void *data, size_t size) bool retro_serialize(void *data, size_t size)
@ -744,11 +747,14 @@ bool retro_serialize(void *data, size_t size)
return false; return false;
void* save_data[2]; void* save_data[2];
size_t state_size[2];
for (int i = 0; i < emulated_devices; i++) for (int i = 0; i < emulated_devices; i++)
{ {
save_data[i] = (uint8_t*)malloc(size * sizeof(uint8_t) / emulated_devices); state_size[i] = GB_get_save_state_size(&gameboy[i]);
save_data[i] = (uint8_t*)malloc(state_size[i]);
GB_save_state_to_buffer(&gameboy[i], (uint8_t*) save_data[i]); GB_save_state_to_buffer(&gameboy[i], (uint8_t*) save_data[i]);
memcpy(data + (size * i / emulated_devices), save_data[i], size / emulated_devices); memcpy(data + (state_size[i] * i), save_data[i], state_size[i]);
free(save_data[i]); free(save_data[i]);
} }
@ -761,13 +767,15 @@ bool retro_serialize(void *data, size_t size)
bool retro_unserialize(const void *data, size_t size) bool retro_unserialize(const void *data, size_t size)
{ {
void* save_data[2]; void* save_data[2];
size_t state_size[2];
int ret; int ret;
for (int i = 0; i < emulated_devices; i++) for (int i = 0; i < emulated_devices; i++)
{ {
save_data[i] = (uint8_t*)malloc(size * sizeof(uint8_t)/ emulated_devices); state_size[i] = GB_get_save_state_size(&gameboy[i]);
memcpy (save_data[i], data + (size * i / emulated_devices), size / emulated_devices); save_data[i] = (uint8_t*)malloc(state_size[i]);
ret = GB_load_state_from_buffer(&gameboy[i], save_data[i], size / emulated_devices); memcpy (save_data[i], data + (state_size[i] * i), state_size[i]);
ret = GB_load_state_from_buffer(&gameboy[i], save_data[i], state_size[i]);
free(save_data[i]); free(save_data[i]);
if (ret != 0) if (ret != 0)