/** Just waits untill the user either unpauses the game or exits */ void get_pause_input () { int paused = TRUE; // Makes the cursor visible curs_set (1); while (paused == TRUE) { int input = getch (); switch (input) { // If we get no input case ERR: break; case 'p': case 'P': paused = FALSE; break; case 'q': case 'Q': engine_exit (); nsnake_exit (); break; default: break; } } // And here it becomes invisible again curs_set (0); }
/** Waits for an user action during the 'Game Over' screen */ void get_game_over_input () { int wait = TRUE; while (wait == TRUE) { int input = getch(); switch (input) { case ERR: // if we get no input break; case 'q': case 'Q': engine_exit (); nsnake_exit (); break; case 'm': case 'M': wait = FALSE; engine_show_main_menu (); case '\n': wait = FALSE; break; default: break; } } }
int main (int argc, char* argv[]) { if (argc > 1) args_handle (argc, argv); if(tron.user_name[0] == 0) { char *tmp = getlogin(); if(tmp == NULL) { ifitron_abort("No username provided and attempts to fetch it automatically, failed.\n"); } strcpy(tron.user_name, tmp); } if(get_server_mode() == 2) { while(TRUE) { int result = play_multiplayer(); if(result == -1) break; } engine_exit(); ifitron_exit(); } if(client_host_addr != NULL) { //Connect to the server int sd = connect_to_server(); if(sd == -1) { ifitron_abort("Couldn't connect to the server.\n"); }else{ set_client_socket_descriptor(sd); } } engine_init (); engine_show_main_menu (); ifitron_init (); while (TRUE) { if (tron.is_alive == FALSE) ifitron_game_over (); engine_get_game_input(); player_update(get_tron()); player_increase_size (get_tron(), 1); player_increase_score (get_tron(), game.level); if (tron.score % 50 == 0 && game.level < 9) game.level++; if (player_hit_self(get_tron()) == TRUE || player_hit_borders(get_tron()) == TRUE) tron.is_alive = FALSE; engine_show_screen (); } return 0; }
/** Function called when receiving an interrupt signal. * It restores the terminal to it's initial state and * frees whatever memory might be allocated from the game. */ void engine_safe_exit(int sig) { engine_exit(); /* since the game doesn't deal with malloc (yet) we're * pretty much safe quitting like this. * I've already set that when we call exit() we quit * from the engine, anyway. */ fprintf(stderr, "Interrupted (signal %d).\n" "Bad game, no donut for you!\n", sig); /* won't call functions registered with atexit() */ _exit(EXIT_FAILURE); }
/** Get the user input during game and make the right decisions */ void engine_get_game_input () { // The input variable MUST be int to accept non-ascii characters int input = getch (); switch (input) { case ERR: // If we get no input break; case KEY_UP: case 'w': case 'W': player_change_direction (UP); break; case KEY_LEFT: case 'a': case 'A': player_change_direction (LEFT); break; case KEY_DOWN: case 's': case 'S': player_change_direction (DOWN); break; case KEY_RIGHT: case 'd': case 'D': player_change_direction (RIGHT); break; case 'q': case 'Q': engine_exit (); nsnake_exit (); break; #ifdef DEBUG //debug key to increase score and size (gcc -DDEBUG) case 'e': case 'E': player_increase_score (100); player_increase_size (2); break; #endif case 'p': case 'P': engine_show_pause (); break; default: break; } }
/** The main function - contains the main loop of the game. * * * @note I tried to make this function as clear as possible, so anyone * could understaing the whole game logic starting by here. @n * Have fun with the source code! */ int main (int argc, char* argv[]) { if (argc > 1) args_handle (argc, argv); engine_init (); engine_show_main_menu (); nsnake_init (); while (TRUE == TRUE) { if (snake.is_alive == FALSE) nsnake_game_over (); engine_get_game_input (); player_update (); fruit_update_bonus (); if (player_hit_fruit () == TRUE) { // Is this score arbitrary? player_increase_score (game.level*3 + fruit.bonus); player_increase_size (2); fruit_init (); } if (player_hit_self () == TRUE) snake.is_alive = FALSE; if (player_collided_with_borders () == TRUE) snake.is_alive = FALSE; engine_show_screen (); } // Even though we have this here, the game always quits during // the main loop engine_exit (); nsnake_exit (); return 0; }
int main(int argc, char **argv) { struct engine *engine = NULL; Display *dpy; int num; XSetWindowAttributes attr; unsigned long mask; Window root; Window win; XVisualInfo *info = NULL; GLXContext ctx = NULL; int conf[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, GL_FALSE, GLX_DEPTH_SIZE, 1, None, }; dpy = XOpenDisplay(NULL); if (!dpy) { _err("failed to open X display\n"); return 1; } num = DefaultScreen(dpy); _inf("use GLX_SGIX_pbuffer on screen %d\n", num); root = RootWindow(dpy, num); info = glXChooseVisual(dpy, num, conf); if (!info) { _err("glXChooseVisual() failed\n"); goto out; } /* window attributes */ attr.border_pixel = 0; attr.colormap = XCreateColormap(dpy, root, info->visual, AllocNone); attr.event_mask = ButtonPressMask | ExposureMask | KeyPressMask; mask = CWBorderPixel | CWColormap | CWEventMask; win = XCreateWindow(dpy, root, 0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, 0, info->depth, InputOutput, info->visual, mask, &attr); ctx = glXCreateContext(dpy, info, NULL, GL_TRUE); if (!ctx) { _err("glXCreateContext() failed\n"); goto out; } XFree(info); info = NULL; XMapWindow(dpy, win); _msg("call glXMakeCurrent()\n"); glXMakeCurrent(dpy, win, ctx); _inf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); _inf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); _inf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); _inf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); _inf("GL_SHADING_LANGUAGE_VERSION = %s\n", (char *) glGetString(GL_SHADING_LANGUAGE_VERSION)); _msg("clear window\n"); glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); glXSwapBuffers(dpy, win); _msg("init engine\n"); if (engine_init(&engine, argc, argv) < 0) { _err("engine_init() failed\n"); goto out; } _msg("start engine\n"); engine_start(engine); glXSwapBuffers(dpy, win); engine_stop(engine); event_loop(dpy); _msg("exit engine\n"); engine_exit(&engine); out: glXMakeCurrent(dpy, 0, 0); if (info) XFree(info); if (ctx) glXDestroyContext(dpy, ctx); XDestroyWindow(dpy, win); XCloseDisplay(dpy); return 0; }
/** Gets the input for the main menu. */ int get_main_menu_input (int* speed_option) { int input = getch(); switch (input) { case ERR: // if we get no input break; case '\n': case ' ': return FALSE; break; case 'q': case 'Q': engine_exit(); nsnake_exit(); break; case KEY_UP: if (game.mode == BORDERS_OFF) game.mode = BORDERS_ON; break; case KEY_DOWN: if (game.mode == BORDERS_ON) game.mode = BORDERS_OFF; break; case KEY_LEFT: if (*speed_option > 1) (*speed_option)--; break; case KEY_RIGHT: if (*speed_option < 9) (*speed_option)++; break; case '1': *speed_option = 1; break; case '2': *speed_option = 2; break; case '3': *speed_option = 3; break; case '4': *speed_option = 4; break; case '5': *speed_option = 5; break; case '6': *speed_option = 6; break; case '7': *speed_option = 7; break; case '8': *speed_option = 8; break; case '9': *speed_option = 9; break; default: break; } return TRUE; }
/** Start things related to the game screen and layout */ int engine_screen_init(int width, int height) { engine.screen.width = width; engine.screen.height = height; /* Starting ncurses! */ initscr(); if ((has_colors() == TRUE) && (global.screen_use_colors)) { start_color(); /* This is a big hack to initialize all possible colors * in ncurses. The thing is, all colors are between * COLOR_BLACK and COLOR_WHITE. * Since I've set a large number of enums covering * all possibilities, I can do it all in a for loop. * Check 'man init_pair' for more details. * * This was taken straight from <curses.h> * * #define COLOR_BLACK 0 * #define COLOR_RED 1 * #define COLOR_GREEN 2 * #define COLOR_YELLOW 3 * #define COLOR_BLUE 4 * #define COLOR_MAGENTA 5 * #define COLOR_CYAN 6 * #define COLOR_WHITE 7 */ int i, j, k = 1; for (i = COLOR_BLACK; i <= COLOR_WHITE; i++) { for (j = COLOR_BLACK; j <= COLOR_WHITE; j++) { init_pair(k, i, j); k++; } } } /* Gets the current width and height */ int current_height, current_width; getmaxyx(stdscr, current_height, current_width); if ((current_width < engine.screen.width) || (current_height < engine.screen.height)) { engine_exit(); /* for now i must keep this - windows doesnt handle stderr */ #if OS_IS_WINDOWS printf( #else fprintf(stderr, #endif "Error! Your console screen is smaller than %dx%d\n" "Please resize your window and try again\n", engine.screen.width, engine.screen.height); exit(EXIT_FAILURE); }