Tested some island and unicode rendering.
This commit is contained in:
109
src/main.c
109
src/main.c
@@ -10,12 +10,15 @@
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <ncurses.h>
|
||||
#include <ncursesw/ncurses.h>
|
||||
#include <termios.h>
|
||||
#include <locale.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include "constants.h"
|
||||
#include "game_state.h"
|
||||
|
||||
void leave(void);
|
||||
void manage_signal(int signal);
|
||||
@@ -28,11 +31,19 @@ static int w = 0, h = 0;
|
||||
|
||||
int main() {
|
||||
bool finished = false;
|
||||
char *home_dir = getenv("HOME");
|
||||
char *data_dir;
|
||||
char *log_file;
|
||||
time_t raw_date;
|
||||
struct tm *current_date;
|
||||
#if defined(_WIN32) || defined(_WIN64) || defined(__MINGW32__)
|
||||
char * home_dir = getenv("APPDATA");
|
||||
#elif defined(__linux__) || defined(__GNUC__)
|
||||
char * home_dir = getenv("HOME");
|
||||
#else
|
||||
#error "Unrecoginized compiler."
|
||||
#endif
|
||||
char * data_dir;
|
||||
char * log_file;
|
||||
time_t raw_date;
|
||||
struct tm * current_date;
|
||||
gs_t * states;
|
||||
int c_state;
|
||||
|
||||
atexit(leave);
|
||||
signal(SIGINT, manage_signal);
|
||||
@@ -45,12 +56,12 @@ int main() {
|
||||
/* If we got the user's home directory, build the data directory path. */
|
||||
data_dir = (char*)malloc(strlen(home_dir) + 7);
|
||||
strcpy(data_dir, home_dir);
|
||||
strcat(data_dir, "/.tomb");
|
||||
strcat(data_dir, F_SEP ".cyjam");
|
||||
|
||||
/* Redirect stderr output to a file. */
|
||||
log_file = (char*)malloc(strlen(data_dir) + 8);
|
||||
strcpy(log_file, data_dir);
|
||||
strcat(log_file, "/stderr");
|
||||
strcat(log_file, F_SEP "stderr");
|
||||
freopen(log_file, "a", stderr);
|
||||
|
||||
/* Log the current date and time. */
|
||||
@@ -61,13 +72,11 @@ int main() {
|
||||
/* Try to create the data directory with permissions 775. */
|
||||
if(mkdir(data_dir, S_IRWXU | S_IWGRP | S_IRGRP| S_IROTH | S_IXOTH) == 0){
|
||||
/* The data directory was sucessfully created. */
|
||||
//init_scores(data_dir);
|
||||
}else{
|
||||
if(errno != EEXIST){
|
||||
/* The directory does not exists and could not be created. */
|
||||
perror("\tmain.c");
|
||||
perror("\t" __FILE__);
|
||||
fprintf(stderr, "\tdata_dir is: %s\n", data_dir);
|
||||
//init_scores(NULL);
|
||||
}else{
|
||||
/* The directory already exits. */
|
||||
//init_scores(data_dir);
|
||||
@@ -75,22 +84,31 @@ int main() {
|
||||
}
|
||||
}else{
|
||||
/* If there is no HOME environment variable, quit. */
|
||||
fprintf(stderr, "\tmain.c: Couldn't find the user's home directory\n");
|
||||
fprintf(stderr, "\t%s: Couldn't find the user's home directory\n", __FILE__);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* Start ncurses. */
|
||||
if(start_ncurses() != 0){
|
||||
fprintf(stderr, "\tmain.c: Ncurses could not be initialized.\n");
|
||||
fprintf(stderr, "\t%s: Ncurses could not be initialized.\n", __FILE__);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
set_colors();
|
||||
|
||||
do{
|
||||
clear_screen();
|
||||
c_state = 2;
|
||||
|
||||
states = (gs_t *)malloc(sizeof(gs_t) * NUM_STATES);
|
||||
initStateArray(&states);
|
||||
|
||||
refresh();
|
||||
}while(!finished);
|
||||
do{
|
||||
clear_screen();
|
||||
|
||||
states[c_state].input();
|
||||
c_state = states[c_state].update();
|
||||
states[c_state].render(w, h);
|
||||
|
||||
refresh();
|
||||
}while(!finished);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -101,8 +119,8 @@ void leave(void){
|
||||
|
||||
/* Finish ncurses. */
|
||||
endwin();
|
||||
/* Close the scores database and todays log. */
|
||||
// close_scores();
|
||||
|
||||
/* Mark the end of this run's log. */
|
||||
for(i = 0; i < 80; i++)
|
||||
fprintf(stderr, "-");
|
||||
fprintf(stderr, "\n");
|
||||
@@ -113,12 +131,19 @@ void manage_signal(int signal){
|
||||
switch(signal){
|
||||
case SIGINT:
|
||||
fprintf(stderr, "\tSIGINT caught.\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
break;
|
||||
|
||||
case SIGSEGV:
|
||||
fprintf(stderr, "\tSegmentation fault.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
|
||||
case SIGTERM:
|
||||
fprintf(stderr, "\tSIGTERM caught.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,40 +152,51 @@ void on_resize(int signal){
|
||||
|
||||
/* Request the new size of the terminal. */
|
||||
ioctl(1, TIOCGWINSZ, &ws);
|
||||
/* Resize ncurse's stdscr. */
|
||||
|
||||
/* Resize ncurses's stdscr. */
|
||||
resizeterm(ws.ws_row, ws.ws_col);
|
||||
|
||||
/* Get the new size of the window. */
|
||||
getmaxyx(stdscr, h, w);
|
||||
|
||||
fprintf(stderr, "\tSIGWINCH caught. (W: %d, H: %d)\n", w, h);
|
||||
}
|
||||
|
||||
int start_ncurses(void){
|
||||
WINDOW *win_ptr;
|
||||
int ret_code;
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
|
||||
/* Prepare the terminal. */
|
||||
win_ptr = initscr();
|
||||
if(win_ptr == NULL)
|
||||
return -1;
|
||||
|
||||
/* Enable special characters. */
|
||||
ret_code = keypad(stdscr, TRUE);
|
||||
if(ret_code == ERR)
|
||||
return -1;
|
||||
|
||||
/* Disable line buffering. */
|
||||
ret_code = cbreak();
|
||||
if(ret_code == ERR)
|
||||
return -1;
|
||||
|
||||
/* Disable echo. */
|
||||
ret_code = noecho();
|
||||
if(ret_code == ERR)
|
||||
return -1;
|
||||
|
||||
/* Hide the cursor. */
|
||||
ret_code = curs_set(FALSE);
|
||||
if(ret_code == ERR)
|
||||
return -1;
|
||||
|
||||
/* Initialize the screen size variables. */
|
||||
getmaxyx(stdscr, h, w);
|
||||
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void set_colors(void){
|
||||
@@ -168,16 +204,29 @@ void set_colors(void){
|
||||
ret_code = start_color();
|
||||
if(ret_code == OK){
|
||||
if(has_colors() == TRUE){
|
||||
init_pair(1, COLOR_WHITE, COLOR_RED); /* The color for the top and bottom bars. */
|
||||
init_pair(2, COLOR_WHITE, COLOR_BLACK); /* Basic text color. */
|
||||
init_pair(3, COLOR_YELLOW, COLOR_BLACK); /* Highlighted text color. */
|
||||
init_pair(4, COLOR_BLUE, COLOR_BLACK); /* Lights off color. */
|
||||
init_pair(5, COLOR_RED, COLOR_BLACK); /* Dim light color. */
|
||||
init_pair(6, COLOR_YELLOW, COLOR_BLACK); /* Lights on color. */
|
||||
init_pair(7, COLOR_YELLOW, COLOR_YELLOW); /* Main GUI bar color. */
|
||||
init_pair(8, COLOR_WHITE, COLOR_WHITE); /* Empty GUI bar color. */
|
||||
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(BSC_COLOR, COLOR_WHITE, COLOR_BLACK); /* Basic 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(DIM_COLOR, COLOR_RED, COLOR_BLACK); /* Dim light 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(EMP_COLOR, COLOR_WHITE, COLOR_WHITE); /* Empty GUI bar color. */
|
||||
|
||||
init_pair(DW_COLOR, COLOR_MAGENTA, COLOR_BLACK);
|
||||
init_pair(SW_COLOR, COLOR_BLUE, COLOR_BLACK);
|
||||
init_pair(SN_COLOR, COLOR_YELLOW, COLOR_BLACK);
|
||||
init_pair(GR_COLOR, COLOR_GREEN, COLOR_BLACK);
|
||||
init_pair(FR_COLOR, COLOR_GREEN, COLOR_BLACK);
|
||||
init_pair(HL_COLOR, COLOR_RED, COLOR_BLACK);
|
||||
init_pair(MN_COLOR, COLOR_WHITE, COLOR_BLACK);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
fprintf(stderr, "\t%s: Colors not supported.\n", __FILE__);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void clear_screen(void){
|
||||
|
Reference in New Issue
Block a user