adventskalender-2009/src/menu.c
Aaron Mueller 01147c0d35 Adding game states and menu
* Implement a simple state machine to simulate different game stats
 * Create a generic menu to display different menus
 * Update the Makefile for TTF support
 * Draw a simple diagram to show the stats
2009-12-16 15:37:24 +01:00

73 lines
1.9 KiB
C

#include "SDL.h"
#include "states.h"
#include "menu.h"
struct menuItem mainMenu[] = {
{"Neues Spiel", STATE_NUMPLAYERS},
{"Credits", STATE_CREDITS},
{"Spiel beenden", STATE_EXIT}
};
struct menuItem numplayersMenu[] = {
{"Einspieler", STATE_RUNNINGGAME},
{"Zweispieler", STATE_RUNNINGGAME},
{"Dreispieller", STATE_RUNNINGGAME},
{"Zurueck", STATE_MAINMENU}
};
struct menuItem creditsMenu[] = {
{"Zurueck", STATE_MAINMENU}
};
struct menuItem rlyquitMenu[] = {
{"Beenden", STATE_MAINMENU},
{"Weiterspielen", STATE_RUNNINGGAME}
};
void handleMenuEvent(struct menuItem items[], int numItems, enum states prevState) {
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_DOWN:
case SDLK_j:
currentMenuActionState++;
if (currentMenuActionState > numItems-1) currentMenuActionState = 0;
break;
case SDLK_UP:
case SDLK_k:
currentMenuActionState--;
if (currentMenuActionState < 0) currentMenuActionState = numItems-1;
break;
case SDLK_RETURN:
case SDLK_SPACE:
currentState = items[currentMenuActionState].targetState;
currentMenuActionState = 0;
break;
case SDLK_ESCAPE:
case SDLK_BACKSPACE:
currentMenuActionState = 0;
currentState = prevState;
break;
default:
break;
}
}
}
void drawMenu(struct menuItem items[], int numItems) {
SDL_Surface *text;
SDL_Rect targetPos;
SDL_Color colorSelected = {0, 255, 66};
SDL_Color colorNormal = {59, 71, 62};
for (int i=0; i<numItems; i++) {
targetPos.x = 200;
targetPos.y = i*60+200;
targetPos.w = SCREEN_WIDTH-200;
targetPos.h = 50;
text = TTF_RenderText_Solid(menuFont, items[i].buttonDescription,
currentMenuActionState == i ? colorSelected : colorNormal);
SDL_BlitSurface(text, NULL, screen, &targetPos);
}
}