Mercurial > sdl-ios-xcode
comparison test/testjoystick.c @ 1662:782fd950bd46 SDL-1.3
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.
The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce
The headers are being converted to automatically generate doxygen documentation.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 28 May 2006 13:04:16 +0000 |
parents | 281d3f4870e5 |
children | 4da1ee79c9af |
comparison
equal
deleted
inserted
replaced
1661:281d3f4870e5 | 1662:782fd950bd46 |
---|---|
8 #include "SDL.h" | 8 #include "SDL.h" |
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 WatchJoystick(SDL_Joystick *joystick) | 13 void |
14 WatchJoystick (SDL_Joystick * joystick) | |
14 { | 15 { |
15 SDL_Surface *screen; | 16 SDL_Surface *screen; |
16 const char *name; | 17 const char *name; |
17 int i, done; | 18 int i, done; |
18 SDL_Event event; | 19 SDL_Event event; |
19 int x, y, draw; | 20 int x, y, draw; |
20 SDL_Rect axis_area[6][2]; | 21 SDL_Rect axis_area[6][2]; |
21 | 22 |
22 /* Set a video mode to display joystick axis position */ | 23 /* Set a video mode to display joystick axis position */ |
23 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0); | 24 screen = SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0); |
24 if ( screen == NULL ) { | 25 if (screen == NULL) { |
25 fprintf(stderr, "Couldn't set video mode: %s\n",SDL_GetError()); | 26 fprintf (stderr, "Couldn't set video mode: %s\n", SDL_GetError ()); |
26 return; | 27 return; |
27 } | 28 } |
28 | 29 |
29 /* Print info about the joystick we are watching */ | 30 /* Print info about the joystick we are watching */ |
30 name = SDL_JoystickName(SDL_JoystickIndex(joystick)); | 31 name = SDL_JoystickName (SDL_JoystickIndex (joystick)); |
31 printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick), | 32 printf ("Watching joystick %d: (%s)\n", SDL_JoystickIndex (joystick), |
32 name ? name : "Unknown Joystick"); | 33 name ? name : "Unknown Joystick"); |
33 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", |
34 SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick), | 35 SDL_JoystickNumAxes (joystick), SDL_JoystickNumHats (joystick), |
35 SDL_JoystickNumBalls(joystick),SDL_JoystickNumButtons(joystick)); | 36 SDL_JoystickNumBalls (joystick), |
37 SDL_JoystickNumButtons (joystick)); | |
36 | 38 |
37 /* Initialize drawing rectangles */ | 39 /* Initialize drawing rectangles */ |
38 memset(axis_area, 0, (sizeof axis_area)); | 40 memset (axis_area, 0, (sizeof axis_area)); |
39 draw = 0; | 41 draw = 0; |
40 | 42 |
41 /* Loop, getting joystick events! */ | 43 /* Loop, getting joystick events! */ |
42 done = 0; | 44 done = 0; |
43 while ( ! done ) { | 45 while (!done) { |
44 while ( SDL_PollEvent(&event) ) { | 46 while (SDL_PollEvent (&event)) { |
45 switch (event.type) { | 47 switch (event.type) { |
46 case SDL_JOYAXISMOTION: | 48 case SDL_JOYAXISMOTION: |
47 printf("Joystick %d axis %d value: %d\n", | 49 printf ("Joystick %d axis %d value: %d\n", |
48 event.jaxis.which, | 50 event.jaxis.which, |
49 event.jaxis.axis, | 51 event.jaxis.axis, event.jaxis.value); |
50 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, | 56 if (event.jhat.value == SDL_HAT_CENTERED) |
55 event.jhat.hat); | 57 printf (" centered"); |
56 if ( event.jhat.value == SDL_HAT_CENTERED ) | 58 if (event.jhat.value & SDL_HAT_UP) |
57 printf(" centered"); | 59 printf (" up"); |
58 if ( event.jhat.value & SDL_HAT_UP ) | 60 if (event.jhat.value & SDL_HAT_RIGHT) |
59 printf(" up"); | 61 printf (" right"); |
60 if ( event.jhat.value & SDL_HAT_RIGHT ) | 62 if (event.jhat.value & SDL_HAT_DOWN) |
61 printf(" right"); | 63 printf (" down"); |
62 if ( event.jhat.value & SDL_HAT_DOWN ) | 64 if (event.jhat.value & SDL_HAT_LEFT) |
63 printf(" down"); | 65 printf (" left"); |
64 if ( event.jhat.value & SDL_HAT_LEFT ) | 66 printf ("\n"); |
65 printf(" left"); | 67 break; |
66 printf("\n"); | 68 case SDL_JOYBALLMOTION: |
67 break; | 69 printf ("Joystick %d ball %d delta: (%d,%d)\n", |
68 case SDL_JOYBALLMOTION: | 70 event.jball.which, |
69 printf("Joystick %d ball %d delta: (%d,%d)\n", | 71 event.jball.ball, event.jball.xrel, event.jball.yrel); |
70 event.jball.which, | 72 break; |
71 event.jball.ball, | 73 case SDL_JOYBUTTONDOWN: |
72 event.jball.xrel, | 74 printf ("Joystick %d button %d down\n", |
73 event.jball.yrel); | 75 event.jbutton.which, event.jbutton.button); |
74 break; | 76 break; |
75 case SDL_JOYBUTTONDOWN: | 77 case SDL_JOYBUTTONUP: |
76 printf("Joystick %d button %d down\n", | 78 printf ("Joystick %d button %d up\n", |
77 event.jbutton.which, | 79 event.jbutton.which, event.jbutton.button); |
78 event.jbutton.button); | 80 break; |
79 break; | 81 case SDL_KEYDOWN: |
80 case SDL_JOYBUTTONUP: | 82 if (event.key.keysym.sym != SDLK_ESCAPE) { |
81 printf("Joystick %d button %d up\n", | 83 break; |
82 event.jbutton.which, | 84 } |
83 event.jbutton.button); | 85 /* Fall through to signal quit */ |
84 break; | 86 case SDL_QUIT: |
85 case SDL_KEYDOWN: | 87 done = 1; |
86 if ( event.key.keysym.sym != SDLK_ESCAPE ) { | 88 break; |
87 break; | 89 default: |
88 } | 90 break; |
89 /* Fall through to signal quit */ | 91 } |
90 case SDL_QUIT: | 92 } |
91 done = 1; | 93 /* Update visual joystick state */ |
92 break; | 94 for (i = 0; i < SDL_JoystickNumButtons (joystick); ++i) { |
93 default: | 95 SDL_Rect area; |
94 break; | |
95 } | |
96 } | |
97 /* Update visual joystick state */ | |
98 for ( i=0; i<SDL_JoystickNumButtons(joystick); ++i ) { | |
99 SDL_Rect area; | |
100 | 96 |
101 area.x = i*34; | 97 area.x = i * 34; |
102 area.y = SCREEN_HEIGHT-34; | 98 area.y = SCREEN_HEIGHT - 34; |
103 area.w = 32; | 99 area.w = 32; |
104 area.h = 32; | 100 area.h = 32; |
105 if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) { | 101 if (SDL_JoystickGetButton (joystick, i) == SDL_PRESSED) { |
106 SDL_FillRect(screen, &area, 0xFFFF); | 102 SDL_FillRect (screen, &area, 0xFFFF); |
107 } else { | 103 } else { |
108 SDL_FillRect(screen, &area, 0x0000); | 104 SDL_FillRect (screen, &area, 0x0000); |
109 } | 105 } |
110 SDL_UpdateRects(screen, 1, &area); | 106 SDL_UpdateRects (screen, 1, &area); |
111 } | 107 } |
112 | 108 |
113 for ( i=0; i<SDL_JoystickNumAxes(joystick)/2 && i < SDL_arraysize(axis_area); ++i ) { | 109 for (i = 0; |
114 /* Erase previous axes */ | 110 i < SDL_JoystickNumAxes (joystick) / 2 |
115 SDL_FillRect(screen, &axis_area[i][draw], 0x0000); | 111 && i < SDL_arraysize (axis_area); ++i) { |
112 /* Erase previous axes */ | |
113 SDL_FillRect (screen, &axis_area[i][draw], 0x0000); | |
116 | 114 |
117 /* Draw the X/Y axis */ | 115 /* Draw the X/Y axis */ |
118 draw = !draw; | 116 draw = !draw; |
119 x = (((int)SDL_JoystickGetAxis(joystick, i*2+0))+32768); | 117 x = (((int) SDL_JoystickGetAxis (joystick, i * 2 + 0)) + 32768); |
120 x *= SCREEN_WIDTH; | 118 x *= SCREEN_WIDTH; |
121 x /= 65535; | 119 x /= 65535; |
122 if ( x < 0 ) { | 120 if (x < 0) { |
123 x = 0; | 121 x = 0; |
124 } else | 122 } else if (x > (SCREEN_WIDTH - 16)) { |
125 if ( x > (SCREEN_WIDTH-16) ) { | 123 x = SCREEN_WIDTH - 16; |
126 x = SCREEN_WIDTH-16; | 124 } |
127 } | 125 y = (((int) SDL_JoystickGetAxis (joystick, i * 2 + 1)) + 32768); |
128 y = (((int)SDL_JoystickGetAxis(joystick, i*2+1))+32768); | 126 y *= SCREEN_HEIGHT; |
129 y *= SCREEN_HEIGHT; | 127 y /= 65535; |
130 y /= 65535; | 128 if (y < 0) { |
131 if ( y < 0 ) { | 129 y = 0; |
132 y = 0; | 130 } else if (y > (SCREEN_HEIGHT - 16)) { |
133 } else | 131 y = SCREEN_HEIGHT - 16; |
134 if ( y > (SCREEN_HEIGHT-16) ) { | 132 } |
135 y = SCREEN_HEIGHT-16; | 133 axis_area[i][draw].x = (Sint16) x; |
136 } | 134 axis_area[i][draw].y = (Sint16) y; |
137 axis_area[i][draw].x = (Sint16)x; | 135 axis_area[i][draw].w = 16; |
138 axis_area[i][draw].y = (Sint16)y; | 136 axis_area[i][draw].h = 16; |
139 axis_area[i][draw].w = 16; | 137 SDL_FillRect (screen, &axis_area[i][draw], 0xFFFF); |
140 axis_area[i][draw].h = 16; | |
141 SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF); | |
142 | 138 |
143 SDL_UpdateRects(screen, 2, axis_area[i]); | 139 SDL_UpdateRects (screen, 2, axis_area[i]); |
144 } | 140 } |
145 } | 141 } |
146 } | 142 } |
147 | 143 |
148 int main(int argc, char *argv[]) | 144 int |
145 main (int argc, char *argv[]) | |
149 { | 146 { |
150 const char *name; | 147 const char *name; |
151 int i; | 148 int i; |
152 SDL_Joystick *joystick; | 149 SDL_Joystick *joystick; |
153 | 150 |
154 /* Initialize SDL (Note: video is required to start event loop) */ | 151 /* Initialize SDL (Note: video is required to start event loop) */ |
155 if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0 ) { | 152 if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { |
156 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); | 153 fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ()); |
157 exit(1); | 154 exit (1); |
158 } | 155 } |
159 | 156 |
160 /* Print information about the joysticks */ | 157 /* Print information about the joysticks */ |
161 printf("There are %d joysticks attached\n", SDL_NumJoysticks()); | 158 printf ("There are %d joysticks attached\n", SDL_NumJoysticks ()); |
162 for ( i=0; i<SDL_NumJoysticks(); ++i ) { | 159 for (i = 0; i < SDL_NumJoysticks (); ++i) { |
163 name = SDL_JoystickName(i); | 160 name = SDL_JoystickName (i); |
164 printf("Joystick %d: %s\n",i,name ? name : "Unknown Joystick"); | 161 printf ("Joystick %d: %s\n", i, name ? name : "Unknown Joystick"); |
165 } | 162 } |
166 | 163 |
167 if ( argv[1] ) { | 164 if (argv[1]) { |
168 joystick = SDL_JoystickOpen(atoi(argv[1])); | 165 joystick = SDL_JoystickOpen (atoi (argv[1])); |
169 if ( joystick == NULL ) { | 166 if (joystick == NULL) { |
170 printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), | 167 printf ("Couldn't open joystick %d: %s\n", atoi (argv[1]), |
171 SDL_GetError()); | 168 SDL_GetError ()); |
172 } else { | 169 } else { |
173 WatchJoystick(joystick); | 170 WatchJoystick (joystick); |
174 SDL_JoystickClose(joystick); | 171 SDL_JoystickClose (joystick); |
175 } | 172 } |
176 } | 173 } |
177 SDL_QuitSubSystem(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK); | 174 SDL_QuitSubSystem (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); |
178 | 175 |
179 return(0); | 176 return (0); |
180 } | 177 } |