begin process at 2012 05 28 14:54:36
  Trouver un code source :
 
dans
 
Accueil > Forum > 

Archive C/C++

 > 

Archives

 > 

Au secours

 > 

BUG SDL et CLAVIER ??? HELP!


Derniers messages déposésPoser une question dans le forum ou lancer une discussion

BUG SDL et CLAVIER ??? HELP!

mercredi 14 avril 2004 à 23:05:54 | BUG SDL et CLAVIER ??? HELP!

broks


Voici Mon code, il est un peu lourd c normal je débute en SDL, mais des ke mon probleme sera resolu, j'allegerai tout ca bien sûr:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <time.h>
#include <SDL/SDL.h>

/*****************************************************************************/
/* SDL */
/*****************************************************************************/

SDL_Surface *bleu;
SDL_Surface *rouge;
SDL_Surface *bleu_jaune;
SDL_Surface *screen;

int xpos=0,ypos=0;


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);
}
}

int InitImages()
{
bleu = SDL_LoadBMP("bleu.bmp");
bleu_jaune = SDL_LoadBMP("bleu_jaune.bmp");
rouge = SDL_LoadBMP("rouge.bmp");
return 0;
}

void DessIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;

dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);

}

void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
SDL_Rect dest;
SDL_Rect dest2;
dest.x = x;
dest.y = y;

dest2.x = x2;
dest2.y = y2;
dest2.w = w;
dest2.h = h;
SDL_BlitSurface(img, &dest2, screen, &dest);
}






/*****************************************************************************/
/****************** FONCTION TESTE UNE LETTRE ET L'AFFICHE *******************/
/*****************************************************************************/

char affiche_saisi (Uint8* keys,int ligne,int colonne,SDL_Surface *couleur,SDL_KeyboardEvent *key)
{

if ( keys[SDLK_a] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,32,160);
SDL_Flip(screen);Sulock(screen);return 'q'; }
if ( keys[SDLK_b] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,64,128);
SDL_Flip(screen);Sulock(screen);return 'b'; }
if ( keys[SDLK_c] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,96,128);
SDL_Flip(screen);Sulock(screen);return 'c'; }
if ( keys[SDLK_d] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,128,128);
SDL_Flip(screen);Sulock(screen);return 'd'; }
if ( keys[SDLK_e] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,160,128);
SDL_Flip(screen);Sulock(screen);return 'e'; }
if ( keys[SDLK_f] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,194,128);
SDL_Flip(screen);Sulock(screen);return 'f'; }
if ( keys[SDLK_g] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,226,128);
SDL_Flip(screen);Sulock(screen);return 'g'; }
if ( keys[SDLK_h] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,258,128);
SDL_Flip(screen);Sulock(screen);return 'h'; }
if ( keys[SDLK_i] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,290,128);
SDL_Flip(screen);Sulock(screen);return 'i'; }
if ( keys[SDLK_j] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,322,128);
SDL_Flip(screen);Sulock(screen);return 'j'; }
if ( keys[SDLK_k] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,354,128);
SDL_Flip(screen);Sulock(screen);return 'k'; }
if ( keys[SDLK_l] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,386,128);
SDL_Flip(screen);Sulock(screen);return 'l'; }
if ( keys[SDLK_SEMICOLON] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,418,128);
SDL_Flip(screen);Sulock(screen);return 'm'; }
if ( keys[SDLK_n] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,450,128);
SDL_Flip(screen);Sulock(screen);return 'n'; }
if ( keys[SDLK_o] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,482,128);
SDL_Flip(screen);Sulock(screen);return 'o'; }
if ( keys[SDLK_p] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,0,160);
SDL_Flip(screen);Sulock(screen);return 'p'; }
if ( keys[SDLK_q] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,32,128);
SDL_Flip(screen);Sulock(screen);return 'a'; }
if ( keys[SDLK_r] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,64,160);
SDL_Flip(screen);Sulock(screen);return 'r'; }
if ( keys[SDLK_s] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,96,160);
SDL_Flip(screen);Sulock(screen);return 's'; }
if ( keys[SDLK_t] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,128,160);
SDL_Flip(screen);Sulock(screen);return 't'; }
if ( keys[SDLK_u] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,160,160);
SDL_Flip(screen);Sulock(screen);return 'u'; }
if ( keys[SDLK_v] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,192,160);
SDL_Flip(screen);Sulock(screen);return 'v'; }
if ( keys[SDLK_w] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,320,160);
SDL_Flip(screen);Sulock(screen);return 'z'; }
if ( keys[SDLK_x] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,256,160);
SDL_Flip(screen);Sulock(screen);return 'x'; }
if ( keys[SDLK_y] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,288,160);
SDL_Flip(screen);Sulock(screen);return 'y'; }
if ( keys[SDLK_z] ) { Slock(screen);DrawIMG(couleur,ligne*32,colonne*32,32,32,224,160);
SDL_Flip(screen);Sulock(screen);return 'w'; }

}





