Fixed a buffer overflow.
This commit is contained in:
@ -11,10 +11,10 @@
|
|||||||
#include "../../poker/player.h"
|
#include "../../poker/player.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
label_t labelChips;
|
label_t labelInfo;
|
||||||
label_t labelName;
|
|
||||||
} pokerplayerui_t;
|
} pokerplayerui_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
pokerplayerui_t players[POKER_PLAYER_COUNT];
|
pokerplayerui_t players[POKER_PLAYER_COUNT];
|
||||||
|
label_t labelGameInfo;
|
||||||
} pokerui_t;
|
} pokerui_t;
|
@ -8,31 +8,30 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../libs.h"
|
#include "../libs.h"
|
||||||
|
|
||||||
#define MENU_ITEMS_COLUMNS_MAX 16
|
/** The maximum number of items a menu can hold */
|
||||||
#define MENU_ITEMS_ROWS_MAX 128
|
#define MENU_ITEMS_MAX 32
|
||||||
#define MENU_ITEMS_MAX MENU_ITEMS_COLUMNS_MAX * MENU_ITEMS_ROWS_MAX
|
|
||||||
|
|
||||||
#define MENU_DIRECTION_DOWN 0x01
|
|
||||||
#define MENU_DIRECTION_UP 0x02
|
|
||||||
#define MENU_DIRECTION_LEFT 0x03
|
|
||||||
#define MENU_DIRECTION_RIGHT 0x04
|
|
||||||
|
|
||||||
#define MENU_HOLD_DURATION 1.0
|
typedef struct _menuitem_t menuitem_t;
|
||||||
|
typedef struct _menu_t menu_t;
|
||||||
|
|
||||||
typedef struct {
|
/** Callback for when menu events are fired. */
|
||||||
|
typedef void menucallback_t(menu_t *m, menuitem_t *t, uint8_t i, void *user);
|
||||||
|
|
||||||
|
typedef struct _menuitem_t {
|
||||||
uint8_t x;
|
uint8_t x;
|
||||||
uint8_t y;
|
uint8_t y;
|
||||||
uint8_t width;
|
uint8_t width;
|
||||||
uint8_t height;
|
uint8_t height;
|
||||||
} menuitem_t;
|
} menuitem_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct _menu_t {
|
||||||
menuitem_t items[MENU_ITEMS_MAX];
|
menuitem_t items[MENU_ITEMS_MAX];
|
||||||
uint8_t itemCount;
|
uint8_t itemCount;
|
||||||
uint8_t selected;
|
uint8_t selected;
|
||||||
uint8_t cursorX;
|
uint8_t cursorX;
|
||||||
uint8_t cursorY;
|
uint8_t cursorY;
|
||||||
|
|
||||||
// bool holdAllow;
|
void *user;
|
||||||
// float holdLast;
|
menucallback_t *onSelect;
|
||||||
} menu_t;
|
} menu_t;
|
@ -18,12 +18,19 @@
|
|||||||
/** Maximum number of items that the list supports */
|
/** Maximum number of items that the list supports */
|
||||||
#define MENULIST_ITEMS_MAX 32
|
#define MENULIST_ITEMS_MAX 32
|
||||||
|
|
||||||
|
typedef struct _menulist_t menulist_t;
|
||||||
|
|
||||||
|
/** Callback for when a menulist item is selected */
|
||||||
|
typedef void menulistcallback_t(menulist_t *list, uint8_t i, void *user);
|
||||||
|
|
||||||
/** Representation of a menu list, a menu with multiple menu items. */
|
/** Representation of a menu list, a menu with multiple menu items. */
|
||||||
typedef struct {
|
typedef struct _menulist_t {
|
||||||
float x, y;
|
float x, y;
|
||||||
char *items[MENULIST_ITEMS_MAX];
|
char *items[MENULIST_ITEMS_MAX];
|
||||||
label_t labels[MENULIST_ITEMS_MAX];
|
label_t labels[MENULIST_ITEMS_MAX];
|
||||||
frame_t frame;
|
frame_t frame;
|
||||||
menu_t menu;
|
menu_t menu;
|
||||||
rectangle_t selection;
|
rectangle_t selection;
|
||||||
|
menulistcallback_t *onSelect;
|
||||||
|
void *user;
|
||||||
} menulist_t;
|
} menulist_t;
|
@ -13,7 +13,7 @@
|
|||||||
* @param b Number to modulo with. (a % b)
|
* @param b Number to modulo with. (a % b)
|
||||||
* @returns The modulo result.
|
* @returns The modulo result.
|
||||||
*/
|
*/
|
||||||
#define mathMod(a,b) (a%b+b)%b
|
#define mathMod(a,b) ((a)%(b)+(b))%(b)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the modulous a result for b. Works for floating point numbers.
|
* Returns the modulous a result for b. Works for floating point numbers.
|
||||||
|
@ -42,7 +42,7 @@
|
|||||||
* @param max Maximum value to generate to. (Exclusive)
|
* @param max Maximum value to generate to. (Exclusive)
|
||||||
* @return Random number between min and max.
|
* @return Random number between min and max.
|
||||||
*/
|
*/
|
||||||
#define randRange(n, min, max) (mathMod(n, max - min) + min)
|
#define randRange(n, min, max) (mathMod(n, (max-min)) + min)
|
||||||
|
|
||||||
#define randInt32Range(min, max) randRange(randInt32(), min, max)
|
#define randInt32Range(min, max) randRange(randInt32(), min, max)
|
||||||
#define randFloatRange(min, max) (fmod(randFloat(), max- min) + min)
|
#define randFloatRange(min, max) (fmod(randFloat(), max- min) + min)
|
||||||
|
@ -7,8 +7,6 @@
|
|||||||
|
|
||||||
#include "pokergame.h"
|
#include "pokergame.h"
|
||||||
|
|
||||||
menulist_t ml;
|
|
||||||
|
|
||||||
bool pokerGameInit(game_t *game) {
|
bool pokerGameInit(game_t *game) {
|
||||||
pokergame_t *pokerGame = &game->pokerGame;
|
pokergame_t *pokerGame = &game->pokerGame;
|
||||||
|
|
||||||
@ -31,11 +29,6 @@ bool pokerGameInit(game_t *game) {
|
|||||||
pokerGameActionStartAdd(pokerGame);
|
pokerGameActionStartAdd(pokerGame);
|
||||||
queueNext(&pokerGame->scene.conversation.actionQueue);
|
queueNext(&pokerGame->scene.conversation.actionQueue);
|
||||||
|
|
||||||
// TESTING
|
|
||||||
menuListInit(&ml, &pokerGame->assets.testTexture);
|
|
||||||
menuListAdd(&ml, &pokerGame->assets.font, "One");
|
|
||||||
menuListAdd(&ml, &pokerGame->assets.font, "Two");
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,9 +51,6 @@ void pokerGameUpdate(game_t *game) {
|
|||||||
// Render the UI
|
// Render the UI
|
||||||
vnSceneRenderGui(&pokerGame->scene, &game->engine, &pokerGame->assets.shader);
|
vnSceneRenderGui(&pokerGame->scene, &game->engine, &pokerGame->assets.shader);
|
||||||
pokerUiRender(pokerGame);
|
pokerUiRender(pokerGame);
|
||||||
|
|
||||||
menuListUpdate(&ml, &game->engine);
|
|
||||||
menuListRender(&ml, &game->pokerGame.assets.shader);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void pokerGameDispose(game_t *game) {
|
void pokerGameDispose(game_t *game) {
|
||||||
|
@ -18,15 +18,12 @@ void pokerUiInit(pokergame_t *pokerGame) {
|
|||||||
playerPoker = pokerGame->poker.players + i;
|
playerPoker = pokerGame->poker.players + i;
|
||||||
character = pokerGame->scene.characters + i;
|
character = pokerGame->scene.characters + i;
|
||||||
|
|
||||||
// Chips label
|
labelInit(&playerUi->labelInfo);
|
||||||
labelInit(&playerUi->labelChips);
|
playerUi->labelInfo.fontSize = 12.0;
|
||||||
playerUi->labelChips.fontSize = 12.0;
|
|
||||||
|
|
||||||
// Name Label
|
|
||||||
labelInit(&playerUi->labelName);
|
|
||||||
playerUi->labelName.fontSize = 12.0;
|
|
||||||
// labelSetText(&playerUi->labelChips, &pokerGame->assets.font, character->name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
labelInit(&pokerGame->ui.labelGameInfo);
|
||||||
|
pokerGame->ui.labelGameInfo.fontSize = 12.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pokerUiRender(pokergame_t *pokerGame) {
|
void pokerUiRender(pokergame_t *pokerGame) {
|
||||||
@ -35,19 +32,28 @@ void pokerUiRender(pokergame_t *pokerGame) {
|
|||||||
uint8_t i;
|
uint8_t i;
|
||||||
char buffer[256];
|
char buffer[256];
|
||||||
|
|
||||||
|
sprintf(buffer, "Pot %i\n", pokerGame->poker.bet.pot);
|
||||||
|
labelSetText(&pokerGame->ui.labelGameInfo, &pokerGame->assets.font, buffer);
|
||||||
|
pokerGame->ui.labelGameInfo.x = 350;
|
||||||
|
labelRender(&pokerGame->ui.labelGameInfo, &pokerGame->assets.shader);
|
||||||
|
|
||||||
for(i = 0; i < POKER_PLAYER_COUNT; i++) {
|
for(i = 0; i < POKER_PLAYER_COUNT; i++) {
|
||||||
break;
|
|
||||||
playerUi = pokerGame->ui.players + i;
|
playerUi = pokerGame->ui.players + i;
|
||||||
playerPoker = pokerGame->poker.players + i;
|
playerPoker = pokerGame->poker.players + i;
|
||||||
|
|
||||||
// Player Chips
|
// Player Chips
|
||||||
sprintf(buffer, "%u chips", playerPoker->chips);
|
sprintf(buffer, "Player %u - %u chips - %s%s",
|
||||||
playerUi->labelChips.y = ((float)i+1.0f) * 32.0f;
|
i,
|
||||||
labelSetText(&playerUi->labelChips, &pokerGame->assets.font, buffer);
|
playerPoker->chips,
|
||||||
labelRender(&playerUi->labelChips, &pokerGame->assets.shader);
|
i == POKER_PLAYER_HUMAN_INDEX ? "Human" : "AI",
|
||||||
|
i == pokerGame->poker.bet.better ? " Betting" :
|
||||||
// Player Name
|
playerPoker->state & POKER_PLAYER_STATE_FOLDED ? " Folded" :
|
||||||
labelRender(&playerUi->labelName, &pokerGame->assets.shader);
|
playerPoker->state & POKER_PLAYER_STATE_OUT ? " Out" :
|
||||||
|
""
|
||||||
|
);
|
||||||
|
playerUi->labelInfo.y = (float)i * 32.0f;
|
||||||
|
labelSetText(&playerUi->labelInfo, &pokerGame->assets.font, buffer);
|
||||||
|
labelRender(&playerUi->labelInfo, &pokerGame->assets.shader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,6 +63,6 @@ void pokerUiDispose(pokergame_t *pokerGame) {
|
|||||||
|
|
||||||
for(i = 0; i < POKER_PLAYER_COUNT; i++) {
|
for(i = 0; i < POKER_PLAYER_COUNT; i++) {
|
||||||
player = pokerGame->ui.players + i;
|
player = pokerGame->ui.players + i;
|
||||||
labelDispose(&player->labelChips);
|
labelDispose(&player->labelInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,28 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2021 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "testscene.h"
|
|
||||||
|
|
||||||
void testSceneInit(testscene_t *scene, game_t *game) {
|
|
||||||
assetFontLoad(&scene->font, "fonts/opensans/OpenSans-Bold.ttf");
|
|
||||||
assetShaderLoad(&scene->shader,
|
|
||||||
"shaders/textured.vert", "shaders/textured.frag"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void testSceneRender(testscene_t *scene, game_t *game) {
|
|
||||||
cameraLookAt(&scene->camera,
|
|
||||||
0.5, 0.5, 0.75,
|
|
||||||
0.5, 0.5, -0.5
|
|
||||||
);
|
|
||||||
cameraPerspective(&scene->camera, 75,
|
|
||||||
game->engine.render.width/game->engine.render.height, 0.01, 1000.0
|
|
||||||
);
|
|
||||||
|
|
||||||
shaderUse(&scene->shader);
|
|
||||||
shaderUseCamera(&scene->shader, &scene->camera);
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2021 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include <dawn/dawn.h>
|
|
||||||
#include "../display/shader.h"
|
|
||||||
#include "../display/camera.h"
|
|
||||||
#include "../display/font.h"
|
|
||||||
#include "../file/asset.h"
|
|
||||||
|
|
||||||
#include "../display/font.h"
|
|
||||||
#include "../display/texture.h"
|
|
||||||
#include "../vn/vncharacter.h"
|
|
||||||
#include "../vn/conversation/vnconversation.h"
|
|
||||||
#include "../vn/conversation/talk.h"
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
shader_t shader;
|
|
||||||
camera_t camera;
|
|
||||||
font_t font;
|
|
||||||
} testscene_t;
|
|
||||||
|
|
||||||
void testSceneInit(testscene_t *scene, game_t *game);
|
|
||||||
void testSceneRender(testscene_t *scene, game_t *game);
|
|
@ -12,6 +12,8 @@ void menuInit(menu_t *menu) {
|
|||||||
menu->selected = 0;
|
menu->selected = 0;
|
||||||
menu->cursorX = 0;
|
menu->cursorX = 0;
|
||||||
menu->cursorY = 0;
|
menu->cursorY = 0;
|
||||||
|
menu->user = NULL;
|
||||||
|
menu->onSelect = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void menuUpdate(menu_t *menu, engine_t *engine) {
|
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;
|
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.
|
// Handle press binds.
|
||||||
if(inputIsPressed(&engine->input, INPUT_DOWN)) {
|
if(inputIsPressed(&engine->input, INPUT_DOWN)) {
|
||||||
x = 0;
|
x = 0;
|
||||||
@ -54,7 +63,7 @@ void menuUpdate(menu_t *menu, engine_t *engine) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the item selected
|
// Get the item selected
|
||||||
j = -1;
|
j = MENU_ITEMS_MAX;
|
||||||
for(i = 0; i < menu->itemCount; i++) {
|
for(i = 0; i < menu->itemCount; i++) {
|
||||||
if(i == menu->selected) continue;
|
if(i == menu->selected) continue;
|
||||||
item = menu->items + i;
|
item = menu->items + i;
|
||||||
@ -69,7 +78,7 @@ void menuUpdate(menu_t *menu, engine_t *engine) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Was a target found?
|
// Was a target found?
|
||||||
if(j == -1) return;
|
if(j == MENU_ITEMS_MAX) return;
|
||||||
menu->selected = j;
|
menu->selected = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,15 +7,27 @@
|
|||||||
|
|
||||||
#include "menulist.h"
|
#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) {
|
void menuListInit(menulist_t *ml, texture_t *texture) {
|
||||||
ml->x = 0;
|
ml->x = 0;
|
||||||
ml->y = 0;
|
ml->y = 0;
|
||||||
|
ml->onSelect = NULL;
|
||||||
|
|
||||||
|
// Initialize menu
|
||||||
menuInit(&ml->menu);
|
menuInit(&ml->menu);
|
||||||
|
ml->menu.onSelect = &_menuListMenuOnSelect;
|
||||||
|
ml->menu.user = (void *)ml;
|
||||||
|
|
||||||
|
// Init the selection rectangle
|
||||||
rectangleInit(&ml->selection);
|
rectangleInit(&ml->selection);
|
||||||
rectangleSetColor(&ml->selection, MENULIST_SELECTION_COLOR);
|
rectangleSetColor(&ml->selection, MENULIST_SELECTION_COLOR);
|
||||||
|
|
||||||
|
// Init the frame
|
||||||
frameInit(&ml->frame);
|
frameInit(&ml->frame);
|
||||||
ml->frame.texture = texture;
|
ml->frame.texture = texture;
|
||||||
}
|
}
|
||||||
|
@ -12,12 +12,46 @@
|
|||||||
#include "frame.h"
|
#include "frame.h"
|
||||||
#include "rectangle.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);
|
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);
|
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);
|
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);
|
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);
|
void menuListDispose(menulist_t *ml);
|
Reference in New Issue
Block a user