~ubuntu-branches/ubuntu/trusty/libsdl2/trusty-proposed

« back to all changes in this revision

Viewing changes to test/testgamecontroller.c

  • Committer: Package Import Robot
  • Author(s): Manuel A. Fernandez Montecelo
  • Date: 2013-12-28 12:31:19 UTC
  • mto: (7.1.3 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: package-import@ubuntu.com-20131228123119-wehupm72qsjvh6vz
Tags: upstream-2.0.1+dfsg1
ImportĀ upstreamĀ versionĀ 2.0.1+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
91
91
    const char *name = SDL_GameControllerName(gamecontroller);
92
92
    const char *basetitle = "Game Controller Test: ";
93
93
    const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
94
 
    char *title = SDL_malloc(titlelen);
 
94
    char *title = (char *)SDL_malloc(titlelen);
95
95
    SDL_Window *window = NULL;
96
96
    SDL_Renderer *screen = NULL;
97
97
    int done = 0;
107
107
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
108
108
                              SCREEN_HEIGHT, 0);
109
109
    if (window == NULL) {
110
 
        fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
 
110
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
111
111
        return;
112
112
    }
113
113
 
114
114
    screen = SDL_CreateRenderer(window, -1, 0);
115
115
    if (screen == NULL) {
116
 
        fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError());
 
116
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
117
117
        SDL_DestroyWindow(window);
118
118
        return;
119
119
    }
124
124
    SDL_RaiseWindow(window);
125
125
 
126
126
    /* Print info about the controller we are watching */
127
 
    printf("Watching controller %s\n",  name ? name : "Unknown Controller");
 
127
    SDL_Log("Watching controller %s\n",  name ? name : "Unknown Controller");
128
128
 
129
129
    /* Loop, getting controller events! */
130
130
    while (!done) {
135
135
        while (SDL_PollEvent(&event)) {
136
136
            switch (event.type) {
137
137
            case SDL_CONTROLLERAXISMOTION:
138
 
                printf("Controller %d axis %d ('%s') value: %d\n",
 
138
                SDL_Log("Controller %d axis %d ('%s') value: %d\n",
139
139
                       event.caxis.which,
140
140
                       event.caxis.axis,
141
 
                       ControllerAxisName(event.caxis.axis),
 
141
                       ControllerAxisName((SDL_GameControllerAxis)event.caxis.axis),
142
142
                       event.caxis.value);
143
143
                break;
144
144
            case SDL_CONTROLLERBUTTONDOWN:
145
 
                printf("Controller %d button %d ('%s') down\n",
 
145
                SDL_Log("Controller %d button %d ('%s') down\n",
146
146
                       event.cbutton.which, event.cbutton.button,
147
 
                       ControllerButtonName(event.cbutton.button));
 
147
                       ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
148
148
                break;
149
149
            case SDL_CONTROLLERBUTTONUP:
150
 
                printf("Controller %d button %d ('%s') up\n",
 
150
                SDL_Log("Controller %d button %d ('%s') up\n",
151
151
                       event.cbutton.which, event.cbutton.button,
152
 
                       ControllerButtonName(event.cbutton.button));
 
152
                       ControllerButtonName((SDL_GameControllerButton)event.cbutton.button));
153
153
                break;
154
154
            case SDL_KEYDOWN:
155
155
                if (event.key.keysym.sym != SDLK_ESCAPE) {
167
167
        /* Update visual controller state */
168
168
        SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
169
169
        for (i = 0; i <SDL_CONTROLLER_BUTTON_MAX; ++i) {
170
 
            if (SDL_GameControllerGetButton(gamecontroller, i) == SDL_PRESSED) {
 
170
            if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
171
171
                DrawRect(screen, i * 34, SCREEN_HEIGHT - 34, 32, 32);
172
172
            }
173
173
        }
176
176
        for (i = 0; i < SDL_CONTROLLER_AXIS_MAX / 2; ++i) {
177
177
            /* Draw the X/Y axis */
178
178
            int x, y;
179
 
            x = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 0)) + 32768);
 
179
            x = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 0))) + 32768);
180
180
            x *= SCREEN_WIDTH;
181
181
            x /= 65535;
182
182
            if (x < 0) {
184
184
            } else if (x > (SCREEN_WIDTH - 16)) {
185
185
                x = SCREEN_WIDTH - 16;
186
186
            }
187
 
            y = (((int) SDL_GameControllerGetAxis(gamecontroller, i * 2 + 1)) + 32768);
 
187
            y = (((int) SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i * 2 + 1))) + 32768);
188
188
            y *= SCREEN_HEIGHT;
189
189
            y /= 65535;
190
190
            if (y < 0) {
217
217
    char guid[64];
218
218
    SDL_GameController *gamecontroller;
219
219
 
 
220
    /* Enable standard application logging */
 
221
        SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
 
222
 
220
223
    /* Initialize SDL (Note: video is required to start event loop) */
221
224
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
222
 
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
 
225
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
223
226
        return 1;
224
227
    }
225
228
 
226
229
    /* Print information about the controller */
227
230
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
228
231
        const char *name;
229
 
        const char *description = "Joystick (not recognized as game controller)";
 
232
        const char *description;
230
233
 
231
234
        SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
232
235
                                  guid, sizeof (guid));
235
238
        {
236
239
            nController++;
237
240
            name = SDL_GameControllerNameForIndex(i);
 
241
            description = "Controller";
238
242
        } else {
239
243
            name = SDL_JoystickNameForIndex(i);
 
244
            description = "Joystick";
240
245
        }
241
 
        printf("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
 
246
        SDL_Log("%s %d: %s (guid %s)\n", description, i, name ? name : "Unknown", guid);
242
247
    }
243
 
    printf("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
 
248
    SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
244
249
 
245
250
    if (argv[1]) {
246
251
        int device = atoi(argv[1]);
247
252
        if (device >= SDL_NumJoysticks()) {
248
 
            printf("%i is an invalid joystick index.\n", device);
 
253
                        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
249
254
            retcode = 1;
250
255
        } else {
251
256
            SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
252
257
                                      guid, sizeof (guid));
253
 
            printf("Attempting to open device %i, guid %s\n", device, guid);
 
258
            SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
254
259
            gamecontroller = SDL_GameControllerOpen(device);
255
260
            if (gamecontroller == NULL) {
256
 
                printf("Couldn't open joystick %d: %s\n", device, SDL_GetError());
 
261
                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open joystick %d: %s\n", device, SDL_GetError());
257
262
                retcode = 1;
258
263
            } else {
259
264
                WatchGameController(gamecontroller);
272
277
int
273
278
main(int argc, char *argv[])
274
279
{
275
 
    fprintf(stderr, "SDL compiled without Joystick support.\n");
 
280
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
276
281
    exit(1);
277
282
}
278
283