Main menu functional.
This commit is contained in:
6
Makefile
6
Makefile
@@ -1,6 +1,5 @@
|
|||||||
CC = gcc
|
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/main_menu.o obj/map.o
|
||||||
OBJECTS = obj/main.o obj/game_state.o obj/in_game.o obj/map.o
|
|
||||||
TARGET = bin/cyjam
|
TARGET = bin/cyjam
|
||||||
CFLAGS = -Wall -I./include -std=c99
|
CFLAGS = -Wall -I./include -std=c99
|
||||||
LDFLAGS = -L./lib
|
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
|
obj/in_game.o: src/in_game.c include/in_game.h include/game_state.h include/map.h
|
||||||
$(CC) -c -o $@ $< $(CFLAGS)
|
$(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
|
obj/map.o: src/map.c include/map.h
|
||||||
$(CC) -c -o $@ $< $(CFLAGS)
|
$(CC) -c -o $@ $< $(CFLAGS)
|
||||||
|
|
||||||
|
13
include/main_menu.h
Normal file
13
include/main_menu.h
Normal file
@@ -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
|
@@ -4,12 +4,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "game_state.h"
|
#include "game_state.h"
|
||||||
|
#include "main_menu.h"
|
||||||
#include "in_game.h"
|
#include "in_game.h"
|
||||||
|
|
||||||
void initStateArray(gs_t ** s){
|
void initStateArray(gs_t ** s){
|
||||||
int i;
|
initMMState(&((*s)[MENU]));
|
||||||
|
initInGameState(&((*s)[IN_GAME]));
|
||||||
/*for(i = 0; i < NUM_STATES; i++){*/
|
|
||||||
initInGameState(&((*s)[2]));
|
|
||||||
/*}*/
|
|
||||||
}
|
}
|
||||||
|
@@ -126,7 +126,13 @@ gsname_t update(){
|
|||||||
|
|
||||||
if(rK) nX = (iX + 1) % mW;
|
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. */
|
/* Find if the player is standing on an exit, then load the next map. */
|
||||||
for(i = 0; i < nO; i++){
|
for(i = 0; i < nO; i++){
|
||||||
|
17
src/main.c
17
src/main.c
@@ -20,7 +20,6 @@ void leave(void);
|
|||||||
void manage_signal(int signal);
|
void manage_signal(int signal);
|
||||||
int start_ncurses(void);
|
int start_ncurses(void);
|
||||||
void set_colors(void);
|
void set_colors(void);
|
||||||
void clear_screen(void);
|
|
||||||
void on_resize(int);
|
void on_resize(int);
|
||||||
|
|
||||||
static int w = 0, h = 0;
|
static int w = 0, h = 0;
|
||||||
@@ -85,7 +84,7 @@ int main() {
|
|||||||
set_colors();
|
set_colors();
|
||||||
|
|
||||||
/* Create the state data structures. */
|
/* Create the state data structures. */
|
||||||
c_state = 2;
|
c_state = MENU;
|
||||||
states = (gs_t *)malloc(sizeof(gs_t) * NUM_STATES);
|
states = (gs_t *)malloc(sizeof(gs_t) * NUM_STATES);
|
||||||
initStateArray(&states);
|
initStateArray(&states);
|
||||||
|
|
||||||
@@ -110,7 +109,9 @@ int main() {
|
|||||||
|
|
||||||
if(c_state == -1) finished = TRUE;
|
if(c_state == -1) finished = TRUE;
|
||||||
|
|
||||||
|
if(c_state >= INTRO && c_state <= GAME_OVER){
|
||||||
states[c_state].render(w, h);
|
states[c_state].render(w, h);
|
||||||
|
}
|
||||||
|
|
||||||
fps++;
|
fps++;
|
||||||
|
|
||||||
@@ -242,15 +243,3 @@ void set_colors(void){
|
|||||||
exit(EXIT_FAILURE);
|
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(" ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
159
src/main_menu.c
Normal file
159
src/main_menu.c
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2014, Miguel Angel Astor Romero. All rights reserved.
|
||||||
|
* See the file LICENSE for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ncursesw/ncurses.h>
|
||||||
|
|
||||||
|
#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(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user