/** * \brief initialisation et début du jeu (mode graphique) * \param argc nombre d'arguments shell * \param argv tableau des arguments shell */ int main (int argc, char *argv[]){ // argument test if (argc != 2 ){ printf("usage : main <fichier grille>\n"); return 1; } // X11 display Display *dpy; Window rootwin; Window win; int scr; // init the display if(!(dpy=XOpenDisplay(NULL))) { fprintf(stderr, "ERROR: Could not open display\n"); exit(1); } scr=DefaultScreen(dpy); rootwin=RootWindow(dpy, scr); win=XCreateSimpleWindow(dpy, rootwin, 1, 1, SIZEX, SIZEY+70, 0, BlackPixel(dpy, scr), BlackPixel(dpy, scr)); XStoreName(dpy, win, "jeu de la vie"); XSelectInput(dpy, win, ExposureMask|ButtonPressMask|KeyPressMask); XMapWindow(dpy, win); // create cairo surface cairo_surface_t *cs; cs=cairo_xlib_surface_create(dpy, win, DefaultVisual(dpy, 0), SIZEX, SIZEY+70); // init program data grille g, gc; init_grille_from_file(argv[1],&g); alloue_grille (g.nbl, g.nbc, &gc); // run the event loop debut_jeu(&g, &gc, dpy, cs); // free program data libere_grille(&g); libere_grille(&gc); cairo_surface_destroy(cs); // destroy cairo surface XCloseDisplay(dpy); // close the display return 0; }
int main (int argc, char ** argv) { if (argc != 2 ) { printf("usage : main <fichier grille>"); return 1; } grille g, gc; init_grille_from_file(argv[1],&g); alloue_grille (g.nbl, g.nbc, &gc); affiche_grille(g); debut_jeu(&g, &gc); libere_grille(&g); libere_grille(&gc); return 0; }
/** \brief charge une grille depuis un fichier. * \param fich, nom du fichier de paramètres de la grille */ int main (int argc, char** argv) { grille g; fourmi f; //erreur de paramètres if (argc != 2 ) { printf("usage : fourmi <fichier grille> \n"); printf("fichier grille : localisation du fichier de la grille à utiliser.\n"); return 1; } init_grille(argv[1], &g, &f); boucle_cairo (); libere_grille(&g); return 0; }
void debut_jeu(grille *g, grille *gc){ char c = getchar(); while (c != 'q') // touche 'q' pour quitter { printf("\e[J"); switch (c) { case '\n' : { // touche "entree" pour évoluer evolue(g,gc); efface_grille(*g); affiche_grille(*g); break; } case 'c' : // touche pour changer de mode cyclique/non-cyclique { cyclique=abs(cyclique-1); compte_voisins_vivants=(cyclique==0) ? compte_cyclique : compte_non_cyclique; printf("\n\e[1A"); break; } case 'v' : // touche pour activer/désactiver le veillissement { viellissement=abs(viellissement-1); if (!viellissement) { //effacer les âges d'avant int i,j; for (i=0; i<g->nbl; i++) for (j=0;j<g->nbc; j++) if (est_vivante(i,j,*g)) set_vivante(i,j,*g); else set_morte(i,j,*g); } printf("\n\e[1A"); break; } case 'o' : // touche pour tester si la colonie est oscillante { int periode=0, failsafe=0; grille gtest,gmorte; //g doit rester inchangée alloue_grille (g->nbl, g->nbc, >est); alloue_grille (g->nbl, g->nbc, &gmorte); copie_grille(*g,gtest); do { periode++; failsafe++; evolue(>est,gc); } while (!( compare_grilles(gtest,*g) || compare_grilles(gtest,gmorte) ) && failsafe < MAX_TEST ); //soit on retombe sur la même configuration (-> oscillant) //soit toutes les cellules seront mortes à un moment (-> pas oscillant) if (compare_grilles(gtest,gmorte) || failsafe==MAX_TEST) printf("Pas oscillant."); else printf("Oscillant. Période: %d ",periode); libere_grille(>est); libere_grille(&gmorte); break; } default : { // touche non traitée printf("\n\e[1A"); break; } } c = getchar(); } return; }