test/testjoystick.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 15 Aug 2012 21:00:33 -0400
changeset 6398 c294faf5fce5
parent 6397 efdf44f20799
child 6586 ab57cc69dcc0
permissions -rw-r--r--
Cleaned up testjoystick.c, improved usage of renderer API, added colors!
     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 static void
    33 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    34 {
    35     const SDL_Rect area = { x, y, w, h };
    36     SDL_RenderFillRect(r, &area);
    37 }
    38 
    39 void
    40 WatchJoystick(SDL_Joystick * joystick)
    41 {
    42     SDL_Window *window = NULL;
    43     SDL_Renderer *screen = NULL;
    44     const char *name = NULL;
    45     int done = 0;
    46     SDL_Event event;
    47     int i;
    48 
    49     /* Create a window to display joystick axis position */
    50     window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
    51                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    52                               SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    53     if (window == NULL) {
    54         fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    55         return;
    56     }
    57 
    58     screen = SDL_CreateRenderer(window, -1, 0);
    59     if (screen == NULL) {
    60         fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
    61         SDL_DestroyWindow(window);
    62         return;
    63     }
    64 
    65     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    66     SDL_RenderClear(screen);
    67     SDL_RenderPresent(screen);
    68 
    69     /* Print info about the joystick we are watching */
    70     name = SDL_JoystickName(SDL_JoystickIndex(joystick));
    71     printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
    72            name ? name : "Unknown Joystick");
    73     printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    74            SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    75            SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
    76 
    77     /* Loop, getting joystick events! */
    78     while (!done) {
    79         /* blank screen, set up for drawing this frame. */
    80         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    81         SDL_RenderClear(screen);
    82 
    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         SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   132         for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
   133             if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   134                 DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
   135             }
   136         }
   137 
   138         SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   139         for (i = 0; i < SDL_JoystickNumAxes(joystick) / 2; ++i) {
   140             /* Draw the X/Y axis */
   141             int x, y;
   142             x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   143             x *= SCREEN_WIDTH;
   144             x /= 65535;
   145             if (x < 0) {
   146                 x = 0;
   147             } else if (x > (SCREEN_WIDTH - 16)) {
   148                 x = SCREEN_WIDTH - 16;
   149             }
   150             y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   151             y *= SCREEN_HEIGHT;
   152             y /= 65535;
   153             if (y < 0) {
   154                 y = 0;
   155             } else if (y > (SCREEN_HEIGHT - 16)) {
   156                 y = SCREEN_HEIGHT - 16;
   157             }
   158 
   159             DrawRect(screen, x, y, 16, 16);
   160         }
   161 
   162         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   163         for (i = 0; i < SDL_JoystickNumHats(joystick); ++i) {
   164             /* Derive the new position */
   165             int x = SCREEN_WIDTH/2;
   166             int y = SCREEN_HEIGHT/2;
   167             const Uint8 hat_pos = SDL_JoystickGetHat(joystick, i);
   168 
   169             if (hat_pos & SDL_HAT_UP) {
   170                 y = 0;
   171             } else if (hat_pos & SDL_HAT_DOWN) {
   172                 y = SCREEN_HEIGHT-8;
   173             }
   174 
   175             if (hat_pos & SDL_HAT_LEFT) {
   176                 x = 0;
   177             } else if (hat_pos & SDL_HAT_RIGHT) {
   178                 x = SCREEN_WIDTH-8;
   179             }
   180 
   181             DrawRect(screen, x, y, 8, 8);
   182         }
   183 
   184         SDL_RenderPresent(screen);
   185     }
   186 
   187     SDL_DestroyRenderer(screen);
   188     SDL_DestroyWindow(window);
   189 }
   190 
   191 int
   192 main(int argc, char *argv[])
   193 {
   194     const char *name;
   195     int i;
   196     SDL_Joystick *joystick;
   197 
   198     /* Initialize SDL (Note: video is required to start event loop) */
   199     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   200         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   201         exit(1);
   202     }
   203 
   204     /* Print information about the joysticks */
   205     printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   206     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   207         name = SDL_JoystickName(i);
   208         printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   209         joystick = SDL_JoystickOpen(i);
   210         if (joystick == NULL) {
   211             fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i,
   212                     SDL_GetError());
   213         } else {
   214             printf("       axes: %d\n", SDL_JoystickNumAxes(joystick));
   215             printf("      balls: %d\n", SDL_JoystickNumBalls(joystick));
   216             printf("       hats: %d\n", SDL_JoystickNumHats(joystick));
   217             printf("    buttons: %d\n", SDL_JoystickNumButtons(joystick));
   218             SDL_JoystickClose(joystick);
   219         }
   220     }
   221 
   222     if (argv[1]) {
   223         joystick = SDL_JoystickOpen(atoi(argv[1]));
   224         if (joystick == NULL) {
   225             printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
   226                    SDL_GetError());
   227         } else {
   228             WatchJoystick(joystick);
   229             SDL_JoystickClose(joystick);
   230         }
   231     }
   232     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   233 
   234     return (0);
   235 }