Je ne comprends vraiment pas pourkoa ca ne marche pas...
C'est surement un probleme au niveau de la fonction saisi puisque je plante lorsque je dois saisir une touche.
Mais la fonction toute seul marche bien.
Pour information c un jeu du type motus (la source est dispo sur mon compte)
Merci aux personnes qui iront jusqu'au bout de la page
Voila le code qui plante a l exec
#include <sdl\sdl.h>
#include <stdio.h>
SDL_Surface *bleu;
SDL_Surface *rouge;
SDL_Surface *bleu_jaune;
SDL_Surface *screen;
void InitIMG()
{
bleu = SDL_LoadBMP("bleu.bmp");
rouge = SDL_LoadBMP("rouge.bmp");
bleu_jaune = SDL_LoadBMP("bleu_jaune.bmp");
}
void Slock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
if ( SDL_LockSurface(screen) < 0 )
{
return;
}
}
}
void Sulock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
SDL_UnlockSurface(screen);
}
}
void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_Rect dest2;
dest2.x = x2;
dest2.y = y2;
dest2.w = w;
dest2.h = h;
SDL_BlitSurface(img, &dest2, screen, &dest);
}
char affiche_caract (char caract,int ligne,int colonne,SDL_Surface *couleur)
{
int i=0,test=0;
while ( test==0 )
{
if ( caract == ('a'+i) )
{
if (i<16)
{
Slock(screen);
DrawIMG(couleur,ligne*32,colonne*32,32,32,32+i*32,128);
SDL_Flip(screen);
Sulock(screen);
test=1;
return ('a'+i);
}
else
{
Slock(screen);
DrawIMG(couleur,ligne*32,colonne*32,32,32,(i-15)*32,160);
SDL_Flip(screen);
Sulock(screen);
test=1;
return ('a'+i);
}
}
i++;
}
}
void DessIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}
////////////////////////////////////////
////ouverture du fichier////////////////
////////////////////////////////////////
char* ouverture()
{
FILE *in;
int nb_caract;
char *fichier;
fichier=(char*)malloc(30*sizeof(char));
do
{
//printf("entrer le nombre de caractere du mot\n");
//printf("entre 5 et 10\n");
//fflush(stdin);
//scanf("%d",&nb_caract);
nb_caract=5;
switch(nb_caract)
{
case 5 :{strcpy(fichier,"c:\\dico\\5.txt");break;}
case 6 :{strcpy(fichier,"c:\\dico\\6.txt");break;}
case 7 :{strcpy(fichier,"c:\\dico\\7.txt");break;}
case 8 :{strcpy(fichier,"c:\\dico\\8.txt");break;}
case 9 :{strcpy(fichier,"c:\\dico\\9.txt");break;}
case 10 :{strcpy(fichier,"c:\\dico\\10.txt");break;}
default :break;
}
if((in = fopen (fichier,"rt"))==NULL) ;
//printf("****\t fichier non trouve \t****\n\n\a");
}while (in==NULL);
fclose(in);
return(fichier);
}
////////////////////////////////////////
////donne le mot a trouver//////////////
////////////////////////////////////////
char* choix_mot(char *fichier)
{FILE *in;
int nb_lignes=0,i,j;
char c,*mot;
int nb_caract;
//printf("%s\n",fichier);
srand(time(NULL));
in = fopen (fichier,"rt");
///on compte le nombre de lignes
while ( c = fgetc(in),! feof (in) )
{
if(c == '\n')
{ nb_lignes++;
}
}
fclose(in);
mot=(char*)malloc(9*sizeof(char));
//choix du mot dans le fichier choisit
in = fopen(fichier, "rt");//pour revenir au debut
srand(time(NULL));
j=rand()%nb_lignes;
j++;
i=0;
do{
fscanf(in,"%s",mot);
i++;
}while(i!=j);
fclose(in);
return(mot);
}
////////////////////////////////////////
//////////saisi du mot//////////////////
////////////////////////////////////////
char * saisi ( int nb_essai )
{
char * mot_saisi,caract;
int quit=0,i;
SDL_Event event;
// SDL_KeyboardEvent *key;
//Uint8 *keys;
mot_saisi=(char*)malloc(10*sizeof(char));
/* Loop until an SDL_QUIT event is found */
while( i!=5 )
{
/* Poll for events */
while( SDL_PollEvent( &event ) )
{
if ( event.type == SDL_KEYDOWN )
{
*(mot_saisi+i)=event.key.keysym.unicode;
//printf("lettre : %c\n",*(mot_saisi+i));
affiche_caract(*(mot_saisi+i),i,nb_essai,bleu);
i++;
}
}
}
//printf("entrer mot\n");
//scanf("%s",mot_saisi);
//printf("mot_saisi\n");
return (mot_saisi);
}
////////////////////////////////////////
//////////comparaison///////////////////
////////////////////////////////////////
char* comparaison(char* un_mot,char* le_mot,char* result,int nb_essai)
{ int i,j;
int flag[strlen(le_mot)],flag2[strlen(le_mot)];
int place[strlen(le_mot)];
for(i=0;i<strlen(le_mot);i++)
{flag[i]=0;flag2[i]=0;place[i]=0;}
for(i=0;i<strlen(le_mot);i++)
{
if( un_mot[i]==le_mot[i]) //si un caractere est bien place
{
result[i]=le_mot[i];
flag[i]=1;flag2[i]=1;
place[i]=2;
}
}
for(i=0;i<strlen(le_mot);i++) //si un caractere est mal place
{ for(j=0;j<strlen(le_mot);j++)
if(un_mot[i]==le_mot[j])
if (flag[j]==0)
if(flag2[i]==0)
{
flag[j]=1;flag2[i]=1;
place[i]=1;
}
}
for(i=0;i<strlen(le_mot);i++)
{if(place[i]==0){affiche_caract(*(un_mot+i),i,nb_essai,bleu);}
if(place[i]==1){affiche_caract(*(un_mot+i),i,nb_essai,bleu_jaune);}
if(place[i]==2){affiche_caract(*(un_mot+i),i,nb_essai,rouge);}
}
return(result);
}
int main ( int argc, char *argv[] )
{
int nb_caract,i,nb_essai;
char *fichier,*le_mot,*un_mot,*result;
fichier=(char*)calloc(sizeof(char),30);
le_mot=(char*)calloc(sizeof(char),9);
un_mot=(char*)calloc(sizeof(char),9);
result=(char*)calloc(sizeof(char),9);
fichier=ouverture();
le_mot=choix_mot(fichier);
//char * mot ;
//int i = 0 ;
InitIMG();
//mot = (char*) malloc (10* sizeof(char));
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
/* Enable Unicode translation */
// SDL_EnableUNICODE( 1 );
SDL_EnableUNICODE( 1 );
*(result+0)=*(le_mot+0);
for(i=1;i<strlen(le_mot);i++)
*(result+i)='.';
affiche_caract(*le_mot ,0,0,bleu);
for(nb_essai=0;nb_essai<5;nb_essai++)
{
//printf("il reste %d chances\n",5-nb_essai);
un_mot=saisi(nb_essai);
if(strlen(le_mot)==strlen(un_mot))
comparaison(un_mot,le_mot,result,nb_essai);
//printf("le_mot: %s\n",le_mot);
//printf("un_mot: %s\n",un_mot);
//printf("resultat: %s\n\n",result);
if((strcmp( un_mot,le_mot)==0))
break;
}
return(0);
}