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