test/testgamecontroller.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 26 Nov 2012 16:37:54 -0800
changeset 6690 9548c8a58103
child 6771 55337ff4256f
permissions -rw-r--r--
Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
     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 game controller 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 SDL_bool s_ForceQuit = SDL_FALSE;
    33 
    34 static void
    35 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    36 {
    37     const SDL_Rect area = { x, y, w, h };
    38     SDL_RenderFillRect(r, &area);
    39 }
    40 
    41 void
    42 WatchGameController(SDL_GameController * gamecontroller)
    43 {
    44     SDL_Window *window = NULL;
    45     SDL_Renderer *screen = NULL;
    46     const char *name = NULL;
    47     int done = 0;
    48     SDL_Event event;
    49     int i;
    50 
    51     /* Create a window to display controller axis position */
    52     window = SDL_CreateWindow("Game Controller Test", SDL_WINDOWPOS_CENTERED,
    53                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    54                               SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    55     if (window == NULL) {
    56         fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    57         return;
    58     }
    59 
    60     screen = SDL_CreateRenderer(window, -1, 0);
    61     if (screen == NULL) {
    62         fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
    63         SDL_DestroyWindow(window);
    64         return;
    65     }
    66 
    67     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    68     SDL_RenderClear(screen);
    69     SDL_RenderPresent(screen);
    70 	SDL_RaiseWindow(window);
    71 
    72     /* Print info about the controller we are watching */
    73     name = SDL_GameControllerName(gamecontroller);
    74     printf("Watching controller %s\n",  name ? name : "Unknown Controller");
    75     
    76     /* Loop, getting controller events! */
    77     while (!done) {
    78         /* blank screen, set up for drawing this frame. */
    79         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    80         SDL_RenderClear(screen);
    81 
    82         while (SDL_PollEvent(&event)) {
    83             switch (event.type) {
    84             case SDL_CONTROLLERAXISMOTION:
    85                 printf("Controller %d axis %d value: %d\n",
    86                        event.caxis.which,
    87                        event.caxis.axis, event.caxis.value);
    88                 break;
    89             case SDL_CONTROLLERBUTTONDOWN:
    90                 printf("Controller %d button %d down\n",
    91                        event.cbutton.which, event.cbutton.button);
    92                 break;
    93             case SDL_CONTROLLERBUTTONUP:
    94                 printf("Controller %d button %d up\n",
    95                        event.cbutton.which, event.cbutton.button);
    96                 break;
    97             case SDL_KEYDOWN:
    98                 if (event.key.keysym.sym != SDLK_ESCAPE) {
    99                     break;
   100                 }
   101                 /* Fall through to signal quit */
   102             case SDL_QUIT:
   103                 done = 1;
   104 				s_ForceQuit = SDL_TRUE;
   105                 break;
   106             default:
   107                 break;
   108             }
   109         }
   110         /* Update visual controller state */
   111         SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   112         for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
   113             if (SDL_GameControllerGetButton(gamecontroller, i) == SDL_PRESSED) {
   114                 DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
   115             }
   116         }
   117 
   118         SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   119         for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
   120             /* Draw the X/Y axis */
   121             int x, y;
   122             x = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 0)) + 32768);
   123             x *= SCREEN_WIDTH;
   124             x /= 65535;
   125             if (x < 0) {
   126                 x = 0;
   127             } else if (x > (SCREEN_WIDTH - 16)) {
   128                 x = SCREEN_WIDTH - 16;
   129             }
   130             y = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 1)) + 32768);
   131             y *= SCREEN_HEIGHT;
   132             y /= 65535;
   133             if (y < 0) {
   134                 y = 0;
   135             } else if (y > (SCREEN_HEIGHT - 16)) {
   136                 y = SCREEN_HEIGHT - 16;
   137             }
   138 
   139             DrawRect(screen, x, y, 16, 16);
   140         }
   141 
   142         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   143 
   144         SDL_RenderPresent(screen);
   145 		
   146 		if ( !done )
   147 			done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
   148     }
   149 
   150     SDL_DestroyRenderer(screen);
   151     SDL_DestroyWindow(window);
   152 }
   153 
   154 int
   155 main(int argc, char *argv[])
   156 {
   157     const char *name;
   158     int i;
   159 	int nController = 0;
   160     SDL_GameController *gamecontroller;
   161 
   162 	SDL_SetHint( SDL_HINT_GAMECONTROLLERCONFIG, "341a3608000000000000504944564944,Aferglow PS3 Controller,a:b1,b:b2,y:b3,x:b0,start:b9,guide:b12,back:b8,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftshoulder:b4,rightshoulder:b5,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7" );
   163     /* Initialize SDL (Note: video is required to start event loop) */
   164     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
   165         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   166         exit(1);
   167     }
   168 
   169     /* Print information about the controller */
   170     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   171 		if ( SDL_IsGameController(i) )
   172 		{
   173 			nController++;
   174 			name = SDL_GameControllerNameForIndex(i);
   175 			printf("Game Controller %d: %s\n", i, name ? name : "Unknown Controller");
   176 		}
   177     }
   178 	printf("There are %d game controllers attached\n", nController);
   179 
   180     if (argv[1]) {
   181 		int nreportederror = 0;
   182 		SDL_Event event;
   183 		gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
   184 		while ( s_ForceQuit == SDL_FALSE ) {
   185 			if (gamecontroller == NULL) {
   186 				if ( nreportederror == 0 ) {
   187 					printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
   188 					nreportederror = 1;
   189 				}
   190 			} else {
   191 				nreportederror = 0;
   192 				WatchGameController(gamecontroller);
   193 				SDL_GameControllerClose(gamecontroller);
   194 			}
   195 			
   196 			gamecontroller = NULL;
   197 			SDL_WaitEvent( &event );
   198 			if ( event.type == SDL_JOYDEVICEADDED )
   199 				gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
   200 		}
   201 	}
   202     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER );
   203 
   204     return (0);
   205 }