void cleanup() { create_set_speeds(hands->c, 0, 0); //Stop moving create_close(hands->c); turret_close(hands->t); free(filt->sonar0); free(filt->sonar1); free(filt->ir0); free(filt->ir1); free(pids->trans); free(pids->sonar); free(pids->angle); free(pids->angleT); free(posData); posData = NULL; free(hands); free(filt); free(pids); }
int main(int argc, char *argv[]) {//start of main function //set gameover = true to stop the game from loading before the player plays GAMEOVER=true; //variable declerations int id; //variables used for the threads pthread_t pthread0; pthread_t pthread1; int threadid0 = 0; int threadid1 = 1; int threadid2 = 2; int threadid3 = 3; int threadid4 = 4; int threadid5 = 5; int threadid6 = 6; //set is intro to true to play the intro bool inIntro =true; //variable used to display the players message char *playermessage; //map variables int mapxoff = 0; int mapyoff = 0; //game speed variables declerations and initalization int frames_per_sec=0; int frames_done=0; int old_time =0; int totaltime=0; //declare allegro variables //BITMAP *buffer; BITMAP *intro; //initalize the classes //player *owl; sprite *hearts[3]; sprhandler = new spritehandler(); snake *snakeboss; //initalize allegro allegro_init(); install_keyboard(); install_timer(); set_keyboard_rate(1000,1000); install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL); //initalize the screen set_color_depth(COLORDEPTH); set_gfx_mode(MODE,WIDTH,HEIGHT,0,0); //lock both the varibale and the funciton to be used by the interrupt handler LOCK_VARIABLE(ticks); LOCK_FUNCTION(ticker); //install the interrupt handler which calls the ticker 60 times a second install_int_ex(ticker,BPS_TO_TIMER(FPS)); //lock both the varibale and the funciton to be used by the interrupt handler //these LOCK_VARIABLE(gametime); LOCK_FUNCTION(game_time_ticker); //install the interrupt handler which calls the game time ticker //game time ticker is in 10ths of seconds install_int_ex(game_time_ticker,BPS_TO_TIMER(10)); //create back buffer buffer = create_bitmap(SCREEN_W, SCREEN_H); //create the intro screen by loading the bitmap into the intro buffer intro = load_bitmap("flightless title.bmp",NULL); if(!intro){ allegro_message("Error loading intro image"); return 1; } //seed the random function srand(time(NULL)); //load fonts FONT *dicot_22_font; PALETTE palette; //load the the font for the header of the application dicot_22_font = load_font("dicot_22.pcx", palette, NULL); //load and display the intro screen while(inIntro) { //display the intro screen for the game blit(intro,screen,0,0,0,0,SCREEN_W,SCREEN_H); //wait for the player to press enter to start the game if(key[KEY_ENTER]){ inIntro=false; GAMEOVER=false; } //if the player presses esc then exit the game and the intro if(key[KEY_ESC]){ inIntro=false; GAMEOVER=true; } } //for as many badies make sprites to hold the bugs for(int i=0;i<BADIES;i++) { //initalize the bug sprite bug[i] = new bugs(); //load the bug sprite sheet if(!bug[i]->load("bug.bmp")){ allegro_message("Error loading bug sprite"); return 1; } //initalize the bug propertys bug[i]->init(); //add sprites to the sprite handler sprhandler->addbugs(bug[i]); } //initalize the player owl = new player(); //load the players sprite sheet if(!owl->load("owl sprite_new.bmp")){ allegro_message("Error loading owl sprite"); return 1; } //initalize the sprite properties owl->init(); //add the player object to the sprite handler sprhandler->addsprite(owl); //create the new snake object snakeboss = new snake(); //load the snake if(!snakeboss->load("snake_Tiles.bmp")){ allegro_message("Error loading snake sprite"); return 1; } //initalize the snake snakeboss->init(); //snakeboss->alive=false; //add the snake to the sprite handler sprhandler->addboss(snakeboss); //create for loop to load the players health indicator for(int i=1;i<4;i++) { //create new sprite objects to hold the images hearts[i-1] = new sprite(); //load the players health image if(!hearts[i-1]->load("FullHealth.bmp")){ allegro_message("Error loading player Health"); return 1; } //set sprite properties hearts[i-1]->x = SCREEN_W-35*i; hearts[i-1]->y = 10; hearts[i-1]->width = 30; hearts[i-1]->height = 30; hearts[i-1]->velx = 0; hearts[i-1]->vely = 0; hearts[i-1]->animcolumns = 0; hearts[i-1]->curframe = 0; hearts[i-1]->totalframes =1; hearts[i-1]->animdir = 0; //object type of non moving sprite hearts[i-1]->objecttype=2; hearts[i-1]->alive=0; //add the players health to the sprite handler sprhandler->addsprite(hearts[i-1]); } //create the music samples SAMPLE *ingamesong; //load the wav file into the samples //main game soundtrack ingamesong=load_sample("melo-24_Clip.wav"); if(ingamesong==NULL){ allegro_message("Error loading game soundtrack"); return 1; } //use mappy.h global function MapLoad to load the .FMP file MapLoad("flightless.FMP"); //play the sample music int playing = play_sample(ingamesong,150,125,1000,true); //start the clock to time the player startTime = time(NULL); //create the thread for player id = pthread_create(&pthread0, NULL, playerdraw_thread, (void*)&threadid5); //create the thread for the bugs id = pthread_create(&pthread1, NULL, wormdraw_thread, (void*)&threadid0); id = pthread_create(&pthread1, NULL, wormdraw_thread, (void*)&threadid1); id = pthread_create(&pthread1, NULL, wormdraw_thread, (void*)&threadid2); id = pthread_create(&pthread1, NULL, wormdraw_thread, (void*)&threadid3); id = pthread_create(&pthread1, NULL, wormdraw_thread, (void*)&threadid4); //create the thread to draw the snake id = pthread_create(&pthread1, NULL, snakedraw_thread, (void*)&threadid0); //main game loop while (!GAMEOVER) { while (ticks==0) { rest(1); } while(ticks>0) { if(key[KEY_ESC]) { playermessage="You quit :( Please come back again."; GAMEOVER=true; } //get the previous value of ticks and store them old_ticks=ticks; //each time we hit this loop we increment the mapxoffset +1 if(mapxoff<mapwidth*mapblockwidth) { //if the player is not hiding update the map if(owl->state!=2) { mapxoff++; } } //draw the background tiles via the mappy global funciton MapDrawBG MapDrawBG(buffer,mapxoff,0,0,0,WIDTH-1,HEIGHT-1); //draw the players health indicator based on the owls health/30 for (int i=0;i<((owl->health)/30);i++) { hearts[i]->draw(buffer); } //lock the varibales to protect them from the unsynced threads pthread_mutex_lock(&threadsafe); //get the player input owl->getinput(); sprhandler->updatebugs(); sprhandler->updatesnake(); //decrement the ticks ticks--; //unlock the mutex pthread_mutex_unlock(&threadsafe); //if the logic is taking too long abort and draw the screen if(old_ticks<=ticks) break; } //for every second that passess we need to call this function if(gametime - old_time >= 10) { //frames_per_sec holds the number of frames drawn in the last sec frames_per_sec = frames_done; //reset these variables for the next second frames_done =0; old_time=gametime; } //Decrement the players health if(gametime%120==0) { owl->takedamage(2); } if(sprhandler->getsnakes(0)->alive==false && gametime%400==0) { sprhandler->getsnakes(0)->resurrect(); } //check players health if(owl->health<=0) { playermessage="You have died...please remeber to eat next time."; GAMEOVER=true; } //stop the window from scrolling if(GAMEOVER==true ||mapxoff>=mapwidth*mapblockwidth-WIDTH) { if(mapxoff>=mapwidth*mapblockwidth-WIDTH) playermessage="Great job player you passed the game! Way to go."; //stop the map from moving mapxoff=mapwidth*mapblockwidth-WIDTH; //stop the music from playing stop_sample(ingamesong); //create the music samples SAMPLE *cheer=NULL; //load the wav file into the sample cheer=load_sample("app.wav"); if(cheer==NULL){ allegro_message("Error loading game soundtrack"); return 1; } //play the sample music play_sample(cheer,255,125,1000,true); //message the player...even if they are a quiter! textprintf_ex(screen,dicot_22_font,200,SCREEN_H/2,makecol(255,0,0),-1,playermessage); rest(2000); GAMEOVER=true; } //show frames fer second on the screen by pressing "cntrl f" if(key[KEY_F]) { if(key_shifts & KB_CTRL_FLAG) { //switch the showFPS flag to display the FPS SHOWFPS=!SHOWFPS; } //SHOWFPS=SHOWFPS; } if(SHOWFPS==true) textprintf_ex(buffer,font,20,20,WHITE,0,"frames= :%d",frames_per_sec); if(key[KEY_H]) { if(key_shifts & KB_CTRL_FLAG) { //switch the SHOWHEALTH flag to display the FPS SHOWHEALTH=!SHOWHEALTH; } //SHOWHEALTH=SHOWHEALTH; } //update the screen using a double buffering technique acquire_screen(); blit(buffer,screen,0,0,0,0,WIDTH,HEIGHT); release_screen(); //everytime we bilt from buffer to screen increase frames_done frames_done++; } //find the time that the game ended endTime = time(NULL); //kill the mutex (thread protection) pthread_mutex_destroy(&threadsafe); //call to create the final image to present to the user create_close(buffer,dicot_22_font,owl); //Garbage Collection clean up all of the objects that are being used in memory remove_keyboard(); remove_timer(); //destory the bitmaps that we are using destroy_bitmap(buffer); destroy_bitmap(intro); //destroy the map MapFreeMem(); //delete the sprites delete sprhandler; //call to allegro exit allegro_exit(); return 0; }//end of main
int main(int argc, const char **argv) { printf("entered program\n"); /* allocate device objects */ c = create_create("/dev/ttyS2"); r = turret_create(); /* open the create serial comm */ if(create_open(c,FULLCONTROL) < 0) { printf("create open failed\n"); return(-1); } /* Open the i2c device */ if(turret_open(r) < 0) { printf("failed to connect to robostix\n"); return(-1); } /* init the robostix board interfaces */ printf("initialize turret\n"); turret_init(r); // sonar if(!WHICH_SENSOR){ turret_SetServo(r,90); } //ir else{ turret_SetServo(r,0); } filter = firFilterCreate(); waypoints[0].x = 7.3; waypoints[0].y = 0.0; waypoints[1].x = 7.3; waypoints[1].y = 7.62; waypoints[2].x = 26.19; waypoints[2].y = 7.62; waypoints[3].x = 26.19; waypoints[3].y = -3.04; waypoints[4].x = 30.15; waypoints[4].y = -3.04; waypoints[5].x = 30.15; waypoints[5].y = -12.18; waypoints[6].x = 7.3; waypoints[6].y = -12.18; waypoints[7].x = 7.3; waypoints[7].y = 0.0; waypoints[8].x = 0.0; waypoints[8].y = 0.0; int i; for(i = 0; i < 9; i++) { Move(c, r, waypoints, i); } /* // read points from file FILE *file = fopen(argv[1], "r"); printf("just read file\n"); if(file != NULL) { char line[32]; char num_lines; // read the first line in the file. // this gives the number of waypoints printf("fgetting line nums\n"); fgets(num_lines, 1, file); num_waypoints = atoi(num_lines); printf("allocating waypoint size\n"); // allocate the size of *waypoints waypoints = malloc(num_waypoints * sizeof(waypoint)); printf("malloc'd\n"); // store the (x,y) coordinates in *waypoints int i = 0; printf("fgetting lines\n"); while(fgets(line,sizeof(line),file) != NULL) { printf("foo\n"); file_tokens = strtok(line, ","); printf("bar\n"); waypoints[i].x = atof(file_tokens[0]); printf("freep\n"); waypoints[i].y = atof(file_tokens[1]); printf("froop\n"); i++; } fclose(file); // Move to each waypoint int j; for(j = 0; j < num_waypoints; j++) { Move(c, r, waypoints[j], j); } } else { perror(argv[1]); }*/ // Shutdown and tidy up create_set_speeds(c,0.0,0.0); create_close(c); create_destroy(c); turret_close(r); turret_destroy(r); exit(0); return 0; }