comparison test/testjoystick.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
9 9
10 #define SCREEN_WIDTH 640 10 #define SCREEN_WIDTH 640
11 #define SCREEN_HEIGHT 480 11 #define SCREEN_HEIGHT 480
12 12
13 void 13 void
14 WatchJoystick (SDL_Joystick * joystick) 14 WatchJoystick(SDL_Joystick * joystick)
15 { 15 {
16 SDL_Surface *screen; 16 SDL_Surface *screen;
17 const char *name; 17 const char *name;
18 int i, done; 18 int i, done;
19 SDL_Event event; 19 SDL_Event event;
20 int x, y, draw; 20 int x, y, draw;
21 SDL_Rect axis_area[6][2]; 21 SDL_Rect axis_area[6][2];
22 22
23 /* Set a video mode to display joystick axis position */ 23 /* Set a video mode to display joystick axis position */
24 screen = SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0); 24 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0);
25 if (screen == NULL) { 25 if (screen == NULL) {
26 fprintf (stderr, "Couldn't set video mode: %s\n", SDL_GetError ()); 26 fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
27 return; 27 return;
28 } 28 }
29 29
30 /* Print info about the joystick we are watching */ 30 /* Print info about the joystick we are watching */
31 name = SDL_JoystickName (SDL_JoystickIndex (joystick)); 31 name = SDL_JoystickName(SDL_JoystickIndex(joystick));
32 printf ("Watching joystick %d: (%s)\n", SDL_JoystickIndex (joystick), 32 printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick),
33 name ? name : "Unknown Joystick"); 33 name ? name : "Unknown Joystick");
34 printf ("Joystick has %d axes, %d hats, %d balls, and %d buttons\n", 34 printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
35 SDL_JoystickNumAxes (joystick), SDL_JoystickNumHats (joystick), 35 SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
36 SDL_JoystickNumBalls (joystick), 36 SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
37 SDL_JoystickNumButtons (joystick));
38 37
39 /* Initialize drawing rectangles */ 38 /* Initialize drawing rectangles */
40 memset (axis_area, 0, (sizeof axis_area)); 39 memset(axis_area, 0, (sizeof axis_area));
41 draw = 0; 40 draw = 0;
42 41
43 /* Loop, getting joystick events! */ 42 /* Loop, getting joystick events! */
44 done = 0; 43 done = 0;
45 while (!done) { 44 while (!done) {
46 while (SDL_PollEvent (&event)) { 45 while (SDL_PollEvent(&event)) {
47 switch (event.type) { 46 switch (event.type) {
48 case SDL_JOYAXISMOTION: 47 case SDL_JOYAXISMOTION:
49 printf ("Joystick %d axis %d value: %d\n", 48 printf("Joystick %d axis %d value: %d\n",
50 event.jaxis.which, 49 event.jaxis.which,
51 event.jaxis.axis, event.jaxis.value); 50 event.jaxis.axis, event.jaxis.value);
52 break; 51 break;
53 case SDL_JOYHATMOTION: 52 case SDL_JOYHATMOTION:
54 printf ("Joystick %d hat %d value:", 53 printf("Joystick %d hat %d value:",
55 event.jhat.which, event.jhat.hat); 54 event.jhat.which, event.jhat.hat);
56 if (event.jhat.value == SDL_HAT_CENTERED) 55 if (event.jhat.value == SDL_HAT_CENTERED)
57 printf (" centered"); 56 printf(" centered");
58 if (event.jhat.value & SDL_HAT_UP) 57 if (event.jhat.value & SDL_HAT_UP)
59 printf (" up"); 58 printf(" up");
60 if (event.jhat.value & SDL_HAT_RIGHT) 59 if (event.jhat.value & SDL_HAT_RIGHT)
61 printf (" right"); 60 printf(" right");
62 if (event.jhat.value & SDL_HAT_DOWN) 61 if (event.jhat.value & SDL_HAT_DOWN)
63 printf (" down"); 62 printf(" down");
64 if (event.jhat.value & SDL_HAT_LEFT) 63 if (event.jhat.value & SDL_HAT_LEFT)
65 printf (" left"); 64 printf(" left");
66 printf ("\n"); 65 printf("\n");
67 break; 66 break;
68 case SDL_JOYBALLMOTION: 67 case SDL_JOYBALLMOTION:
69 printf ("Joystick %d ball %d delta: (%d,%d)\n", 68 printf("Joystick %d ball %d delta: (%d,%d)\n",
70 event.jball.which, 69 event.jball.which,
71 event.jball.ball, event.jball.xrel, event.jball.yrel); 70 event.jball.ball, event.jball.xrel, event.jball.yrel);
72 break; 71 break;
73 case SDL_JOYBUTTONDOWN: 72 case SDL_JOYBUTTONDOWN:
74 printf ("Joystick %d button %d down\n", 73 printf("Joystick %d button %d down\n",
75 event.jbutton.which, event.jbutton.button); 74 event.jbutton.which, event.jbutton.button);
76 break; 75 break;
77 case SDL_JOYBUTTONUP: 76 case SDL_JOYBUTTONUP:
78 printf ("Joystick %d button %d up\n", 77 printf("Joystick %d button %d up\n",
79 event.jbutton.which, event.jbutton.button); 78 event.jbutton.which, event.jbutton.button);
80 break; 79 break;
81 case SDL_KEYDOWN: 80 case SDL_KEYDOWN:
82 if (event.key.keysym.sym != SDLK_ESCAPE) { 81 if (event.key.keysym.sym != SDLK_ESCAPE) {
83 break; 82 break;
84 } 83 }
89 default: 88 default:
90 break; 89 break;
91 } 90 }
92 } 91 }
93 /* Update visual joystick state */ 92 /* Update visual joystick state */
94 for (i = 0; i < SDL_JoystickNumButtons (joystick); ++i) { 93 for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
95 SDL_Rect area; 94 SDL_Rect area;
96 95
97 area.x = i * 34; 96 area.x = i * 34;
98 area.y = SCREEN_HEIGHT - 34; 97 area.y = SCREEN_HEIGHT - 34;
99 area.w = 32; 98 area.w = 32;
100 area.h = 32; 99 area.h = 32;
101 if (SDL_JoystickGetButton (joystick, i) == SDL_PRESSED) { 100 if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) {
102 SDL_FillRect (screen, &area, 0xFFFF); 101 SDL_FillRect(screen, &area, 0xFFFF);
103 } else { 102 } else {
104 SDL_FillRect (screen, &area, 0x0000); 103 SDL_FillRect(screen, &area, 0x0000);
105 } 104 }
106 SDL_UpdateRects (screen, 1, &area); 105 SDL_UpdateRects(screen, 1, &area);
107 } 106 }
108 107
109 for (i = 0; 108 for (i = 0;
110 i < SDL_JoystickNumAxes (joystick) / 2 109 i < SDL_JoystickNumAxes(joystick) / 2
111 && i < SDL_arraysize (axis_area); ++i) { 110 && i < SDL_arraysize(axis_area); ++i) {
112 /* Erase previous axes */ 111 /* Erase previous axes */
113 SDL_FillRect (screen, &axis_area[i][draw], 0x0000); 112 SDL_FillRect(screen, &axis_area[i][draw], 0x0000);
114 113
115 /* Draw the X/Y axis */ 114 /* Draw the X/Y axis */
116 draw = !draw; 115 draw = !draw;
117 x = (((int) SDL_JoystickGetAxis (joystick, i * 2 + 0)) + 32768); 116 x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768);
118 x *= SCREEN_WIDTH; 117 x *= SCREEN_WIDTH;
119 x /= 65535; 118 x /= 65535;
120 if (x < 0) { 119 if (x < 0) {
121 x = 0; 120 x = 0;
122 } else if (x > (SCREEN_WIDTH - 16)) { 121 } else if (x > (SCREEN_WIDTH - 16)) {
123 x = SCREEN_WIDTH - 16; 122 x = SCREEN_WIDTH - 16;
124 } 123 }
125 y = (((int) SDL_JoystickGetAxis (joystick, i * 2 + 1)) + 32768); 124 y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768);
126 y *= SCREEN_HEIGHT; 125 y *= SCREEN_HEIGHT;
127 y /= 65535; 126 y /= 65535;
128 if (y < 0) { 127 if (y < 0) {
129 y = 0; 128 y = 0;
130 } else if (y > (SCREEN_HEIGHT - 16)) { 129 } else if (y > (SCREEN_HEIGHT - 16)) {
132 } 131 }
133 axis_area[i][draw].x = (Sint16) x; 132 axis_area[i][draw].x = (Sint16) x;
134 axis_area[i][draw].y = (Sint16) y; 133 axis_area[i][draw].y = (Sint16) y;
135 axis_area[i][draw].w = 16; 134 axis_area[i][draw].w = 16;
136 axis_area[i][draw].h = 16; 135 axis_area[i][draw].h = 16;
137 SDL_FillRect (screen, &axis_area[i][draw], 0xFFFF); 136 SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF);
138 137
139 SDL_UpdateRects (screen, 2, axis_area[i]); 138 SDL_UpdateRects(screen, 2, axis_area[i]);
140 } 139 }
141 } 140 }
142 } 141 }
143 142
144 int 143 int
145 main (int argc, char *argv[]) 144 main(int argc, char *argv[])
146 { 145 {
147 const char *name; 146 const char *name;
148 int i; 147 int i;
149 SDL_Joystick *joystick; 148 SDL_Joystick *joystick;
150 149
151 /* Initialize SDL (Note: video is required to start event loop) */ 150 /* Initialize SDL (Note: video is required to start event loop) */
152 if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { 151 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
153 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); 152 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
154 exit (1); 153 exit(1);
155 } 154 }
156 155
157 /* Print information about the joysticks */ 156 /* Print information about the joysticks */
158 printf ("There are %d joysticks attached\n", SDL_NumJoysticks ()); 157 printf("There are %d joysticks attached\n", SDL_NumJoysticks());
159 for (i = 0; i < SDL_NumJoysticks (); ++i) { 158 for (i = 0; i < SDL_NumJoysticks(); ++i) {
160 name = SDL_JoystickName (i); 159 name = SDL_JoystickName(i);
161 printf ("Joystick %d: %s\n", i, name ? name : "Unknown Joystick"); 160 printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
162 } 161 }
163 162
164 if (argv[1]) { 163 if (argv[1]) {
165 joystick = SDL_JoystickOpen (atoi (argv[1])); 164 joystick = SDL_JoystickOpen(atoi(argv[1]));
166 if (joystick == NULL) { 165 if (joystick == NULL) {
167 printf ("Couldn't open joystick %d: %s\n", atoi (argv[1]), 166 printf("Couldn't open joystick %d: %s\n", atoi(argv[1]),
168 SDL_GetError ()); 167 SDL_GetError());
169 } else { 168 } else {
170 WatchJoystick (joystick); 169 WatchJoystick(joystick);
171 SDL_JoystickClose (joystick); 170 SDL_JoystickClose(joystick);
172 } 171 }
173 } 172 }
174 SDL_QuitSubSystem (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); 173 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
175 174
176 return (0); 175 return (0);
177 } 176 }