Fixed a buffer overflow.

This commit is contained in:
2021-09-03 07:59:36 -07:00
parent 1938d44667
commit 282be95493
12 changed files with 104 additions and 103 deletions

View File

@ -11,10 +11,10 @@
#include "../../poker/player.h"
typedef struct {
label_t labelChips;
label_t labelName;
label_t labelInfo;
} pokerplayerui_t;
typedef struct {
pokerplayerui_t players[POKER_PLAYER_COUNT];
label_t labelGameInfo;
} pokerui_t;

View File

@ -8,31 +8,30 @@
#pragma once
#include "../libs.h"
#define MENU_ITEMS_COLUMNS_MAX 16
#define MENU_ITEMS_ROWS_MAX 128
#define MENU_ITEMS_MAX MENU_ITEMS_COLUMNS_MAX * MENU_ITEMS_ROWS_MAX
/** The maximum number of items a menu can hold */
#define MENU_ITEMS_MAX 32
#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 y;
uint8_t width;
uint8_t height;
} menuitem_t;
typedef struct {
typedef struct _menu_t {
menuitem_t items[MENU_ITEMS_MAX];
uint8_t itemCount;
uint8_t selected;
uint8_t cursorX;
uint8_t cursorY;
// bool holdAllow;
// float holdLast;
void *user;
menucallback_t *onSelect;
} menu_t;

View File

@ -18,12 +18,19 @@
/** Maximum number of items that the list supports */
#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. */
typedef struct {
typedef struct _menulist_t {
float x, y;
char *items[MENULIST_ITEMS_MAX];
label_t labels[MENULIST_ITEMS_MAX];
frame_t frame;
menu_t menu;
rectangle_t selection;
menulistcallback_t *onSelect;
void *user;
} menulist_t;

View File

@ -13,7 +13,7 @@
* @param b Number to modulo with. (a % b)
* @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.

View File

@ -42,7 +42,7 @@
* @param max Maximum value to generate to. (Exclusive)
* @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 randFloatRange(min, max) (fmod(randFloat(), max- min) + min)

View File

@ -7,8 +7,6 @@
#include "pokergame.h"
menulist_t ml;
bool pokerGameInit(game_t *game) {
pokergame_t *pokerGame = &game->pokerGame;
@ -31,11 +29,6 @@ bool pokerGameInit(game_t *game) {
pokerGameActionStartAdd(pokerGame);
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;
}
@ -58,9 +51,6 @@ void pokerGameUpdate(game_t *game) {
// Render the UI
vnSceneRenderGui(&pokerGame->scene, &game->engine, &pokerGame->assets.shader);
pokerUiRender(pokerGame);
menuListUpdate(&ml, &game->engine);
menuListRender(&ml, &game->pokerGame.assets.shader);
}
void pokerGameDispose(game_t *game) {

View File

@ -18,15 +18,12 @@ void pokerUiInit(pokergame_t *pokerGame) {
playerPoker = pokerGame->poker.players + i;
character = pokerGame->scene.characters + i;
// Chips label
labelInit(&playerUi->labelChips);
playerUi->labelChips.fontSize = 12.0;
// Name Label
labelInit(&playerUi->labelName);
playerUi->labelName.fontSize = 12.0;
// labelSetText(&playerUi->labelChips, &pokerGame->assets.font, character->name);
labelInit(&playerUi->labelInfo);
playerUi->labelInfo.fontSize = 12.0;
}
labelInit(&pokerGame->ui.labelGameInfo);
pokerGame->ui.labelGameInfo.fontSize = 12.0f;
}
void pokerUiRender(pokergame_t *pokerGame) {
@ -35,19 +32,28 @@ void pokerUiRender(pokergame_t *pokerGame) {
uint8_t i;
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++) {
break;
playerUi = pokerGame->ui.players + i;
playerPoker = pokerGame->poker.players + i;
// Player Chips
sprintf(buffer, "%u chips", playerPoker->chips);
playerUi->labelChips.y = ((float)i+1.0f) * 32.0f;
labelSetText(&playerUi->labelChips, &pokerGame->assets.font, buffer);
labelRender(&playerUi->labelChips, &pokerGame->assets.shader);
// Player Name
labelRender(&playerUi->labelName, &pokerGame->assets.shader);
sprintf(buffer, "Player %u - %u chips - %s%s",
i,
playerPoker->chips,
i == POKER_PLAYER_HUMAN_INDEX ? "Human" : "AI",
i == pokerGame->poker.bet.better ? " Betting" :
playerPoker->state & POKER_PLAYER_STATE_FOLDED ? " Folded" :
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++) {
player = pokerGame->ui.players + i;
labelDispose(&player->labelChips);
labelDispose(&player->labelInfo);
}
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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);