Implemented in game menu

This commit is contained in:
Linus Probert 2018-02-13 06:44:09 +01:00
parent 9a887ab964
commit dcafbbc6ac
2 changed files with 88 additions and 26 deletions

View File

@ -25,9 +25,12 @@ x XP
x Statistics x Statistics
x More stuff to count x More stuff to count
x Particle engine for blood splatter (keep it light!!!) x Particle engine for blood splatter (keep it light!!!)
- Prevent level exit from spawning on occupied spot x Prevent level exit from spawning on occupied spot
- Menus o Menus
x Main menu
x In game menu
- Player death (graphic or text?) - Player death (graphic or text?)
- Sound?
- Make things less difficult and more interesting - Make things less difficult and more interesting
- Interesting items - Interesting items
- A different license perhaps? - A different license perhaps?

View File

@ -20,6 +20,7 @@
#include "gui_button.h" #include "gui_button.h"
#include "particle_engine.h" #include "particle_engine.h"
#include "menu.h" #include "menu.h"
#include "keyboard.h"
static SDL_Window *gWindow = NULL; static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL; static SDL_Renderer *gRenderer = NULL;
@ -32,6 +33,7 @@ static unsigned int cLevel = 1;
static float deltaTime = 1.0; static float deltaTime = 1.0;
static double renderScale = 1.0; static double renderScale = 1.0;
static Menu *mainMenu = NULL; static Menu *mainMenu = NULL;
static Menu *inGameMenu = NULL;
static Timer *menuTimer = NULL; static Timer *menuTimer = NULL;
static GameState gGameState; static GameState gGameState;
static Camera gCamera; static Camera gCamera;
@ -40,7 +42,16 @@ static SDL_Rect bottomGuiViewport;
static SDL_Rect rightGuiViewport; static SDL_Rect rightGuiViewport;
static SDL_Rect menuViewport; static SDL_Rect menuViewport;
static SDL_Color C_MENU_DEFAULT = { 255, 255, 0, 0 };
static SDL_Color C_MENU_HOVER = { 255, 0, 0, 0 };
struct MENU_ITEM {
char label[20];
void (*callback)(void*);
};
static void resetGame(void); static void resetGame(void);
static void initMainMenu(void);
static static
bool initSDL(void) bool initSDL(void)
@ -159,34 +170,38 @@ exitGame(void *unused)
} }
static void static void
initMainMenu(void) toggleInGameMenu(void *unused)
{ {
static SDL_Color C_DEFAULT = { 255, 255, 0, 0 }; UNUSED(unused);
static SDL_Color C_HOVER = { 255, 0, 0, 0 }; if (gGameState == PLAYING)
gGameState = IN_GAME_MENU;
else
gGameState = PLAYING;
}
struct MENU_ITEM { static void
char label[20]; goToMainMenu(void *unused)
void (*callback)(void*); {
}; UNUSED(unused);
struct MENU_ITEM menu_items[] = { gGameState = MENU;
{ "PLAY", startGame }, menu_destroy(inGameMenu);
{ "QUIT", exitGame }, inGameMenu = NULL;
}; initMainMenu();
}
mainMenu = menu_create(); static void
createMenu(Menu **menu, struct MENU_ITEM menu_items[], unsigned int size)
{
if (*menu == NULL)
*menu = menu_create();
if (gMap) for (unsigned int i = 0; i < size; ++i) {
map_destroy(gMap);
gMap = map_lua_generator_single_room__run(cLevel, gRenderer);
for (unsigned int i = 0; i < 2; ++i) {
int hcenter; int hcenter;
Sprite *s1 = sprite_create(); Sprite *s1 = sprite_create();
sprite_load_text_texture(s1, "assets/GUI/SDS_8x8.ttf", 0, 25); sprite_load_text_texture(s1, "assets/GUI/SDS_8x8.ttf", 0, 25);
texture_load_from_text(s1->textures[0], menu_items[i].label, texture_load_from_text(s1->textures[0], menu_items[i].label,
C_DEFAULT, gRenderer); C_MENU_DEFAULT, gRenderer);
hcenter = (SCREEN_WIDTH/2) - (s1->textures[0]->dim.width/2); hcenter = (SCREEN_WIDTH/2) - (s1->textures[0]->dim.width/2);
s1->pos = (Position) { hcenter, 200 + (i*50) }; s1->pos = (Position) { hcenter, 200 + (i*50) };
@ -195,15 +210,46 @@ initMainMenu(void)
Sprite *s2 = sprite_create(); Sprite *s2 = sprite_create();
sprite_load_text_texture(s2, "assets/GUI/SDS_8x8.ttf", 0, 25); sprite_load_text_texture(s2, "assets/GUI/SDS_8x8.ttf", 0, 25);
texture_load_from_text(s2->textures[0], menu_items[i].label, texture_load_from_text(s2->textures[0], menu_items[i].label,
C_HOVER, gRenderer); C_MENU_HOVER, gRenderer);
s2->pos = (Position) { hcenter, 200 + (i*50) }; s2->pos = (Position) { hcenter, 200 + (i*50) };
s2->fixed = true; s2->fixed = true;
menu_item_add(mainMenu, s1, s2, menu_items[i].callback); menu_item_add(*menu, s1, s2, menu_items[i].callback);
} }
} }
static void
initInGameMenu(void)
{
struct MENU_ITEM menu_items[] = {
{ "RESUME", toggleInGameMenu },
{ "MAIN MENU", goToMainMenu },
{ "QUIT", exitGame },
};
if (inGameMenu)
menu_destroy(inGameMenu);
createMenu(&inGameMenu, menu_items, 3);
}
static void
initMainMenu(void)
{
struct MENU_ITEM menu_items[] = {
{ "PLAY", startGame },
{ "QUIT", exitGame },
};
if (gMap)
map_destroy(gMap);
gMap = map_lua_generator_single_room__run(cLevel, gRenderer);
createMenu(&mainMenu, menu_items, 2);
}
static void static void
resetGame(void) resetGame(void)
{ {
@ -214,9 +260,12 @@ resetGame(void)
mainMenu = NULL; mainMenu = NULL;
} }
initInGameMenu();
if (gMap) if (gMap)
map_destroy(gMap); map_destroy(gMap);
cLevel = 1;
info("Building new map"); info("Building new map");
gMap = map_lua_generator_run(cLevel, gRenderer); gMap = map_lua_generator_run(cLevel, gRenderer);
gPlayer->sprite->pos = (Position) { gPlayer->sprite->pos = (Position) {
@ -256,6 +305,8 @@ bool handle_events(void)
while (SDL_PollEvent(&event) != 0) { while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) { if (event.type == SDL_QUIT) {
quit = true; quit = true;
} else if (keyboard_press(SDLK_ESCAPE, &event)) {
toggleInGameMenu(NULL);
} else if (gGameState == PLAYING) { } else if (gGameState == PLAYING) {
gPlayer->handle_event(gPlayer, gPlayer->handle_event(gPlayer,
gRoomMatrix, gRoomMatrix,
@ -265,6 +316,8 @@ bool handle_events(void)
roommatrix_handle_event(gRoomMatrix, &event); roommatrix_handle_event(gRoomMatrix, &event);
} else if (gGameState == MENU) { } else if (gGameState == MENU) {
menu_handle_event(mainMenu, &event); menu_handle_event(mainMenu, &event);
} else if (gGameState == IN_GAME_MENU) {
menu_handle_event(inGameMenu, &event);
} }
pointer_handle_event(gPointer, &event); pointer_handle_event(gPointer, &event);
} }
@ -340,6 +393,12 @@ run_game(void)
BOTTOM_GUI_HEIGHT, &gCamera); BOTTOM_GUI_HEIGHT, &gCamera);
SDL_RenderSetViewport(gRenderer, NULL); SDL_RenderSetViewport(gRenderer, NULL);
if (gGameState == IN_GAME_MENU) {
SDL_Rect dimmer = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 150);
SDL_RenderFillRect(gRenderer, &dimmer);
menu_render(inGameMenu, &gCamera);
}
pointer_render(gPointer, &gCamera); pointer_render(gPointer, &gCamera);
SDL_RenderPresent(gRenderer); SDL_RenderPresent(gRenderer);
@ -396,14 +455,12 @@ void run(void)
switch (gGameState) { switch (gGameState) {
case PLAYING: case PLAYING:
case IN_GAME_MENU:
run_game(); run_game();
break; break;
case MENU: case MENU:
run_menu(); run_menu();
break; break;
case IN_GAME_MENU:
fatal("IN_GAME_MENU not implemented");
break;
case GAME_OVER: case GAME_OVER:
fatal("GAME_OVER not implemented"); fatal("GAME_OVER not implemented");
break; break;
@ -440,6 +497,8 @@ void close(void)
map_destroy(gMap); map_destroy(gMap);
if (mainMenu) if (mainMenu)
menu_destroy(mainMenu); menu_destroy(mainMenu);
if (inGameMenu)
menu_destroy(inGameMenu);
roommatrix_destroy(gRoomMatrix); roommatrix_destroy(gRoomMatrix);
gui_destroy(gGui); gui_destroy(gGui);