void cleanExit(World* world, Interface* interface){ cleanWorld(world); cleanInterface(interface); free(BUTTON_OF_MENU.regles); free(BUTTON_OF_MENU.choix_carte); free(BUTTON_OF_MENU.jouer); free(BUTTON_OF_MENU.close_rules); //Vidage VRAM clearMenuTextures(); clearGameGraphics(); freeListComplete(BUTTON_OF_MENU.lstMapButton); freeListComplete(BUTTON_OF_MENU.lstMapName); freeListComplete(BUTTON_OF_MENU.lstMapTextureIndex); TTF_Quit(); SDL_Quit(); }
void muerte() { g_muerte = true; g_posxPersonaje = ANCHO_MUNDO / 2; cleanWorld(); }
int main(int argc, char* argv[]) { int width = 1024, height = 768; if(argc == 5 && strcmp(argv[2], "-r") == 0 ){ width = atoi(argv[3]); height = atoi(argv[4]); } else if(argc == 4 && strcmp(argv[1], "-r") == 0 ){ width = atoi(argv[2]); height = atoi(argv[3]); } /*Initialisation SDL, OpenGL etc */ if( initWindow(width, height) == EXIT_FAILURE){ perror("Impossible d'initialiser la fenêtre SDL, le programme va fermer.\n"); exit(-1); } /* initialisation de SDL_TTF*/ if(TTF_Init()== -1){ printf("Error loading TTF: %s\n",TTF_GetError()); exit(1); } bool askedForQuit = false; World world; world.towersList = NULL; world.map.name = NULL; world.map.tabXYConstruct = NULL; world.map.image = NULL; world.map.pathNodeList = NULL; Interface interface; interface.lstButtons = NULL; char* mapName= NULL; BUTTON_OF_MENU.lstMapName = NULL; BUTTON_OF_MENU.lstMapButton = NULL; BUTTON_OF_MENU.lstMapTextureIndex = NULL; bool play = false; List* lstMaps = createEmptyList(); readDirectory(lstMaps); /* selection d'une carte en ligne de commande*/ if (argc >= 2 && strcmp(argv[1], "-r") != 0){ char* curMap = NULL; while( (curMap = (char*) nextData(lstMaps)) != NULL){ if (strcmp(argv[1],curMap)==0){ mapName = (char*) malloc(strlen(argv[1])*sizeof(char)); if(mapName == NULL){ fprintf(stderr, "Erreur fatale : impossible d'allouer la mémoire nécessaire.\n"); exit(EXIT_FAILURE); } strcpy(mapName,argv[1]); play = true; break; } } if(! play) fprintf(stderr, "Erreur le nom donné en paramètre ne correspond à aucun fichier map\n"); } freeListComplete(lstMaps); /*-------------- GESTION DU MENU --------------------*/ do{ interface.lstButtons = NULL; /* chargement des polices */ int playIsPush = 0; int menuOpen = 0; int aideOpen = 0; initMenuGraphics(); /* ouverture du répertoire data */ while(!play && askedForQuit == false) { /* Récupération du temps au début de la boucle */ Uint32 startTime = SDL_GetTicks(); /* Placer ici le code de dessin du menu */ drawMenu(&menuOpen,&aideOpen,&playIsPush,mapName); /* Echange du front et du back buffer : mise à jour de la fenêtre */ SDL_GL_SwapBuffers(); /* Renvoie une chaine de caractère contenant le nom du fichier ITD choisi par l'utilisateur ou NULL si rien n'a encore été choisi */ askedForQuit = handleMenuActions(&mapName,&playIsPush, &menuOpen,&aideOpen); if(playIsPush == 2) play = true; /* Calcul du temps écoulé */ Uint32 elapsedTime = SDL_GetTicks() - startTime; /* Si trop peu de temps s'est écoulé, on met en pause le programme */ if(elapsedTime < FRAMERATE_MILLISECONDS) { SDL_Delay(FRAMERATE_MILLISECONDS - elapsedTime); } } /*-------------- GESTION DU JEU --------------------*/ bool gameFinished = false; //Surtout à appeler APRES avoir initialisé la SDL char mapPath[50] = "data/"; float width = .15;//10% de largeur float height = 1.; //Toute la hauteur float positionX = 0.85; //A 90% de la largeur float positionY = .0; //A 100% de la hauter if(!askedForQuit){ strcat(mapPath, mapName); world = initWorld(mapPath); initGameGraphics(); GAME_TEXTURES_ID.MAP_ID = makeTextureFromSurface(world.map.image); //Initialisation interface interface = initGameInterface(width, height, positionX, positionY); startWorld(&world); } while(!gameFinished && !askedForQuit) { /* Récupération du temps au début de la boucle */ Uint32 startTime = SDL_GetTicks(); /* On tente un nouveau cycle de tours de jeu si besoin. Le temps est géré par la fonction. La plupart du temps plusieurs tours de jeu sont joués d'affilé. */ worldNewStep(&world); drawWorld(&world); drawInterface(&interface, &world); /* Calcul du temps écoulé, si temps < 10 ms, on ne passe pas au tour suivant. */ Uint32 elapsedTime = SDL_GetTicks() - startTime; /* Si trop peu de temps s'est écoulé, on ne dessine rien. */ if(elapsedTime < FRAMERATE_MILLISECONDS) { /* Echange du front et du back buffer : mise à jour de la fenêtre */ SDL_GL_SwapBuffers(); SDL_Delay(FRAMERATE_MILLISECONDS - elapsedTime); } /* Boucle traitant les evenements */ askedForQuit = handleGameActions(&world, &interface, &gameFinished); } play = false; mapName = NULL; cleanWorld(&world); cleanInterface(&interface); }while(! askedForQuit); /* Liberation des ressources */ cleanExit(&world, &interface); return EXIT_SUCCESS; }
int main(int argc, char **argv) { int i, j; int x, y, size; int w_breeding, s_breeding, w_starvation, gen_num; char type_code; FILE * input_file; w_number = 0; if(argc <= 5) { printf("ERROR: Expected 5 arguments provided %d.\n", argc); printf("Expected:\n./wolves-squirrels-serial <InputFile> <WolfBreedingPeriod> <SquirrelBreedingPeriod> <WolfStarvationPerior> <Generations>\n"); return -1; } w_breeding = atoi(argv[2]); s_breeding = atoi(argv[3]); w_starvation = atoi(argv[4]); gen_num = atoi(argv[5]); input_file = fopen(argv[1], "r"); if(input_file == NULL) { printf("ERROR: A valid input file is expected. %s is not a valid file.\n", argv[1]); return -1; } fscanf(input_file, "%d", &size); initWorld(size); while(fscanf(input_file, "%d %d %c", &x, &y, &type_code) != EOF) { world[0][x][y].type = type_code; world[1][x][y].type = type_code; if(world[0][x][y].type == wolf) { world[0][x][y].breeding_period = w_breeding; world[0][x][y].starvation_period = w_starvation; } else if(world[0][x][y].type == squirrel || world[0][x][y].type == squirrel_on_tree) { world[0][x][y].breeding_period = s_breeding; } } fclose(input_file); #ifdef VERBOSE printf("INITIAL WORLD - w_number = %d\n", w_number); printWorldDetailed(size); printf("\n"); #endif /* Generate */ while(gen_num != 0) { cleanWorld(size); /* 1st sub-generation - RED */ for(i=0; i<size; i++) { for(j = i%2 == 0 ? 0 : 1 ; j<size; j+=2) { computeCell(i, j, s_breeding, w_breeding, w_starvation, size); } } /* 2nd sub-generation */ for(i=0; i<size; i++) { for(j = i%2 == 0 ? 1 : 0 ; j<size; j+=2) { computeCell(i, j, s_breeding, w_breeding, w_starvation, size); } } #ifdef VERBOSE printf("\n\nIteration %d:\n", gen_num); printWorldDetailed(size); printf("\n"); #endif gen_num--; w_number = (w_number+1) % 2; } /* Output */ printWorldFormatted(size); return 0; }