[AD] Updated exjoy.c (joystick example)

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Hello,

I updated exjoy.c to show all joystick axis in analog mode.
Previously, only Stick 0 Axis 0 and 1 values were shown.

Also made cosmetic and positionning changes to save space on
screen due to the additionnal printing (eg: buttons are now all
printed on the same line).

Omar Cornut
(I made this modification to help debugging a DirectInput
driver bug, about which I'll submit another patch soon). 
--- exjoy.c.old	2003-11-09 19:43:08.000000000 +0000
+++ exjoy.c	2004-02-10 17:26:34.000000000 +0000
@@ -18,6 +18,7 @@
    int x=160, y=100;       /* these will be used to show the target sight */
    int analogmode;
    AL_CONST char *msg;
+   char buf[MAX_JOYSTICK_BUTTONS * 4 + 1];
    int c;
 
    if (allegro_init() != 0)/* you NEED this man! ;-) */
@@ -123,6 +124,7 @@
 
       textout_centre_ex(bmp, font, joystick_driver->name, 160, 150, palette_color[255], 0);
 
+      /* display bottom information */
       if (analogmode)
 	 textout_centre_ex(bmp, font, "Analog mode selected", 160, 160, palette_color[255], 0);
       else
@@ -132,62 +134,95 @@
       textout_centre_ex(bmp, font, "Press any key to exit", 160, 180, palette_color[255], 0);
       textout_centre_ex(bmp, font, "Made by Grzegorz Adam Hankiewicz", 160, 190, palette_color[255], 0);
 
-      /* if we detect any buttons, we print a message on the screen */
+      /* create a message showing all pressed buttons, by appending them all
+       * into a string buffer, then print the message on the screen
+       */
+      buf[0] = '\0';
       for (c=0; c<joy[0].num_buttons; c++) {
-	 if (joy[0].button[c].b)
-	    textprintf_centre_ex(bmp, font, 160, c*10, palette_color[15], 0, "%s pressed", joy[0].button[c].name);
+         if (joy[0].button[c].b) {
+            char buf_button[16];
+            sprintf(buf_button, "%d ", c);
+            strcat(buf, buf_button);
+         }
       }
+      textprintf_ex(bmp, font, 0, 10, palette_color[15], 0, "Buttons pressed:");
+      textprintf_ex(bmp, font, 0, 20, palette_color[15], 0, " %s", (buf[0] != '\0') ? buf : "none");
 
       if (!analogmode) {
-	 /* now we have to check individually every possible movement
+         /* draw cross (this is one ugly drawing) */
+         textout_centre_ex(bmp, font,  "|",  160, 70, palette_color[255], 0);
+         textout_centre_ex(bmp, font, "---", 160, 80, palette_color[255], 0);
+         textout_centre_ex(bmp, font,  "|",  160, 90, palette_color[255], 0);
+
+         /* now we have to check individually every possible movement
 	  * and actualize the coordinates of the target sight.
 	  */
 	 if (joy[0].stick[0].axis[0].d1) {
 	    if (x > 0)
 	       x--;
-	    textout_centre_ex(bmp, font, "Left", 120, 100, palette_color[255], 0);
+	    textout_centre_ex(bmp, font, "Left", 120, 80, palette_color[255], 0);
 	 }
 	 if (joy[0].stick[0].axis[0].d2) {
 	    if (x < 319)
 	       x++;
-	    textout_centre_ex(bmp, font, "Right", 200, 100, palette_color[255], 0);
+	    textout_centre_ex(bmp, font, "Right", 200, 80, palette_color[255], 0);
 	 }
 	 if (joy[0].stick[0].axis[1].d1) {
 	    if (y > 0)
 	       y--;
-	    textout_centre_ex(bmp, font, "Up", 160, 70, palette_color[255], 0);
+	    textout_centre_ex(bmp, font, "Up", 160, 50, palette_color[255], 0);
 	 }
 	 if (joy[0].stick[0].axis[1].d2) {
 	    if (y < 199)
 	       y++;
-	    textout_centre_ex(bmp, font, "Down", 160, 130, palette_color[255], 0);
+	    textout_centre_ex(bmp, font, "Down", 160, 110, palette_color[255], 0);
 	 }
       }
       else {
+         int stick_n;
+         int line_n;
+
 	 /* yeah! Remember the 'ifs' of the digital part? This looks
 	  * much better, only 2 lines.
+          * note that only the standard horizontal/vertical axis of stick 0 are shown here.
 	  */
 	 x += joy[0].stick[0].axis[0].pos/40;
 	 y += joy[0].stick[0].axis[1].pos/40;
 
-	 /* for informational purposes, show the input values on screen */
-         textprintf_ex(bmp, font, 0,  0, palette_color[255], 0, "Axis 0: %d", joy[0].stick[0].axis[0].pos);
-         textprintf_ex(bmp, font, 0, 10, palette_color[255], 0, "Axis 1: %d", joy[0].stick[0].axis[1].pos);
+         /* for informational purposes, show the input values on screen.
+          * all sticks, and then all their axis values are displayed.
+          */
+         textprintf_ex(bmp, font, 0, 30, palette_color[15], 0, "Axis:");
+         line_n = 0;
+         for (stick_n = 0; stick_n < joy[0].num_sticks; stick_n++) {
+            JOYSTICK_STICK_INFO *stick = &joy[0].stick[stick_n];
+            int axis_n;
+            for (axis_n = 0; axis_n < stick->num_axis; axis_n++) {
+               JOYSTICK_AXIS_INFO *axis = &stick->axis[axis_n];
+               textprintf_ex(bmp, font, 0, 40 + (line_n * 10), palette_color[255], 0, " stick %d axis %d: %d", stick_n, axis_n, axis->pos);
+               line_n++;
+            }
+         }
+
+         /* draw cross (this is one ugly drawing) */
+         textout_centre_ex(bmp, font,  "|",  240, 40, palette_color[255], 0);
+         textout_centre_ex(bmp, font, "---", 240, 50, palette_color[255], 0);
+         textout_centre_ex(bmp, font,  "|",  240, 60, palette_color[255], 0);
 
 	 /* by checking if the values were positive or negative, we
 	  * can know in which the direction the user pulled the joy.
 	  */
-	 if (joy[0].stick[0].axis[0].pos/40 < 0) 
-	    textout_centre_ex(bmp, font, "Left", 120, 100, palette_color[255], 0);
+         if (joy[0].stick[0].axis[0].pos/40 < 0) 
+	    textout_centre_ex(bmp, font, "Left", 200, 50, palette_color[255], 0);
 
 	 if (joy[0].stick[0].axis[0].pos/40 > 0) 
-	    textout_centre_ex(bmp, font, "Right", 200, 100, palette_color[255], 0);
+	    textout_centre_ex(bmp, font, "Right", 280, 50, palette_color[255], 0);
 
 	 if (joy[0].stick[0].axis[1].pos/40 < 0) 
-	    textout_centre_ex(bmp, font, "Up", 160, 70, palette_color[255], 0);
+	    textout_centre_ex(bmp, font, "Up", 240, 20, palette_color[255], 0);
 
 	 if (joy[0].stick[0].axis[1].pos/40 > 0) 
-	    textout_centre_ex(bmp, font, "Down", 160, 130, palette_color[255], 0);
+	    textout_centre_ex(bmp, font, "Down", 240, 80, palette_color[255], 0);
 
 	 /* WARNING! An analog joystick can move more than 1 pixel at
 	  * a time and the checks we did with the digital part don't


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/