int main(void) { lcd.clearMarkers(); //init analog if needed init_analog(); srand(adc_read(0)); //pin 0 on portb output. //led stays on if iterations between fields are the same (same number of cells) DDRB |= _BV(PB0); //clear the buffer. clearArray(buffer); //if house define insert that. #ifdef PATTERN insert_pattern(field, Glider, 0,0); #endif //else random field. #ifdef RANDOM createRandomField(field); #endif //set contrast. lcd.setContrast(17); //make sure to start at location 0,0 lcd.setCursor(0,0); //position is field size, I refresh the screen Backwards. position = fieldSize; while(position--) { //display field with current position showField(field, fieldSize-position); //here the rules of the game of life are checked. //if a position has a cell (1), //then look how many around, //if 2 or 3 around it lives, else it dies. //if(field[position]) //{ // if(totalAround(field, position)==surviveAbility) // { // buffer[position]=1; // } // else if(totalAround(field, position)==surviveAbility+1) // { // buffer[position]=1; // } // else // { // buffer[position]=0; // } //} //else //{ //but if a position in the field is empty //and it has 3 around, that position becomes alife. // if(totalAround(field, position)==reproductiveNumber) // { // buffer[position] = 1; // } // else // { // buffer[position] = 0; // } //} //position keeps position in the field array, //and at the same time drawing location on //screen. if(position == 0) { //insert changes into the field. copy_buffer(buffer, field); //reset position to 0 position = fieldSize; //set frame rate with a blocking delay.. delay(adc_read(1)); //check wether we are in a steady state or just still evolving. currentState = checkField(field); //set contrast with pot meter on analog pin 1 (not 0) lcd.setContrast(32/2); //change field if field the same a while, or iterations goes above a certain number which meens it's probaly in a loop //check if button is pressed and create a new field. if(changeCount == holdingNumber || (iterations > 1000) || (PINB & (1<<PB2))) { //reset changeCount changeCount = 0; //reset iteration count. iterations = 0; //create a random playing field. #ifdef PATTERN insert_pattern(field, Glider, 0,0); #endif //createRandomField(field); //put a pattern we created onto the field. //insert_field(stable, field); } //if the field states stay the same. else if(currentState == previousState) { PORTB |= (1<<PB0); changeCount++; } //if the field states are different. else { PORTB &= ~(1<<PB0); previousState = currentState; //that is why setting it to zero. iterations++; //changeCount shouldn't change if the inbetween states happen te be the same. changeCount = 0; } //set location and nicely print something. lcd.setCursor(0,5); writeFormated(iterations,changeCount,"Game of Life:"); } } return 0; }
int gameloop(dataStore *data,SDL_Surface *screen) { createRandomField(data); clock_t innerStartTime, innerStopTime, startTime, stopTime, diffTime,mouseTime,mT,mousetimewait=500; int done=0,i=0,aVal,motionPath=0,runPath=0,drawPath=0,mouseDown=0,ownpath=0; position *lastmouse=NULL,*mouse_pos=NULL,*tmp=NULL,*lastpath=NULL; SDL_Event event; // MUSIC FADEOUT AND IN //Mix_HaltMusic(); Mix_PlayMusic( data->ingamemusic, -1); GraphicUpdate(screen,data); while (!done) { /* Check for events */ startTime = SDL_GetTicks(); while ( SDL_PollEvent(&event) ) { innerStartTime = SDL_GetTicks(); switch (event.type) { case SDL_QUIT: done = 1; quitSDL(data); break; case SDL_MOUSEMOTION: //Prüft ob sich die Maus aus dem 50x50 Feld bewegt hat mouse_pos=calloc(1,sizeof(struct position)); SDL_GetMouseState(&mouse_pos->x,&mouse_pos->y); if(mouse_pos->x > 24 && 226 > mouse_pos->x && mouse_pos->y > 24 && 51 > mouse_pos->y) { aStar(data,&(data->home)); GraphicUpdate(screen,data); positionListDelete(data); break; } else if(mouse_pos->x > 574 && 776 > mouse_pos->x && mouse_pos->y > 24 && 51 > mouse_pos->y) { aStar(data,&(data->stash)); GraphicUpdate(screen,data); positionListDelete(data); break; } else { mouse_pos=pixelToGrid(mouse_pos); if(lastmouse!=NULL && mouseDown) { if(lastmouse->x!=mouse_pos->x || lastmouse->y!=mouse_pos->y || ownpath==0) { tmp=calloc(1,sizeof(struct position)); tmp->x=mouse_pos->x+data->horizontalScroll; tmp->y=mouse_pos->y+data->verticalScroll; if(data->player.anfang!=NULL&& data->hedgewood[tmp->y][tmp->x].aStarValue*data->hedgewood[tmp->y][tmp->x].visible>-1 && aStarManhatten(*lastpath,*tmp)==AVGASTAR) { lastpath=tmp; positionQListAdd(data,tmp); ownpath++; GraphicUpdate(screen,data); } else { if(tmp->x==data->player.p_pos.x && tmp->y==data->player.p_pos.y) { lastpath=tmp; positionQListAdd(data,tmp); ownpath++; GraphicUpdate(screen,data); } else if(lastmouse->x!=mouse_pos->x || lastmouse->y!=mouse_pos->y)printf("Die Startposition muss die Spielerposition sein\n"); } //free(tmp); } } else if(lastmouse!=NULL && ownpath==0) { if(lastmouse->x!=mouse_pos->x || lastmouse->y!=mouse_pos->y)mouseTime=SDL_GetTicks(),drawPath=1; else { if(drawPath) { mT=SDL_GetTicks(); if((mouseTime + mousetimewait)< mT) { motionPath=1; } } } } else if(ownpath==0) { mouseTime = SDL_GetTicks(); drawPath=1; if((lastmouse=calloc(1,sizeof(position))) == NULL) { printf("Kein Speicherplatz vorhanden fuer position\n"); return -1; } } memcpy(lastmouse,mouse_pos,sizeof(position)); } free(mouse_pos); break; case SDL_MOUSEBUTTONDOWN: mouseDown=1; if(DEBUG)printf("MOUSE DOWN\n"); break; case SDL_MOUSEBUTTONUP: if(ownpath>0) { runPath=1; mouseDown=0; break; } mouseDown=0; mouse_pos=calloc(1,sizeof(struct position)); SDL_GetMouseState(&mouse_pos->x,&mouse_pos->y); printf("Cusor-Position x: %d y: %d\n",mouse_pos->x,mouse_pos->y); if(mouse_pos->x > 24 && 226 > mouse_pos->x && mouse_pos->y > 24 && 51 > mouse_pos->y) { aStar(data,&(data->home)); runPath=1; break; } else if(mouse_pos->x > 574 && 776 > mouse_pos->x && mouse_pos->y > 24 && 51 > mouse_pos->y) { aStar(data,&(data->stash)); runPath=1; break; } else { mouse_pos=pixelToGrid(mouse_pos); if(DEBUG)printf("Cusor-Feld x: %d y: %d\n",mouse_pos->x,mouse_pos->y); if(lastmouse==NULL) { if((lastmouse=calloc(1,sizeof(position))) == NULL) { printf("Kein Speicherplatz vorhanden fuer position\n"); return -1; } } memcpy(lastmouse,mouse_pos,sizeof(position)); free(mouse_pos); mouse_pos=NULL; motionPath=1; runPath=1; break; } case SDL_KEYDOWN: switch( event.key.keysym.sym ) { case SDLK_m: if( Mix_PlayingMusic() == 0 ) Mix_PlayMusic( data->ingamemusic, -1); if( Mix_PausedMusic() == 1 ) Mix_ResumeMusic(); else Mix_PauseMusic(); break; case SDLK_s: if (data->soundEnabled) { Mix_HaltChannel(-1); } data->soundEnabled=!data->soundEnabled; break; case SDLK_0: printf ("Music off\n"); Mix_HaltMusic(); Mix_HaltChannel(-1); data->soundEnabled=0; break; case SDLK_ESCAPE: done = ingameMenuStart(screen, data); if (!done) GraphicUpdate(screen, data); break; case SDLK_q: done = 1; break; default: break; } break; default: break; } innerStopTime = SDL_GetTicks(); diffTime=(innerStopTime-innerStartTime); if (MS_FRAMETIME>diffTime)SDL_Delay(MS_FRAMETIME-diffTime); } mT=SDL_GetTicks(); if((mouseTime + mousetimewait)< mT && lastmouse!=NULL && ownpath==0 &&runPath==0) { motionPath=1; } if((motionPath || runPath) && !done) { if(motionPath) { lastmouse->y+=data->verticalScroll; lastmouse->x+=data->horizontalScroll; aStar(data,lastmouse); if(DEBUG)printf("Player-Feld x: %d y: %d\n",data->player.p_pos.x,data->player.p_pos.y); GraphicUpdate(screen, data); free(lastmouse); lastmouse=NULL; motionPath=0; drawPath=0; } if(runPath) { i=1; tmp=data->player.anfang; /*sound*/ if(data->soundEnabled&&data->player.cutSpeed>=2) Mix_PlayChannel(CHAINSAWCHANNEL1, data->chainpause, -1); while(tmp!=NULL&&i) { //Schleife um die Laufbewegung Abzubrechen while ( SDL_PollEvent(&event) ) { switch (event.type) { case SDL_MOUSEBUTTONUP: i=0; break; case SDL_KEYDOWN: switch( event.key.keysym.sym ) { case SDLK_m: printf ("Music on /Pause\n"); if( Mix_PlayingMusic() == 0 ) Mix_PlayMusic( data->ingamemusic, -1); if( Mix_PausedMusic() == 1 ) Mix_ResumeMusic(); else Mix_PauseMusic(); break; case SDLK_s: if (data->soundEnabled) { Mix_HaltChannel(-1); } data->soundEnabled=!data->soundEnabled; break; case SDLK_0: printf ("Music off\n"); Mix_HaltMusic(); Mix_HaltChannel(-1); data->soundEnabled=0; break; case SDLK_ESCAPE: i=0; break; default: break; } default: break; } } tmp=positionListRead(data); if(tmp!=NULL) { if(DEBUG)printf("Position Stack x: %d y: %d\n",tmp->x,tmp->y); if(headPositionUpdate(data,tmp,screen)) { aVal=data->hedgewood[data->player.p_pos.y][data->player.p_pos.x].aStarValue; if(aVal>0) { data->player.currentEnergy-=aVal; if(data->player.currentEnergy<0) { printf("YOU ARE DEAD\nNEW GAME\n"); done=1; i=0; } else { data->hedgewood[data->player.p_pos.y][data->player.p_pos.x].type=6; data->hedgewood[data->player.p_pos.y][data->player.p_pos.x].aStarValue=2; data->player.bp.currentVolume+=data->hedgewood[data->player.p_pos.y][data->player.p_pos.x].currency; if(data->player.bp.currentVolume > data->player.bp.maxVolume) data->player.bp.currentVolume=data->player.bp.maxVolume; data->hedgewood[data->player.p_pos.y][data->player.p_pos.x].currency=0; } } if(!done)GraphicUpdate(screen,data); } else positionListDelete(data); } } /*sound off*/ Mix_HaltChannel(CHAINSAWCHANNEL1); runPath=0; ownpath=0; } positionListDelete(data); } stopTime = SDL_GetTicks(); diffTime = (stopTime-startTime); if (MS_FRAMETIME>diffTime)SDL_Delay(MS_FRAMETIME-diffTime); } addHighscore(screen,data,calcHighscore(data)); return 0; }