Added main menu.

This commit is contained in:
2024-10-09 09:47:05 -05:00
parent 825cc88e17
commit 6c062df9bb
11 changed files with 67 additions and 42 deletions

View File

@ -7,8 +7,20 @@
#include "drawstatemainmenu.h"
#include "display/draw/drawtext.h"
#include "display/draw/drawui.h"
#include "ui/mainmenu.h"
void drawStateMainMenu() {
// Draw the logo
drawText("Dawn", 4, 0, 0, COLOR_WHITE);
// Draw the menu
uint16_t width = menuGetLongestStringLength(&MAIN_MENU) + 3;
uint16_t height = MAIN_MENU.rows + 2;
drawUIMenuBox(
&MAIN_MENU,
(FRAME_WIDTH - width) / 2, FRAME_HEIGHT - height - 4,
width, height,
COLOR_WHITE, COLOR_WHITE, COLOR_WHITE
);
}

View File

@ -36,5 +36,4 @@ void drawStateOverworld() {
// Draw UI
drawUITextbox();
// drawUITestMenu();
}

View File

@ -226,12 +226,3 @@ void drawUITextbox() {
FRAME_BUFFER[DRAW_UI_TEXTBOX_CURSOR_POS + 1] = blink ? '>' : ' ';
FRAME_BUFFER[DRAW_UI_TEXTBOX_CURSOR_POS + 2] = blink ? ' ' : '>';
}
void drawUITestMenu() {
drawUIMenuBox(
&TEST_MENU.menu,
0, 0,
FRAME_WIDTH, 5,
COLOR_CYAN, COLOR_RED, COLOR_WHITE
);
}

View File

@ -81,8 +81,3 @@ void drawUIMenuBox(
* Draws the UI textbox to the frame buffer.
*/
void drawUITextbox();
/**
* Draws the UI test menu to the frame buffer.
*/
void drawUITestMenu();

View File

@ -40,8 +40,10 @@ void frameUpdate() {
case GAME_STATE_MAIN_MENU:
drawStateMainMenu();
break;
default:
printf("Rendering unknown game state: %d\n", GAME.state);
break;
}
}

View File

@ -10,7 +10,7 @@
#include "input.h"
#include "display/display.h"
#include "asset/asset.h"
#include "asset/assetmap.h"
#include "ui/textbox.h"
#include "ui/testmenu.h"
#include "ui/mainmenu.h"
@ -33,7 +33,6 @@ void gameInit() {
testMenuInit();
mainMenuInit();
GAME.mapNext = MAP_LIST_TEST;
GAME.state = GAME_STATE_INITIAL;
}
@ -61,6 +60,9 @@ gameupdateresult_t gameUpdate(const float_t delta) {
case GAME_STATE_MAIN_MENU:
gameStateMainMenuUpdate();
break;
default:
printf("Updating unknown state %d\n", GAME.state);
}
// Perform render.

View File

@ -104,18 +104,8 @@ tile_t mapTileGetByPosition(
const uint16_t y,
const uint8_t layer
) {
assertTrue(
x < map->width,
"X position must be less than map width."
);
assertTrue(
y < map->height,
"Y position must be less than map height."
);
assertTrue(
layer < map->layers,
"Layer must be less than map layers."
);
if(x >= map->width || y >= map->height) return TILE_NULL;
if(layer >= map->layers) return TILE_NULL;
return map->tiles[
(layer * map->width * map->height) +

View File

@ -6,8 +6,9 @@
*/
#include "mainmenu.h"
#include "game/game.h"
mainmenu_t MAIN_MENU;
menu_t MAIN_MENU;
void mainMenuInit() {
const char_t* strings[] = {
@ -19,13 +20,13 @@ void mainMenuInit() {
uint16_t columns = 1;
uint16_t rows = 4;
menuInit(&MAIN_MENU.menu, columns, rows);
memcpy(MAIN_MENU.menu.strings, strings, sizeof(strings));
MAIN_MENU.menu.selectCallback = mainMenuSelectCallback;
menuInit(&MAIN_MENU, columns, rows);
memcpy(MAIN_MENU.strings, strings, sizeof(strings));
MAIN_MENU.selectCallback = mainMenuSelectCallback;
}
void mainMenuUpdate() {
menuUpdate(&MAIN_MENU.menu);
menuUpdate(&MAIN_MENU);
}
void mainMenuSelectCallback(
@ -34,5 +35,22 @@ void mainMenuSelectCallback(
const uint16_t y,
const char_t *str
) {
printf("Selected: %s\n", str);
if(strcmp(str, "New Game") == 0) {
GAME.mapNext = MAP_LIST_TEST;
GAME.state = GAME_STATE_MAP_CHANGE;
return;
}
if(strcmp(str, "Load Game") == 0) {
return;
}
if(strcmp(str, "Options") == 0) {
return;
}
if(strcmp(str, "Exit") == 0) {
GAME.shouldExit = true;
return;
}
}

View File

@ -8,11 +8,7 @@
#pragma once
#include "ui/menu.h"
typedef struct {
menu_t menu;
} mainmenu_t;
extern mainmenu_t MAIN_MENU;
extern menu_t MAIN_MENU;
/**
* Initializes the main menu.

View File

@ -132,3 +132,15 @@ void menuPositionSet(menu_t *menu, const uint16_t x, const uint16_t y) {
}
}
}
size_t menuGetLongestStringLength(const menu_t *menu) {
assertNotNull(menu, "Menu cannot be NULL.");
size_t longest = 0;
for(uint16_t i = 0; i < MENU_ITEM_COUNT_MAX; i++) {
if(menu->strings[i] == NULL) continue;
size_t len = strlen(menu->strings[i]);
if(len > longest) longest = len;
}
return longest;
}

View File

@ -71,3 +71,11 @@ void menuPositionMove(menu_t *menu, const menudirection_t direction);
* @param y Y position.
*/
void menuPositionSet(menu_t *menu, const uint16_t x, const uint16_t y);
/**
* Gets the length of the longest string in the menu.
*
* @param menu Menu to get the longest string from.
* @return The length of the longest string.
*/
size_t menuGetLongestStringLength(const menu_t *menu);