Cleaned up main a bit and moved some code

This commit is contained in:
Linus Probert 2018-09-11 15:32:33 +02:00
parent cb732a80ec
commit 5043a86377
3 changed files with 54 additions and 47 deletions

View File

@ -170,15 +170,6 @@ static Sprite *fpsSprite = NULL;
static Pointer *gPointer = NULL; static Pointer *gPointer = NULL;
#endif // DEBUG #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 resetGame(void);
static void initMainMenu(void); static void initMainMenu(void);
static bool is_player_dead(void); static bool is_player_dead(void);
@ -372,38 +363,6 @@ goToMainMenu(void *unused)
camera_follow_position(gCamera, &p); 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 static void
showHowToTooltip(void *unused) showHowToTooltip(void *unused)
{ {
@ -415,20 +374,20 @@ showHowToTooltip(void *unused)
static void static void
initInGameMenu(void) initInGameMenu(void)
{ {
struct 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", exitGame },
}; };
createMenu(&inGameMenu, menu_items, 4); menu_create_text_menu(&inGameMenu, &menu_items[0], 4, gRenderer);
} }
static void static void
createInGameGameOverMenu(void) createInGameGameOverMenu(void)
{ {
struct MENU_ITEM menu_items[] = { static TEXT_MENU_ITEM menu_items[] = {
{ "NEW GAME", startGame }, { "NEW GAME", startGame },
{ "MAIN MENU", goToMainMenu }, { "MAIN MENU", goToMainMenu },
{ "QUIT", exitGame }, { "QUIT", exitGame },
@ -438,7 +397,7 @@ createInGameGameOverMenu(void)
menu_destroy(inGameMenu); menu_destroy(inGameMenu);
inGameMenu = NULL; inGameMenu = NULL;
} }
createMenu(&inGameMenu, menu_items, 3); menu_create_text_menu(&inGameMenu, &menu_items[0], 3, gRenderer);
} }
static void static void
@ -458,7 +417,7 @@ viewScoreScreen(void *unused)
static void static void
initMainMenu(void) initMainMenu(void)
{ {
struct MENU_ITEM menu_items[] = { static TEXT_MENU_ITEM menu_items[] = {
{ "PLAY", startGame }, { "PLAY", startGame },
{ "SCORES", viewScoreScreen }, { "SCORES", viewScoreScreen },
{ "CREDITS", viewCredits }, { "CREDITS", viewCredits },
@ -470,7 +429,7 @@ initMainMenu(void)
gMap = map_lua_generator_single_room__run(cLevel, gRenderer); 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); mixer_play_music(MENU_MUSIC);
creditsScreen = screen_create_credits(gRenderer); creditsScreen = screen_create_credits(gRenderer);
scoreScreen = screen_create_hiscore(gRenderer); scoreScreen = screen_create_hiscore(gRenderer);

View File

@ -27,6 +27,10 @@
#include "mixer.h" #include "mixer.h"
#include "collisions.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 { typedef struct MenuItems {
Sprite *sprite; Sprite *sprite;
Sprite *hsprite; Sprite *hsprite;
@ -42,6 +46,42 @@ menu_create(void)
return menu; 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 static bool
handle_keyboard_input(Menu *m, Input *input) handle_keyboard_input(Menu *m, Input *input)
{ {

View File

@ -25,6 +25,11 @@
#include "texture.h" #include "texture.h"
#include "sprite.h" #include "sprite.h"
typedef struct TEXT_MENU_ITEM {
char label[20];
void (*callback)(void*);
} TEXT_MENU_ITEM;
typedef struct Menu_t { typedef struct Menu_t {
LinkedList *items; LinkedList *items;
int selected; int selected;
@ -33,6 +38,9 @@ typedef struct Menu_t {
Menu * Menu *
menu_create(void); menu_create(void);
void
menu_create_text_menu(Menu **menu, TEXT_MENU_ITEM *menu_items, unsigned int size, SDL_Renderer *);
void void
menu_update(Menu*, Input*); menu_update(Menu*, Input*);