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 <F1> :Make<cr>
nnoremap <F2> :Make lint test<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> nnoremap <F4> :ter ++close env LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./ ./_build/debug/breakhack<cr>
packadd termdebug packadd termdebug

View File

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

View File

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

View File

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