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