Player character now moves.

This commit is contained in:
2014-03-03 21:54:15 -04:30
parent 073407c752
commit fe08d66af4
5 changed files with 303 additions and 185 deletions

View File

@@ -15,21 +15,21 @@
#endif #endif
enum COLORS { enum COLORS {
BAR_COLOR = 1, BAR_COLOR = 1,
BSC_COLOR, BSC_COLOR,
HLT_COLOR, HLT_COLOR,
OFF_COLOR, OFF_COLOR,
DIM_COLOR, DIM_COLOR,
LIT_COLOR, LIT_COLOR,
GUI_COLOR, GUI_COLOR,
EMP_COLOR, EMP_COLOR,
DW_COLOR, DW_COLOR,
SW_COLOR, SW_COLOR,
SN_COLOR, SN_COLOR,
GR_COLOR, GR_COLOR,
FR_COLOR, FR_COLOR,
HL_COLOR, HL_COLOR,
MN_COLOR MN_COLOR
}; };
#endif #endif

View File

@@ -11,10 +11,10 @@ static const int NUM_STATES = 4;
typedef enum GAME_STATE_NAMES { INTRO = 0, MENU = 1, IN_GAME = 2, GAME_OVER = 3 } gsname_t; typedef enum GAME_STATE_NAMES { INTRO = 0, MENU = 1, IN_GAME = 2, GAME_OVER = 3 } gsname_t;
typedef struct GAME_STATE { typedef struct GAME_STATE {
gsname_t name; gsname_t name;
void (*input)(); void (*input)();
gsname_t (*update)(); gsname_t (*update)();
void (*render)(int, int); void (*render)(int, int);
} gs_t; } gs_t;
extern void initStateArray(gs_t **); extern void initStateArray(gs_t **);

View File

@@ -7,9 +7,9 @@
#include "in_game.h" #include "in_game.h"
void initStateArray(gs_t ** s){ void initStateArray(gs_t ** s){
int i; int i;
for(i = 0; i < NUM_STATES; i++){ for(i = 0; i < NUM_STATES; i++){
initInGameState(&((*s)[i])); initInGameState(&((*s)[i]));
} }
} }

View File