/*****************************************************************************/
/************************ FONCTION TESTE UNE LETTRE **************************/
/*****************************************************************************/

char test_lettre (Uint8* keys)
{

if ( keys[SDLK_a] ) { return 'q'; }
if ( keys[SDLK_b] ) { return 'b'; }
if ( keys[SDLK_c] ) { return 'c'; }
if ( keys[SDLK_d] ) { return 'd'; }
if ( keys[SDLK_e] ) { return 'e'; }
if ( keys[SDLK_f] ) { return 'f'; }
if ( keys[SDLK_g] ) { return 'g'; }
if ( keys[SDLK_h] ) { return 'h'; }
if ( keys[SDLK_i] ) { return 'i'; }
if ( keys[SDLK_j] ) { return 'j'; }
if ( keys[SDLK_k] ) { return 'k'; }
if ( keys[SDLK_l] ) { return 'l'; }
if ( keys[SDLK_SEMICOLON] ) { return 'm'; }
if ( keys[SDLK_n] ) { return 'n'; }
if ( keys[SDLK_o] ) { return 'o'; }
if ( keys[SDLK_p] ) { return 'p'; }
if ( keys[SDLK_q] ) { return 'a'; }
if ( keys[SDLK_r] ) { return 'r'; }
if ( keys[SDLK_s] ) { return 's'; }
if ( keys[SDLK_t] ) { return 't'; }
if ( keys[SDLK_u] ) { return 'u'; }
if ( keys[SDLK_v] ) { return 'v'; }
if ( keys[SDLK_w] ) { return 'z'; }
if ( keys[SDLK_x] ) { return 'x'; }
if ( keys[SDLK_y] ) { return 'y'; }
if ( keys[SDLK_z] ) { return 'w'; }

return(0);
}






/*****************************************************************************/
/*********************** FONCTION AFFICHE UN CARACTERE ***********************/
/*****************************************************************************/

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;
}
else
{
Slock(screen);
DrawIMG(couleur,ligne*32,colonne*32,32,32,(i-15)*32,160);
SDL_Flip(screen);
Sulock(screen);
test=1;
}
}
i++;
}
}





/*****************************************************************************/
/****************************** FONCTION PIOCHE ******************************/
/*****************************************************************************/

char * pioche()
{
FILE *in;
int nb_caract,io,nb_lignes=0;
int i=0,j=0,k=0;
char *fichier, *mot;
time_t timer;
char c;
//char *a="bonjour";
//void randomize();
srand (time(&timer));
//void clrscr();
fichier=(char*)malloc(30*sizeof(char));
//ouverture du dictio.
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);
in = fopen(fichier, "rt");
while ( c = fgetc(in),! feof (in) )
{ //printf("%c",c);
if(c == '\n')
{ nb_lignes++ ;//printf("\n");
}
}
fclose(in);

//printf("nb_lignes: %d",nb_lignes);

fclose(in);
mot=(char*)malloc(nb_caract*sizeof(char));
//choix du mot dans le fichier choisit
in = fopen(fichier, "rt");
j=rand()%nb_lignes;
j++;
i=0;
do{
fscanf(in,"%s",mot);
i++;
}while(i!=j);
printf("\nmot trouve: %d %s\n",j, mot);
fclose(in);
affiche_caract (*mot,0,0,bleu);
return (mot);

}





/*****************************************************************************/
/************************** FONCTION SAISI D UN MOT **************************/
/*****************************************************************************/

char * saisi (int nb_caract , int nb_essai )
{
char *mot_saisi,une_lettre;
int done=0,i=0;
Uint8 *keys;

mot_saisi=(char*)malloc(10*sizeof(char));

while ( done == 0 )
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{
//if ( event.type == SDL_QUIT ) { done = 1; }

if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_RETURN ) { done = 1; }

else

{
keys = SDL_GetKeyState(NULL);
*(mot_saisi+i)=affiche_saisi(keys,i,nb_essai,bleu,&event.key);
i++;
}
}

}

}
return (mot_saisi);
}






/*****************************************************************************/
/*************************** FONCTION RAPPEL RESULT **************************/
/*****************************************************************************/

void rappel_result (char *result, int essai)
{
int i;
for (i=0;i<strlen(result);i++)
{
if (*(result+i) != '.' )
affiche_caract (*(result+i),i,essai,bleu);
}
}






/*****************************************************************************/
/**************************** FONCTION COMPARAISON ***************************/
/*****************************************************************************/

