test/testjoystick.c
changeset 6731 572559876818
parent 6730 36158e4061f4
child 6732 b4201930fb9f
equal deleted inserted replaced
6730:36158e4061f4 6731:572559876818
    64     }
    64     }
    65 
    65 
    66     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    66     SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
    67     SDL_RenderClear(screen);
    67     SDL_RenderClear(screen);
    68     SDL_RenderPresent(screen);
    68     SDL_RenderPresent(screen);
    69 	SDL_RaiseWindow(window);
    69     SDL_RaiseWindow(window);
    70 
    70 
    71     /* Print info about the joystick we are watching */
    71     /* Print info about the joystick we are watching */
    72     name = SDL_JoystickName(joystick);
    72     name = SDL_JoystickName(joystick);
    73     printf("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
    73     printf("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
    74            name ? name : "Unknown Joystick");
    74            name ? name : "Unknown Joystick");
   227             SDL_JoystickClose(joystick);
   227             SDL_JoystickClose(joystick);
   228         }
   228         }
   229     }
   229     }
   230 
   230 
   231     if (argv[1]) {
   231     if (argv[1]) {
   232 		int nreportederror = 0;
   232         SDL_bool reportederror = SDL_FALSE;
   233         SDL_bool keepGoing = SDL_TRUE;
   233         SDL_bool keepGoing = SDL_TRUE;
   234 		SDL_Event event;
   234         SDL_Event event;
   235 		joystick = SDL_JoystickOpen(atoi(argv[1]));
   235         joystick = SDL_JoystickOpen(atoi(argv[1]));
   236 		while ( keepGoing ) {
   236         while ( keepGoing ) {
   237 			if (joystick == NULL) {
   237             if (joystick == NULL) {
   238 				if ( nreportederror == 0 ) {
   238                 if ( !reportederror ) {
   239 					printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
   239                     printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError());
   240 					nreportederror = 1;
   240                     reportederror = SDL_TRUE;
   241 				}
   241                 }
   242 			} else {
   242             } else {
   243 				nreportederror = 0;
   243                 reportederror = SDL_FALSE;
   244 				keepGoing = WatchJoystick(joystick);
   244                 keepGoing = WatchJoystick(joystick);
   245 				SDL_JoystickClose(joystick);
   245                 SDL_JoystickClose(joystick);
   246 			}
   246             }
   247 			
   247 
   248             if (keepGoing) {
   248             if (keepGoing) {
   249     			joystick = NULL;
   249                 joystick = NULL;
   250     			SDL_WaitEvent( &event );
   250                 SDL_WaitEvent( &event );
   251     			if ( event.type == SDL_JOYDEVICEADDED )
   251                 if ( event.type == SDL_JOYDEVICEADDED ) {
   252     				joystick = SDL_JoystickOpen(atoi(argv[1]));
   252                     joystick = SDL_JoystickOpen(atoi(argv[1]));
   253             }
   253                 }
   254 		}
   254             }
   255 	}
   255         }
       
   256     }
   256     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   257     SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   257 
   258 
   258     return 0;
   259     return 0;
   259 }
   260 }