Fixed a buffer overflow.
This commit is contained in:
@ -12,6 +12,8 @@ void menuInit(menu_t *menu) {
|
||||
menu->selected = 0;
|
||||
menu->cursorX = 0;
|
||||
menu->cursorY = 0;
|
||||
menu->user = NULL;
|
||||
menu->onSelect = NULL;
|
||||
}
|
||||
|
||||
void menuUpdate(menu_t *menu, engine_t *engine) {
|
||||
@ -21,6 +23,13 @@ void menuUpdate(menu_t *menu, engine_t *engine) {
|
||||
|
||||
current = menu->items + menu->selected;
|
||||
|
||||
if(inputIsPressed(&engine->input, INPUT_ACCEPT)) {
|
||||
if(menu->onSelect != NULL) {
|
||||
menu->onSelect(menu, current, menu->selected, menu->user);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle press binds.
|
||||
if(inputIsPressed(&engine->input, INPUT_DOWN)) {
|
||||
x = 0;
|
||||
@ -54,7 +63,7 @@ void menuUpdate(menu_t *menu, engine_t *engine) {
|
||||
}
|
||||
|
||||
// Get the item selected
|
||||
j = -1;
|
||||
j = MENU_ITEMS_MAX;
|
||||
for(i = 0; i < menu->itemCount; i++) {
|
||||
if(i == menu->selected) continue;
|
||||
item = menu->items + i;
|
||||
@ -69,7 +78,7 @@ void menuUpdate(menu_t *menu, engine_t *engine) {
|
||||
}
|
||||
|
||||
// Was a target found?
|
||||
if(j == -1) return;
|
||||
if(j == MENU_ITEMS_MAX) return;
|
||||
menu->selected = j;
|
||||
}
|
||||
}
|
||||
|
@ -7,15 +7,27 @@
|
||||
|
||||
#include "menulist.h"
|
||||
|
||||
void _menuListMenuOnSelect(menu_t *menu, menuitem_t *item, uint8_t i, void *u) {
|
||||
menulist_t *ml = (menulist_t *)u;
|
||||
if(ml->onSelect == NULL) return;
|
||||
ml->onSelect(ml, i, ml->user);
|
||||
}
|
||||
|
||||
void menuListInit(menulist_t *ml, texture_t *texture) {
|
||||
ml->x = 0;
|
||||
ml->y = 0;
|
||||
ml->onSelect = NULL;
|
||||
|
||||
// Initialize menu
|
||||
menuInit(&ml->menu);
|
||||
|
||||
ml->menu.onSelect = &_menuListMenuOnSelect;
|
||||
ml->menu.user = (void *)ml;
|
||||
|
||||
// Init the selection rectangle
|
||||
rectangleInit(&ml->selection);
|
||||
rectangleSetColor(&ml->selection, MENULIST_SELECTION_COLOR);
|
||||
|
||||
// Init the frame
|
||||
frameInit(&ml->frame);
|
||||
ml->frame.texture = texture;
|
||||
}
|
||||
|
@ -12,12 +12,46 @@
|
||||
#include "frame.h"
|
||||
#include "rectangle.h"
|
||||
|
||||
/** Callback to listen for when the menu is selected. */
|
||||
void _menuListMenuOnSelect(menu_t *menu, menuitem_t *item, uint8_t i, void *u);
|
||||
|
||||
/**
|
||||
* Initialize a menu list.
|
||||
*
|
||||
* @param ml Menu List to initialize.
|
||||
* @param texture Texture to use for the frame.
|
||||
*/
|
||||
void menuListInit(menulist_t *ml, texture_t *texture);
|
||||
|
||||
/**
|
||||
* Add an item to the menu list.
|
||||
*
|
||||
* @param ml Menu list to add to.
|
||||
* @param font Font to use for the label.
|
||||
* @param text Text to add.
|
||||
* @return Index that refers to the label/menu item.
|
||||
*/
|
||||
uint8_t menuListAdd(menulist_t *ml, font_t *font, char *text);
|
||||
|
||||
/**
|
||||
* Tick the menu list and listen for input changes.
|
||||
*
|
||||
* @param ml Menu list to update.
|
||||
* @param engine Engine to update with.
|
||||
*/
|
||||
void menuListUpdate(menulist_t *ml, engine_t *engine);
|
||||
|
||||
/**
|
||||
* Render a menu list.
|
||||
*
|
||||
* @param ml Menu list to render.
|
||||
* @param shader Shader to use.
|
||||
*/
|
||||
void menuListRender(menulist_t *ml, shader_t *shader);
|
||||
|
||||
/**
|
||||
* Cleanup a previously created menu list.
|
||||
*
|
||||
* @param ml Menu list to clean up.
|
||||
*/
|
||||
void menuListDispose(menulist_t *ml);
|
Reference in New Issue
Block a user