140
|
1 #include <SDL.h>
|
|
2 #include "Joystick.h"
|
|
3
|
|
4 static const int CROSS = 0;
|
|
5 static const int CIRCLE = 1;
|
|
6 static const int SQUARE = 2;
|
|
7 static const int TRIANGLE = 3;
|
|
8 static const int L1 = 4;
|
|
9 static const int R1 = 5;
|
|
10 static const int L2 = 6;
|
|
11 static const int R2 = 7;
|
|
12 static const int START = 8;
|
|
13 static const int SELECT = 9;
|
|
14 static const int L3 = 10;
|
|
15 static const int R3 = 11;
|
|
16 static const int UP = 12;
|
|
17 static const int DOWN = 13;
|
|
18 static const int RIGHT = 14;
|
|
19 static const int LEFT = 15;
|
|
20 static const int ESCAPE = 16;
|
|
21 static const int SPACE = 17;
|
|
22
|
|
23 Joystick::Joystick(SDL_Joystick *j)
|
|
24 {
|
|
25 joy = j;
|
|
26 }
|
|
27
|
|
28 void
|
|
29 Joystick::check(void)
|
|
30 {
|
|
31 SDL_JoystickUpdate();
|
|
32
|
|
33 if (SDL_JoystickGetButton(joy,CROSS)==SDL_PRESSED) {
|
|
34 cross.push_work();
|
|
35 } else {
|
|
36 cross.release_work();
|
|
37 }
|
|
38
|
|
39 if (SDL_JoystickGetButton(joy,CIRCLE)==SDL_PRESSED) {
|
|
40 circle.push_work();
|
|
41 } else {
|
|
42 circle.release_work();
|
|
43 }
|
|
44
|
|
45 if (SDL_JoystickGetButton(joy,SQUARE)==SDL_PRESSED) {
|
|
46 square.push_work();
|
|
47 } else {
|
|
48 square.release_work();
|
|
49 }
|
|
50
|
|
51 if (SDL_JoystickGetButton(joy,TRIANGLE)==SDL_PRESSED) {
|
|
52 triangle.push_work();
|
|
53 } else {
|
|
54 triangle.release_work();
|
|
55 }
|
|
56
|
|
57 if (SDL_JoystickGetButton(joy,L1)==SDL_PRESSED) {
|
|
58 l1.push_work();
|
|
59 } else {
|
|
60 l1.release_work();
|
|
61 }
|
|
62
|
|
63 if (SDL_JoystickGetButton(joy,R1)==SDL_PRESSED) {
|
|
64 r1.push_work();
|
|
65 } else {
|
|
66 r1.release_work();
|
|
67 }
|
|
68
|
|
69 if (SDL_JoystickGetButton(joy,L2)==SDL_PRESSED) {
|
|
70 l2.push_work();
|
|
71 } else {
|
|
72 l2.release_work();
|
|
73 }
|
|
74
|
|
75 if (SDL_JoystickGetButton(joy,R2)==SDL_PRESSED) {
|
|
76 r2.push_work();
|
|
77 } else {
|
|
78 r2.release_work();
|
|
79 }
|
|
80
|
|
81 if (SDL_JoystickGetButton(joy,START)==SDL_PRESSED) {
|
|
82 start.push_work();
|
|
83 } else {
|
|
84 start.release_work();
|
|
85 }
|
|
86
|
|
87 if (SDL_JoystickGetButton(joy,SELECT)==SDL_PRESSED) {
|
|
88 select.push_work();
|
|
89 } else {
|
|
90 select.release_work();
|
|
91 }
|
|
92
|
|
93 if (SDL_JoystickGetButton(joy,L3)==SDL_PRESSED) {
|
|
94 l3.push_work();
|
|
95 } else {
|
|
96 l3.release_work();
|
|
97 }
|
|
98
|
|
99 if (SDL_JoystickGetButton(joy,R3)==SDL_PRESSED) {
|
|
100 r3.push_work();
|
|
101 } else {
|
|
102 r3.release_work();
|
|
103 }
|
|
104
|
|
105 axis = SDL_JoystickGetAxis(joy,0);
|
|
106 if (axis >= 3200) {
|
|
107 left.release_work();
|
|
108 right.push_work();
|
|
109 } else if (axis <= -3200) {
|
|
110 right.release_work();
|
|
111 left.push_work();
|
|
112 } else {
|
|
113 left.release_work();
|
|
114 right.release_work();
|
|
115 }
|
|
116
|
|
117 axis = SDL_JoystickGetAxis(joy,1);
|
|
118 if (axis>=3200) {
|
|
119 up.release_work();
|
|
120 down.push_work();
|
|
121 } else if (axis<=-3200) {
|
|
122 down.release_work();
|
|
123 up.push_work();
|
|
124 } else {
|
|
125 up.release_work();
|
|
126 down.release_work();
|
|
127 }
|
|
128 }
|
|
129
|
|
130 void
|
|
131 Joystick::close(void)
|
|
132 {
|
|
133 SDL_JoystickClose(joy);
|
|
134 }
|