@@ -11,132 +11,250 @@
#include "constants.h" #include "constants.h"
#include "in_game.h" #include "in_game.h"
static const int I_SIZE = 257; typedef struct PLAYER {
unsigned short x;
unsigned short y;
} player_t;
static const int I_SIZE = 513;
static int ** imap; static int ** imap;
static bool ** wmap; static bool ** wmap;
static bool w_mov = FALSE; static bool w_mov = FALSE;
static bool uK, dK, lK, rK;
static clock_t then; static clock_t then;
static player_t player;
void input(); void input();
gsname_t update(); gsname_t update();
void render(int, int); void render(int, int);
void drawGui(int, int);
void setPlayerStart();
void initInGameState( gs_t * gs) { void initInGameState( gs_t * gs) {
int n, i, j; int n, i, j;
float ** map; float ** map;
gs->name = IN_GAME; gs->name = IN_GAME;
gs->input = &input; gs->input = &input;
gs->update = &update; gs->update = &update;
gs->render = &render; gs->render = &render;
n = I_SIZE; n = I_SIZE;
srand(time(NULL)); srand(time(NULL));
map = ( float ** ) malloc ( sizeof ( float * ) * n); map = ( float ** ) malloc ( sizeof ( float * ) * n);
for ( i = 0; i < n; ++i ) { for ( i = 0; i < n; ++i ) {
map[ i ] = ( float * ) calloc ( n, sizeof ( float ) ); map[ i ] = ( float * ) calloc ( n, sizeof ( float ) );
} }
imap = ( int ** ) malloc ( sizeof ( int * ) * n); imap = ( int ** ) malloc ( sizeof ( int * ) * n);
for ( i = 0; i < n; ++i ) { for ( i = 0; i < n; ++i ) {
imap[ i ] = ( int * ) calloc ( n, sizeof ( int ) ); imap[ i ] = ( int * ) calloc ( n, sizeof ( int ) );
} }
wmap = ( bool ** ) malloc ( sizeof ( bool * ) * n); wmap = ( bool ** ) malloc ( sizeof ( bool * ) * n);
for ( i = 0; i < n; ++i ) { for ( i = 0; i < n; ++i ) {
wmap[ i ] = ( bool * ) calloc ( n, sizeof ( bool ) ); wmap[ i ] = ( bool * ) calloc ( n, sizeof ( bool ) );
for(j = 0; j < n; ++j){ for(j = 0; j < n; ++j){
wmap[i][j] = rand() % 2; wmap[i][j] = rand() % 2;
} }
} }
ds ( &map, n ); ds ( &map, n );
island ( &imap, n ); island ( &imap, n );
normInt ( &imap, n ); normInt ( &imap, n );
norm ( &map, n ); norm ( &map, n );
mult ( &map, &imap, n ); mult ( &map, &imap, n );
smooth( &imap, n ); smooth( &imap, n );
normInt ( &imap, n ); normInt ( &imap, n );
for ( i = 0; i < n; ++i ) { for ( i = 0; i < n; ++i ) {
free(map[ i ]); free(map[ i ]);
} }
free(map); free(map);
setPlayerStart();
uK = dK = lK = rK = FALSE;
} }
void input(){ void input(){
int key = 0; int key = 0;
key = getch(); key = getch();
if(key != ERR){ if(key != ERR){
fprintf(stderr, "\t%s: Caught keycode %d\n", __FILE__, key); fprintf(stderr, "\t%s: Caught keycode %d\n", __FILE__, key);
} if(key == KEY_UP) uK = TRUE;
if(key == KEY_DOWN) dK = TRUE;
if(key == KEY_LEFT) lK = TRUE;
if(key == KEY_RIGHT) rK = TRUE;
}
} }
gsname_t update(){ gsname_t update(){
return IN_GAME; if(uK){
if(terrainType( imap[player.x][player.y - 1] ) != DEEP_WATER && terrainType( imap[player.x][player.y - 1] ) != MOUNTAIN) player.y -= 1;
uK = FALSE;
}
if(dK){
if(terrainType( imap[player.x][player.y + 1]) != DEEP_WATER && terrainType( imap[player.x][player.y + 1]) != MOUNTAIN ) player.y += 1;
dK = FALSE;
}
if(lK){
if(terrainType( imap[player.x - 1][player.y]) != DEEP_WATER && terrainType( imap[player.x - 1][player.y]) != MOUNTAIN) player.x -= 1;
lK = FALSE;
}
if(rK){
if(terrainType( imap[player.x + 1][player.y]) != DEEP_WATER && terrainType( imap[player.x + 1][player.y]) != MOUNTAIN) player.x += 1;
rK = FALSE;
}
return IN_GAME;
} }
void render(int w, int h){ void render(int w, int h){
clock_t now, delta; clock_t now, delta;
int i, j; int i, j, pi, pj, ioff, joff, di, dj;
now = clock(); now = clock();
delta = now - then; delta = now - then;
if((float)delta / (float)CLOCKS_PER_SEC >= 0.25f){ if((float)delta / (float)CLOCKS_PER_SEC >= 0.25f){
then = now; then = now;
w_mov = TRUE; w_mov = TRUE;
} }
for(i = 0; i < w; i++){ pi = (((w - 1) - 27) / 2) + 27;
for(j = 0; j < h; j++){ pj = (h - 2) / 2 + 1;
move(j, i);
switch(terrainType( imap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE] )){ ioff = (w - 28) / 2;
case DEEP_WATER: joff = (h - 2) / 2;
attron(COLOR_PAIR(DW_COLOR));
if(w_mov) for(i = 27; i < w - 1; i++){
wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE] = !wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE]; for(j = 1; j < h - 1; j++){
if(wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE]) move(j, i);
printw("\u2248");
else di = i - 27 + player.x - ioff;
printw("~"); dj = j - 1 + player.y - joff;
break;
case SHALLOW_WATER: if( di < 0 || di >= I_SIZE){
attron(COLOR_PAIR(SW_COLOR)); printw(" ");
if(w_mov) }else{
wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE] = !wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE]; switch(terrainType( imap[di][dj] )){
if(wmap[(i + (I_SIZE/4)) % I_SIZE][(j + (I_SIZE/4)) % I_SIZE]) case DEEP_WATER:
printw("\u2248"); attron(COLOR_PAIR(DW_COLOR));
else if(w_mov)
printw("~"); wmap[di][dj] = !wmap[di][dj];
break; if(wmap[di][dj])
case SAND: printw("\u2248");
attron(COLOR_PAIR(SN_COLOR)); else
printw("."); printw("~");
break; break;
case GRASS: case SHALLOW_WATER:
attron(COLOR_PAIR(GR_COLOR)); attron(COLOR_PAIR(SW_COLOR));
printw("n"); if(w_mov)
break; wmap[di][dj] = !wmap[di][dj];
case FOREST: if(wmap[di][dj])
attron(COLOR_PAIR(FR_COLOR)); printw("\u2248");
printw("\u2660"); else
break; printw("~");
case HILL: break;
attron(COLOR_PAIR(HL_COLOR)); case SAND:
printw("\u2302"); attron(COLOR_PAIR(SN_COLOR));
break; printw(".");
case MOUNTAIN: break;
attron(COLOR_PAIR(MN_COLOR)); case GRASS:
printw("\u25B2"); attron(COLOR_PAIR(GR_COLOR));
break; printw("n");
} break;
//printw("\u2588"); case FOREST:
attron(COLOR_PAIR(FR_COLOR));
printw("\u2660");
break;
case HILL:
attron(COLOR_PAIR(HL_COLOR));
printw("\u2302");
break;
case MOUNTAIN:
attron(COLOR_PAIR(MN_COLOR));
printw("\u25B2");
break;
}
}
}
}
w_mov = FALSE;
move(pj, pi);
attron(COLOR_PAIR(BSC_COLOR));
printw(/*"\u263A"*/ "@");
drawGui(w, h);
}
void drawGui(int w, int h){
int i, j;
attron(COLOR_PAIR(BSC_COLOR));
/* Clear the gui space. */
for(i = 1; i < 26; i++){
for(j = 1; j < h - 1; j++){
move(j, i);
printw(" ");
}
}
/* Upper horizontal bar. */
move(0, 0);
printw("\u2554");
for(i = 0; i < w - 2; i++){
if(i != 25){
printw("\u2550");
}else{
printw("\u2566");
}
}
printw("\u2557");
/* Lower horizontal bar. */
move(h - 1, 0);
printw("\u255A");
for(i = 0; i < w - 2; i++){
if(i != 25){
printw("\u2550");
}else{
printw("\u2569");
}
}
printw("\u255D");
/* Vertical bars. */
for(i = 1; i < h - 1; i++){
move(i, 0);
printw("\u2551");
move(i, 26);
printw("\u2551");
move(i, w-1);
printw("\u2551");
}
}
void setPlayerStart(){
int x, y;
bool posFound = false;
while(!posFound){
x = (I_SIZE / 4) + (rand() % (I_SIZE / 2));
y = (I_SIZE / 4) + (rand() % (I_SIZE / 2));
if(terrainType(imap[x][y]) == GRASS){
player.x = x;
player.y = y;
posFound = true;
} }
} }
w_mov = FALSE;
} }

