test/testgamecontroller.c
changeset 6690 9548c8a58103
child 6771 55337ff4256f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testgamecontroller.c	Mon Nov 26 16:37:54 2012 -0800
     1.3 @@ -0,0 +1,205 @@
     1.4 +/*
     1.5 +  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     1.6 +
     1.7 +  This software is provided 'as-is', without any express or implied
     1.8 +  warranty.  In no event will the authors be held liable for any damages
     1.9 +  arising from the use of this software.
    1.10 +
    1.11 +  Permission is granted to anyone to use this software for any purpose,
    1.12 +  including commercial applications, and to alter it and redistribute it
    1.13 +  freely.
    1.14 +*/
    1.15 +
    1.16 +/* Simple program to test the SDL game controller routines */
    1.17 +
    1.18 +#include <stdio.h>
    1.19 +#include <stdlib.h>
    1.20 +#include <string.h>
    1.21 +
    1.22 +#include "SDL.h"
    1.23 +
    1.24 +#ifdef __IPHONEOS__
    1.25 +#define SCREEN_WIDTH	320
    1.26 +#define SCREEN_HEIGHT	480
    1.27 +#else
    1.28 +#define SCREEN_WIDTH	640
    1.29 +#define SCREEN_HEIGHT	480
    1.30 +#endif
    1.31 +
    1.32 +#define MAX_NUM_AXES 6
    1.33 +#define MAX_NUM_HATS 2
    1.34 +
    1.35 +static SDL_bool s_ForceQuit = SDL_FALSE;
    1.36 +
    1.37 +static void
    1.38 +DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
    1.39 +{
    1.40 +    const SDL_Rect area = { x, y, w, h };
    1.41 +    SDL_RenderFillRect(r, &area);
    1.42 +}
    1.43 +
    1.44 +void
    1.45 +WatchGameController(SDL_GameController * gamecontroller)
    1.46 +{
    1.47 +    SDL_Window *window = NULL;
    1.48 +    SDL_Renderer *screen = NULL;
    1.49 +    const char *name = NULL;
    1.50 +    int done = 0;
    1.51 +    SDL_Event event;
    1.52 +    int i;
    1.53 +
    1.54 +    /* Create a window to display controller axis position */
    1.55 +    window = SDL_CreateWindow("Game Controller Test", SDL_WINDOWPOS_CENTERED,
    1.56 +                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
    1.57 +                              SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    1.58 +    if (window == NULL) {
    1.59 +        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
    1.60 +        return;
    1.61 +    }
    1.62 +
    1.63 +    screen = SDL_CreateRenderer(window, -1, 0);
    1.64 +    if (screen == NULL) {
    1.65 +        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
    1.66 +        SDL_DestroyWindow(window);
    1.67 +        return;
    1.68 +    }
    1.69 +
    1.70 +    SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.71 +    SDL_RenderClear(screen);
    1.72 +    SDL_RenderPresent(screen);
    1.73 +	SDL_RaiseWindow(window);
    1.74 +
    1.75 +    /* Print info about the controller we are watching */
    1.76 +    name = SDL_GameControllerName(gamecontroller);
    1.77 +    printf("Watching controller %s\n",  name ? name : "Unknown Controller");
    1.78 +    
    1.79 +    /* Loop, getting controller events! */
    1.80 +    while (!done) {
    1.81 +        /* blank screen, set up for drawing this frame. */
    1.82 +        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    1.83 +        SDL_RenderClear(screen);
    1.84 +
    1.85 +        while (SDL_PollEvent(&event)) {
    1.86 +            switch (event.type) {
    1.87 +            case SDL_CONTROLLERAXISMOTION:
    1.88 +                printf("Controller %d axis %d value: %d\n",
    1.89 +                       event.caxis.which,
    1.90 +                       event.caxis.axis, event.caxis.value);
    1.91 +                break;
    1.92 +            case SDL_CONTROLLERBUTTONDOWN:
    1.93 +                printf("Controller %d button %d down\n",
    1.94 +                       event.cbutton.which, event.cbutton.button);
    1.95 +                break;
    1.96 +            case SDL_CONTROLLERBUTTONUP:
    1.97 +                printf("Controller %d button %d up\n",
    1.98 +                       event.cbutton.which, event.cbutton.button);
    1.99 +                break;
   1.100 +            case SDL_KEYDOWN:
   1.101 +                if (event.key.keysym.sym != SDLK_ESCAPE) {
   1.102 +                    break;
   1.103 +                }
   1.104 +                /* Fall through to signal quit */
   1.105 +            case SDL_QUIT:
   1.106 +                done = 1;
   1.107 +				s_ForceQuit = SDL_TRUE;
   1.108 +                break;
   1.109 +            default:
   1.110 +                break;
   1.111 +            }
   1.112 +        }
   1.113 +        /* Update visual controller state */
   1.114 +        SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
   1.115 +        for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
   1.116 +            if (SDL_GameControllerGetButton(gamecontroller, i) == SDL_PRESSED) {
   1.117 +                DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
   1.118 +            }
   1.119 +        }
   1.120 +
   1.121 +        SDL_SetRenderDrawColor(screen, 0xFF, 0x00, 0x00, SDL_ALPHA_OPAQUE);
   1.122 +        for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
   1.123 +            /* Draw the X/Y axis */
   1.124 +            int x, y;
   1.125 +            x = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 0)) + 32768);
   1.126 +            x *= SCREEN_WIDTH;
   1.127 +            x /= 65535;
   1.128 +            if (x < 0) {
   1.129 +                x = 0;
   1.130 +            } else if (x > (SCREEN_WIDTH - 16)) {
   1.131 +                x = SCREEN_WIDTH - 16;
   1.132 +            }
   1.133 +            y = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 1)) + 32768);
   1.134 +            y *= SCREEN_HEIGHT;
   1.135 +            y /= 65535;
   1.136 +            if (y < 0) {
   1.137 +                y = 0;
   1.138 +            } else if (y > (SCREEN_HEIGHT - 16)) {
   1.139 +                y = SCREEN_HEIGHT - 16;
   1.140 +            }
   1.141 +
   1.142 +            DrawRect(screen, x, y, 16, 16);
   1.143 +        }
   1.144 +
   1.145 +        SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0xFF, SDL_ALPHA_OPAQUE);
   1.146 +
   1.147 +        SDL_RenderPresent(screen);
   1.148 +		
   1.149 +		if ( !done )
   1.150 +			done = SDL_GameControllerGetAttached( gamecontroller ) == 0;
   1.151 +    }
   1.152 +
   1.153 +    SDL_DestroyRenderer(screen);
   1.154 +    SDL_DestroyWindow(window);
   1.155 +}
   1.156 +
   1.157 +int
   1.158 +main(int argc, char *argv[])
   1.159 +{
   1.160 +    const char *name;
   1.161 +    int i;
   1.162 +	int nController = 0;
   1.163 +    SDL_GameController *gamecontroller;
   1.164 +
   1.165 +	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" );
   1.166 +    /* Initialize SDL (Note: video is required to start event loop) */
   1.167 +    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
   1.168 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.169 +        exit(1);
   1.170 +    }
   1.171 +
   1.172 +    /* Print information about the controller */
   1.173 +    for (i = 0; i < SDL_NumJoysticks(); ++i) {
   1.174 +		if ( SDL_IsGameController(i) )
   1.175 +		{
   1.176 +			nController++;
   1.177 +			name = SDL_GameControllerNameForIndex(i);
   1.178 +			printf("Game Controller %d: %s\n", i, name ? name : "Unknown Controller");
   1.179 +		}
   1.180 +    }
   1.181 +	printf("There are %d game controllers attached\n", nController);
   1.182 +
   1.183 +    if (argv[1]) {
   1.184 +		int nreportederror = 0;
   1.185 +		SDL_Event event;
   1.186 +		gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
   1.187 +		while ( s_ForceQuit == SDL_FALSE ) {
   1.188 +			if (gamecontroller == NULL) {
   1.189 +				if ( nreportederror == 0 ) {
   1.190 +					printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
   1.191 +					nreportederror = 1;
   1.192 +				}
   1.193 +			} else {
   1.194 +				nreportederror = 0;
   1.195 +				WatchGameController(gamecontroller);
   1.196 +				SDL_GameControllerClose(gamecontroller);
   1.197 +			}
   1.198 +			
   1.199 +			gamecontroller = NULL;
   1.200 +			SDL_WaitEvent( &event );
   1.201 +			if ( event.type == SDL_JOYDEVICEADDED )
   1.202 +				gamecontroller = SDL_GameControllerOpen(atoi(argv[1]));
   1.203 +		}
   1.204 +	}
   1.205 +    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER );
   1.206 +
   1.207 +    return (0);
   1.208 +}