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