void Apep::run(){ assert("You must initialize engine before run!" && back_buffer != nullptr); clean_screen(); if (!game_setup()) return; while (game_update()) { while (_kbhit()) key(_getch()); game_draw(); flush(); Sleep(10); } game_cleanup(); }
//Game functions void * game_f( void *data ) { struct gamedata_t *mygamedata; mygamedata = (struct gamedata_t *) data; struct playerdata_t *myplayers; bool run=true; int retval; struct epoll_event event_setup; myplayers = malloc( sizeof (struct playerdata_t) * MAX_PLAYERS ); for( int i = 0; i < MAX_PLAYERS; i++ ){ myplayers[i].playerid = -1; } mygamedata->numplayers=0; // (MAX_PLAYERS+1) because of the extra pipe to talk to the server mygamedata->epfd = epoll_create ( MAX_PLAYERS + 1 ); //printf( "NG %d ip rd %d ip wr %d op rd %d op wr %d epfd %d tid %lu\n", mygamedata->gamenumber, mygamedata->input[READPIPE], mygamedata->input[WRITEPIPE], mygamedata->output[READPIPE], mygamedata->output[WRITEPIPE], mygamedata->epfd, pthread_self() ); printf( "New Game %d\n", mygamedata->gamenumber ); //Setting bit 63 == this is a server command event_setup.data.u64 = SRV_CMD; event_setup.events = EPOLLIN; if ( -1 == epoll_ctl( mygamedata->epfd, EPOLL_CTL_ADD, mygamedata->input[READPIPE], &event_setup ) ) { retval = errno; //we return just a success/failure value printf( "Error adding server fd to epoll list\n" ); pthread_exit( (void *) (long)retval ); } while( run ) { int numevents; struct epoll_event event_list[MAX_EVENTS]; // Wait forever for an event from a player or the server // Later, for a dyamic game that has timed updates, the // -1 is replaced with how many ms to wait // printf( "Game %d waiting for event\n", mygamedata->gamenumber ); numevents = epoll_wait( mygamedata->epfd, event_list, MAX_EVENTS, -1 ); // Save a variable by counting down ( currently MAX_EVENTS // is 1) , does it matter if we start at the end? for( numevents--;numevents >= 0; numevents-- ) { if( event_list[numevents].data.u64 == ( SRV_CMD ) ) { run = process_server_command( mygamedata, myplayers ); } else { // Process player command printf( "Player command received\n" ); run = process_player_command( mygamedata, myplayers, (int) event_list[numevents].data.u64 ); } } } game_cleanup( mygamedata, myplayers ); retval = EXIT_SUCCESS; pthread_exit( (void *) (long)retval ); }
/* ** Animates a game until it is finished, and then cleans up allocated stuff. ** Returns final status of the game. */ int play_game() { unsigned int status; int i; Boolean newgame; if (num_combatants == 0) return GAME_FAILED; /* In an ultimate game, only reset at the beginning, since we want don't want scores to reset after every goal. */ if (settings.si.game == ULTIMATE_GAME) { init_combatants(); newgame = TRUE; } do { #ifdef X11 button_up(ANIM_WIN, FALSE); follow_mouse(ANIM_WIN, FALSE); #endif if (settings.si.game != ULTIMATE_GAME) { init_combatants(); newgame = TRUE; } if (setup_game(newgame) == GAME_FAILED) return GAME_FAILED; newgame = FALSE; game_running = True; #ifdef SOUND play_all(START_SOUND); #endif /*SOUND*/ do { status = animate(); } while (status == GAME_RUNNING); #ifdef SOUND play_all(END_SOUND); #endif /*SOUND*/ game_running = False; status = display_game_stats(status); game_cleanup(); /* nuking all the vehicles here is overkill */ } while (status == GAME_RESET); /* Unmap battle windows and clear all other windows on all the terminals */ for (i = 0; i < num_terminals; i++) { set_terminal(i); unmap_battle_windows(); clear_windows(); } /* Return to terminal 0 for the user interface */ set_terminal(0); #ifdef X11 button_up(ANIM_WIN, TRUE); follow_mouse(ANIM_WIN, TRUE); #endif return status; }