diff --git a/src/main.c b/src/main.c index bd84a83..c8011ab 100644 --- a/src/main.c +++ b/src/main.c @@ -170,15 +170,6 @@ static Sprite *fpsSprite = NULL; static Pointer *gPointer = NULL; #endif // DEBUG -static SDL_Color C_MENU_DEFAULT = { 255, 255, 0, 255 }; -static SDL_Color C_MENU_OUTLINE_DEFAULT = { 0, 0, 0, 255 }; -static SDL_Color C_MENU_HOVER = { 255, 0, 0, 255 }; - -struct MENU_ITEM { - char label[20]; - void (*callback)(void*); -}; - static void resetGame(void); static void initMainMenu(void); static bool is_player_dead(void); @@ -372,38 +363,6 @@ goToMainMenu(void *unused) camera_follow_position(gCamera, &p); } -static void -createMenu(Menu **menu, struct MENU_ITEM menu_items[], unsigned int size) -{ - if (*menu == NULL) - *menu = menu_create(); - - for (unsigned int i = 0; i < size; ++i) { - unsigned int hcenter; - - Sprite *s1 = sprite_create(); - sprite_load_text_texture(s1, "GUI/SDS_8x8.ttf", 0, 25, 2); - texture_load_from_text(s1->textures[0], menu_items[i].label, - C_MENU_DEFAULT, C_MENU_OUTLINE_DEFAULT, gRenderer); - - hcenter = (SCREEN_WIDTH/2) - (s1->textures[0]->dim.width/2); - s1->pos = (Position) { (int) hcenter, (int) 200 + (i*50) }; - s1->dim = s1->textures[0]->dim; - s1->fixed = true; - - Sprite *s2 = sprite_create(); - sprite_load_text_texture(s2, "GUI/SDS_8x8.ttf", 0, 25, 2); - texture_load_from_text(s2->textures[0], menu_items[i].label, - C_MENU_HOVER, C_MENU_OUTLINE_DEFAULT, gRenderer); - - s2->pos = (Position) { (int) hcenter, (int) 200 + (i*50) }; - s2->dim = s2->textures[0]->dim; - s2->fixed = true; - - menu_item_add(*menu, s1, s2, menu_items[i].callback); - } -} - static void showHowToTooltip(void *unused) { @@ -415,20 +374,20 @@ showHowToTooltip(void *unused) static void initInGameMenu(void) { - struct MENU_ITEM menu_items[] = { + static TEXT_MENU_ITEM menu_items[] = { { "RESUME", toggleInGameMenu }, { "HOW TO PLAY", showHowToTooltip }, { "MAIN MENU", goToMainMenu }, { "QUIT", exitGame }, }; - createMenu(&inGameMenu, menu_items, 4); + menu_create_text_menu(&inGameMenu, &menu_items[0], 4, gRenderer); } static void createInGameGameOverMenu(void) { - struct MENU_ITEM menu_items[] = { + static TEXT_MENU_ITEM menu_items[] = { { "NEW GAME", startGame }, { "MAIN MENU", goToMainMenu }, { "QUIT", exitGame }, @@ -438,7 +397,7 @@ createInGameGameOverMenu(void) menu_destroy(inGameMenu); inGameMenu = NULL; } - createMenu(&inGameMenu, menu_items, 3); + menu_create_text_menu(&inGameMenu, &menu_items[0], 3, gRenderer); } static void @@ -458,7 +417,7 @@ viewScoreScreen(void *unused) static void initMainMenu(void) { - struct MENU_ITEM menu_items[] = { + static TEXT_MENU_ITEM menu_items[] = { { "PLAY", startGame }, { "SCORES", viewScoreScreen }, { "CREDITS", viewCredits }, @@ -470,7 +429,7 @@ initMainMenu(void) gMap = map_lua_generator_single_room__run(cLevel, gRenderer); - createMenu(&mainMenu, menu_items, 4); + menu_create_text_menu(&mainMenu, &menu_items[0], 4, gRenderer); mixer_play_music(MENU_MUSIC); creditsScreen = screen_create_credits(gRenderer); scoreScreen = screen_create_hiscore(gRenderer); diff --git a/src/menu.c b/src/menu.c index e0cac1a..b64cef8 100644 --- a/src/menu.c +++ b/src/menu.c @@ -27,6 +27,10 @@ #include "mixer.h" #include "collisions.h" +static SDL_Color C_MENU_DEFAULT = { 255, 255, 0, 255 }; +static SDL_Color C_MENU_OUTLINE_DEFAULT = { 0, 0, 0, 255 }; +static SDL_Color C_MENU_HOVER = { 255, 0, 0, 255 }; + typedef struct MenuItems { Sprite *sprite; Sprite *hsprite; @@ -42,6 +46,42 @@ menu_create(void) return menu; } +void +menu_create_text_menu(Menu **menu, TEXT_MENU_ITEM *menu_items, unsigned int size, SDL_Renderer *renderer) +{ + if (*menu != NULL) { + menu_destroy(*menu); + *menu = NULL; + } + + *menu = menu_create(); + + for (unsigned int i = 0; i < size; ++i) { + unsigned int hcenter; + + Sprite *s1 = sprite_create(); + sprite_load_text_texture(s1, "GUI/SDS_8x8.ttf", 0, 25, 2); + texture_load_from_text(s1->textures[0], menu_items[i].label, + C_MENU_DEFAULT, C_MENU_OUTLINE_DEFAULT, renderer); + + hcenter = (SCREEN_WIDTH/2) - (s1->textures[0]->dim.width/2); + s1->pos = (Position) { (int) hcenter, (int) 200 + (i*50) }; + s1->dim = s1->textures[0]->dim; + s1->fixed = true; + + Sprite *s2 = sprite_create(); + sprite_load_text_texture(s2, "GUI/SDS_8x8.ttf", 0, 25, 2); + texture_load_from_text(s2->textures[0], menu_items[i].label, + C_MENU_HOVER, C_MENU_OUTLINE_DEFAULT, renderer); + + s2->pos = (Position) { (int) hcenter, (int) 200 + (i*50) }; + s2->dim = s2->textures[0]->dim; + s2->fixed = true; + + menu_item_add(*menu, s1, s2, menu_items[i].callback); + } +} + static bool handle_keyboard_input(Menu *m, Input *input) { diff --git a/src/menu.h b/src/menu.h index f4a572e..da43d83 100644 --- a/src/menu.h +++ b/src/menu.h @@ -25,6 +25,11 @@ #include "texture.h" #include "sprite.h" +typedef struct TEXT_MENU_ITEM { + char label[20]; + void (*callback)(void*); +} TEXT_MENU_ITEM; + typedef struct Menu_t { LinkedList *items; int selected; @@ -33,6 +38,9 @@ typedef struct Menu_t { Menu * menu_create(void); +void +menu_create_text_menu(Menu **menu, TEXT_MENU_ITEM *menu_items, unsigned int size, SDL_Renderer *); + void menu_update(Menu*, Input*);