char* compare (char * le_mot , int nb_caract)
{
char *result , *un_mot;
int i,j=0,lettre_rouge;
result=(char*)calloc(nb_caract,sizeof(char));
un_mot=(char*)malloc(nb_caract*sizeof(char));

for (i=0;i<nb_caract;i++)
*(result+i)='.';


while ( ( lettre_rouge != nb_caract ) && j<5 )
{
un_mot=saisi(nb_caract,j);

//if ( *un_mot != *le_mot ) //PREMIERES LETTRES DIFFERENTES DONC PAS DE TEST
// i=nb_caract; //à revoir mal place

lettre_rouge=0;

for (i=0;i<nb_caract;i++)
{
if ( *le_mot != *un_mot ) //PREMIERES LETTRES DIFFERENTES DONC PAS DE TEST
i=nb_caract;

else
{

if (*(le_mot+i)==*(un_mot+i)) //LETTRE BIEN PLACEE

{
*(result+i)=*(un_mot+i);
affiche_caract(*(un_mot+i), i, j,rouge);
lettre_rouge++;
}

else

if (*(result+i)=='.') //LETTRE MALE PLACEE

{
*(result+i)='.';
affiche_caract(*(un_mot+i), i, j,bleu);
}

else //LETTRE DEJA TROUVEE

affiche_caract(*(un_mot+i), i, j,bleu);

}
}
j++;

if ( lettre_rouge != nb_caract)
rappel_result(result,j);

}

return(un_mot);
}






/*****************************************************************************/
/************************************ MAIN ***********************************/
/*****************************************************************************/

int main(int argc, char *argv[])
{

char *mot,*mot_saisi;
mot=(char*)malloc(10*sizeof(char));
//mot_saisi=(char*)malloc(10*sizeof(char));
//result=(char*)malloc(10*sizeof(char));

InitImages(); //chargement des pointeurs vers les images et ecran

//Initialisation de la SDL
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(320,200,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}

SDL_EnableUNICODE( 1 );

mot=pioche();

//affiche_mot(mot); ca fait rien
//printf("\n mot pioche : %s\n",mot);
//affiche(mot);
//mot_saisi = saisi(strlen(mot),0);
/*printf("\n mot saisi : %s\n",mot_saisi);
printf("\n longueur du mot %d ",strlen(mot));
printf("\n mot trouve final : %s\n",compare(mot,mot_saisi,strlen(mot)));
*/
compare(mot,strlen(mot));
//system("pause");
SDL_Quit();
return 0;

}


C'est un motus! vous l avez remarqué? bravo!
Mon probleme c que cela marchait bien je pouvais faire 5 essai de mots de 5 lettres (sauf qd le prg fini ca buggai un peu)
maintenant ca plante au second essai (on dirait qu il y a une boucle infini mais je ne comprends pas)
Avis aux dieux de SDL help me pls!!!!
mercredi 14 avril 2004 à 23:17:56 | Re : BUG SDL et CLAVIER ??? HELP!

broks


Je viens d up la source avec les images ca sera mieux qd mm

pseudo: broks
jeux: motus sdl

Aligato!!!!!


Cette discussion est classée dans : return, sdl, keys, screen, sdlk


Répondre à ce message

Sujets en rapport avec ce message

probleme de compilation SDL [ par darmoor ] Je suis dsl , le sujet doit etre souvent demandé mais j'ai deja cherché sur google et j'ai pas encore trouvé ce qu'il faut. Bon alors j'ai commencé a SDL : linker error [ par bigben89 ] Ca fais à peine quelques mois que j'fais de la programmation (en C) et j'aimerais faire un peu de 2d et de 3d avec SDL. Je regarde des tutoriaux sur GESTION CLAVIER SDL [ par Batman60 ] voici un bout de code qui  sert à me déplacer dans une scène 3D opengl ,le problème c'est qu'il faut à chaque fois appuyer sur la touche pour ce dépla Gros soucis SDL ...... argh [ par courjete ] Alors voila, je me met à la programmation en c++ et dans une volonté de tester mes connaissances, préparer des fonctions d'affichage, je cherche à fai [SDL] Fenetre qui s' affiche et disparait aussitot [ par jonezy ] Bonjour Je debute en programmation, j'ai telechargé ce code qui est cencé etre un jeu de serpent avec SDL. Quand je l'execute la fenêtre apparaît et SDL execution ? [ par Neszreth ] Bonjour vous tous, j'ai un problème assez étrange, j'essaie d'exécuter un programme très simple fonctionnant à l'aide de la librairie sdl, lorsque je Problème de gestion de clavier avec SDL [ par vincemail ] Bonjour, j'utilise SDL pour gérer le clavier : les flèches directionnelles doivent déplacer un personnage à l'écran. Mon problème est qu'il faut relac SDL conditions affichage image [ par MattCand ] Bonjour à tous, je suis débutant je cherche un code SDL qui, pour un paramètre donné, va afficher la bonne image. Je me suis basé sur le code SDL de b Probleme avec la librairie sdl et code::blocks [ par BencoAndCo ] Bonjour, J'ai un problème avec la lib sdl et code::blocks quand je compile le prog suivant : #include #include #include int main(int argc, ch [Xcode]Aide integrer image [ par mirmoleboss ] Bonjour, Voilà j'ai fait ce petit code mais il ne marche pas enfin il marchait mais l'intégration d'une image ne marche pas, je ne vois pas d'où vient


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,858 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales