// Fonction pour choisir la couleur du Snake en début de jeu void chooseColor (int *couleur) { #define YMAXCOLOR 15 #define YMINCOLOR 3 #define XMAXCOLOR 48 #define XMINCOLOR 6 int x = XMINCOLOR; int y = YMINCOLOR; int choice = 1; int touche = 0; gotoxy(x,y); printAirMenu(XMINCOLOR, YMINCOLOR, XMAXCOLOR, YMAXCOLOR); gotoxy((XMAXCOLOR)/2-11,YMINCOLOR+1); fprintf (stdout, "Choose Your Favorite Color !"); printMenuColor(choice, &(*couleur)); mode_raw(1); while (touche != 13) { if (kbhit()) { // CAPTURE TOUCHE FRAPPE AU CLAVIER do { touche=getchar(); } while (touche != 91 && touche != 13); if (touche != 13) // SI LA TOUCHE APPUYE EST "ENTER", c'est que le choix est définitif donc on sort du while { // Sinon c'est qu'on souhaite parcourir le menu touche=getchar(); yourChoice(touche, &choice, MENU_COLOR ); if (choice != 13) cleartoxy (15, 6, 8); // ON CLEAR SEULEMENT CETTE PARTIE DE L'ECRAN ! printMenuColor(choice, &(*couleur)); } } } mode_raw(0); }
// Fonction pour choisir le mode de jeu void chooseMode (int *mode) { #define YMAXMODE 13 #define YMINMODE 3 #define XMAXMODE 75 #define XMINMODE 6 int x = XMINMODE; int y = YMINMODE; int choice = 1; int touche = 0; gotoxy(x,y); printAirMenu(XMINMODE, YMINMODE, XMAXMODE, YMAXMODE); gotoxy((XMAXMODE)/2-8,YMINMODE+1); fprintf (stdout, "Choose Your Mode !"); printMenuMode(choice, &(*mode)); mode_raw(1); while (touche != 13) { if (kbhit()) { // CAPTURE TOUCHE FRAPPE AU CLAVIER do { touche=getchar(); } while (touche != 91 && touche != 13); if (touche != 13) // SI LA TOUCHE APPUYE EST "ENTER", c'est que le choix est définitif donc on sort du while { // Sinon c'est qu'on souhaite parcourir le menu touche=getchar(); yourChoice(touche, &choice, MENU_MODE ); if (choice != 13) { cleartoxy (15, 6, 5); cleartoxy (35, 6, 5); } // ON CLEAR SEULEMENT CETTE PARTIE DE L'ECRAN ! printMenuMode(choice, &(*mode)); } } } mode_raw(0); }
int RssReader::dump_multiple_files() { TiXmlElement *current_node; int idx = 1; std::cout << "\033[2J\033[1;1H"; mode_raw(1); // Mode raw: buffering without 'Enter' key while (idx != -2) // Code to QUIT program { if (idx != -1) { std::cout << "\033[2J\033[1;1H"; this->open_file(idx); current_node = this->_handle->FirstChildElement("rss") .FirstChildElement("channel").Element(); if (current_node == NULL) { std::cout << "No item !" << std::endl; return (EXIT_FAILURE); } this->dump_channel_header(current_node); current_node = this->_handle->FirstChildElement("rss") .FirstChildElement("channel") .FirstChildElement("item").Element(); if (current_node == NULL) { std::cout << "No item !" << std::endl; return (EXIT_FAILURE); } for (; current_node; (current_node = current_node->NextSiblingElement())) { if (current_node->NoChildren() == false) this->dump_item(current_node); } } idx = getCommand(idx); } mode_raw(0); // Mode Cooked return (EXIT_SUCCESS); }
void play_client(config cfg) { //time of the last step that occured. in ticks. int ok; //signal we're waiting for before begining char c; //key that is pressed int ret; //value returned by 'read(0)', 0 if no new key was pressed int ret_serv; queue p1_queue = new_queue(MAX_INPUT_STACK); //queue used to stack player input direction cur_dir; direction* players_dir = malloc(cfg.nb_players*sizeof(direction)); //let's wait for server's signal ret_serv = read(sockfd, &ok, 1*sizeof(int)); if(ret_serv < 0){ perror("handle_server read waiting for signal"); } else if(ret_serv == 0){ printf("Server on socket %i closed connection.\n", sockfd); clear(); safe_quit(1); } if(ok != 1){ printf("Wrong signal recieved from server. Received %i.\n", ok); safe_quit(1); } printf("Signal received. Starting now\n"); //sleep(3); //GO clear(); mode_raw(1); //creating field field* map = new_field(WIDTH, HEIGHT); //creating snakes int i; snake** snakes = malloc(cfg.nb_players*sizeof(snake*)); for(i = 0; i < cfg.nb_players; i++){ snakes[i] = new_snake(T_SNAKE, cfg.size, i, map); } fflush(stdout); struct timeval last_step_time; gettimeofday(&last_step_time, NULL); struct timeval now; int elapsed_time; //elapsed time while(1){ //SUMMARY //1 - let's check if it's time to retrieve input //2 - let's retrieve and sort every input //3 - let's send the server our direction //4 - let's wait for server's directions //5 - let's make snakes move //6 - let's update last_step_time //-------------------------------------- //1 - let's check if it's time to retrieve input gettimeofday(&now, NULL); elapsed_time = diff_time(now, last_step_time); //time elapsed since last round. in ms. if(elapsed_time > TIME_STEP - PING){ //2 - let's retrieve and sort every input while((ret = read(0, &c, sizeof(char))) != 0){ if(ret == -1){ perror("read in 'play()'"); safe_quit(1); } if(c == C_QUIT){ mode_raw(0); clear(); free_queue(&p1_queue); free_all_client(map, snakes, cfg.nb_players); return; } else if(key_is_p1_dir(c)){ //if the key is a move key for player 1: if(! queue_full(&p1_queue)){ enqueue(&p1_queue, key_to_dir(c)); } } else{ //key pressed was a useless key. Do nothing. } } //3 - let's send the server our direction cur_dir = (! queue_empty(&p1_queue)) ? dequeue(&p1_queue) : snakes[cfg.id]->dir; cur_dir = (cur_dir == opposite(snakes[cfg.id]->dir)) ? snakes[cfg.id]->dir : cur_dir; if(write(sockfd, &cur_dir, 1*sizeof(direction)) < 0){ perror("handle_server write"); safe_quit(1); } switch(cur_dir){ case 0: write(2, "sent 0\n", 7*sizeof(char)); break; case 1: write(2, "sent 1\n", 7*sizeof(char)); break; case 2: write(2, "sent 2\n", 7*sizeof(char)); break; case 3: write(2, "sent 3\n", 7*sizeof(char)); break; } //4 - let's wait for server's directions ret_serv = read(sockfd, players_dir, cfg.nb_players*sizeof(direction)); if(ret_serv == -1){ perror("handle_server read"); safe_quit(1); } else if(ret_serv == 0){ clear(); printf("Server closed connection.\n"); safe_quit(1); } //5 - let's make snakes move for(i = 0; i<cfg.nb_players; i++){ if(players_dir[i] != 4){ //if the player is not dead (dir = 4 => dead player) move(snakes[i], players_dir[i], map); } } fflush(stdout); //6 - let's update last_step_time gettimeofday(&last_step_time, NULL); } else{ //sleep 90% of the remaining time usleep( (((TIME_STEP - PING) - elapsed_time) * 0.9) * 1000); } } }
int safe_quit(int return_value){ mode_raw(0); close(sockfd); exit(return_value); }