View File

@@ -29,21 +29,21 @@ static bool resize = FALSE;
int main() { int main() {
bool finished = FALSE; bool finished = FALSE;
#if defined(_WIN32) || defined(_WIN64) || defined(__MINGW32__) #if defined(_WIN32) || defined(_WIN64) || defined(__MINGW32__)
char * home_dir = getenv("APPDATA"); char * home_dir = getenv("APPDATA");
#elif defined(__linux__) || defined(__GNUC__) #elif defined(__linux__) || defined(__GNUC__)
char * home_dir = getenv("HOME"); char * home_dir = getenv("HOME");
#else #else
#error "Unrecognized system." #error "Unrecognized system."
#endif #endif
FILE * f; /* To avoid a warning. */ FILE * f; /* To avoid a warning. */
clock_t then, now, delta; clock_t then, now, delta;
unsigned int fps = 0, pfps = 0; unsigned int fps = 0, pfps = 0;
char * data_dir; char * data_dir;
char * log_file; char * log_file;
time_t raw_date; time_t raw_date;
struct tm * current_date; struct tm * current_date;
gs_t * states; gs_t * states;
int c_state; int c_state;
atexit(leave); atexit(leave);
signal(SIGINT, manage_signal); signal(SIGINT, manage_signal);
@@ -94,52 +94,52 @@ int main() {
} }
set_colors(); set_colors();
/* Create the state data structures. */ /* Create the state data structures. */
c_state = 2; c_state = 2;
states = (gs_t *)malloc(sizeof(gs_t) * NUM_STATES); states = (gs_t *)malloc(sizeof(gs_t) * NUM_STATES);
initStateArray(&states); initStateArray(&states);
/* Start the game loop. */ /* Start the game loop. */
then = clock(); then = clock();
do{ do{
/* Handle terminal resize. */ /* Handle terminal resize. */
if(resize){ if(resize){
endwin(); endwin();
refresh(); refresh();
getmaxyx(stdscr, h, w); getmaxyx(stdscr, h, w);
fprintf(stderr, "\tSIGWINCH caught. (W: %d, H: %d)\n", w, h); fprintf(stderr, "\tSIGWINCH caught. (W: %d, H: %d)\n", w, h);
resize = FALSE; resize = FALSE;
signal(SIGWINCH, on_resize); signal(SIGWINCH, on_resize);
} }
states[c_state].input(); states[c_state].input();
c_state = states[c_state].update(); c_state = states[c_state].update();
if(c_state == -1) finished = TRUE; if(c_state == -1) finished = TRUE;
states[c_state].render(w, h); states[c_state].render(w, h);
fps++; fps++;
now = clock(); now = clock();
delta = now - then; delta = now - then;
if((int)delta / (int)CLOCKS_PER_SEC == 1){ if((int)delta / (int)CLOCKS_PER_SEC == 1){
then = now; then = now;
pfps = fps; pfps = fps;
fps = 0; fps = 0;
} }
move(0, 0); move(1, 1);
attron(COLOR_PAIR(BAR_COLOR)); attron(COLOR_PAIR(BSC_COLOR));
printw("FPS: %u", pfps); printw("FPS: %u", pfps);
refresh(); refresh();
}while(!finished); }while(!finished);
fclose(f); fclose(f);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@@ -151,7 +151,7 @@ void leave(void){
/* Finish ncurses. */ /* Finish ncurses. */
endwin(); endwin();
/* Mark the end of this run's log. */ /* Mark the end of this run's log. */
for(i = 0; i < 80; i++) for(i = 0; i < 80; i++)
fprintf(stderr, "-"); fprintf(stderr, "-");
fprintf(stderr, "\n"); fprintf(stderr, "\n");
@@ -162,30 +162,30 @@ void manage_signal(int signal){
switch(signal){ switch(signal){
case SIGINT: case SIGINT:
fprintf(stderr, "\tSIGINT caught.\n"); fprintf(stderr, "\tSIGINT caught.\n");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case SIGSEGV: case SIGSEGV:
fprintf(stderr, "\tSegmentation fault.\n"); fprintf(stderr, "\tSegmentation fault.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
break; break;
case SIGTERM: case SIGTERM:
fprintf(stderr, "\tSIGTERM caught.\n"); fprintf(stderr, "\tSIGTERM caught.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
break; break;
} }
} }
void on_resize(int signal){ void on_resize(int signal){
resize = TRUE; resize = TRUE;
} }
int start_ncurses(void){ int start_ncurses(void){
WINDOW *win_ptr; WINDOW *win_ptr;
int ret_code; int ret_code;
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
/* Prepare the terminal. */ /* Prepare the terminal. */
win_ptr = initscr(); win_ptr = initscr();
@@ -212,9 +212,9 @@ int start_ncurses(void){
if(ret_code == ERR) if(ret_code == ERR)
return -1; return -1;
/* Set delay. */ /* Set delay. */
ret_code = nodelay(stdscr, TRUE); ret_code = nodelay(stdscr, TRUE);
if(ret_code == ERR) if(ret_code == ERR)
return -1; return -1;
/* Initialize the screen size variables. */ /* Initialize the screen size variables. */
@@ -228,29 +228,29 @@ void set_colors(void){
ret_code = start_color(); ret_code = start_color();
if(ret_code == OK){ if(ret_code == OK){
if(has_colors() == TRUE){ if(has_colors() == TRUE){
init_color(COLOR_MAGENTA, 0, 0, 500); init_color(COLOR_MAGENTA, 0, 0, 500);
init_pair(BAR_COLOR, COLOR_WHITE, COLOR_RED); /* The color for the top and bottom bars. */ init_pair(BAR_COLOR, COLOR_WHITE, COLOR_RED); /* The color for the top and bottom bars. */
init_pair(BSC_COLOR, COLOR_WHITE, COLOR_BLACK); /* Basic text color. */ init_pair(BSC_COLOR, COLOR_WHITE, COLOR_BLACK); /* Basic text color. */
init_pair(HLT_COLOR, COLOR_YELLOW, COLOR_BLACK); /* Highlighted text color. */ init_pair(HLT_COLOR, COLOR_YELLOW, COLOR_BLACK); /* Highlighted text color. */
init_pair(OFF_COLOR, COLOR_BLUE, COLOR_BLACK); /* Lights off color. */ init_pair(OFF_COLOR, COLOR_BLUE, COLOR_BLACK); /* Lights off color. */
init_pair(DIM_COLOR, COLOR_RED, COLOR_BLACK); /* Dim light color. */ init_pair(DIM_COLOR, COLOR_RED, COLOR_BLACK); /* Dim light color. */
init_pair(LIT_COLOR, COLOR_YELLOW, COLOR_BLACK); /* Lights on color. */ init_pair(LIT_COLOR, COLOR_YELLOW, COLOR_BLACK); /* Lights on color. */
init_pair(GUI_COLOR, COLOR_YELLOW, COLOR_YELLOW); /* Main GUI bar color. */ init_pair(GUI_COLOR, COLOR_YELLOW, COLOR_YELLOW); /* Main GUI bar color. */
init_pair(EMP_COLOR, COLOR_WHITE, COLOR_WHITE); /* Empty GUI bar color. */ init_pair(EMP_COLOR, COLOR_WHITE, COLOR_WHITE); /* Empty GUI bar color. */
init_pair(DW_COLOR, COLOR_BLUE, COLOR_BLACK); init_pair(DW_COLOR, COLOR_BLUE, COLOR_BLACK);
init_pair(SW_COLOR, COLOR_CYAN, COLOR_BLACK); init_pair(SW_COLOR, COLOR_CYAN, COLOR_BLACK);
init_pair(SN_COLOR, COLOR_YELLOW, COLOR_BLACK); init_pair(SN_COLOR, COLOR_YELLOW, COLOR_BLACK);
init_pair(GR_COLOR, COLOR_GREEN, COLOR_BLACK); init_pair(GR_COLOR, COLOR_GREEN, COLOR_BLACK);
init_pair(FR_COLOR, COLOR_GREEN, COLOR_BLACK); init_pair(FR_COLOR, COLOR_GREEN, COLOR_BLACK);
init_pair(HL_COLOR, COLOR_WHITE, COLOR_BLACK); init_pair(HL_COLOR, COLOR_WHITE, COLOR_BLACK);
init_pair(MN_COLOR, COLOR_WHITE, COLOR_BLACK); init_pair(MN_COLOR, COLOR_WHITE, COLOR_BLACK);
} }
}else{ }else{
fprintf(stderr, "\t%s: Colors not supported.\n", __FILE__); fprintf(stderr, "\t%s: Colors not supported.\n", __FILE__);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
void clear_screen(void){ void clear_screen(void){