test/testgamecontroller.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Wed, 26 Dec 2012 22:26:44 -0800
changeset 6773 5ebc5a9e35b4
parent 6771 55337ff4256f
child 6914 8a9915ab24f0
permissions -rw-r--r--
Add surface test suite; minor improvements to render suite; refactor image saving into test lib compare function; fix for Haiku build
     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 #ifndef SDL_JOYSTICK_DISABLED
    22 
    23 #ifdef __IPHONEOS__
    24 #define SCREEN_WIDTH	320
    25 #define SCREEN_HEIGHT	480
    26 #else
    27 #define SCREEN_WIDTH	640
    28 #define SCREEN_HEIGHT	480
    29 #endif
    30 
    31 #define MAX_NUM_AXES 6
    32 #define MAX_NUM_HATS 2
    33 
    34 static SDL_bool s_ForceQuit = SDL_FALSE;
    35 
    36 static void
    37 DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    38 {
    39     const SDL_Rect area = { x, y, w, h };
    40     SDL_RenderFillRect(r, &area);
    41 }
    42 
    43 void
    44 WatchGameController(SDL_GameController * gamecontroller)
    45 {
    46     SDL_Window *window = NULL;
    47     SDL_Renderer *screen = NULL;
    48     const char *name = NULL;
    49     int done = 0;
    50     SDL_Event event;
    51     int i;
    52 
    53     /* Create a window to display controller axis position */
    54     window = SDL_CreateWindow("Game Controller Test", SDL_WINDOWPOS_CENTERED,
    55                               SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    56                               SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    57     if (window == NULL) {
    58         fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    59         return;
    60     }
    61 
    62     screen = SDL_CreateRenderer(window, -1, 0);
    63     if (screen == NULL) {
    64         fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
    65         SDL_DestroyWindow(window);
    66         return;
    67     }
    68 
    69     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    70     SDL_RenderClear(screen);
    71     SDL_RenderPresent(screen);
    72 	SDL_RaiseWindow(window);
    73 
    74     /* Print info about the controller we are watching */
    75     name = SDL_GameControllerName(gamecontroller);
    76     printf("Watching controller %s\n",  name ? name : "Unknown Controller");
    77     
    78     /* Loop, getting controller events! */
    79     while (!done) {
    80         /* blank screen, set up for drawing this frame. */
    81         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    82         SDL_RenderClear(screen);
    83 
    84         while (SDL_PollEvent(&event)) {
    85             switch (event.type) {
    86             case SDL_CONTROLLERAXISMOTION:
    87                 printf("Controller %d axis %d value: %d\n",
    88                        event.caxis.which,
    89                        event.caxis.axis, event.caxis.value);
    90                 break;
    91             case SDL_CONTROLLERBUTTONDOWN:
    92                 printf("Controller %d button %d down\n",
    93                        event.cbutton.which, event.cbutton.button);
    94                 break;
    95             case SDL_CONTROLLERBUTTONUP:
    96                 printf("Controller %d button %d up\n",
    97                        event.cbutton.which, event.cbutton.button);
    98                 break;
    99             case SDL_KEYDOWN:
   100                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   101                     break;
   102                 }
   103                 /* Fall through to signal quit */
   104             case SDL_QUIT:
   105                 done = 1;
   106 				s_ForceQuit = SDL_TRUE;
   107                 break;
   108             default:
   109                 break;
   110             }
   111         }
   112         /* Update visual controller state */
   113         SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   114         for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
   115             if (SDL_GameControllerGetButton(gamecontroller, i) == SDL_PRESSED) {
   116                 DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
   117             }
   118         }
   119 
   120         SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   121         for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
   122             /* Draw the X/Y axis */
   123             int x, y;
   124             x = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 0)) + 32768);
   125             x *= SCREEN_WIDTH;
   126             x /= 65535;
   127             if (x < 0) {
   128                 x = 0;
   129             } else if (x > (SCREEN_WIDTH - 16)) {
   130                 x = SCREEN_WIDTH - 16;
   131             }
   132             y = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 1)) + 32768);
   133             y *= SCREEN_HEIGHT;
   134             y /= 65535;
   135             if (y < 0) {
   136                 y = 0;
   137             } else if (y > (SCREEN_HEIGHT - 16)) {
   138                 y = SCREEN_HEIGHT - 16;
   139             }
   140 
   141             DrawRect(screen, x, y, 16, 16);
   142         }
   143 
   144         SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   145 
   146         SDL_RenderPresent(screen);
   147 		
   148 		if ( !done )
   149 			done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
   150     }
   151 
   152     SDL_DestroyRenderer(screen);
   153     SDL_DestroyWindow(window);
   154 }
   155 
   156 int
   157 main(int argc, char *argv[])
   158 {
   159     const char *name;
   160     int i;
   161 	int nController = 0;
   162     SDL_GameController *gamecontroller;
   163 
   164 	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" );
   165     /* Initialize SDL (Note: video is required to start event loop) */
   166     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
   167         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   168         exit(1);
   169     }
   170 
   171     /* Print information about the controller */
   172     for (i = 0; i < SDL_NumJoysticks(); ++i) {
   173 		if ( SDL_IsGameController(i) )
   174 		{
   175 			nController++;
   176 			name = SDL_GameControllerNameForIndex(i);
   177 			printf("Game Controller %d: %s\n", i, name ? name : "Unknown Controller");
   178 		}
   179     }
   180 	printf("There are %d game controllers attached\n", nController);
   181 
   182     if (argv[1]) {
   183 		int nreportederror = 0;
   184 		SDL_Event event;
   185 		gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
   186 		while ( s_ForceQuit == SDL_FALSE ) {
   187 			if (gamecontroller == NULL) {
   188 				if ( nreportederror == 0 ) {
   189 					printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
   190 					nreportederror = 1;
   191 				}
   192 			} else {
   193 				nreportederror = 0;
   194 				WatchGameController(gamecontroller);
   195 				SDL_GameControllerClose(gamecontroller);
   196 			}
   197 			
   198 			gamecontroller = NULL;
   199 			SDL_WaitEvent( &event );
   200 			if ( event.type == SDL_JOYDEVICEADDED )
   201 				gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
   202 		}
   203 	}
   204     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER );
   205 
   206     return (0);
   207 }
   208 
   209 #else
   210 
   211 int
   212 main(int argc, char *argv[])
   213 {
   214     fprintf(stderr, "SDL compiled without Joystick support.\n");
   215     exit(1);
   216 }
   217 
   218 #endif