test/testjoystick.c
changeset 0 74212992fb08
child 1661 281d3f4870e5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testjoystick.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,178 @@
     1.4 +
     1.5 +/* Simple program to test the SDL joystick routines */
     1.6 +
     1.7 +#include <stdio.h>
     1.8 +#include <stdlib.h>
     1.9 +#include <string.h>
    1.10 +
    1.11 +#include "SDL.h"
    1.12 +
    1.13 +#define SCREEN_WIDTH	640
    1.14 +#define SCREEN_HEIGHT	480
    1.15 +
    1.16 +void WatchJoystick(SDL_Joystick *joystick)
    1.17 +{
    1.18 +	SDL_Surface *screen;
    1.19 +	const char *name;
    1.20 +	int i, done;
    1.21 +	SDL_Event event;
    1.22 +	int x, y, draw;
    1.23 +	SDL_Rect axis_area[2];
    1.24 +
    1.25 +	/* Set a video mode to display joystick axis position */
    1.26 +	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
    1.27 +	if ( screen == NULL ) {
    1.28 +		fprintf(stderr, "Couldn't set video mode: %s\n",SDL_GetError());
    1.29 +		return;
    1.30 +	}
    1.31 +
    1.32 +	/* Print info about the joystick we are watching */
    1.33 +	name = SDL_JoystickName(SDL_JoystickIndex(joystick));
    1.34 +	printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
    1.35 +	       name ? name : "Unknown Joystick");
    1.36 +	printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
    1.37 +	       SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
    1.38 +	       SDL_JoystickNumBalls(joystick),SDL_JoystickNumButtons(joystick));
    1.39 +
    1.40 +	/* Initialize drawing rectangles */
    1.41 +	memset(axis_area, 0, (sizeof axis_area));
    1.42 +	draw = 0;
    1.43 +
    1.44 +	/* Loop, getting joystick events! */
    1.45 +	done = 0;
    1.46 +	while ( ! done ) {
    1.47 +		while ( SDL_PollEvent(&event) ) {
    1.48 +			switch (event.type) {
    1.49 +			    case SDL_JOYAXISMOTION:
    1.50 +				printf("Joystick %d axis %d value: %d\n",
    1.51 +				       event.jaxis.which,
    1.52 +				       event.jaxis.axis,
    1.53 +				       event.jaxis.value);
    1.54 +				break;
    1.55 +			    case SDL_JOYHATMOTION:
    1.56 +				printf("Joystick %d hat %d value:",
    1.57 +				       event.jhat.which,
    1.58 +				       event.jhat.hat);
    1.59 +				if ( event.jhat.value == SDL_HAT_CENTERED )
    1.60 +					printf(" centered");
    1.61 +				if ( event.jhat.value & SDL_HAT_UP )
    1.62 +					printf(" up");
    1.63 +				if ( event.jhat.value & SDL_HAT_RIGHT )
    1.64 +					printf(" right");
    1.65 +				if ( event.jhat.value & SDL_HAT_DOWN )
    1.66 +					printf(" down");
    1.67 +				if ( event.jhat.value & SDL_HAT_LEFT )
    1.68 +					printf(" left");
    1.69 +				printf("\n");
    1.70 +				break;
    1.71 +			    case SDL_JOYBALLMOTION:
    1.72 +				printf("Joystick %d ball %d delta: (%d,%d)\n",
    1.73 +				       event.jball.which,
    1.74 +				       event.jball.ball,
    1.75 +				       event.jball.xrel,
    1.76 +				       event.jball.yrel);
    1.77 +				break;
    1.78 +			    case SDL_JOYBUTTONDOWN:
    1.79 +				printf("Joystick %d button %d down\n",
    1.80 +				       event.jbutton.which,
    1.81 +				       event.jbutton.button);
    1.82 +				break;
    1.83 +			    case SDL_JOYBUTTONUP:
    1.84 +				printf("Joystick %d button %d up\n",
    1.85 +				       event.jbutton.which,
    1.86 +				       event.jbutton.button);
    1.87 +				break;
    1.88 +			    case SDL_KEYDOWN:
    1.89 +				if ( event.key.keysym.sym != SDLK_ESCAPE ) {
    1.90 +					break;
    1.91 +				}
    1.92 +				/* Fall through to signal quit */
    1.93 +			    case SDL_QUIT:
    1.94 +				done = 1;
    1.95 +				break;
    1.96 +			    default:
    1.97 +				break;
    1.98 +			}
    1.99 +		}
   1.100 +		/* Update visual joystick state */
   1.101 +		for ( i=0; i<SDL_JoystickNumButtons(joystick); ++i ) {
   1.102 +			SDL_Rect area;
   1.103 +
   1.104 +			area.x = i*34;
   1.105 +			area.y = SCREEN_HEIGHT-34;
   1.106 +			area.w = 32;
   1.107 +			area.h = 32;
   1.108 +			if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
   1.109 +				SDL_FillRect(screen, &area, 0xFFFF);
   1.110 +			} else {
   1.111 +				SDL_FillRect(screen, &area, 0x0000);
   1.112 +			}
   1.113 +			SDL_UpdateRects(screen, 1, &area);
   1.114 +		}
   1.115 +
   1.116 +		/* Erase previous axes */
   1.117 +		SDL_FillRect(screen, &axis_area[draw], 0x0000);
   1.118 +
   1.119 +		/* Draw the X/Y axis */
   1.120 +		draw = !draw;
   1.121 +		x = (((int)SDL_JoystickGetAxis(joystick, 0))+32768);
   1.122 +		x *= SCREEN_WIDTH;
   1.123 +		x /= 65535;
   1.124 +		if ( x < 0 ) {
   1.125 +			x = 0;
   1.126 +		} else
   1.127 +		if ( x > (SCREEN_WIDTH-16) ) {
   1.128 +			x = SCREEN_WIDTH-16;
   1.129 +		}
   1.130 +		y = (((int)SDL_JoystickGetAxis(joystick, 1))+32768);
   1.131 +		y *= SCREEN_HEIGHT;
   1.132 +		y /= 65535;
   1.133 +		if ( y < 0 ) {
   1.134 +			y = 0;
   1.135 +		} else
   1.136 +		if ( y > (SCREEN_HEIGHT-16) ) {
   1.137 +			y = SCREEN_HEIGHT-16;
   1.138 +		}
   1.139 +		axis_area[draw].x = (Sint16)x;
   1.140 +		axis_area[draw].y = (Sint16)y;
   1.141 +		axis_area[draw].w = 16;
   1.142 +		axis_area[draw].h = 16;
   1.143 +		SDL_FillRect(screen, &axis_area[draw], 0xFFFF);
   1.144 +
   1.145 +		SDL_UpdateRects(screen, 2, axis_area);
   1.146 +	}
   1.147 +}
   1.148 +
   1.149 +int main(int argc, char *argv[])
   1.150 +{
   1.151 +	const char *name;
   1.152 +	int i;
   1.153 +	SDL_Joystick *joystick;
   1.154 +
   1.155 +	/* Initialize SDL (Note: video is required to start event loop) */
   1.156 +	if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0 ) {
   1.157 +		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   1.158 +		exit(1);
   1.159 +	}
   1.160 +
   1.161 +	/* Print information about the joysticks */
   1.162 +	printf("There are %d joysticks attached\n", SDL_NumJoysticks());
   1.163 +	for ( i=0; i<SDL_NumJoysticks(); ++i ) {
   1.164 +		name = SDL_JoystickName(i);
   1.165 +		printf("Joystick %d: %s\n",i,name ? name : "Unknown Joystick");
   1.166 +	}
   1.167 +
   1.168 +	if ( argv[1] ) {
   1.169 +		joystick = SDL_JoystickOpen(atoi(argv[1]));
   1.170 +		if ( joystick == NULL ) {
   1.171 +			printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
   1.172 +			       SDL_GetError());
   1.173 +		} else {
   1.174 +			WatchJoystick(joystick);
   1.175 +			SDL_JoystickClose(joystick);
   1.176 +		}
   1.177 +	}
   1.178 +	SDL_QuitSubSystem(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK);
   1.179 +
   1.180 +	return(0);
   1.181 +}