Fixed buffer overflowing because of mouse input
This commit is contained in:
		@@ -19,19 +19,19 @@
 | 
			
		||||
#define INPUT_DEBUG_PLUS  (inputbind_t)0x08
 | 
			
		||||
#define INPUT_DEBUG_MINUS (inputbind_t)0x09
 | 
			
		||||
 | 
			
		||||
/** Reserved Inputs (Starts at 64) */
 | 
			
		||||
#define INPUT_MOUSE_X     (inputbind_t)0x40
 | 
			
		||||
#define INPUT_MOUSE_Y     (inputbind_t)0x41
 | 
			
		||||
/** Real Inputs (Starts at 64/0x40) */
 | 
			
		||||
#define INPUT_UP          (inputbind_t)0x40
 | 
			
		||||
#define INPUT_DOWN        (inputbind_t)0x41
 | 
			
		||||
#define INPUT_LEFT        (inputbind_t)0x42
 | 
			
		||||
#define INPUT_RIGHT       (inputbind_t)0x43
 | 
			
		||||
#define INPUT_ACCEPT      (inputbind_t)0x44
 | 
			
		||||
 | 
			
		||||
/** Real Inputs (Starts at 128/0x80) */
 | 
			
		||||
#define INPUT_UP          (inputbind_t)0x80
 | 
			
		||||
#define INPUT_DOWN        (inputbind_t)0x81
 | 
			
		||||
#define INPUT_LEFT        (inputbind_t)0x82
 | 
			
		||||
#define INPUT_RIGHT       (inputbind_t)0x83
 | 
			
		||||
#define INPUT_ACCEPT      (inputbind_t)0x84
 | 
			
		||||
/** Additional sources */
 | 
			
		||||
#define INPUT_MOUSE_X     (inputsource_t)0x20
 | 
			
		||||
#define INPUT_MOUSE_Y     (inputsource_t)0x21
 | 
			
		||||
 | 
			
		||||
#define INPUT_BIND_COUNT 0xFF
 | 
			
		||||
#define INPUT_SOURCE_COUNT 1024
 | 
			
		||||
#define INPUT_SOURCE_COUNT 0xFFFF
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Input Bind, a specific action bind reference for the game engine to use.
 | 
			
		||||
 
 | 
			
		||||
@@ -120,9 +120,9 @@ void glfwOnKey(GLFWwindow *window,
 | 
			
		||||
) {
 | 
			
		||||
  input_t *input = &GAME_STATE->engine.input;
 | 
			
		||||
  if(action == GLFW_PRESS) {
 | 
			
		||||
    input->buffer[key] = 1;
 | 
			
		||||
    input->buffer[(inputsource_t)key] = 1;
 | 
			
		||||
  } else if(action == GLFW_RELEASE) {
 | 
			
		||||
    input->buffer[key] = 0;
 | 
			
		||||
    input->buffer[(inputsource_t)key] = 0;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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.
 | 
			
		||||
 * 
 | 
			
		||||
 
 | 
			
		||||
@@ -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");
 | 
			
		||||
 
 | 
			
		||||
@@ -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(
 | 
			
		||||
        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(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(
 | 
			
		||||
      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;
 | 
			
		||||
  }
 | 
			
		||||
  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;
 | 
			
		||||
}
 | 
			
		||||
@@ -27,3 +27,11 @@ void menuInit(menu_t *menu);
 | 
			
		||||
 * @param engine Engine to update from.
 | 
			
		||||
 */
 | 
			
		||||
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);
 | 
			
		||||
		Reference in New Issue
	
	Block a user