Introduces menu option descriptions

This commit is contained in:
Linus Probert 2018-10-22 09:04:16 +02:00
parent f5a1c2f48a
commit 1a0d806459
4 changed files with 82 additions and 21 deletions

2
.vimrc
View File

@ -1,6 +1,6 @@
nnoremap <F1> :Make<cr>
nnoremap <F2> :Make lint test<cr>
nnoremap <F3> :Termdebug LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./ _build/debug/breakhack<cr>
nnoremap <F3> :Termdebug _build/debug/breakhack<cr>
nnoremap <F4> :ter ++close env LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./ ./_build/debug/breakhack<cr>
packadd termdebug

View File

@ -384,7 +384,7 @@ static void
goToCharacterMenu(void *unused)
{
UNUSED(unused);
charSelectMenu = menu_create_character_selector(on_character_select);
charSelectMenu = menu_create_character_selector(on_character_select, gCamera);
gGameState = CHARACTER_MENU;
}
@ -415,10 +415,10 @@ static void
initInGameMenu(void)
{
static TEXT_MENU_ITEM menu_items[] = {
{ "RESUME", toggleInGameMenu },
{ "HOW TO PLAY", showHowToTooltip },
{ "MAIN MENU", goToMainMenu },
{ "QUIT", exitGame },
{ "RESUME", "", toggleInGameMenu },
{ "HOW TO PLAY", "", showHowToTooltip },
{ "MAIN MENU", "", goToMainMenu },
{ "QUIT", "Exit game", exitGame },
};
menu_create_text_menu(&inGameMenu, &menu_items[0], 4, gRenderer);
@ -428,9 +428,10 @@ static void
createInGameGameOverMenu(void)
{
static TEXT_MENU_ITEM menu_items[] = {
{ "NEW GAME", goToCharacterMenu },
{ "MAIN MENU", goToMainMenu },
{ "QUIT", exitGame },
{ "NEW GAME", "Start a new game",
goToCharacterMenu },
{ "MAIN MENU", "", goToMainMenu },
{ "QUIT", "Exit game", exitGame },
};
if (inGameMenu) {
@ -458,10 +459,10 @@ static void
initMainMenu(void)
{
static TEXT_MENU_ITEM menu_items[] = {
{ "PLAY", goToCharacterMenu },
{ "SCORES", viewScoreScreen },
{ "CREDITS", viewCredits },
{ "QUIT", exitGame },
{ "PLAY", "Play a standard 20 level game", goToCharacterMenu },
{ "SCORES", "View your top 10 scores", viewScoreScreen },
{ "CREDITS", "View game credits", viewCredits },
{ "QUIT", "Exit game", exitGame },
};
if (gMap)
@ -780,7 +781,7 @@ run_game_update(void)
static UpdateData updateData;
if (gGameState == IN_GAME_MENU)
menu_update(inGameMenu, &input);
menu_update(inGameMenu, &input, gCamera);
populateUpdateData(&updateData, deltaTime);
@ -988,9 +989,9 @@ run_menu(void)
return;
if (gGameState == MENU)
menu_update(mainMenu, &input);
menu_update(mainMenu, &input, gCamera);
else if (gGameState == CHARACTER_MENU)
menu_update(charSelectMenu, &input);
menu_update(charSelectMenu, &input, gCamera);
SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 0);
SDL_RenderClear(gRenderer);

View File

@ -38,12 +38,25 @@ typedef struct MenuItem {
GuiButton *button;
} MenuItem;
static void redraw_description(Menu *m, Camera *cam);
Menu *
menu_create(void)
{
Menu *menu = ec_malloc(sizeof(Menu));
menu->items = linkedlist_create();
menu->descriptions = linkedlist_create();
menu->selected = 0;
menu->menuDescription = sprite_create();
sprite_load_text_texture(menu->menuDescription,
"GUI/SDS_8x8.ttf",
0,
12,
1);
menu->menuDescription->fixed = true;
menu->menuDescription->pos = POS(20, SCREEN_HEIGHT - 20);
menu->menuDescription->hidden = true;
return menu;
}
@ -80,11 +93,12 @@ menu_create_text_menu(Menu **menu, TEXT_MENU_ITEM *menu_items, unsigned int size
s2->fixed = true;
menu_item_add(*menu, s1, s2, menu_items[i].callback);
linkedlist_append(&(*menu)->descriptions, (void*) menu_items[i].description);
}
}
Menu *
menu_create_character_selector(void (*onCharacterSelect)(const char *))
menu_create_character_selector(void (*onCharacterSelect)(const char *), Camera *cam)
{
const char *spriteSheets[] = {
"Commissions/Warrior.png",
@ -96,6 +110,11 @@ menu_create_character_selector(void (*onCharacterSelect)(const char *))
"rogue"
};
static char *descriptions[] = {
"Play as the warrior",
"Play as the rogue",
};
Menu *menu = menu_create();
int xoffset = 224;
for (size_t i = 0; i < 2; ++i) {
@ -117,8 +136,13 @@ menu_create_character_selector(void (*onCharacterSelect)(const char *))
MenuItem *item = linkedlist_get(&menu->items, (Uint32) i);
item->button->usrdata = callbackData[i];
xoffset += 224;
linkedlist_append(&menu->descriptions, descriptions[i]);
}
menu->selected = 0;
redraw_description(menu, cam);
return menu;
}
@ -178,9 +202,31 @@ handle_mouse_motion(Menu *m, Input *input)
}
void
menu_update(Menu *m, Input *input)
static void
redraw_description(Menu *m, Camera *cam)
{
char *description = linkedlist_get(&m->descriptions, m->selected);
if (!description || strlen(description) <= 1) {
m->menuDescription->hidden = true;
return;
}
m->menuDescription->hidden = false;
texture_load_from_text(m->menuDescription->textures[0],
description,
C_WHITE,
C_BLACK,
cam->renderer);
m->menuDescription->dim = DIM(
m->menuDescription->textures[0]->dim.width,
m->menuDescription->textures[0]->dim.height);
}
void
menu_update(Menu *m, Input *input, Camera *cam)
{
static int lastSelected = -1;
if (handle_keyboard_input(m, input)) {
return;
}
@ -199,6 +245,11 @@ menu_update(Menu *m, Input *input)
return;
}
}
if (lastSelected != m->selected) {
lastSelected = m->selected;
redraw_description(m, cam);
}
}
void
@ -236,6 +287,8 @@ menu_render(Menu *m, Camera *cam)
index++;
}
sprite_render(m->menuDescription, cam);
}
static void
@ -255,6 +308,10 @@ menu_destroy(Menu *m)
while (m->items)
menu_item_destroy(linkedlist_pop(&m->items));
while (m->descriptions)
linkedlist_pop(&m->descriptions);
sprite_destroy(m->menuDescription);
free(m);
}

View File

@ -27,11 +27,14 @@
typedef struct TEXT_MENU_ITEM {
char label[20];
char description[100];
void (*callback)(void*);
} TEXT_MENU_ITEM;
typedef struct Menu_t {
LinkedList *items;
LinkedList *descriptions;
Sprite *menuDescription;
int selected;
} Menu;
@ -42,10 +45,10 @@ void
menu_create_text_menu(Menu **menu, TEXT_MENU_ITEM *menu_items, unsigned int size, SDL_Renderer *);
Menu *
menu_create_character_selector(void (*onCharacterSelect)(const char *));
menu_create_character_selector(void (*onCharacterSelect)(const char *), Camera *cam);
void
menu_update(Menu*, Input*);
menu_update(Menu*, Input*, Camera *cam);
void
menu_item_add(Menu*, Sprite*, Sprite*, void (*)(void*));