33 #else |
24 #else |
34 #define SCREEN_WIDTH 640 |
25 #define SCREEN_WIDTH 640 |
35 #define SCREEN_HEIGHT 480 |
26 #define SCREEN_HEIGHT 480 |
36 #endif |
27 #endif |
37 |
28 |
|
29 #define MAX_NUM_AXES 6 |
|
30 #define MAX_NUM_HATS 2 |
|
31 |
38 void |
32 void |
39 WatchJoystick(SDL_Joystick * joystick) |
33 WatchJoystick(SDL_Joystick * joystick) |
40 { |
34 { |
41 SDL_Surface *screen; |
35 SDL_Window *window; |
|
36 SDL_Renderer *screen; |
42 const char *name; |
37 const char *name; |
43 int i, done; |
38 int i, done; |
44 SDL_Event event; |
39 SDL_Event event; |
45 int x, y, draw; |
40 int x, y; |
46 SDL_Rect axis_area[6][2]; |
41 SDL_Rect axis_area[MAX_NUM_AXES][2]; |
47 |
42 int axis_draw[MAX_NUM_AXES]; |
48 /* Set a video mode to display joystick axis position */ |
43 SDL_Rect hat_area[MAX_NUM_HATS][2]; |
49 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0); |
44 int hat_draw[MAX_NUM_HATS]; |
|
45 Uint8 hat_pos; |
|
46 |
|
47 /* Create a window to display joystick axis position */ |
|
48 window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED, |
|
49 SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, |
|
50 SCREEN_HEIGHT, SDL_WINDOW_SHOWN); |
|
51 if (window == NULL) { |
|
52 fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError()); |
|
53 return; |
|
54 } |
|
55 |
|
56 screen = SDL_CreateRenderer(window, -1, 0); |
50 if (screen == NULL) { |
57 if (screen == NULL) { |
51 fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError()); |
58 fprintf(stderr, "Couldn't create renderer: %s\n", SDL_GetError()); |
|
59 SDL_DestroyWindow(window); |
52 return; |
60 return; |
53 } |
61 } |
|
62 |
|
63 SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE); |
|
64 SDL_RenderClear(screen); |
54 |
65 |
55 /* Print info about the joystick we are watching */ |
66 /* Print info about the joystick we are watching */ |
56 name = SDL_JoystickName(SDL_JoystickIndex(joystick)); |
67 name = SDL_JoystickName(SDL_JoystickIndex(joystick)); |
57 printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick), |
68 printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick), |
58 name ? name : "Unknown Joystick"); |
69 name ? name : "Unknown Joystick"); |
60 SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick), |
71 SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick), |
61 SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick)); |
72 SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick)); |
62 |
73 |
63 /* Initialize drawing rectangles */ |
74 /* Initialize drawing rectangles */ |
64 memset(axis_area, 0, (sizeof axis_area)); |
75 memset(axis_area, 0, (sizeof axis_area)); |
65 draw = 0; |
76 memset(axis_draw, 0, (sizeof axis_draw)); |
|
77 memset(hat_area, 0, (sizeof hat_area)); |
|
78 memset(hat_draw, 0, (sizeof hat_draw)); |
66 |
79 |
67 /* Loop, getting joystick events! */ |
80 /* Loop, getting joystick events! */ |
68 done = 0; |
81 done = 0; |
69 while (!done) { |
82 while (!done) { |
70 while (SDL_PollEvent(&event)) { |
83 while (SDL_PollEvent(&event)) { |
121 area.x = i * 34; |
134 area.x = i * 34; |
122 area.y = SCREEN_HEIGHT - 34; |
135 area.y = SCREEN_HEIGHT - 34; |
123 area.w = 32; |
136 area.w = 32; |
124 area.h = 32; |
137 area.h = 32; |
125 if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) { |
138 if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) { |
126 SDL_FillRect(screen, &area, 0xFFFF); |
139 SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE); |
127 } else { |
140 } else { |
128 SDL_FillRect(screen, &area, 0x0000); |
141 SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE); |
129 } |
142 } |
130 SDL_UpdateRects(screen, 1, &area); |
143 SDL_RenderFillRect(screen, &area); |
|
144 SDL_RenderPresent(screen); |
131 } |
145 } |
132 |
146 |
133 for (i = 0; |
147 for (i = 0; |
134 i < SDL_JoystickNumAxes(joystick) / 2 |
148 i < SDL_JoystickNumAxes(joystick) / 2 |
135 && i < SDL_arraysize(axis_area); ++i) { |
149 && i < SDL_arraysize(axis_area); ++i) { |
|
150 |
136 /* Erase previous axes */ |
151 /* Erase previous axes */ |
137 SDL_FillRect(screen, &axis_area[i][draw], 0x0000); |
152 SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE); |
|
153 SDL_RenderFillRect(screen, &axis_area[i][axis_draw[i]]); |
138 |
154 |
139 /* Draw the X/Y axis */ |
155 /* Draw the X/Y axis */ |
140 draw = !draw; |
156 axis_draw[i] = !axis_draw[i]; |
141 x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768); |
157 x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768); |
142 x *= SCREEN_WIDTH; |
158 x *= SCREEN_WIDTH; |
143 x /= 65535; |
159 x /= 65535; |
144 if (x < 0) { |
160 if (x < 0) { |
145 x = 0; |
161 x = 0; |
153 y = 0; |
169 y = 0; |
154 } else if (y > (SCREEN_HEIGHT - 16)) { |
170 } else if (y > (SCREEN_HEIGHT - 16)) { |
155 y = SCREEN_HEIGHT - 16; |
171 y = SCREEN_HEIGHT - 16; |
156 } |
172 } |
157 |
173 |
158 axis_area[i][draw].x = (Sint16) x; |
174 axis_area[i][axis_draw[i]].x = (Sint16) x; |
159 axis_area[i][draw].y = (Sint16) y; |
175 axis_area[i][axis_draw[i]].y = (Sint16) y; |
160 axis_area[i][draw].w = 16; |
176 axis_area[i][axis_draw[i]].w = 16; |
161 axis_area[i][draw].h = 16; |
177 axis_area[i][axis_draw[i]].h = 16; |
162 SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF); |
178 |
163 |
179 SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE); |
164 SDL_UpdateRects(screen, 2, axis_area[i]); |
180 SDL_RenderFillRect(screen, &axis_area[i][axis_draw[i]]); |
165 } |
181 SDL_RenderPresent(screen); |
166 } |
182 } |
|
183 |
|
184 for (i = 0; |
|
185 i < SDL_JoystickNumHats(joystick) |
|
186 && i < SDL_arraysize(hat_area); ++i) { |
|
187 |
|
188 /* Erase previous hat position */ |
|
189 SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE); |
|
190 SDL_RenderFillRect(screen, &hat_area[i][hat_draw[i]]); |
|
191 |
|
192 hat_draw[i] = !hat_draw[i]; |
|
193 |
|
194 /* Derive the new position */ |
|
195 hat_pos = SDL_JoystickGetHat(joystick, i); |
|
196 |
|
197 hat_area[i][hat_draw[i]].x = SCREEN_WIDTH/2; |
|
198 hat_area[i][hat_draw[i]].y = SCREEN_HEIGHT/2; |
|
199 hat_area[i][hat_draw[i]].w = 8; |
|
200 hat_area[i][hat_draw[i]].h = 8; |
|
201 |
|
202 if (hat_pos & SDL_HAT_UP) { |
|
203 hat_area[i][hat_draw[i]].y = 0; |
|
204 } else if (hat_pos & SDL_HAT_DOWN) { |
|
205 hat_area[i][hat_draw[i]].y = SCREEN_HEIGHT-8; |
|
206 } |
|
207 |
|
208 if (hat_pos & SDL_HAT_LEFT) { |
|
209 hat_area[i][hat_draw[i]].x = 0; |
|
210 } else if (hat_pos & SDL_HAT_RIGHT) { |
|
211 hat_area[i][hat_draw[i]].x = SCREEN_WIDTH-8; |
|
212 } |
|
213 |
|
214 /* Draw it */ |
|
215 SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE); |
|
216 SDL_RenderFillRect(screen, &hat_area[i][hat_draw[i]]); |
|
217 SDL_RenderPresent(screen); |
|
218 } |
|
219 } |
|
220 |
|
221 SDL_DestroyRenderer(screen); |
|
222 SDL_DestroyWindow(window); |
167 } |
223 } |
168 |
224 |
169 int |
225 int |
170 main(int argc, char *argv[]) |
226 main(int argc, char *argv[]) |
171 { |
227 { |