void main_loop(void) { Doubly_linked_node *iterator; int direction, dx, dy, count; int x, y; uint32_t last_time; uint32_t current_time; uint32_t ellapsed_time; uint32_t start_time; Point *save_eye; float speed; last_time = SDL_GetTicks(); while (!conf->key[SDLK_ESCAPE] && !conf->quit) { start_time = SDL_GetTicks(); save_eye = point_new(conf->eye->x, conf->eye->y, conf->eye->z); speed = (conf->free_fly) ? 5 : 0.5; update_event(); if (!conf->viewMode && conf->key[SDLK_F1]) { conf->key[SDLK_F1] = 0; conf->free_fly = !conf->free_fly; if (!conf->free_fly) { save_eye->x = CELL_SIZE / 2; save_eye->y = CELL_SIZE / 2; save_eye->z = CHARACTER_SIZE; } else { conf->jump_duration = 0; } } if (!conf->viewMode && (conf->key[SDLK_LSHIFT] || conf->key[SDLK_LALT])) { if ( conf->jump_duration == 0 ) { /* j'ai du mal a courir quand je saute */ speed = (conf->free_fly) ? 7.51337 : 0.91337; } } if (!conf->viewMode && conf->key[SDLK_LCTRL]) { speed = (conf->free_fly) ? 0.1 : 0.3; } if (conf->key[SDLK_F2]) { conf->key[SDLK_F2] = 0; conf->time = !conf->time; } if (conf->key[SDLK_F3]) { conf->key[SDLK_F3] = 0; conf->display = !conf->display; } if (!conf->viewMode && conf->key[SDLK_F4]) { conf->key[SDLK_F4] = 0; conf->quadTreeView = !conf->quadTreeView; } if (conf->key[SDLK_F5]) { conf->key[SDLK_F5] = 0; if (!strcmp(conf->music, "music/music2.mp3")) { conf->music = "music/music.mp3"; music_new(); } else if (!strcmp(conf->music, "music/music.mp3")) { conf->music = "music/music2.mp3"; music_new(); } } if (!conf->viewMode && (conf->key[SDLK_UP] || conf->key[SDLK_z])) { forward_move(save_eye, speed); } if (!conf->viewMode && (conf->key[SDLK_DOWN] || conf->key[SDLK_s])) { backward_move(save_eye, speed); } if (!conf->viewMode && (conf->key[SDLK_RIGHT] || conf->key[SDLK_d])) { right_move(save_eye, speed); } if (!conf->viewMode && (conf->key[SDLK_LEFT] || conf->key[SDLK_q])) { left_move(save_eye, speed); } if (!conf->viewMode && conf->key[SDLK_a]) { conf->key[SDLK_a] = 0; conf->theta += 180; } if (!conf->viewMode && conf->key[SDLK_r]) { conf->key[SDLK_r] = 0; conf->life = MAX_HEALTH; save_eye->x = CELL_SIZE / 2; save_eye->y = CELL_SIZE / 2; save_eye->z = CHARACTER_SIZE; portals->bleu->actif = 0; portals->orange->actif = 0; conf->timer = SDL_GetTicks(); } if (conf->key[SDLK_KP2] || (conf->key[SDLK_n])) { if (conf->free_fly) { save_eye->z -= 3; } else if (conf->viewMode) { save_eye->z -= 10; } } if (conf->key[SDLK_KP8] || (conf->key[SDLK_SPACE])) { if (!conf->free_fly && !conf->viewMode) { conf->key[SDLK_KP8] = 0; conf->key[SDLK_SPACE] = 0; if ( !conf->viewMode && conf->jump_duration == 0 ) { conf->jump_duration = 120; } } else if (conf->free_fly){ save_eye->z += 3; } else { save_eye->z += 10; } } conf->eye->z += jump(save_eye); if ( conf->key[SDLK_p] ) { conf->key[SDLK_p] = 0; if(Mix_PausedMusic() == 1) { Mix_ResumeMusic(); } else { Mix_PauseMusic(); } } if ( conf->key[SDLK_PLUS] || conf->key[SDLK_m] || conf->key[SDLK_KP_PLUS] ) { change_volume(CHANG_VOL); } if ( conf->key[SDLK_MINUS] || conf->key[SDLK_l] || conf->key[SDLK_KP_MINUS] ) { change_volume(-CHANG_VOL); } if (!conf->mousebutton[SDL_BUTTON_LEFT] && !conf->mousebutton[SDL_BUTTON_RIGHT]) { conf->shoot = 0; } if (!conf->viewMode && conf->mousebutton[SDL_BUTTON_LEFT]) { conf->mousebutton[SDL_BUTTON_LEFT] = 0; conf->mousebutton[SDL_BUTTON_RIGHT] = 0; conf->shoot = 1; } if (!conf->viewMode && conf->mousebutton[SDL_BUTTON_RIGHT]) { conf->mousebutton[SDL_BUTTON_LEFT] = 0; conf->mousebutton[SDL_BUTTON_RIGHT] = 0; conf->shoot = 2; } if (!conf->viewMode && portals->orange->actif && portals->bleu->actif ) { if ( abs(save_eye->x - portals->bleu->portail->x ) < TRIGGER_DISTANCE && abs(save_eye->y - portals->bleu->portail->y) < TRIGGER_DISTANCE) { save_eye->x = portals->orange->portail->x - (sin(portals->orange->rotation) * PUSH_DISTANCE); save_eye->y = portals->orange->portail->y + (cos(portals->orange->rotation) * PUSH_DISTANCE); conf->theta += 180 + ( portals->orange->rotation - portals->bleu->rotation); } else if ( abs(save_eye->x - portals->orange->portail->x ) < TRIGGER_DISTANCE && abs(save_eye->y - portals->orange->portail->y) < TRIGGER_DISTANCE) { save_eye->x = portals->bleu->portail->x - (sin(portals->bleu->rotation) * PUSH_DISTANCE); save_eye->y = portals->bleu->portail->y + (cos(portals->bleu->rotation) * PUSH_DISTANCE); conf->theta += 180 + ( portals->bleu->rotation - portals->orange->rotation); } } if (COORD((int)(save_eye->x / CELL_SIZE), (int)(save_eye->y / CELL_SIZE)) != COORD((int)((conf->eye)->x / CELL_SIZE), (int)((conf->eye)->y / CELL_SIZE))) { iterator = mwl->last; while (1) { if ((iterator->object)->type == MOVING_WALL) { x = (((int)((iterator->object)->anchor)->x ) / CELL_SIZE); y = (((int)((iterator->object)->anchor)->y ) / CELL_SIZE); count = 0; direction = rand() % 4; while (count < 4) { dx = 0; dy = 0; switch (direction) { case 0: dx = 1; break; case 1: dy = 1; break; case 2: dx = -1; break; default: dy = -1; break; } if(COORD((x+dx),(y+dy)) == COORD(((int)save_eye->x / CELL_SIZE), ((int)save_eye->y / CELL_SIZE)) || !IS_PLAYABLE(COORD((x+dx),(y+dy))) || (dx == 1 && END_RIGHT(COORD(x,y))) || (dy == 1 && END_TOP(COORD(x,y))) || (dx == -1 && END_LEFT(COORD(x,y))) || (dy == -1 && END_BOTTOM(COORD(x,y))) ) { direction = (direction + 1) % 4; ++count; } else { if(laby->matrix[COORD(x,y)] == SPIKES_MW) { laby->matrix[COORD(x,y)] = SPIKES; } else if (laby->matrix[COORD(x,y)] == MOVING_WALL) { laby->matrix[COORD(x,y)] = PASS; } if(laby->matrix[COORD((x+dx),(y+dy))] == PASS) { laby->matrix[COORD((x+dx),(y+dy))] = MOVING_WALL; } else if (laby->matrix[COORD((x+dx),(y+dy))] == SPIKES) { laby->matrix[COORD((x+dx),(y+dy))] = SPIKES_MW; } ((iterator->object)->anchor)->x += (dx * CELL_SIZE); ((iterator->object)->anchor)->y += (dy * CELL_SIZE); count = 4; } } } if (iterator->next != NULL) { iterator = iterator->next; } else { break; } } } /*fprintf(stderr, "%d %d\n", conf->mousex - SCREEN_MID_WIDTH, conf->mousey - SCREEN_MID_HEIGHT);*/ if (!conf->viewMode && (( save_eye->x > 2 && save_eye->y > 2 && save_eye->x < (CELL_SIZE * WIDTH) - 2 && save_eye->y < (CELL_SIZE * HEIGHT) - 2 /*&& save_eye->z <= CHARACTER_SIZE && save_eye->z > CHARACTER_SIZE - 3*/ && IS_PLAYABLE(COORD((int)(save_eye->x / CELL_SIZE),(int)(save_eye->y / CELL_SIZE))) && IS_PLAYABLE(COORD((int)((save_eye->x + 2) / CELL_SIZE),(int)((save_eye->y) / CELL_SIZE))) && IS_PLAYABLE(COORD((int)((save_eye->x) / CELL_SIZE),(int)((save_eye->y + 2) / CELL_SIZE))) && IS_PLAYABLE(COORD((int)((save_eye->x - 2) / CELL_SIZE),(int)((save_eye->y) / CELL_SIZE))) && IS_PLAYABLE(COORD((int)((save_eye->x) / CELL_SIZE),(int)((save_eye->y - 2) / CELL_SIZE))) ) || conf->free_fly == 1 )) { conf->eye->x = save_eye->x; conf->eye->y = save_eye->y; conf->eye->z = save_eye->z; } /* Mouse motion */ if(!conf->viewMode) { conf->theta -= (conf->mousex - SCREEN_MID_WIDTH) * SENSITIVITY; conf->phi -= (conf->mousey - SCREEN_MID_HEIGHT) * SENSITIVITY; SDL_WarpMouse(SCREEN_MID_WIDTH, SCREEN_MID_HEIGHT); modify_direction(); change_center(); } else { conf->eye->x = conf->center->x + CELL_SIZE * WIDTH * cos(conf->theta * M_PI / 180); conf->eye->y = conf->center->y + CELL_SIZE * HEIGHT * sin(conf->theta * M_PI / 180); conf->theta += 0.5; if (conf->theta >= 360) { conf->theta = 0; } } if (conf->life <= 0) { fprintf(stderr," .,---.\n"); fprintf(stderr," ,/XM#MMMX;,\n"); fprintf(stderr," -%%##########M%%,\n"); fprintf(stderr," -@######%% $###@=\n"); fprintf(stderr," .,--, -H#######$ $###M:\n"); fprintf(stderr," ,;$M###MMX; .;##########$;HM###X=\n"); fprintf(stderr," ,/@##########H= ;################+\n"); fprintf(stderr,"-+#############M/, %%##############+\n"); fprintf(stderr,"%%M###############= /##############:\n"); fprintf(stderr,"H################ .M#############;.\n"); fprintf(stderr,"@###############M ,@###########M:.\n"); fprintf(stderr,"X################, -$=X#######@:\n"); fprintf(stderr,"/@##################%%- +######$-\n"); fprintf(stderr,".;##################X .X#####+,\n"); fprintf(stderr," .;H################/ -X####+.\n"); fprintf(stderr," ,;X##############, .MM/\n"); fprintf(stderr," ,:+$H@M#######M#$- .$$=\n"); fprintf(stderr," .,-=;+$@###X: ;/=.\n"); fprintf(stderr," .,/X$; .::,\n"); fprintf(stderr," ., ..\n"); fprintf(stderr,"Haw Haw ! You lose !\n"); conf->quit = 1; } if (IS_EXIT(COORD((int)(conf->eye->x / CELL_SIZE), (int)(conf->eye->y / CELL_SIZE))) && !conf->free_fly) { if (!conf->win) { fprintf(stderr, "\n #,\n"); fprintf(stderr, " ###\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ####\n"); fprintf(stderr, " |\n"); fprintf(stderr, " #####\n"); fprintf(stderr, " ######\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ## ##########\n"); fprintf(stderr, " ## #############\n"); fprintf(stderr, " ####### ###############\n"); fprintf(stderr, " #############################\n"); fprintf(stderr, " .###################################\n"); fprintf(stderr, " #####################################;\n"); fprintf(stderr, " ## ##.\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " #####################################\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " ## ###\n"); fprintf(stderr, " ##### #####\n"); fprintf(stderr, " ### ##################################### ###\n"); fprintf(stderr, " ### ## ## ###\n"); fprintf(stderr, " ## ## ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ## ##\n"); fprintf(stderr, " ## ##################################### ##\n"); fprintf(stderr, " ## ##\n"); fprintf(stderr, " #### ####\n"); fprintf(stderr, " ###### ######\n"); fprintf(stderr, " ###############################\n"); fprintf(stderr, "The cake was not a lie !\n"); } if (strcmp(conf->music, "music/music3.mp3")) { conf->music = "music/music3.mp3"; music_new(); } conf->win = 1; } /* Display with FPS care */ current_time = SDL_GetTicks(); ellapsed_time = current_time - last_time; last_time = current_time; display(); ellapsed_time = SDL_GetTicks() - start_time; if (ellapsed_time < 25) { SDL_Delay(25 - ellapsed_time); } point_free(save_eye); } }
void turtle_t::exec(turtle_com_t *com) { if (com->cname==F) { turtle_fwd_t* fcom = dynamic_cast<turtle_fwd_t*>(com); if (fcom) forward(fcom->dist); } else if (com->cname==B) { turtle_bck_t* bcom = dynamic_cast<turtle_bck_t*>(com); if (bcom) back(bcom->dist); } else if (com->cname==L) { turtle_lft_t* lcom = dynamic_cast<turtle_lft_t*>(com); if (lcom) turn_left(lcom->angl); } else if (com->cname==R) { turtle_rht_t* rcom = dynamic_cast<turtle_rht_t*>(com); if (rcom) turn_right(rcom->angl); } else if (com->cname==MF) { turtle_mfwd_t* mfcom = dynamic_cast<turtle_mfwd_t*>(com); if (mfcom) forward_move(mfcom->dist); } else if (com->cname==MB) { turtle_mbck_t* mbcom = dynamic_cast<turtle_mbck_t*>(com); if (mbcom) backward_move(mbcom->dist); } else if (com->cname==CLS) { turtle_cls_t* clscom = dynamic_cast<turtle_cls_t*>(com); if (clscom) clear(); } else if (com->cname==RESET) { turtle_rst_t* rstcom = dynamic_cast<turtle_rst_t*>(com); if (rstcom) reset(); } else if (com->cname==COL) { turtle_col_t* colcom = dynamic_cast<turtle_col_t*>(com); if (colcom) set_col(colcom->r, colcom->g, colcom->b); } else if (com->cname==BGCOL) { turtle_bgcol_t* bgcolcom = dynamic_cast<turtle_bgcol_t*>(com); if (bgcolcom) set_bgcol(bgcolcom->r, bgcolcom->g, bgcolcom->b); } else if (com->cname==SCALE) { turtle_scale_t* scalecom = dynamic_cast<turtle_scale_t*>(com); if (scalecom) scale(scalecom->s); } else if (com->cname==PAUSE) { turtle_pause_t* pausecom = dynamic_cast<turtle_pause_t*>(com); if (pausecom) pause(pausecom->t); } else if (com->cname==REPEAT) { turtle_rep_t *repcom = dynamic_cast<turtle_rep_t*>(com); if (repcom) { unsigned int times = repcom->times; turtle_com_list_t sublist = repcom->replist; repeat(times, sublist); } } else if ((com->cname==ENDREP) || (com->cname==END) || (com->cname==BEGIN)) { //These commands are place holders and used for program structure //But no execution is necessary - generate a NoOP ; } else { std::cerr<<"Unknown Command: Ignoring"<<std::endl; exit(-1); } }