// 3.4 Attend que l'utilisateur clique sur le bouton gauche // Renvoie les coordonnées du point cliqué // Instruction bloquante POINT wait_clic() { int encore = 1; POINT P; SDL_Event event; P.x = 0; P.y = 0; #ifdef EN_LOCAL // A ne mettre que si on est en local, sur les ordi des étudiants, c'est trop lent #ifdef SDL_TTF_OK POINT E,F; char S[16]; E.x = WIDTH - 55; E.y = 15; F.x = WIDTH; F.y = 0; #endif #endif while (SDL_WaitEvent(&event) && encore) { /* Si l'utilisateur clique avec la souris */ if ((event.type == SDL_MOUSEBUTTONDOWN) && (event.button.button == SDL_BUTTON_LEFT)) { encore=0; P.x = event.button.x; P.y = HEIGHT-event.button.y; } /* Si l'utilisateur déplace la souris */ if (event.type == SDL_MOUSEMOTION) { #ifdef EN_LOCAL // A ne mettre que si on est en local, sur les ordi des étudiants, c'est trop lent #ifdef SDL_TTF_OK if (police[10]) { draw_fill_rectangle(E,F,noir); sprintf(S,"%4d %4d",event.motion.x,HEIGHT - event.motion.y); aff_pol(S,10,E,gris); affiche_all(); } #endif #endif printf("%cEn attente de clic ... %4d %4d %c",13,event.motion.x,HEIGHT - event.motion.y,13); fflush(stdout); } /* Si l'utilisateur a demandé à fermer la fenêtre, on quitte */ if (event.type == SDL_QUIT) exit(0); } #ifdef EN_LOCAL // A ne mettre que si on est en local, sur les ordi des étudiants, c'est trop lent #ifdef SDL_TTF_OK aff_pol(S,10,E,noir); //draw_fill_rectangle(E,F,jaune); affiche_all(); #endif #endif printf("%cClic en %4d %4d \n",13,P.x,P.y); ___MOUSE_POSITION = P; return P; }
/*Affiche le score et le niveau actuel*/ void draw_score(int score, int level) { POINT p1; char score_c[50]; sprintf(score_c, "Score : %d", score); p1.x=WIDTH+10; p1.y=150; aff_pol(score_c, FONT_SIZE, p1, blanc); sprintf(score_c, "Level : %d", level+1); p1.x=WIDTH+10; p1.y=200; aff_pol(score_c, FONT_SIZE, p1, blanc); }
/*Rajouter le centrage des éléments * Vérifier que tout entre dans l'ecran * sinon réduire la police ou faire * des colonnes*/ int draw_menu(Menu menu) { int i; Input in; memset(&in,0,sizeof(in)); while(!menu.available[menu.selection]) menu.selection=(menu.selection+1)%(menu.nb_options); for(i=0; i<menu.nb_options; i++) if(!menu.available[i]) menu.couleur[i]=gris; while(!in.quit) { menu.couleur[menu.selection]=blanc; UpdateEvents(&in); if(in.key[SDLK_RETURN]) { in.key[SDLK_RETURN]=0; return menu.selection; } else if(in.key[SDLK_DOWN]) { in.key[SDLK_DOWN]=0; menu.selection=(menu.selection+1)%(menu.nb_options); while(!menu.available[menu.selection]) menu.selection=(menu.selection+1)%(menu.nb_options); } else if(in.key[SDLK_UP]) { in.key[SDLK_UP]=0; if(!(menu.selection)) menu.selection=menu.nb_options-1; else menu.selection=(menu.selection-1)%(menu.nb_options); while(!menu.available[menu.selection]) { if(!(menu.selection)) menu.selection=menu.nb_options-1; else menu.selection=(menu.selection-1)%(menu.nb_options); } } SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); menu.couleur[menu.selection]=jaune; menu.p1.x=100; menu.p1.y=25; aff_pol(menu.title, 50, menu.p1, jaune); menu.p1.x=300; menu.p1.y=100; menu.pos.x=menu.p1.x-60; menu.pos.y=menu.p1.y+(menu.selection*menu.space)-5; SDL_BlitSurface(menu.img, NULL, screen, &(menu.pos)); for(i=0; i<menu.nb_options; i++) { aff_pol(menu.options[i], menu.font_size, menu.p1, menu.couleur[i]); menu.p1.y=menu.p1.y+menu.space; } SDL_Flip(screen); } return menu.nb_options-1; }
void afficheTexte(char* texte, POINT HG, COULEUR c) { //Affiche du texte centré et de taille dynamique int taille = 0, longeur = strlen(texte); if (longeur <= 13) { HG.x += (13 - longeur)*6; taille = 25; } else if (longeur <= 20) { HG.x += (20 - longeur)*2; taille = 20; } else if (longeur <= 25) { HG.x += (25 - longeur)/3; taille = 15; } else if (longeur <= 30) { HG.x += (30 - longeur)/5; taille = 10; } else { HG.x += (40 - longeur)/10; taille = 12; } aff_pol(texte, taille, HG, c); affiche_all(); }
// 5.3 Affiche dans la fenêtre graphique comme dans une fenêtre // shell mais sans déroulement. Commence en haut et // se termine en bas. void write_text(char *a_ecrire) { #ifdef SDL_TTF_OK static char s[256]; static int fin = 0; static int premier = 1; static POINT position; if (premier) {position.x = 10; position.y = HEIGHT; premier = 0;} if (police_ok) { // printf("verd\n"); if (a_ecrire) { if (fin+strlen(a_ecrire) > 256) {fprintf(stderr,"Chaine de caractère trop longue\n"); return;} strcat(s+fin,a_ecrire); // printf("a_ecr : #%s#\n",s); aff_pol(s,20,position,blanc); fin += strlen(a_ecrire); s[fin] = '\0'; } else { position.y -= 20; fin = 0; s[fin] = '\0'; } } else { if (a_ecrire) {printf("%s",a_ecrire); fflush(stdout);} else printf("\n"); } #else if (a_ecrire) {printf("%s",a_ecrire); fflush(stdout);} else printf("\n"); #endif }
// 3.3 Attend que l'on tape Echap et quitte // Instruction bloquante void wait_escape() { int display = 1; SDL_Event event; POINT p; char *texte = "Appuyer sur Echap pour terminer"; int taille = 20; p.x = WIDTH/2 - largeur_texte(texte,taille)/2; p.y = hauteur_texte(texte,taille); aff_pol(texte,taille,p,gris); affiche_all(); while (SDL_WaitEvent(&event) && display) { /* Si l'utilisateur a demandé à fermer la fenêtre, on quitte */ if (event.type == SDL_QUIT) exit(0); /* Si l'utilisateur a appuyé sur une touche */ if (event.type == SDL_KEYDOWN) { switch (event.key.keysym.sym) { case SDLK_ESCAPE : display=0; break; default : break; } } } /* Fermeture de la police */ #ifdef SDL_TTF_OK int i; if (police_ok) for (i=0;i<256;i++) if (police[i]) TTF_CloseFont(police[i]); TTF_Quit(); #endif SDL_Quit(); }
/*Quand tu perds*/ void lost_menu() { POINT p1; p1.x=200; p1.y=250; aff_pol("YOU LOST !", 75, p1, jaune); SDL_Flip(screen); SDL_Delay(3000); }
void affiche_legende() { POINT p; char *s = "Fermer la fenêtre pour terminer"; p.x = WIDTH - largeur_texte(s,15); p.y = HEIGHT; aff_pol(s,15,p,gris); }
void pol_style(int style) { POINT p; p.x = -10; p.y = -10; aff_pol("",10,p,black); // Appel de aff_pol pour fixer la valeur de ___pol if ((style < 0x00) || (0x04 < style)) style = 0x00; TTF_SetFontStyle(___pol,style); }
int hauteur_texte(char *texte, int taille) { int w,h; POINT p; p.x = -10; p.y = -10; aff_pol("",taille,p,black); // Appel de aff_pol pour fixer la valeur de ___pol TTF_SizeText(___pol,texte,&w,&h); return h; }
/*Affiche en début de niveau le niveau actuel * et un petit compteur de 3 sec*/ void play_menu(int level) { char tmp[16], time[4]; POINT p1,p2; int counter=3; sprintf(tmp, "LEVEL %d", level+1); p1.x=320; p1.y=200; p2.x=380; p2.y=260; while(counter) { SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); sprintf(time, "%d", counter); aff_pol(tmp, 50, p1, jaune); aff_pol(time, 50, p2, jaune); SDL_Flip(screen); SDL_Delay(1000); counter--; } }
void new_file_menu(char *new_file) { POINT p1; Input in; memset(&in,0,sizeof(in)); new_file[0]='\0'; while(!in.quit) { UpdateEvents(&in); SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); if(in.quit) exit(EXIT_FAILURE); if(in.key[SDLK_RETURN]) return; p1.x=100; p1.y=50; aff_pol("TYPE NEW LEVEL NAME", 50, p1, jaune); print_key(new_file, &in, 32); //permet de taper le nom du niveau p1.x=50; p1.y=200; aff_pol(new_file, 30, p1, jaune); SDL_Flip(screen); } }
void afficheBoutonRecommencer() { POINT P1 = {730,530}; POINT P2 = {860,580}; draw_fill_rectangle(P1,P2,couleur_RGB(38,196,236)); draw_rectangle(P1,P2,black); P1.x = 737; P1.y = 566; aff_pol("RECOMMENCER",15,P1,noir); affiche_all(); }
void afficheBoutonRecommencerSombre() { POINT P1 = {730,530}; POINT P2 = {860,580}; draw_fill_rectangle(P1,P2,couleur_RGB(25,180,220)); draw_rectangle(P1,P2,black); P1.x = 737; P1.y = 566; aff_pol("RECOMMENCER",15,P1,noir); affiche_all(); attendre(150); }
// 3.5 Attend que l'on clique et renvoie dans button le bouton cliqué : // *button vaut soit 'G' (pour Gauche), soit 'M' (pour milieu), // soit 'D' (pour Droit) en fonction du bouton cliqué // Instruction bloquante POINT wait_clic_GMD(char *button) { int encore = 1; POINT P; SDL_Event event; P.x = 0; P.y = 0; #ifdef SDL_TTF_OK POINT E,F; char S[16]; E.x = WIDTH - 55; E.y = 15; F.x = WIDTH; F.y = 0; #endif affiche_all(); printf("Cliquer dans la fenêtre..."); while (SDL_WaitEvent(&event) && encore) { /* Si l'utilisateur a demandé à fermer la fenêtre, on quitte */ if (event.type == SDL_QUIT) exit(0); /* Si l'utilisateur a cliqué avec la souris */ if ((event.type == SDL_MOUSEBUTTONDOWN)) { #ifdef SDL_TTF_OK if (!police[10]) { draw_fill_rectangle(E,F,noir); sprintf(S,"%4d %4d",event.motion.x,HEIGHT - event.motion.y); aff_pol(S,10,E,gris); affiche_all(); } #endif printf("%cCliquer dans la fenêtre ... %4d %4d %c",13,event.motion.x,HEIGHT - event.motion.y,13); fflush(stdout); encore=0; P.x = event.button.x; P.y = HEIGHT-event.button.y; if (event.button.button == SDL_BUTTON_LEFT) *button = 'G'; if (event.button.button == SDL_BUTTON_MIDDLE) *button = 'M'; if (event.button.button == SDL_BUTTON_RIGHT) *button = 'D'; } } #ifdef SDL_TTF_OK draw_fill_rectangle(E,F,noir); #endif printf("Clic '%c' en %4d %4d \n",*button,P.x,P.y); ___MOUSE_POSITION = P; return P; }
/*Ceci est un menu special * Il ne suit pas les memes * regles que les autres menus * Accessoirement c'est tres moche*/ void options_menu(config *cfg) { config tmp = *cfg; int i, j=0, nb=9, couleur[nb], available[nb], selection=0; SDL_Surface *pac[2], *ghost[2], *fleche[2]; SDL_Rect pos; POINT p1; Input in; char title[64]; char options[nb][64]; for(i=0; i<nb; i++) { available[i]=1; couleur[i]=blanc; } pac[0] = IMG_Load("image/pacman/2.png"); pac[1] = IMG_Load("image/pacman/3.png"); ghost[0] = IMG_Load("image/ghosts/2.png"); ghost[1] = IMG_Load("image/ghosts/3.png"); fleche[0] = IMG_Load("image/menu/triangler.png"); fleche[1] = IMG_Load("image/menu/trianglel.png"); if(pac == NULL || ghost == NULL) { fprintf(stderr, "Erreur loading pngs in >>options_menu()<<\n"); exit(EXIT_FAILURE); } memset(&in,0,sizeof(in)); strcpy(title, "OPTIONS"); strcpy(options[0], "Nb players"); sprintf(options[1], "%d", cfg->nb_players); strcpy(options[2], "Player 1"); strcpy(options[3], "Player 2"); strcpy(options[4], "Sound"); strcpy(options[5], "Reset score"); strcpy(options[6], "Reset"); strcpy(options[7], "Cancel"); strcpy(options[8], "Save"); available[1]=0; available[4]=0; if(cfg->nb_players==0) { available[2]=0; available[3]=0; } else if(cfg->nb_players==1) available[3]=0; DELAY=60; while(!in.quit) { SDL_Delay(DELAY); for(i=0; i<nb; i++) { if(!available[i] && i!=1) couleur[i]=gris; else couleur[i]=blanc; } couleur[selection]=blanc; UpdateEvents(&in); if(in.key[SDLK_ESCAPE]) exit(EXIT_SUCCESS); else if(in.key[SDLK_RETURN]) { in.key[SDLK_RETURN]=0; if(selection==nb-4) //reset score { reset_score("data/results.txt"); reset_score("data/survivor.txt"); } if(selection==nb-3) { load_default_config(cfg); //default config sprintf(options[1], "%d", cfg->nb_players); available[2]=1; available[3]=0; } else if(selection==nb-2) //Cancel { *cfg=tmp; return; } else if(selection==nb-1) //Save { if(cfg->nb_players==2) { if(cfg->players[0].character == PACMAN && cfg->players[1].character == PACMAN) fprintf(stderr, "Only one pacman is allowed!\n"); //Should not happen else return; } else return; } } else if(in.key[SDLK_DOWN]) { in.key[SDLK_DOWN]=0; selection=(selection+1)%nb; while(!available[selection]) selection=(selection+1)%nb; } else if(in.key[SDLK_UP]) { in.key[SDLK_UP]=0; if(!(selection)) selection=nb-1; else selection=(selection-1)%nb; while(!available[selection]) { if(!selection) selection=nb-1; else selection=(selection-1)%nb; } } else if(in.key[SDLK_LEFT]) { in.key[SDLK_LEFT]=0; if(selection==0) { if(cfg->nb_players>0) { available[cfg->nb_players+1]=0; cfg->nb_players=cfg->nb_players-1; sprintf(options[1], "%d", cfg->nb_players); } } else if(selection==2 || selection==3) { if(cfg->players[selection-2].character == PACMAN) cfg->players[selection-2].character = GHOST; else if(cfg->players[selection-2].character == GHOST) cfg->players[selection-2].character = PACMAN; } } else if(in.key[SDLK_RIGHT]) { in.key[SDLK_RIGHT]=0; if(selection==0) { if(cfg->nb_players<2) { available[cfg->nb_players+2]=1; cfg->nb_players=(cfg->nb_players+1); sprintf(options[1], "%d", cfg->nb_players); } } else if(selection==2 || selection==3) { if(cfg->players[selection-2].character == PACMAN) cfg->players[selection-2].character = GHOST; else if(cfg->players[selection-2].character == GHOST) cfg->players[selection-2].character = PACMAN; } } SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); couleur[selection]=jaune; p1.x=300; p1.y=50; aff_pol(title, 50, p1, jaune); //Le titre p1.x=310; p1.y=150; aff_pol(options[0], 30, p1, couleur[0]); //Nb players pos.x=p1.x+120; pos.y=p1.y; SDL_BlitSurface(fleche[1], NULL, screen, &pos); p1.x+=150; aff_pol(options[1], 30, p1, couleur[1]); //Nb players pos.x+=50; SDL_BlitSurface(fleche[0], NULL, screen, &pos); //Afficher des fleches j=(j+1)%2; p1.x=250; p1.y=225; pos.x=p1.x+10; pos.y=p1.y+50; aff_pol(options[2], 30, p1, couleur[2]); //Player 1 SDL_BlitSurface(fleche[1], NULL, screen, &pos); pos.x+=25; if(cfg->players[0].character == GHOST) SDL_BlitSurface(ghost[j], NULL, screen, &pos); else SDL_BlitSurface(pac[j], NULL, screen, &pos); pos.x+=25; SDL_BlitSurface(fleche[0], NULL, screen, &pos); //Afficher des fleches p1.x+=200; pos.x=p1.x+10; aff_pol(options[3], 30, p1, couleur[3]); //Player 2 SDL_BlitSurface(fleche[1], NULL, screen, &pos); pos.x+=25; if(cfg->players[1].character == GHOST) SDL_BlitSurface(ghost[j], NULL, screen, &pos); else SDL_BlitSurface(pac[j], NULL, screen, &pos); pos.x+=25; SDL_BlitSurface(fleche[0], NULL, screen, &pos); //Afficher des fleches p1.x=250; p1.y+=100; aff_pol(options[4], 30, p1, couleur[4]); //Sound //Afficher des images pour le son p1.x=250; p1.y+=75; aff_pol(options[5], 30, p1, couleur[5]); //Reset score p1.x=150; p1.y+=100; for(i=6; i<nb; i++) { aff_pol(options[i], 30, p1, couleur[i]); p1.x+=225; } SDL_Flip(screen); } }
// 5.2 Affiche un entier // Meme sémantique que aff_pol() void aff_int(int n, int taille, POINT p, COULEUR C) { char s[32]; sprintf(s,"%d",n); aff_pol(s,taille,p,C); }
/*Affiche la version du jeu*/ void draw_version() { POINT p1; p1.x=EDIT_WIDTH-50; p1.y=EDIT_HEIGHT-30; aff_pol(VERSION, 20, p1, jaune); }