this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #1184 from RobLoach/state

libretro: Fix segfaults around the state

authored by

Vadim Grigoruk and committed by
GitHub
5a8613f6 ae1b3fcf

+26 -17
+26 -17
src/system/libretro/tic80_libretro.c
··· 10 10 #include "../../ticapi.h" 11 11 12 12 /** 13 - * system.h is used for TIC80_OFFSET_LEFT and TIC80_OFFSET_TOP 13 + * system.h is used for: 14 + * - TIC80_OFFSET_LEFT 15 + * - TIC80_OFFSET_TOP, 16 + * - TIC_NAME 17 + * - TIC_VERSION_LABEL 14 18 */ 15 19 #include "../../system.h" 16 20 ··· 99 103 */ 100 104 RETRO_API void retro_init(void) 101 105 { 102 - // Ensure the state is initialized. 103 - if (!state) { 104 - state = (struct tic80_state*) malloc(sizeof(struct tic80_state)); 106 + // Ensure the state is ready. 107 + if (state != NULL) { 108 + retro_deinit(); 105 109 } 110 + 111 + // Initialize the base state. 112 + state = (struct tic80_state*) malloc(sizeof(struct tic80_state)); 106 113 state->quit = false; 107 114 state->variablePointerApi = false; 108 115 state->mouseCursor = 0; ··· 213 220 { 214 221 // Make sure the game is unloaded. 215 222 retro_unload_game(); 223 + 224 + // Free up the state. 216 225 if (state != NULL) { 217 226 free(state); 218 227 state = NULL; ··· 349 358 * 350 359 * @see tic80_libretro_update() 351 360 */ 352 - void tic80_libretro_input_descriptors() { 361 + void tic80_libretro_input_descriptors() 362 + { 353 363 // TIC-80's controller has flipped A/B and X/Y buttons than RetroPad. 354 364 struct retro_input_descriptor desc[] = { 355 365 ··· 412 422 * 413 423 * @see tic80_libretro_update() 414 424 */ 415 - void tic80_libretro_update_gamepad(tic80_gamepad* gamepad, int player) { 425 + void tic80_libretro_update_gamepad(tic80_gamepad* gamepad, int player) 426 + { 416 427 // D-Pad 417 428 gamepad->up = input_state_cb(player, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP); 418 429 gamepad->down = input_state_cb(player, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN); ··· 662 673 663 674 // Check if the game requested to quit. 664 675 if (state->quit) { 676 + retro_deinit(); 665 677 environ_cb(RETRO_ENVIRONMENT_SHUTDOWN, NULL); 666 - retro_unload_game(); 667 - retro_deinit(); 668 678 return; 669 679 } 670 680 ··· 686 696 */ 687 697 RETRO_API bool retro_load_game(const struct retro_game_info *info) 688 698 { 699 + // TODO: Warn that Audio Synchronization required to run at a proper speed. 700 + // TODO: Warn that the core doesn't support Runahead. 701 + 689 702 // Initialize the core if it hasn't been yet. 690 703 if (state == NULL) { 691 704 retro_init(); 692 705 } 693 - 694 - // TODO: Warn that Audio Synchronization required to run at a proper speed. 695 - // TODO: Warn that the core doesn't support Runahead. 696 706 697 707 // Pixel format. 698 708 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888; ··· 713 723 return false; 714 724 } 715 725 716 - // Ensure a game is not currently loaded before loading. 717 - retro_unload_game(); 718 - 719 726 // Set up the TIC-80 environment. 720 727 state->tic = tic80_create(TIC80_SAMPLERATE); 721 728 if (state->tic == NULL) { ··· 757 764 */ 758 765 RETRO_API void retro_unload_game(void) 759 766 { 760 - if (state != NULL && state->tic != NULL) { 761 - tic80_delete(state->tic); 762 - state->tic = NULL; 767 + if (state != NULL) { 768 + if (state->tic != NULL) { 769 + tic80_delete(state->tic); 770 + state->tic = NULL; 771 + } 763 772 } 764 773 } 765 774