From 1f2560e7e31d2d506ecbe0e00d1096563fddb4ee Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Sat, 8 Mar 2014 19:15:31 -0430 Subject: [PATCH] Main menu functional. --- Makefile | 6 +- include/main_menu.h | 13 ++++ src/game_state.c | 8 +-- src/in_game.c | 8 ++- src/main.c | 19 ++---- src/main_menu.c | 159 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 190 insertions(+), 23 deletions(-) create mode 100644 include/main_menu.h create mode 100644 src/main_menu.c diff --git a/Makefile b/Makefile index 91ee777..236a63e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ CC = gcc -SOURCES = src/main.c src/game_state.c src/in_game.c src/map.c -OBJECTS = obj/main.o obj/game_state.o obj/in_game.o obj/map.o +OBJECTS = obj/main.o obj/game_state.o obj/in_game.o obj/main_menu.o obj/map.o TARGET = bin/cyjam CFLAGS = -Wall -I./include -std=c99 LDFLAGS = -L./lib @@ -24,6 +23,9 @@ obj/game_state.o: src/game_state.c include/game_state.h include/in_game.h obj/in_game.o: src/in_game.c include/in_game.h include/game_state.h include/map.h $(CC) -c -o $@ $< $(CFLAGS) +obj/main_menu.o: src/main_menu.c include/main_menu.h include/game_state.h + $(CC) -c -o $@ $< $(CFLAGS) + obj/map.o: src/map.c include/map.h $(CC) -c -o $@ $< $(CFLAGS) diff --git a/include/main_menu.h b/include/main_menu.h new file mode 100644 index 0000000..9185a05 --- /dev/null +++ b/include/main_menu.h @@ -0,0 +1,13 @@ +/** + * Copyright (c) 2014, Miguel Angel Astor Romero. All rights reserved. + * See the file LICENSE for more details. + */ + +#ifndef MAIN_MENU_H +#define MAIN_MENU_H + +#include "game_state.h" + +extern void initMMState(gs_t *); + +#endif diff --git a/src/game_state.c b/src/game_state.c index 8bc192b..b35b26b 100644 --- a/src/game_state.c +++ b/src/game_state.c @@ -4,12 +4,10 @@ */ #include "game_state.h" +#include "main_menu.h" #include "in_game.h" void initStateArray(gs_t ** s){ - int i; - - /*for(i = 0; i < NUM_STATES; i++){*/ - initInGameState(&((*s)[2])); - /*}*/ + initMMState(&((*s)[MENU])); + initInGameState(&((*s)[IN_GAME])); } diff --git a/src/in_game.c b/src/in_game.c index 00ed4cd..ca27ea6 100644 --- a/src/in_game.c +++ b/src/in_game.c @@ -126,7 +126,13 @@ gsname_t update(){ if(rK) nX = (iX + 1) % mW; - if(esc) return IN_GAME; + if(esc){ + /* Reset the game and go to the main menu. */ + esc = FALSE; + initObjects(); + loadMap("maps/start.map"); + return MENU; + } /* Find if the player is standing on an exit, then load the next map. */ for(i = 0; i < nO; i++){ diff --git a/src/main.c b/src/main.c index 5578155..46f1911 100644 --- a/src/main.c +++ b/src/main.c @@ -20,7 +20,6 @@ void leave(void); void manage_signal(int signal); int start_ncurses(void); void set_colors(void); -void clear_screen(void); void on_resize(int); static int w = 0, h = 0; @@ -85,7 +84,7 @@ int main() { set_colors(); /* Create the state data structures. */ - c_state = 2; + c_state = MENU; states = (gs_t *)malloc(sizeof(gs_t) * NUM_STATES); initStateArray(&states); @@ -110,7 +109,9 @@ int main() { if(c_state == -1) finished = TRUE; - states[c_state].render(w, h); + if(c_state >= INTRO && c_state <= GAME_OVER){ + states[c_state].render(w, h); + } fps++; @@ -242,15 +243,3 @@ void set_colors(void){ exit(EXIT_FAILURE); } } - -void clear_screen(void){ - int i, j; - move(0,0); - attron(COLOR_PAIR(BSC_COLOR)); - for(i = 0; i < w; i++){ - for(j = 0; j < h; j++){ - move(j, i); - printw(" "); - } - } -} diff --git a/src/main_menu.c b/src/main_menu.c new file mode 100644 index 0000000..ed6be07 --- /dev/null +++ b/src/main_menu.c @@ -0,0 +1,159 @@ +/** + * Copyright (c) 2014, Miguel Angel Astor Romero. All rights reserved. + * See the file LICENSE for more details. + */ + +#include +#include +#include + +#include "constants.h" +#include "main_menu.h" + +static char * title = "TITLE PENDING"; +static char * subtitle = "A game for the Cyberpunk Jam 2014"; +static char * opt1 = "New game"; +static char * opt2 = "Replay intro"; +static char * opt3 = "Quit"; +static char * creds = "Designed and programmed by Miky"; +static char * info = "Press enter to select an option. Scroll with the arrow keys."; +static char * info2 = "Press escape while in game to return to the main menu."; + +static int selOpt = 0; +static bool uK, dK, esc, enter; + +void mmInput(); +gsname_t mmUpdate(); +void mmRender(int, int); +void clear_screen(int, int); + +void initMMState(gs_t * gs){ + gs->name = MENU; + gs->input = &mmInput; + gs->update = &mmUpdate; + gs->render = &mmRender; +} + +void mmInput(){ + int key = 0; + + key = getch(); + + if(key != ERR){ + if(key == KEY_UP) uK = TRUE; + if(key == KEY_DOWN) dK = TRUE; + if(key == 27) esc = TRUE; + if(key == KEY_ENTER || key == '\n') enter = TRUE; + } +} + +gsname_t mmUpdate(){ + if(uK){ + selOpt = selOpt - 1 < 0 ? 2 : selOpt - 1; + uK = FALSE; + } + + if(dK){ + selOpt = (selOpt + 1) % 3; + dK = FALSE; + } + + if(esc) return -1; + + if(enter){ + enter = FALSE; + if(selOpt == 0) return IN_GAME; + else if(selOpt == 1) return MENU; + else return -1; + } + + return MENU; +} + +void mmRender(int w, int h){ + int sW; + + clear_screen(w, h); + + /* Print the title. */ + sW = strlen(title); + sW /= 2; + + attron(A_BOLD); + + move(1, (w / 2) - sW); + attron(COLOR_PAIR(SN_COLOR)); + printw(title); + + /* Print the subtitle. */ + sW = strlen(subtitle); + sW /= 2; + + move(2, (w / 2) - sW); + attron(COLOR_PAIR(SW_COLOR)); + printw(subtitle); + + attroff(A_BOLD); + + /* Print the menu options. */ + sW = strlen(opt1); + sW /= 2; + + move((h / 2) - 2, (w / 2) - sW); + if(selOpt == 0) attron(COLOR_PAIR(GR_COLOR)); + else attron(COLOR_PAIR(DW_COLOR)); + printw(opt1); + + sW = strlen(opt2); + sW /= 2; + + move((h / 2) - 1, (w / 2) - sW); + if(selOpt == 1) attron(COLOR_PAIR(GR_COLOR)); + else attron(COLOR_PAIR(DW_COLOR)); + printw(opt2); + + sW = strlen(opt3); + sW /= 2; + + move((h / 2), (w / 2) - sW); + if(selOpt == 2) attron(COLOR_PAIR(GR_COLOR)); + else attron(COLOR_PAIR(DW_COLOR)); + printw(opt3); + + /* Print help. */ + sW = strlen(info); + sW /= 2; + + move(h - 5, (w / 2) - sW); + attron(COLOR_PAIR(MN_COLOR)); + printw(info); + + sW = strlen(info2); + sW /= 2; + + move(h - 4, (w / 2) - sW); + attron(COLOR_PAIR(MN_COLOR)); + printw(info2); + + /* Print credits. */ + sW = strlen(creds); + sW /= 2; + + attron(A_BOLD); + move(h - 2, (w / 2) - sW); + attron(COLOR_PAIR(SW_COLOR)); + printw(creds); + attroff(A_BOLD); +} + +void clear_screen(int w, int h){ + int i, j; + move(0,0); + attron(COLOR_PAIR(BSC_COLOR)); + for(i = 0; i < w; i++){ + for(j = 0; j < h; j++){ + move(j, i); + printw(" "); + } + } +}