Guest User

Untitled

a guest
May 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.18 KB | None | 0 0
  1. #include <allegro.h>
  2. #include<time.h>
  3.  
  4. #define ECRAN_X 800
  5. #define ECRAN_Y 600
  6.  
  7. /******************************************************************************
  8. *******************************************************************************/
  9. int main()
  10. {
  11. int x, y, rx, ry, choix, done=0;
  12.  
  13.     allegro_init();
  14.     install_keyboard();
  15.     install_mouse();
  16.     srand(time(NULL));
  17.  
  18.     set_color_depth(32);
  19.  
  20.     if (set_gfx_mode(GFX_AUTODETECT,ECRAN_X,ECRAN_Y,0,0)!=0){
  21.             set_gfx_mode(GFX_TEXT,0,0,0,0);
  22.             allegro_message(allegro_error);
  23.             allegro_exit();
  24.             return 1;
  25.     }
  26.    
  27.     //----------------------------AFFICHAGE INFOS
  28.     textprintf_ex(screen,font, 100, 80,makecol(0,0,255),-1,
  29.     "Bouger la souris :");
  30.     textprintf_ex(screen,font, 100, 120,makecol(0,0,255),-1,
  31.     "Tester clics gauche et droit");
  32.     textprintf_centre_ex(screen,font, SCREEN_W/2, 230,makecol(0,255,255),-1,
  33.     "Il n'y a pas de raffraichissement de l'ecran");
  34.     textprintf_ex(screen,font, 100, SCREEN_H-140,makecol(255,255,0),-1,
  35.     "Touches fleches clavier : test clavier via scancodes,");
  36.     textprintf_ex(screen,font, 100, SCREEN_H-130,makecol(255,255,0),-1,
  37.     "                          (essayer plusieurs en meme temps)");
  38.     textprintf_ex(screen,font, 100, SCREEN_H-120,makecol(255,255,0),-1,
  39.     "                          Quitter avec SPACE");
  40.     textprintf_ex(screen,font, 100, SCREEN_H-100,makecol(255,0,255),-1,
  41.     "Touches F1, F2, F3, F4  : test clavier via buffer clavier");
  42.     textprintf_ex(screen,font, 100, SCREEN_H-90,makecol(255,0,255),-1,
  43.     "                          (essayer plusieurs en meme temps)");
  44.     textprintf_ex(screen,font, 100, SCREEN_H-80,makecol(255,0,255),-1,
  45.     "                           Quitter avec Echapp");
  46.  
  47.     //interdit la répétition de touche pour le buffer clavier
  48.     set_keyboard_rate(0,0);
  49.    
  50.     // récupe en x et y de la position souris
  51.     x=mouse_x;
  52.     y=mouse_y;
  53.  
  54.     //----------------------------BOUCLE EVENTS
  55.     while (!done){
  56.  
  57.             //-------------------------LA SOURIS
  58.             // 1) les coordonnées
  59.             if ( x!= mouse_x || y!=mouse_y){
  60.                 textprintf_ex(screen,font, 100, 100,makecol(0,0,0),-1, "%d %d", x,y);
  61.                 x=mouse_x;
  62.                 y=mouse_y;
  63.                 textprintf_ex(screen,font, 100, 100,makecol(0,255,0),-1, "%d %d", x,y);
  64.             }
  65.      
  66.             // 2) les clics
  67.             if (mouse_b & 1) // gauche
  68.                 rectfill(screen,x, y, x+20, y+20, makecol(255,0,0));
  69.             if (mouse_b & 2) // droit
  70.                 rectfill(screen,x, y, x+20, y+20, makecol(0,0,255));
  71.          
  72.             if (mouse_b &4) // mileu ou les deux à la fois fin du programme
  73.                 done=1;
  74.    
  75.             //------------------------LE CLAVIER
  76.  
  77.             // méthode 1 : switch plus buffer clavier
  78.             if (keypressed()){
  79.                     choix=readkey();
  80.  
  81.                 // recupe de la valeur de scancode à partir du buffer clavier
  82.             switch (choix>>8){  
  83.                     case KEY_F1 :
  84.                             rectfill(screen,350 ,100 , 450, 200, makecol(rand()%255,0,255));
  85.                     break;
  86.  
  87.                     case KEY_F2 :
  88.                             rectfill(screen,350 ,100 , 450, 200, makecol(0,255,rand()%255));
  89.                     break;
  90.  
  91.                             case KEY_F3 :
  92.                             rectfill(screen,350 ,100 , 450, 200,makecol(255,rand()%255,0));  
  93.                     break;
  94.  
  95.                     case KEY_F4 :
  96.                             rectfill(screen,350 ,100 , 450, 200, makecol(0,0,0));                    
  97.                     break;
  98.  
  99.                     case KEY_ESC : done=1;     break;
  100.                         default : break;
  101.                 }
  102.             }
  103.      
  104.             // clavier méthode 2 : if plus scancodes
  105.             if (key[KEY_UP])
  106.                     rectfill(screen,350 ,270 , 450, 370, makecol(rand()%255,0,255));
  107.             if (key[KEY_DOWN])
  108.                     rectfill(screen,350 ,270 , 450, 370, makecol(0,255,rand()%255));
  109.             if (key[KEY_LEFT])
  110.                     rectfill(screen,350 ,270 , 450, 370, makecol(255,rand()%255,0));
  111.             if (key[KEY_RIGHT])
  112.                     rectfill(screen,350 ,270 , 450, 370, makecol(0,0,0));
  113.             if (key[KEY_SPACE])
  114.             done=1;
  115.     }
  116.     exit(EXIT_SUCCESS);
  117. }
  118. END_OF_MAIN();
Add Comment
Please, Sign In to add comment