Fixed buffer overflowing because of mouse input

This commit is contained in:
2021-08-30 22:50:16 -07:00
parent e2e1e5fbac
commit 46752c6d22
7 changed files with 102 additions and 39 deletions

View File

@ -7,6 +7,9 @@
#include "pokergame.h"
menu_t menu;
frame_t frame;
bool pokerGameInit(game_t *game) {
pokergame_t *pokerGame = &game->pokerGame;
@ -29,6 +32,19 @@ bool pokerGameInit(game_t *game) {
pokerGameActionStartAdd(pokerGame);
queueNext(&pokerGame->scene.conversation.actionQueue);
frameInit(&frame);
frame.texture = &pokerGame->assets.testTexture;
menuitem_t *item;
menuInit(&menu);
item = menuAdd(&menu);
item->x = 0;
item->y = 0;
item = menuAdd(&menu);
item->x = 1;
item->y = 0;
return true;
}
@ -51,6 +67,22 @@ void pokerGameUpdate(game_t *game) {
// Render the UI
vnSceneRenderGui(&pokerGame->scene, &game->engine, &pokerGame->assets.shader);
pokerUiRender(pokerGame);
uint8_t i;
menuUpdate(&menu, &game->engine);
for(i = 0; i < menu.itemCount; i++) {
menuitem_t *item = menu.items + i;
frame.x = item->x * (FRAME_BORDER_SIZE + FRAME_BORDER_SIZE);
frame.y = item->y * (FRAME_BORDER_SIZE + FRAME_BORDER_SIZE);
if(menu.selected == i) frame.y -= FRAME_BORDER_SIZE;
frameSetSize(&frame,
item->width * (FRAME_BORDER_SIZE + FRAME_BORDER_SIZE),
item->height * (FRAME_BORDER_SIZE + FRAME_BORDER_SIZE)
);
frameRender(&frame, &pokerGame->assets.shader);
}
}
void pokerGameDispose(game_t *game) {

View File

@ -17,6 +17,9 @@
#include "pokerworld.h"
#include "actions/start.h"
#include "../../ui/menu.h"
#include "../../ui/frame.h"
/**
* Initializes the game state for the poker game.
*

View File

@ -20,7 +20,7 @@ bool pokerGameAssetsInit(pokergameassets_t *assets) {
// Load the world textures.
assetTextureLoad(&assets->testTexture, "test_texture.png");
assetTextureLoad(&assets->roomTexture, "world/pub/pub_skywall_low.png");
assetTextureLoad(&assets->roomTexture, "world/pub/pub_skywall.png");
// Load the character textures.
assetTextureLoad(&assets->pennyTexture, "characters/penny/sprites/sheet.png");

View File

@ -38,36 +38,56 @@ void menuUpdate(menu_t *menu, engine_t *engine) {
} else if(inputIsPressed(&engine->input, INPUT_RIGHT)) {
x = 1;
y = 0;
} else {
x = 0;
y = 0;
}
// Update cursor positions
if(x > 0) {
menu->cursorX = (current->x + current->width - 1) + x;
} else if(x < 0) {
menu->cursorX = current->x + x;
}
if(x != 0 || y != 0) {
if(x > 0) {
menu->cursorX = (current->x + current->width - 1) + x;
} else if(x < 0) {
menu->cursorX = current->x + x;
}
if(y > 0) {
menu->cursorY = (current->y + current->height - 1) + y;
} else if(y < 0) {
menu->cursorY = current->y + y;
}
// Get the item selected
int32_t targetIndex = -1;
for(i = 0; i < menu->itemCount; i++) {
if(i == menu->selected) continue;
item = menu->items + i;
if(y > 0) {
menu->cursorY = (current->y + current->height - 1) + y;
} else if(y < 0) {
menu->cursorY = current->y + y;
}
if(
item->i == -1 ||
item->x > menu->cursorX || (item->x + item->width - 1) < menu->cursorX ||
item->y > menu->cursorY || (item->y + item->height - 1) < menu->cursorY
) continue;
// Get the item selected
int32_t targetIndex = -1;
for(i = 0; i < menu->itemCount; i++) {
if(i == menu->selected) continue;
item = menu->items + i;
targetIndex = i;
break;
if(
item->i == -1 ||
item->x > menu->cursorX || (item->x+item->width-1) < menu->cursorX ||
item->y > menu->cursorY || (item->y+item->height-1) < menu->cursorY
) continue;
targetIndex = i;
break;
}
// Was a target found?
if(targetIndex == -1) return;
menu->selected = targetIndex;
}
if(targetIndex == -1) return;
menu->selected = targetIndex;
}
menuitem_t * menuAdd(menu_t *menu) {
menuitem_t *item = menu->items + menu->itemCount;
item->i = menu->itemCount;
item->x = 0;
item->y = 0;
item->width = 1;
item->height = 1;
menu->itemCount++;
return item;
}

View File

@ -26,4 +26,12 @@ void menuInit(menu_t *menu);
* @param menu Menu to update.
* @param engine Engine to update from.
*/
void menuUpdate(menu_t *menu, engine_t *engine);
void menuUpdate(menu_t *menu, engine_t *engine);
/**
* Add an item to the menu
*
* @param menu Menu to add to.
* @return Item to add to.
*/
menuitem_t * menuAdd(menu_t *menu);