test/testjoystick.c
changeset 1895 c121d94672cb
parent 1855 5ff2c01e475e
child 2200 893c862eed86
     1.1 --- a/test/testjoystick.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/test/testjoystick.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -10,169 +10,167 @@
     1.4  #define SCREEN_WIDTH	640
     1.5  #define SCREEN_HEIGHT	480
     1.6  
     1.7 -void WatchJoystick(SDL_Joystick *joystick)
     1.8 +void
     1.9 +WatchJoystick(SDL_Joystick * joystick)
    1.10  {
    1.11 -	SDL_Surface *screen;
    1.12 -	const char *name;
    1.13 -	int i, done;
    1.14 -	SDL_Event event;
    1.15 -	int x, y, draw;
    1.16 -	SDL_Rect axis_area[2];
    1.17 +    SDL_Surface *screen;
    1.18 +    const char *name;
    1.19 +    int i, done;
    1.20 +    SDL_Event event;
    1.21 +    int x, y, draw;
    1.22 +    SDL_Rect axis_area[6][2];
    1.23  
    1.24 -	/* Set a video mode to display joystick axis position */
    1.25 -	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
    1.26 -	if ( screen == NULL ) {
    1.27 -		fprintf(stderr, "Couldn't set video mode: %s\n",SDL_GetError());
    1.28 -		return;
    1.29 -	}
    1.30 +    /* Set a video mode to display joystick axis position */
    1.31 +    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
    1.32 +    if (screen == NULL) {
    1.33 +        fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
    1.34 +        return;
    1.35 +    }
    1.36  
    1.37 -	/* Print info about the joystick we are watching */
    1.38 -	name = SDL_JoystickName(SDL_JoystickIndex(joystick));
    1.39 -	printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
    1.40 -	       name ? name : "Unknown Joystick");
    1.41 -	printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    1.42 -	       SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    1.43 -	       SDL_JoystickNumBalls(joystick),SDL_JoystickNumButtons(joystick));
    1.44 +    /* Print info about the joystick we are watching */
    1.45 +    name = SDL_JoystickName(SDL_JoystickIndex(joystick));
    1.46 +    printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
    1.47 +           name ? name : "Unknown Joystick");
    1.48 +    printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    1.49 +           SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    1.50 +           SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
    1.51  
    1.52 -	/* Initialize drawing rectangles */
    1.53 -	memset(axis_area, 0, (sizeof axis_area));
    1.54 -	draw = 0;
    1.55 +    /* Initialize drawing rectangles */
    1.56 +    memset(axis_area, 0, (sizeof axis_area));
    1.57 +    draw = 0;
    1.58  
    1.59 -	/* Loop, getting joystick events! */
    1.60 -	done = 0;
    1.61 -	while ( ! done ) {
    1.62 -		while ( SDL_PollEvent(&event) ) {
    1.63 -			switch (event.type) {
    1.64 -			    case SDL_JOYAXISMOTION:
    1.65 -				printf("Joystick %d axis %d value: %d\n",
    1.66 -				       event.jaxis.which,
    1.67 -				       event.jaxis.axis,
    1.68 -				       event.jaxis.value);
    1.69 -				break;
    1.70 -			    case SDL_JOYHATMOTION:
    1.71 -				printf("Joystick %d hat %d value:",
    1.72 -				       event.jhat.which,
    1.73 -				       event.jhat.hat);
    1.74 -				if ( event.jhat.value == SDL_HAT_CENTERED )
    1.75 -					printf(" centered");
    1.76 -				if ( event.jhat.value & SDL_HAT_UP )
    1.77 -					printf(" up");
    1.78 -				if ( event.jhat.value & SDL_HAT_RIGHT )
    1.79 -					printf(" right");
    1.80 -				if ( event.jhat.value & SDL_HAT_DOWN )
    1.81 -					printf(" down");
    1.82 -				if ( event.jhat.value & SDL_HAT_LEFT )
    1.83 -					printf(" left");
    1.84 -				printf("\n");
    1.85 -				break;
    1.86 -			    case SDL_JOYBALLMOTION:
    1.87 -				printf("Joystick %d ball %d delta: (%d,%d)\n",
    1.88 -				       event.jball.which,
    1.89 -				       event.jball.ball,
    1.90 -				       event.jball.xrel,
    1.91 -				       event.jball.yrel);
    1.92 -				break;
    1.93 -			    case SDL_JOYBUTTONDOWN:
    1.94 -				printf("Joystick %d button %d down\n",
    1.95 -				       event.jbutton.which,
    1.96 -				       event.jbutton.button);
    1.97 -				break;
    1.98 -			    case SDL_JOYBUTTONUP:
    1.99 -				printf("Joystick %d button %d up\n",
   1.100 -				       event.jbutton.which,
   1.101 -				       event.jbutton.button);
   1.102 -				break;
   1.103 -			    case SDL_KEYDOWN:
   1.104 -				if ( event.key.keysym.sym != SDLK_ESCAPE ) {
   1.105 -					break;
   1.106 -				}
   1.107 -				/* Fall through to signal quit */
   1.108 -			    case SDL_QUIT:
   1.109 -				done = 1;
   1.110 -				break;
   1.111 -			    default:
   1.112 -				break;
   1.113 -			}
   1.114 -		}
   1.115 -		/* Update visual joystick state */
   1.116 -		for ( i=0; i<SDL_JoystickNumButtons(joystick); ++i ) {
   1.117 -			SDL_Rect area;
   1.118 +    /* Loop, getting joystick events! */
   1.119 +    done = 0;
   1.120 +    while (!done) {
   1.121 +        while (SDL_PollEvent(&event)) {
   1.122 +            switch (event.type) {
   1.123 +            case SDL_JOYAXISMOTION:
   1.124 +                printf("Joystick %d axis %d value: %d\n",
   1.125 +                       event.jaxis.which,
   1.126 +                       event.jaxis.axis, event.jaxis.value);
   1.127 +                break;
   1.128 +            case SDL_JOYHATMOTION:
   1.129 +                printf("Joystick %d hat %d value:",
   1.130 +                       event.jhat.which, event.jhat.hat);
   1.131 +                if (event.jhat.value == SDL_HAT_CENTERED)
   1.132 +                    printf(" centered");
   1.133 +                if (event.jhat.value & SDL_HAT_UP)
   1.134 +                    printf(" up");
   1.135 +                if (event.jhat.value & SDL_HAT_RIGHT)
   1.136 +                    printf(" right");
   1.137 +                if (event.jhat.value & SDL_HAT_DOWN)
   1.138 +                    printf(" down");
   1.139 +                if (event.jhat.value & SDL_HAT_LEFT)
   1.140 +                    printf(" left");
   1.141 +                printf("\n");
   1.142 +                break;
   1.143 +            case SDL_JOYBALLMOTION:
   1.144 +                printf("Joystick %d ball %d delta: (%d,%d)\n",
   1.145 +                       event.jball.which,
   1.146 +                       event.jball.ball, event.jball.xrel, event.jball.yrel);
   1.147 +                break;
   1.148 +            case SDL_JOYBUTTONDOWN:
   1.149 +                printf("Joystick %d button %d down\n",
   1.150 +                       event.jbutton.which, event.jbutton.button);
   1.151 +                break;
   1.152 +            case SDL_JOYBUTTONUP:
   1.153 +                printf("Joystick %d button %d up\n",
   1.154 +                       event.jbutton.which, event.jbutton.button);
   1.155 +                break;
   1.156 +            case SDL_KEYDOWN:
   1.157 +                if (event.key.keysym.sym != SDLK_ESCAPE) {
   1.158 +                    break;
   1.159 +                }
   1.160 +                /* Fall through to signal quit */
   1.161 +            case SDL_QUIT:
   1.162 +                done = 1;
   1.163 +                break;
   1.164 +            default:
   1.165 +                break;
   1.166 +            }
   1.167 +        }
   1.168 +        /* Update visual joystick state */
   1.169 +        for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
   1.170 +            SDL_Rect area;
   1.171  
   1.172 -			area.x = i*34;
   1.173 -			area.y = SCREEN_HEIGHT-34;
   1.174 -			area.w = 32;
   1.175 -			area.h = 32;
   1.176 -			if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   1.177 -				SDL_FillRect(screen, &area, 0xFFFF);
   1.178 -			} else {
   1.179 -				SDL_FillRect(screen, &area, 0x0000);
   1.180 -			}
   1.181 -			SDL_UpdateRects(screen, 1, &area);
   1.182 -		}
   1.183 +            area.x = i * 34;
   1.184 +            area.y = SCREEN_HEIGHT - 34;
   1.185 +            area.w = 32;
   1.186 +            area.h = 32;
   1.187 +            if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   1.188 +                SDL_FillRect(screen, &area, 0xFFFF);
   1.189 +            } else {
   1.190 +                SDL_FillRect(screen, &area, 0x0000);
   1.191 +            }
   1.192 +            SDL_UpdateRects(screen, 1, &area);
   1.193 +        }
   1.194  
   1.195 -		/* Erase previous axes */
   1.196 -		SDL_FillRect(screen, &axis_area[draw], 0x0000);
   1.197 +        for (i = 0;
   1.198 +             i < SDL_JoystickNumAxes(joystick) / 2
   1.199 +             && i < SDL_arraysize(axis_area); ++i) {
   1.200 +            /* Erase previous axes */
   1.201 +            SDL_FillRect(screen, &axis_area[i][draw], 0x0000);
   1.202  
   1.203 -		/* Draw the X/Y axis */
   1.204 -		draw = !draw;
   1.205 -		x = (((int)SDL_JoystickGetAxis(joystick, 0))+32768);
   1.206 -		x *= SCREEN_WIDTH;
   1.207 -		x /= 65535;
   1.208 -		if ( x < 0 ) {
   1.209 -			x = 0;
   1.210 -		} else
   1.211 -		if ( x > (SCREEN_WIDTH-16) ) {
   1.212 -			x = SCREEN_WIDTH-16;
   1.213 -		}
   1.214 -		y = (((int)SDL_JoystickGetAxis(joystick, 1))+32768);
   1.215 -		y *= SCREEN_HEIGHT;
   1.216 -		y /= 65535;
   1.217 -		if ( y < 0 ) {
   1.218 -			y = 0;
   1.219 -		} else
   1.220 -		if ( y > (SCREEN_HEIGHT-16) ) {
   1.221 -			y = SCREEN_HEIGHT-16;
   1.222 -		}
   1.223 -		axis_area[draw].x = (Sint16)x;
   1.224 -		axis_area[draw].y = (Sint16)y;
   1.225 -		axis_area[draw].w = 16;
   1.226 -		axis_area[draw].h = 16;
   1.227 -		SDL_FillRect(screen, &axis_area[draw], 0xFFFF);
   1.228 +            /* Draw the X/Y axis */
   1.229 +            draw = !draw;
   1.230 +            x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
   1.231 +            x *= SCREEN_WIDTH;
   1.232 +            x /= 65535;
   1.233 +            if (x < 0) {
   1.234 +                x = 0;
   1.235 +            } else if (x > (SCREEN_WIDTH - 16)) {
   1.236 +                x = SCREEN_WIDTH - 16;
   1.237 +            }
   1.238 +            y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
   1.239 +            y *= SCREEN_HEIGHT;
   1.240 +            y /= 65535;
   1.241 +            if (y < 0) {
   1.242 +                y = 0;
   1.243 +            } else if (y > (SCREEN_HEIGHT - 16)) {
   1.244 +                y = SCREEN_HEIGHT - 16;
   1.245 +            }
   1.246 +            axis_area[i][draw].x = (Sint16) x;
   1.247 +            axis_area[i][draw].y = (Sint16) y;
   1.248 +            axis_area[i][draw].w = 16;
   1.249 +            axis_area[i][draw].h = 16;
   1.250 +            SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF);
   1.251  
   1.252 -		SDL_UpdateRects(screen, 2, axis_area);
   1.253 -	}
   1.254 +            SDL_UpdateRects(screen, 2, axis_area[i]);
   1.255 +        }
   1.256 +    }
   1.257  }
   1.258  
   1.259 -int main(int argc, char *argv[])
   1.260 +int
   1.261 +main(int argc, char *argv[])
   1.262  {
   1.263 -	const char *name;
   1.264 -	int i;
   1.265 -	SDL_Joystick *joystick;
   1.266 +    const char *name;
   1.267 +    int i;
   1.268 +    SDL_Joystick *joystick;
   1.269  
   1.270 -	/* Initialize SDL (Note: video is required to start event loop) */
   1.271 -	if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0 ) {
   1.272 -		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   1.273 -		exit(1);
   1.274 -	}
   1.275 +    /* Initialize SDL (Note: video is required to start event loop) */
   1.276 +    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   1.277 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.278 +        exit(1);
   1.279 +    }
   1.280  
   1.281 -	/* Print information about the joysticks */
   1.282 -	printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   1.283 -	for ( i=0; i<SDL_NumJoysticks(); ++i ) {
   1.284 -		name = SDL_JoystickName(i);
   1.285 -		printf("Joystick %d: %s\n",i,name ? name : "Unknown Joystick");
   1.286 -	}
   1.287 +    /* Print information about the joysticks */
   1.288 +    printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   1.289 +    for (i = 0; i < SDL_NumJoysticks(); ++i) {
   1.290 +        name = SDL_JoystickName(i);
   1.291 +        printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
   1.292 +    }
   1.293  
   1.294 -	if ( argv[1] ) {
   1.295 -		joystick = SDL_JoystickOpen(atoi(argv[1]));
   1.296 -		if ( joystick == NULL ) {
   1.297 -			printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
   1.298 -			       SDL_GetError());
   1.299 -		} else {
   1.300 -			WatchJoystick(joystick);
   1.301 -			SDL_JoystickClose(joystick);
   1.302 -		}
   1.303 -	}
   1.304 -	SDL_QuitSubSystem(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK);
   1.305 +    if (argv[1]) {
   1.306 +        joystick = SDL_JoystickOpen(atoi(argv[1]));
   1.307 +        if (joystick == NULL) {
   1.308 +            printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
   1.309 +                   SDL_GetError());
   1.310 +        } else {
   1.311 +            WatchJoystick(joystick);
   1.312 +            SDL_JoystickClose(joystick);
   1.313 +        }
   1.314 +    }
   1.315 +    SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
   1.316  
   1.317 -	return(0);
   1.318 +    return (0);
   1.319  }