Added the ability to optionally pass a destructor to ui_menu_alloc_state()

This commit is contained in:
Nathan Woods 2008-07-26 18:34:17 +00:00
parent 37f422ebec
commit b3ee2ffd1f
2 changed files with 18 additions and 7 deletions

View File

@ -119,6 +119,7 @@ struct _ui_menu
ui_menu_event event; /* the UI event that occurred */ ui_menu_event event; /* the UI event that occurred */
ui_menu * parent; /* pointer to parent menu */ ui_menu * parent; /* pointer to parent menu */
void * state; /* menu-specific state */ void * state; /* menu-specific state */
ui_menu_destroy_state_func destroy_state; /* destroy state callback */
int resetpos; /* reset position */ int resetpos; /* reset position */
void * resetref; /* reset reference */ void * resetref; /* reset reference */
int selected; /* which item is selected */ int selected; /* which item is selected */
@ -454,7 +455,11 @@ void ui_menu_free(ui_menu *menu)
/* free the state */ /* free the state */
if (menu->state != NULL) if (menu->state != NULL)
{
if (menu->destroy_state != NULL)
(*menu->destroy_state)(menu, menu->state);
free(menu->state); free(menu->state);
}
/* free the menu */ /* free the menu */
free(menu); free(menu);
@ -610,11 +615,16 @@ void ui_menu_set_custom_render(ui_menu *menu, ui_menu_custom_func custom, float
memory to represent the menu's state memory to represent the menu's state
-------------------------------------------------*/ -------------------------------------------------*/
void *ui_menu_alloc_state(ui_menu *menu, size_t size) void *ui_menu_alloc_state(ui_menu *menu, size_t size, ui_menu_destroy_state_func destroy_state)
{ {
if (menu->state != NULL) if (menu->state != NULL)
{
if (menu->destroy_state != NULL)
(*menu->destroy_state)(menu, menu->state);
free(menu->state); free(menu->state);
}
menu->state = malloc_or_die(size); menu->state = malloc_or_die(size);
menu->destroy_state = destroy_state;
memset(menu->state, 0, size); memset(menu->state, 0, size);
return menu->state; return menu->state;
@ -1662,7 +1672,7 @@ static void menu_input_common(running_machine *machine, ui_menu *menu, void *par
/* if no state, allocate now */ /* if no state, allocate now */
if (state == NULL) if (state == NULL)
state = ui_menu_alloc_state(menu, sizeof(*menustate)); state = ui_menu_alloc_state(menu, sizeof(*menustate), NULL);
menustate = state; menustate = state;
/* if the menu isn't built, populate now */ /* if the menu isn't built, populate now */
@ -1894,7 +1904,7 @@ static void menu_settings_common(running_machine *machine, ui_menu *menu, void *
/* if no state, allocate now */ /* if no state, allocate now */
if (state == NULL) if (state == NULL)
state = ui_menu_alloc_state(menu, sizeof(*menustate)); state = ui_menu_alloc_state(menu, sizeof(*menustate), NULL);
menustate = state; menustate = state;
/* if the menu isn't built, populate now */ /* if the menu isn't built, populate now */
@ -2321,7 +2331,7 @@ static void menu_bookkeeping(running_machine *machine, ui_menu *menu, void *para
/* if no state, allocate some */ /* if no state, allocate some */
if (state == NULL) if (state == NULL)
state = ui_menu_alloc_state(menu, sizeof(*prevtime)); state = ui_menu_alloc_state(menu, sizeof(*prevtime), NULL);
prevtime = state; prevtime = state;
/* if the time has rolled over another second, regenerate */ /* if the time has rolled over another second, regenerate */
@ -2417,7 +2427,7 @@ static void menu_memory_card(running_machine *machine, ui_menu *menu, void *para
/* if no state, allocate some */ /* if no state, allocate some */
if (state == NULL) if (state == NULL)
state = ui_menu_alloc_state(menu, sizeof(*cardnum)); state = ui_menu_alloc_state(menu, sizeof(*cardnum), NULL);
cardnum = state; cardnum = state;
/* if the menu isn't built, populate now */ /* if the menu isn't built, populate now */
@ -2710,7 +2720,7 @@ static void menu_select_game(running_machine *machine, ui_menu *menu, void *para
/* if no state, allocate some */ /* if no state, allocate some */
if (state == NULL) if (state == NULL)
{ {
state = ui_menu_alloc_state(menu, sizeof(*menustate) + sizeof(menustate->driverlist) * driver_list_get_count(drivers)); state = ui_menu_alloc_state(menu, sizeof(*menustate) + sizeof(menustate->driverlist) * driver_list_get_count(drivers), NULL);
if (parameter != NULL) if (parameter != NULL)
strcpy(((select_game_state *)state)->search, parameter); strcpy(((select_game_state *)state)->search, parameter);
} }

View File

@ -57,6 +57,7 @@ typedef struct _ui_menu ui_menu;
/* menu-related callback functions */ /* menu-related callback functions */
typedef void (*ui_menu_handler_func)(running_machine *machine, ui_menu *menu, void *parameter, void *state); typedef void (*ui_menu_handler_func)(running_machine *machine, ui_menu *menu, void *parameter, void *state);
typedef void (*ui_menu_custom_func)(running_machine *machine, ui_menu *menu, void *state, void *selectedref, float top, float bottom, float x, float y, float x2, float y2); typedef void (*ui_menu_custom_func)(running_machine *machine, ui_menu *menu, void *state, void *selectedref, float top, float bottom, float x, float y, float x2, float y2);
typedef void (*ui_menu_destroy_state_func)(ui_menu *menu, void *state);
/* menu-related events */ /* menu-related events */
@ -106,7 +107,7 @@ const ui_menu_event *ui_menu_process(ui_menu *menu, UINT32 flags);
void ui_menu_set_custom_render(ui_menu *menu, ui_menu_custom_func custom, float top, float bottom); void ui_menu_set_custom_render(ui_menu *menu, ui_menu_custom_func custom, float top, float bottom);
/* allocate permanent memory to represent the menu's state */ /* allocate permanent memory to represent the menu's state */
void *ui_menu_alloc_state(ui_menu *menu, size_t size); void *ui_menu_alloc_state(ui_menu *menu, size_t size, ui_menu_destroy_state_func destroy_state);
/* allocate temporary memory from the menu's memory pool */ /* allocate temporary memory from the menu's memory pool */
void *ui_menu_pool_alloc(ui_menu *menu, size_t size); void *ui_menu_pool_alloc(ui_menu *menu, size_t size);