Debian Codegraph @

input.cpp

raw symbols SCIP
1 /* ************************************************************************* *
2 SDL-Ball - DX-Ball/Breakout remake with openGL and SDL for Linux
3 Copyright (C) 2008 Jimmy Christensen ( dusted at dusted dot dk )
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * ************************************************************************* */
18
19 #include <unistd.h>
20
21 class controllerClass {
22 private:
23 paddle_class *paddle;
24 bulletsClass *bullet;
25 ballManager *bMan;
26 #define ITEMSELECTTIME 300 /* Milliseconds before changing item */
27 int shotTime, itemSelectTime;
28 float accel;
29 SDL_Joystick *joystick;
30 Sint16 joystickx;
31 Sint16 joysticky;
32 Uint8 joystickbtnA, joystickbtnB;
33 bool joyBtnALock; //TODO: implement this in the rest of the joystick functions (current only using it in cal)
34 GLfloat joystickLeftX; //There are two because the calibrated values differ
35 GLfloat joystickRightX;
36 int calMin, calMax, calLowJitter, calHighJitter;
37
38 #ifdef WITH_WIIUSE
39 wiimote** wiimotes;
40 float motePitch;
41 GLfloat moteAccel;
42 #endif
43
44 public:
45 controllerClass(paddle_class *pc, bulletsClass *bu, ballManager *bm);
46 ~controllerClass();
47 void movePaddle(GLfloat px);
48 void btnPress();
49 bool get();
50 void calibrate();
51 #ifdef WITH_WIIUSE
52 bool connectMote();
53 #endif
54 };
55
56 controllerClass::controllerClass(paddle_class *pc, bulletsClass *bu, ballManager *bm)
57 {
58 paddle = pc;
59 bullet = bu;
60 bMan = bm;
61 shotTime=200;
62 itemSelectTime=0;
63
64 //Try to open a joystick.
65 if(setting.joyEnabled && SDL_NumJoysticks() > 0)
66 {
67 joystickLeftX = setting.controlMaxSpeed / setting.JoyCalMin;
68 joystickRightX = setting.controlMaxSpeed / setting.JoyCalMax;
69 joystick = NULL;
70 joystick = SDL_JoystickOpen(0);
71 if(SDL_JoystickOpened(0))
72 {
73 cout << "Using joystick: '"<<SDL_JoystickName(0)<<"' as "<<(setting.joyIsDigital ? "digital":"analog")<<"."<<endl;
74 SDL_JoystickEventState( SDL_ENABLE );
75 } else {
76 cout << "Failed to open joystick: '"<<SDL_JoystickName(0)<<"'"<<endl;
77 }
78 }
79
80 #ifdef WITH_WIIUSE
81 var.wiiConnect=0;
82 moteAccel = setting.controlMaxSpeed / 90.0;
83 #endif
84
85 }
86
87 controllerClass::~controllerClass()
88 {
89 SDL_JoystickClose(0);
90
91 #ifdef WITH_WIIUSE
92 if(var.wiiConnect)
93 {
94 wiiuse_cleanup(wiimotes, MAX_WIIMOTES);
95 }
96 #endif
97 }
98
99 void controllerClass::movePaddle(GLfloat px)
100 {
101 if(!var.paused)
102 {
103 paddle->posx = px;
104 if(paddle->posx > 1.66-paddle->width-0.06)
105 {
106 paddle->posx=1.66-paddle->width-0.06;
107 }
108 else if(paddle->posx < -1.66+paddle->width+0.06)
109 {
110 paddle->posx=-1.66+paddle->width+0.06;
111 }
112 }
113 }
114
115 void controllerClass::btnPress()
116 {
117 if(var.titleScreenShow)
118 {
119 var.titleScreenShow=0;
120 SDL_WarpMouse(var.halfresx,0);
121 return;
122 }
123
124 struct pos p;
125 if(shotTime > 150)
126 {
127 shotTime=0;
128 if(!var.menu && !var.paused)
129 {
130 bMan->unglue();
131 var.startedPlaying=1;
132 if(player.powerup[PO_GUN])
133 {
134 p.x = paddle->posx-paddle->width/1.5;
135 p.y = paddle->posy;
136 bullet->shoot(p);
137 p.x = paddle->posx+paddle->width/1.5;
138 bullet->shoot(p);
139 }
140 }
141 }
142 }
143
144 bool controllerClass::get()
145 {
146 Uint8 *keyStates;
147 Uint8 keyDown[3]; //Need this since its not a good idea to write to keyStates for some reason
148 shotTime += globalTicks;
149 SDL_PumpEvents();
150 keyStates = SDL_GetKeyState( NULL );
151 keyDown[0] = keyStates[setting.keyLeft];
152 keyDown[1] = keyStates[setting.keyRight];
153 keyDown[2] = keyStates[setting.keyShoot];
154
155 itemSelectTime += globalTicks;
156 //Read joystick here so we can override keypresses if the joystick is digital
157 //We shouldn't need to check if the joystick is enabled, since it won't be opened if its not enabled anyway.
158 if(setting.joyEnabled && SDL_JoystickOpened(0))
159 {
160 joystickx = SDL_JoystickGetAxis(joystick, 0);
161 joysticky = SDL_JoystickGetAxis(joystick, 1);
162 joystickbtnA = SDL_JoystickGetButton(joystick, 0);
163 joystickbtnB = SDL_JoystickGetButton(joystick, 1);
164
165
166 if(joystickbtnA)
167 {
168 keyDown[2] = 1;
169 }
170 if(joystickbtnB && itemSelectTime > ITEMSELECTTIME)
171 {
172 itemSelectTime=0;
173 gVar.shopBuyItem=1;
174 }
175
176 if(setting.joyIsDigital)
177 {
178 if(joystickx < -200)
179 {
180 keyDown[0]=1;
181 } else if(joystickx > 200)
182 {
183 keyDown[1]=1;
184 }
185 if(joysticky < -200 && itemSelectTime > ITEMSELECTTIME)
186 {
187 itemSelectTime=0;
188 gVar.shopNextItem = 1;
189 } else if(joysticky > 200 && itemSelectTime > ITEMSELECTTIME)
190 {
191 itemSelectTime=0;
192 gVar.shopPrevItem = 1;
193 }
194
195 } else {
196 GLfloat x = 0.0; //This is the actual traveling speed of the paddle
197 if(joystickx > setting.JoyCalHighJitter)
198 {
199 x = joystickRightX * joystickx;
200 } else if(joystickx < setting.JoyCalLowJitter)
201 {
202 x = -(joystickLeftX * joystickx);
203 }
204
205 if(joysticky < setting.JoyCalLowJitter && itemSelectTime > ITEMSELECTTIME)
206 {
207 itemSelectTime=0;
208 gVar.shopNextItem = 1;
209 } else if(joysticky > setting.JoyCalHighJitter && itemSelectTime > ITEMSELECTTIME)
210 {
211 itemSelectTime=0;
212 gVar.shopPrevItem = 1;
213 }
214 //Move the paddle:
215 movePaddle( paddle->posx += (x*globalMilliTicks) );
216 }
217 }
218
219 #ifdef WITH_WIIUSE
220 if(var.wiiConnect)
221 {
222 if (wiiuse_poll(wiimotes, MAX_WIIMOTES)) {
223 switch(wiimotes[0]->event)
224 {
225 case WIIUSE_EVENT:
226 if(IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_TWO))
227 {
228 keyDown[2]=1;
229 } else if(IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_ONE) && itemSelectTime > ITEMSELECTTIME)
230 {
231 gVar.shopBuyItem = 1;
232 itemSelectTime=0;
233 }else if(IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_UP) && itemSelectTime > ITEMSELECTTIME)
234 {
235 itemSelectTime=0;
236 gVar.shopPrevItem = 1;
237 }else if(IS_PRESSED(wiimotes[0], WIIMOTE_BUTTON_DOWN) && itemSelectTime > ITEMSELECTTIME)
238 {
239 itemSelectTime=0;
240 gVar.shopNextItem = 1;
241 } else if(WIIUSE_USING_ACC(wiimotes[0]))
242 {
243 motePitch = wiimotes[0]->orient.pitch;
244 motePitch *=-1;
245 }
246
247 break;
248 case WIIUSE_DISCONNECT:
249 case WIIUSE_UNEXPECTED_DISCONNECT:
250 var.wiiConnect=0;
251 cout << "WiiMote disconnected." << endl;
252 wiiuse_cleanup(wiimotes, MAX_WIIMOTES);
253 break;
254 }
255 }
256 if(motePitch < -0.2 || motePitch > 0.2)
257 {
258 movePaddle( paddle->posx += ( moteAccel*motePitch)*globalMilliTicks );
259 }
260 }
261 #endif
262
263 //React to keystates here, this way, if joyisdig it will press keys
264 if(keyDown[0])
265 {
266 accel+=globalMilliTicks*setting.controlAccel;
267 if(accel > setting.controlMaxSpeed)
268 accel=setting.controlMaxSpeed;
269 movePaddle( paddle->posx - ( accel*globalMilliTicks) );
270 } else if(keyDown[1])
271 {
272 accel+=globalMilliTicks*setting.controlAccel;
273 if(accel > setting.controlMaxSpeed)
274 accel=setting.controlMaxSpeed;
275 movePaddle( paddle->posx + ( accel*globalMilliTicks) );
276 } else {
277 accel = setting.controlStartSpeed;
278 }
279
280 if(keyDown[2])
281 {
282 btnPress();
283 return(1);
284 } else {
285 return(0);
286 }
287 }
288
289 void controllerClass::calibrate()
290 {
291
292 Sint16 x=0;
293 if(SDL_JoystickOpened(0))
294 {
295
296 x = SDL_JoystickGetAxis(joystick, 0);
297
298 if(SDL_JoystickGetButton(joystick, 0))
299 {
300 if(!joyBtnALock && var.menuJoyCalStage != 5)
301 {
302 var.menuJoyCalStage++;
303 joyBtnALock=1;
304 }
305 } else {
306 joyBtnALock=0;
307 }
308 }
309
310 switch(var.menuJoyCalStage)
311 {
312 case 0:
313 calMin=0;
314 calMax=0;
315 calLowJitter=0;
316 calHighJitter=0;
317 break;
318 case 1:
319 if(x < calLowJitter)
320 calLowJitter=x;
321 else if(x > calHighJitter)
322 calHighJitter=x;
323 break;
324 case 2:
325 if(x < calMin)
326 calMin=x;
327 break;
328 case 3:
329 if(x > calMax)
330 calMax=x;
331 break;
332 case 4:
333 setting.JoyCalMin=calMin;
334 setting.JoyCalMax=calMax;
335 setting.JoyCalLowJitter=calLowJitter;
336 setting.JoyCalHighJitter=calHighJitter;
337 cout << "Joystick calibration report:" << endl;
338 cout << "calMin: " << calMin << endl << "calMax: " << calMax << endl;
339 cout << "lowJit: " << calLowJitter << endl << "higJit: " << calHighJitter << endl;
340 var.menuJoyCalStage++;
341 writeSettings();
342 break;
343 #ifdef WITH_WIIUSE
344 case -1: //We do this to make it draw a frame before freezing (searching)
345 var.menuJoyCalStage--;
346 break;
347 case -2:
348 wiimotes = wiiuse_init(MAX_WIIMOTES);
349 if(wiiuse_find(wiimotes, MAX_WIIMOTES, 4))
350 {
351 var.wiiConnect=1;
352 } else {
353 var.wiiConnect=0;
354 }
355 if(var.wiiConnect)
356 {
357 var.wiiConnect=0;
358 if(wiiuse_connect(wiimotes, MAX_WIIMOTES))
359 {
360 var.wiiConnect=1;
361 wiiuse_set_leds(wiimotes[0], WIIMOTE_LED_1);
362 wiiuse_rumble(wiimotes[0], 1);
363 usleep(500000);
364 wiiuse_rumble(wiimotes[0], 0);
365 wiiuse_motion_sensing(wiimotes[0], 1);
366 }
367 var.menuJoyCalStage=-3;
368 } else {
369 var.menuJoyCalStage=-4;
370 }
371 break;
372 #endif
373 }
374 }
375