Mercurial > sdl-ios-xcode
annotate test/testjoystick.c @ 4920:a4032241deb5
Fixed massive stack memory usage in the gesture functions
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 30 Nov 2010 18:07:31 -0800 |
parents | f55c87ae336b |
children |
rev | line source |
---|---|
0 | 1 |
2 /* Simple program to test the SDL joystick routines */ | |
3 | |
4 #include <stdio.h> | |
5 #include <stdlib.h> | |
6 #include <string.h> | |
7 | |
8 #include "SDL.h" | |
9 | |
2765
f55c87ae336b
Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
2201
diff
changeset
|
10 #ifdef __IPHONEOS__ |
f55c87ae336b
Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
2201
diff
changeset
|
11 #define SCREEN_WIDTH 320 |
f55c87ae336b
Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
2201
diff
changeset
|
12 #define SCREEN_HEIGHT 480 |
f55c87ae336b
Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
2201
diff
changeset
|
13 #else |
0 | 14 #define SCREEN_WIDTH 640 |
15 #define SCREEN_HEIGHT 480 | |
2765
f55c87ae336b
Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
2201
diff
changeset
|
16 #endif |
0 | 17 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
18 void |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
19 WatchJoystick(SDL_Joystick * joystick) |
0 | 20 { |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
21 SDL_Surface *screen; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
22 const char *name; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
23 int i, done; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
24 SDL_Event event; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
25 int x, y, draw; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
26 SDL_Rect axis_area[6][2]; |
0 | 27 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
28 /* Set a video mode to display joystick axis position */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
29 screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16, 0); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
30 if (screen == NULL) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
31 fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError()); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
32 return; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
33 } |
0 | 34 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
35 /* Print info about the joystick we are watching */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
36 name = SDL_JoystickName(SDL_JoystickIndex(joystick)); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
37 printf("Watching joystick %d: (%s)\n", SDL_JoystickIndex(joystick), |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
38 name ? name : "Unknown Joystick"); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
39 printf("Joystick has %d axes, %d hats, %d balls, and %d buttons\n", |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
40 SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick), |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
41 SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick)); |
0 | 42 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
43 /* Initialize drawing rectangles */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
44 memset(axis_area, 0, (sizeof axis_area)); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
45 draw = 0; |
0 | 46 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
47 /* Loop, getting joystick events! */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
48 done = 0; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
49 while (!done) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
50 while (SDL_PollEvent(&event)) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
51 switch (event.type) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
52 case SDL_JOYAXISMOTION: |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
53 printf("Joystick %d axis %d value: %d\n", |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
54 event.jaxis.which, |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
55 event.jaxis.axis, event.jaxis.value); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
56 break; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
57 case SDL_JOYHATMOTION: |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
58 printf("Joystick %d hat %d value:", |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
59 event.jhat.which, event.jhat.hat); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
60 if (event.jhat.value == SDL_HAT_CENTERED) |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
61 printf(" centered"); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
62 if (event.jhat.value & SDL_HAT_UP) |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
63 printf(" up"); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
64 if (event.jhat.value & SDL_HAT_RIGHT) |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
65 printf(" right"); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
66 if (event.jhat.value & SDL_HAT_DOWN) |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
67 printf(" down"); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
68 if (event.jhat.value & SDL_HAT_LEFT) |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
69 printf(" left"); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
70 printf("\n"); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
71 break; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
72 case SDL_JOYBALLMOTION: |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
73 printf("Joystick %d ball %d delta: (%d,%d)\n", |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
74 event.jball.which, |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
75 event.jball.ball, event.jball.xrel, event.jball.yrel); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
76 break; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
77 case SDL_JOYBUTTONDOWN: |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
78 printf("Joystick %d button %d down\n", |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
79 event.jbutton.which, event.jbutton.button); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
80 break; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
81 case SDL_JOYBUTTONUP: |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
82 printf("Joystick %d button %d up\n", |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
83 event.jbutton.which, event.jbutton.button); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
84 break; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
85 case SDL_KEYDOWN: |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
86 if (event.key.keysym.sym != SDLK_ESCAPE) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
87 break; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
88 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
89 /* Fall through to signal quit */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
90 case SDL_QUIT: |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
91 done = 1; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
92 break; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
93 default: |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
94 break; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
95 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
96 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
97 /* Update visual joystick state */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
98 for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
99 SDL_Rect area; |
0 | 100 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
101 area.x = i * 34; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
102 area.y = SCREEN_HEIGHT - 34; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
103 area.w = 32; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
104 area.h = 32; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
105 if (SDL_JoystickGetButton(joystick, i) == SDL_PRESSED) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
106 SDL_FillRect(screen, &area, 0xFFFF); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
107 } else { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
108 SDL_FillRect(screen, &area, 0x0000); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
109 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
110 SDL_UpdateRects(screen, 1, &area); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
111 } |
0 | 112 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
113 for (i = 0; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
114 i < SDL_JoystickNumAxes(joystick) / 2 |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
115 && i < SDL_arraysize(axis_area); ++i) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
116 /* Erase previous axes */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
117 SDL_FillRect(screen, &axis_area[i][draw], 0x0000); |
0 | 118 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
119 /* Draw the X/Y axis */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
120 draw = !draw; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
121 x = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 0)) + 32768); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
122 x *= SCREEN_WIDTH; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
123 x /= 65535; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
124 if (x < 0) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
125 x = 0; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
126 } else if (x > (SCREEN_WIDTH - 16)) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
127 x = SCREEN_WIDTH - 16; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
128 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
129 y = (((int) SDL_JoystickGetAxis(joystick, i * 2 + 1)) + 32768); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
130 y *= SCREEN_HEIGHT; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
131 y /= 65535; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
132 if (y < 0) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
133 y = 0; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
134 } else if (y > (SCREEN_HEIGHT - 16)) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
135 y = SCREEN_HEIGHT - 16; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
136 } |
2765
f55c87ae336b
Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
2201
diff
changeset
|
137 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
138 axis_area[i][draw].x = (Sint16) x; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
139 axis_area[i][draw].y = (Sint16) y; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
140 axis_area[i][draw].w = 16; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
141 axis_area[i][draw].h = 16; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
142 SDL_FillRect(screen, &axis_area[i][draw], 0xFFFF); |
1854 | 143 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
144 SDL_UpdateRects(screen, 2, axis_area[i]); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
145 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
146 } |
0 | 147 } |
148 | |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
149 int |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
150 main(int argc, char *argv[]) |
0 | 151 { |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
152 const char *name; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
153 int i; |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
154 SDL_Joystick *joystick; |
0 | 155 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
156 /* Initialize SDL (Note: video is required to start event loop) */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
157 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
158 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
159 exit(1); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
160 } |
0 | 161 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
162 /* Print information about the joysticks */ |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
163 printf("There are %d joysticks attached\n", SDL_NumJoysticks()); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
164 for (i = 0; i < SDL_NumJoysticks(); ++i) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
165 name = SDL_JoystickName(i); |
2201 | 166 printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick"); |
2200
893c862eed86
Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents:
1895
diff
changeset
|
167 joystick = SDL_JoystickOpen(i); |
893c862eed86
Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents:
1895
diff
changeset
|
168 if (joystick == NULL) { |
2201 | 169 fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i, |
170 SDL_GetError()); | |
2200
893c862eed86
Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents:
1895
diff
changeset
|
171 } else { |
893c862eed86
Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents:
1895
diff
changeset
|
172 printf(" axes: %d\n", SDL_JoystickNumAxes(joystick)); |
893c862eed86
Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents:
1895
diff
changeset
|
173 printf(" balls: %d\n", SDL_JoystickNumBalls(joystick)); |
893c862eed86
Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents:
1895
diff
changeset
|
174 printf(" hats: %d\n", SDL_JoystickNumHats(joystick)); |
893c862eed86
Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents:
1895
diff
changeset
|
175 printf(" buttons: %d\n", SDL_JoystickNumButtons(joystick)); |
893c862eed86
Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents:
1895
diff
changeset
|
176 SDL_JoystickClose(joystick); |
893c862eed86
Merged r3292:3293 from branches/SDL-1.2: testjoystick verbose info.
Ryan C. Gordon <icculus@icculus.org>
parents:
1895
diff
changeset
|
177 } |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
178 } |
0 | 179 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
180 if (argv[1]) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
181 joystick = SDL_JoystickOpen(atoi(argv[1])); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
182 if (joystick == NULL) { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
183 printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
184 SDL_GetError()); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
185 } else { |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
186 WatchJoystick(joystick); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
187 SDL_JoystickClose(joystick); |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
188 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
189 } |
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
190 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); |
0 | 191 |
1895
c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
1855
diff
changeset
|
192 return (0); |
0 | 193 } |