test/testjoystick.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 15 Aug 2012 20:53:24 -0400
changeset 6397 efdf44f20799
parent 6256 1d905b13b102
child 6398 c294faf5fce5
permissions -rw-r--r--
Updated testjoystick.c for SDL2 API and draw more information.

Fixes Bugzilla #1570.

Thanks to Ondra Hosek for the patch!
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Simple program to test the SDL joystick routines */
    14 
    15 #include <stdio.h>
    16 #include <stdlib.h>
    17 #include <string.h>
    18 
    19 #include "SDL.h"
    20 
    21 #ifdef __IPHONEOS__
    22 #define SCREEN_WIDTH	320
    23 #define SCREEN_HEIGHT	480
    24 #else
    25 #define SCREEN_WIDTH	640
    26 #define SCREEN_HEIGHT	480
    27 #endif
    28 
    29 #define MAX_NUM_AXES 6
    30 #define MAX_NUM_HATS 2
    31 
    32 void
    33 WatchJoystick(SDL_Joystick * joystick)
    34 {
    35     SDL_Window *window;
    36     SDL_Renderer *screen;
    37     const char *name;
    38     int i, done;
    39     SDL_Event event;
    40     int x, y;
    41     SDL_Rect axis_area[MAX_NUM_AXES][2];
    42     int axis_draw[MAX_NUM_AXES];
    43     SDL_Rect hat_area[MAX_NUM_HATS][2];
    44     int hat_draw[MAX_NUM_HATS];
    45     Uint8 hat_pos;
    46 
    47     /* Create a window to display joystick axis position */
    48     window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
    49                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    50                               SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    51     if (window == NULL) {
    52         fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    53         return;
    54     }
    55 
    56     screen = SDL_CreateRenderer(window, -1, 0);
    57     if (screen == NULL) {
    58         fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
    59         SDL_DestroyWindow(window);
    60         return;
    61     }
    62 
    63     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    64     SDL_RenderClear(screen);
    65 
    66     /* Print info about the joystick we are watching */
    67     name = SDL_JoystickName(SDL_JoystickIndex(joystick));
    68     printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
    69            name ? name : "Unknown Joystick");
    70     printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    71            SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    72            SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
    73 
    74     /* Initialize drawing rectangles */
    75     memset(axis_area, 0, (sizeof axis_area));
    76     memset(axis_draw, 0, (sizeof axis_draw));
    77     memset(hat_area, 0, (sizeof hat_area));
    78     memset(hat_draw, 0, (sizeof hat_draw));
    79 
    80     /* Loop, getting joystick events! */
    81     done = 0;
    82     while (!done) {
    83         while (SDL_PollEvent(&event)) {
    84             switch (event.type) {
    85             case SDL_JOYAXISMOTION:
    86                 printf("Joystick %d axis %d value: %d\n",
    87                        event.jaxis.which,
    88                        event.jaxis.axis, event.jaxis.value);
    89                 break;
    90             case SDL_JOYHATMOTION:
    91                 printf("Joystick %d hat %d value:",
    92                        event.jhat.which, event.jhat.hat);
    93                 if (event.jhat.value == SDL_HAT_CENTERED)
    94                     printf(" centered");
    95                 if (event.jhat.value & SDL_HAT_UP)
    96                     printf(" up");
    97                 if (event.jhat.value & SDL_HAT_RIGHT)
    98                     printf(" right");
    99                 if (event.jhat.value & SDL_HAT_DOWN)
   100                     printf(" down");
   101                 if (event.jhat.value & SDL_HAT_LEFT)
   102                     printf(" left");
   103                 printf("\n");
   104                 break;
   105             case SDL_JOYBALLMOTION:
   106                 printf("Joystick %d ball %d delta: (%d,%d)\n",
   107                        event.jball.which,
   108                        event.jball.ball, event.jball.xrel, event.jball.yrel);
   109                 break;
   110             case SDL_JOYBUTTONDOWN:
   111                 printf("Joystick %d button %d down\n",
   112                        event.jbutton.which, event.jbutton.button);
   113                 break;
   114             case SDL_JOYBUTTONUP:
   115                 printf("Joystick %d button %d up\n",
   116                        event.jbutton.which, event.jbutton.button);
   117                 break;
   118             case SDL_KEYDOWN:
   119                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   120                     break;
   121                 }
   122                 /* Fall through to signal quit */
   123             case SDL_QUIT:
   124                 done = 1;
   125                 break;
   126             default:
   127                 break;
   128             }
   129         }
   130         /* Update visual joystick state */
   131         for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
   132             SDL_Rect area;
   133 
   134             area.x = i * 34;
   135             area.y = SCREEN_HEIGHT - 34;
   136             area.w = 32;
   137             area.h = 32;
   138             if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   139                 SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
   140             } else {
   141                 SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   142             }
   143             SDL_RenderFillRect(screen, &area);
   144             SDL_RenderPresent(screen);
   145         }
   146 
   147         for (i = 0;
   148              i < SDL_JoystickNumAxes(joystick) / 2
   149              && i < SDL_arraysize(axis_area); ++i) {
   150 
   151             /* Erase previous axes */
   152             SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   153             SDL_RenderFillRect(screen, &axis_area[i][axis_draw[i]]);
   154 
   155             /* Draw the X/Y axis */
   156             axis_draw[i] = !axis_draw[i];
   157             x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   158             x *= SCREEN_WIDTH;
   159             x /= 65535;
   160             if (x < 0) {
   161                 x = 0;
   162             } else if (x > (SCREEN_WIDTH - 16)) {
   163                 x = SCREEN_WIDTH - 16;
   164             }
   165             y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   166             y *= SCREEN_HEIGHT;
   167             y /= 65535;
   168             if (y < 0) {
   169                 y = 0;
   170             } else if (y > (SCREEN_HEIGHT - 16)) {
   171                 y = SCREEN_HEIGHT - 16;
   172             }
   173 
   174             axis_area[i][axis_draw[i]].x = (Sint16) x;
   175             axis_area[i][axis_draw[i]].y = (Sint16) y;
   176             axis_area[i][axis_draw[i]].w = 16;
   177             axis_area[i][axis_draw[i]].h = 16;
   178 
   179             SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
   180             SDL_RenderFillRect(screen, &axis_area[i][axis_draw[i]]);
   181             SDL_RenderPresent(screen);
   182         }
   183 
   184         for (i = 0;
   185              i < SDL_JoystickNumHats(joystick)
   186              && i < SDL_arraysize(hat_area); ++i) {
   187 
   188             /* Erase previous hat position */
   189             SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   190             SDL_RenderFillRect(screen, &hat_area[i][hat_draw[i]]);
   191 
   192             hat_draw[i] = !hat_draw[i];
   193 
   194             /* Derive the new position */
   195             hat_pos = SDL_JoystickGetHat(joystick, i);
   196 
   197             hat_area[i][hat_draw[i]].x = SCREEN_WIDTH/2;
   198             hat_area[i][hat_draw[i]].y = SCREEN_HEIGHT/2;
   199             hat_area[i][hat_draw[i]].w = 8;
   200             hat_area[i][hat_draw[i]].h = 8;
   201 
   202             if (hat_pos & SDL_HAT_UP) {
   203                 hat_area[i][hat_draw[i]].y = 0;
   204             } else if (hat_pos & SDL_HAT_DOWN) {
   205                 hat_area[i][hat_draw[i]].y = SCREEN_HEIGHT-8;
   206             }
   207 
   208             if (hat_pos & SDL_HAT_LEFT) {
   209                 hat_area[i][hat_draw[i]].x = 0;
   210             } else if (hat_pos & SDL_HAT_RIGHT) {
   211                 hat_area[i][hat_draw[i]].x = SCREEN_WIDTH-8;
   212             }
   213 
   214             /* Draw it */
   215             SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
   216             SDL_RenderFillRect(screen, &hat_area[i][hat_draw[i]]);
   217             SDL_RenderPresent(screen);
   218         }
   219     }
   220 
   221     SDL_DestroyRenderer(screen);
   222     SDL_DestroyWindow(window);
   223 }
   224 
   225 int
   226 main(int argc, char *argv[])
   227 {
   228     const char *name;
   229     int i;
   230     SDL_Joystick *joystick;
   231 
   232     /* Initialize SDL (Note: video is required to start event loop) */
   233     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   234         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   235         exit(1);
   236     }
   237 
   238     /* Print information about the joysticks */
   239     printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   240     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   241         name = SDL_JoystickName(i);
   242         printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   243         joystick = SDL_JoystickOpen(i);
   244         if (joystick == NULL) {
   245             fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
   246                     SDL_GetError());
   247         } else {
   248             printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   249             printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   250             printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
   251             printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   252             SDL_JoystickClose(joystick);
   253         }
   254     }
   255 
   256     if (argv[1]) {
   257         joystick = SDL_JoystickOpen(atoi(argv[1]));
   258         if (joystick == NULL) {
   259             printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
   260                    SDL_GetError());
   261         } else {
   262             WatchJoystick(joystick);
   263             SDL_JoystickClose(joystick);
   264         }
   265     }
   266     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   267 
   268     return (0);
   269 }