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, MENU,
PLAYING, PLAYING,
IN_GAME_MENU, IN_GAME_MENU,
GAME_OVER GAME_OVER,
QUIT
} GameState; } GameState;
#endif // GAMESTATE_H_ #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) if (event->button.button != SDL_BUTTON_LEFT)
return; return;
if (button->hover) if (button->hover && button->event)
button->event(button->usrdata); 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 bottomGuiViewport;
static SDL_Rect rightGuiViewport; static SDL_Rect rightGuiViewport;
static void resetGame(void);
static static
bool initSDL(void) bool initSDL(void)
{ {
@ -131,6 +133,21 @@ initGame(void)
return true; return true;
} }
static void
startGame(void *unused)
{
UNUSED(unused);
gGameState = PLAYING;
resetGame();
}
static void
exitGame(void *unused)
{
UNUSED(unused);
gGameState = QUIT;
}
static void static void
initMainMenu(void) initMainMenu(void)
{ {
@ -139,11 +156,11 @@ initMainMenu(void)
struct MENU_ITEM { struct MENU_ITEM {
char label[20]; char label[20];
void (*callback)(void); void (*callback)(void*);
}; };
struct MENU_ITEM menu_items[] = { struct MENU_ITEM menu_items[] = {
{ "PLAY", NULL }, { "PLAY", startGame },
{ "QUIT", NULL }, { "QUIT", exitGame },
}; };
mainMenu = menu_create(); mainMenu = menu_create();
@ -163,7 +180,7 @@ initMainMenu(void)
s2->pos = (Position) { 200, 100 + (i*50) }; s2->pos = (Position) { 200, 100 + (i*50) };
s2->fixed = true; 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: case GAME_OVER:
fatal("GAME_OVER not implemented"); fatal("GAME_OVER not implemented");
break; break;
case QUIT:
quit = true;
break;
default: default:
break; break;
} }
@ -374,6 +394,10 @@ void close(void)
pointer_destroy(gPointer); pointer_destroy(gPointer);
item_builder_close(); item_builder_close();
particle_engine_close(); particle_engine_close();
if (mainMenu)
menu_destroy(mainMenu);
SDL_DestroyRenderer(gRenderer); SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow); SDL_DestroyWindow(gWindow);
gWindow = NULL; gWindow = NULL;

View File

@ -24,26 +24,19 @@ menu_create(void)
void void
menu_handle_event(Menu *m, SDL_Event *event) menu_handle_event(Menu *m, SDL_Event *event)
{ {
UNUSED(m);
LinkedList *items; LinkedList *items;
if (event->type != SDL_KEYDOWN && event->type != SDL_MOUSEMOTION) {
return;
}
items = m->items; items = m->items;
if (event->type == SDL_MOUSEMOTION) {
while (items) { while (items) {
MenuItem *item = items->data; MenuItem *item = items->data;
items = items->next; items = items->next;
gui_button_handle_event(item->button, event); gui_button_handle_event(item->button, event);
item->selected = item->button->hover; item->selected = item->button->hover;
} }
}
} }
void 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)); MenuItem *item = ec_malloc(sizeof(MenuItem));
item->sprite = s1; 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.width,
item->sprite->textures[0]->dim.height 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); linkedlist_append(&m->items, item);
} }

View File

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