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