Saisie clavier sous allegro

Contenu du snippet

Voici exemple de saisie clavier (de type fgets) sous Allegro.
A vous de modifier et d?améliorer le système de saisie selon vos besoins?

PS : je ne sais pas si un code équivalant existe sur le site

Source / Exemple :


/* Simule le fgets() sous Allegro */

#include <allegro.h>
#define SAISIE_MAX 10

void saisie();

void init();
void deinit();

int main() {
	init();

    saisie();
    
    textprintf(screen,font,10,470,makecol(255,255,255),"Appuyer sur une touche pour continuer...");
    readkey();

	deinit();
	return 0;
}
END_OF_MAIN()

void init() {
	int depth, res;
	allegro_init();
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);
	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
	if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();
	install_keyboard();
	install_mouse();
	/* add other initializations here */
}

void deinit() {
	clear_keybuf();
	/* add other deinitializations here */
}

/* Permet de faire saisie clavier sous Allegro */ 
void saisie()
{
  int touche, touche1, touche2;
  int i=0;
  char masaisie[SAISIE_MAX+1]; // stockage de la totalité de la saisie
  char lastsaisie[2];    // stockage la derniere touche saisie
  
  masaisie[SAISIE_MAX]=0;
  lastsaisie[1]=0;   
  clear_keybuf();
  /* affichage curseur */
  textprintf(screen,font,10+8*(i+1),30,makecol(0,255,255),"_");
  
  while(!key[KEY_ENTER] && !key[KEY_ENTER_PAD])
  {
    
    touche=readkey();
    touche1=touche & 0xFF; // code ASCII
    touche2=touche >> 8;   // scancode
    //* affiche code numerique de la touche saisie...
    textprintf(screen,font,10,80,makecol(0,255,0),"                                            ");
    textprintf(screen,font,10,80,makecol(0,155,0),"touche1 : %d -- touche2 : %d",touche1,touche2);
    // selection des touches (la selection est totalement arbitraire)
    if (( touche1>31 && touche1<58) || ( touche1>64 && touche1<123))
    {
      if (i>=SAISIE_MAX)
       i=SAISIE_MAX;
      else
      { 
        masaisie[i]=touche1;
        lastsaisie[0]=touche1;
        masaisie[i+1]=0;
        /*  on affiche la touche saisie */
        textprintf(screen,font,10+8*i,30,makecol(0,255,255),"%s",lastsaisie);
        i++;
        textprintf(screen,font,10+8*i,30,makecol(0,255,255),"_");
      }
    }
    //* si effacement
    if ( touche2==KEY_BACKSPACE )
    {
      i--;
      if ( i<0 )
        i=0;
      textprintf(screen,font,10+8*i,30,makecol(0,255,255),"_");
      textprintf(screen,font,10+8*(i+1),30,makecol(0,255,255)," ");
    }
    //* si validation
    if ( (touche2==KEY_ENTER_PAD) || (touche2==KEY_ENTER) )
    {
      textprintf(screen,font,10+8*i,30,makecol(0,0,255)," ");
      if (i==0)
        masaisie[0]=32; // space
      masaisie[i+1]=0;
    }
  }
  textprintf(screen,font,10,130,makecol(255,100,100),"votre saisie est : %s", masaisie);
  clear_keybuf();
}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.