The menu is working but it's a bit wonky still.

Starting the game seems to cause some missbehaviour.
This commit is contained in:
Linus Probert 2018-02-09 07:26:44 +01:00
parent 31e006b69f
commit fdfc6fcf77
5 changed files with 43 additions and 21 deletions

View File

@ -5,7 +5,8 @@ typedef enum GameState_t {
MENU,
PLAYING,
IN_GAME_MENU,
GAME_OVER
GAME_OVER,
QUIT
} GameState;
#endif // GAMESTATE_H_

View File

@ -29,8 +29,12 @@ gui_button_handle_event(GuiButton *button, SDL_Event *event)
if (event->button.button != SDL_BUTTON_LEFT)
return;
if (button->hover)
if (button->hover && button->event)
button->event(button->usrdata);
} else if (event->type == SDL_MOUSEMOTION) {
Position p = { event->motion.x, event->motion.y };
button->hover = position_in_rect(&p, &button->area);
}
}

View File

@ -38,6 +38,8 @@ static SDL_Rect gameViewport;
static SDL_Rect bottomGuiViewport;
static SDL_Rect rightGuiViewport;
static void resetGame(void);
static
bool initSDL(void)
{
@ -131,6 +133,21 @@ initGame(void)
return true;
}
static void
startGame(void *unused)
{
UNUSED(unused);
gGameState = PLAYING;
resetGame();
}
static void
exitGame(void *unused)
{
UNUSED(unused);
gGameState = QUIT;
}
static void
initMainMenu(void)
{
@ -139,11 +156,11 @@ initMainMenu(void)
struct MENU_ITEM {
char label[20];
void (*callback)(void);
void (*callback)(void*);
};
struct MENU_ITEM menu_items[] = {
{ "PLAY", NULL },
{ "QUIT", NULL },
{ "PLAY", startGame },
{ "QUIT", exitGame },
};
mainMenu = menu_create();
@ -163,7 +180,7 @@ initMainMenu(void)
s2->pos = (Position) { 200, 100 + (i*50) };
s2->fixed = true;
menu_item_add(mainMenu, s1, s2);
menu_item_add(mainMenu, s1, s2, menu_items[i].callback);
}
}
@ -343,6 +360,9 @@ void run(void)
case GAME_OVER:
fatal("GAME_OVER not implemented");
break;
case QUIT:
quit = true;
break;
default:
break;
}
@ -374,6 +394,10 @@ void close(void)
pointer_destroy(gPointer);
item_builder_close();
particle_engine_close();
if (mainMenu)
menu_destroy(mainMenu);
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;

View File

@ -24,15 +24,9 @@ menu_create(void)
void
menu_handle_event(Menu *m, SDL_Event *event)
{
UNUSED(m);
LinkedList *items;
if (event->type != SDL_KEYDOWN && event->type != SDL_MOUSEMOTION) {
return;
}
items = m->items;
if (event->type == SDL_MOUSEMOTION) {
while (items) {
MenuItem *item = items->data;
items = items->next;
@ -40,10 +34,9 @@ menu_handle_event(Menu *m, SDL_Event *event)
item->selected = item->button->hover;
}
}
}
void
menu_item_add(Menu *m, Sprite *s1, Sprite *s2)
menu_item_add(Menu *m, Sprite *s1, Sprite *s2, void (*event)(void*))
{
MenuItem *item = ec_malloc(sizeof(MenuItem));
item->sprite = s1;
@ -56,7 +49,7 @@ menu_item_add(Menu *m, Sprite *s1, Sprite *s2)
item->sprite->textures[0]->dim.width,
item->sprite->textures[0]->dim.height
};
item->button = gui_button_create(area, NULL, NULL);
item->button = gui_button_create(area, event, NULL);
linkedlist_append(&m->items, item);
}

View File

@ -19,7 +19,7 @@ void
menu_handle_event(Menu*, SDL_Event*);
void
menu_item_add(Menu*, Sprite*, Sprite*);
menu_item_add(Menu*, Sprite*, Sprite*, void (*)(void*));
void
menu_render(Menu*, Camera*);