home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / chapter05 / scanjoystick / main.c next >
Encoding:
C/C++ Source or Header  |  2006-09-10  |  2.3 KB  |  90 lines

  1. ////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 5, ScanJoystick Program
  4. ////////////////////////////////////////////////////
  5.  
  6. #include <stdio.h>
  7. #include <allegro.h>
  8.  
  9. #define WHITE makecol(255,255,255)
  10. #define LTGREEN makecol(192,255,192)
  11. #define LTRED makecol(255,192,192)
  12. #define LTBLUE makecol(192,192,255)
  13. int curline = 1;
  14.  
  15. void print(char *s, int color)
  16. {
  17.     //print text with automatic linefeed
  18.     textout_ex(screen, font, s, 10, (curline++) * 12, color, 0);
  19. }
  20.  
  21. void printjoyinfo()
  22. {
  23.     char *s="";
  24.     int n, ax;
  25.     
  26.     //display joystick information
  27.     sprintf(s, "Number of Joysticks: %d", num_joysticks);
  28.     print(s, WHITE);
  29.     print("",0);
  30.     
  31.     //display stick information
  32.     sprintf(s, "Number of Sticks: %d", joy[0].num_sticks);
  33.     print(s, LTGREEN);
  34.     for (n=0; n<joy[0].num_sticks; n++)
  35.     {
  36.         sprintf(s, "Stick %d: %s", n, joy[0].stick[n].name);
  37.         print(s, LTGREEN);
  38.         sprintf(s, "  Number of Axes: %d", joy[0].stick[n].num_axis);
  39.         print(s, LTBLUE);
  40.         for (ax=0; ax<joy[0].stick[n].num_axis; ax++)
  41.         {
  42.             sprintf(s,"    Axis %d: %s", ax, 
  43.                 joy[0].stick[n].axis[ax].name);
  44.             print(s, LTRED);
  45.         }
  46.     }
  47.     
  48.     //display button information
  49.     print("",0);
  50.     sprintf(s, "Number of Buttons: %d", joy[0].num_buttons);
  51.     print(s, LTGREEN);
  52.     for (n=0; n<joy[0].num_buttons; n++)
  53.     {
  54.         sprintf(s,"  Button %d: %s", n, joy[0].button[n].name);
  55.         print(s, LTBLUE);
  56.     }
  57. }
  58.  
  59. int main(void)
  60. {
  61.  
  62.     //initialize program    
  63.     allegro_init();
  64.     set_color_depth(16);
  65.     set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
  66.     install_keyboard();
  67.  
  68.     //install the joystick handler
  69.     install_joystick(JOY_TYPE_AUTODETECT);
  70.     poll_joystick();
  71.  
  72.     //display title
  73.     print("ScanJoystick Program", WHITE);
  74.     print("---------------------------------------", WHITE);
  75.  
  76.     //look for a joystick
  77.     if (num_joysticks > 0)
  78.         printjoyinfo();
  79.     else
  80.         print("No joystick could be found", WHITE);
  81.     
  82.     //pause and exit
  83.     print("",0);
  84.     print("Press any key...", WHITE);
  85.     while (!keypressed()) { }
  86.     allegro_exit();
  87.     return 0;
  88. }
  89. END_OF_MAIN()
  90.