Merge branches 'master' and 'getitem'
[Projeto-PCG.git] / controlewii.cpp
blob1b3a67b2ea181e375a2c96cafbf90c9f1bf01026
1 #include "controlewii.h"
2 #include "usereventtype.h"
3 #include "weaponitem.h"
4 #include <cmath>
6 ControleWii::ControleWii(Player &p) : Controle(p) {
7 connected = false;
8 initializeWiimote();
9 stickX = 0xffff;
10 stickY = 0xffff;
11 buttonsNunchuck = 0;
12 buttonsWii = 0;
13 for (int i = 0; i < CWIID_IR_SRC_COUNT; i++)
14 cyclesSinceSeen[i] = -1;
17 void ControleWii::handleOther() {
18 Uint8 *keystates = SDL_GetKeyState( NULL );
19 if (keystates[SDLK_DOWN]) {
20 jogador.onGround = false;
21 jogador.bypass = true;
23 if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(1)) { //botao esquerdo do mouse pressionado
24 jogador.fire();
26 if (jogador.onGround &&
27 buttonsNunchuck & 2) { //pulo com botao C ou para cima
28 jogador.addSpeed( 0, -8);
29 jogador.onGround = false;
32 if (buttonsNunchuck & 1) {//descer com botao Z ou para baixo
33 jogador.onGround = false;
34 jogador.bypass = true;
37 if (buttonsWii & 4) { //R atira
38 jogador.fire();
43 void ControleWii::handleEvent(SDL_Event &e) {
44 std::list<WeaponItem*>::iterator it;
45 bool pegou = false;
46 switch( e.type ) {
47 case SDL_USEREVENT:
48 switch (e.user.code) {
49 case WIIMOTEEVENT:
50 cwiid_mesg_type* type = (cwiid_mesg_type*)e.user.data1;
51 union cwiid_mesg *mesg = (union cwiid_mesg*)e.user.data2;
52 switch (*type) {
53 case CWIID_MESG_IR: {
54 int sources = 0;
55 double x = 0.0;
56 double y = 0.0;
57 for (int j = 0; j < CWIID_IR_SRC_COUNT; j++) {
58 if (cyclesSinceSeen[j] != -1)
59 cyclesSinceSeen[j]++;
60 if (mesg->ir_mesg.src[j].valid) {
61 cyclesSinceSeen[j] = 0;
62 lastSeenAt[j].x = mesg->ir_mesg.src[j].pos[CWIID_X];
63 lastSeenAt[j].y = mesg->ir_mesg.src[j].pos[CWIID_Y];
66 for (int i = 0; i < CWIID_IR_SRC_COUNT; i++)
67 if (cyclesSinceSeen[i] != -1) {
68 x += lastSeenAt[i].x;
69 y += lastSeenAt[i].y;
70 sources++;
72 if (sources > 0) {
73 /*y = y / sources;
74 double angle = (y / 738.0) * PI;
75 y = jogador.getY() + jogador.pescoco().y - jogador.game->camera.y - 100 * cos(angle);
76 x = jogador.getX() + jogador.pescoco().x - jogador.game->camera.x + 100 * sin(angle);
77 jogador.setAim(x,y);*/
78 x = x / sources;
79 y = y / sources;
80 x = jogador.game->config->screen["width"] - (x / 1024.0) * jogador.game->config->screen["width"];
81 y = (y / 738.0) * jogador.game->config->screen["height"];
82 jogador.setAim(x+game->camera.x,y+game->camera.y);
85 break;
86 case CWIID_MESG_BTN: {
87 //printf("Button Report: %.4X\n", mesg->btn_mesg.buttons);
88 buttonsWii = mesg->btn_mesg.buttons;
89 if (mesg->btn_mesg.buttons & 4) { //R atira
90 jogador.fire();
93 break;
94 case CWIID_MESG_NUNCHUK: {
95 int newX = mesg->nunchuk_mesg.stick[CWIID_X] - 120;
96 int newY = mesg->nunchuk_mesg.stick[CWIID_Y] - 131;
98 printf("Nunchuk Report: btns=%.2X stick=(%d,%d) acc.x=%d acc.y=%d "
99 "acc.z=%d\n", mesg->nunchuk_mesg.buttons,
100 newX,
101 newY,
102 mesg->nunchuk_mesg.acc[CWIID_X],
103 mesg->nunchuk_mesg.acc[CWIID_Y],
104 mesg->nunchuk_mesg.acc[CWIID_Z]);*/
106 if (stickX != 0xffff) { //para calibrar
107 //jogador.addSpeed(-3.0*stickX/128,0);
108 jogador.setSpeed(3.0*newX/128,jogador.getSpeedY());
110 stickX = newX;
111 stickY = newY;
112 buttonsNunchuck = mesg->nunchuk_mesg.buttons;
114 if (jogador.onGround &&
115 ((mesg->nunchuk_mesg.buttons & 2) ||
116 newY > 60)) { //pulo com botao C ou para cima
117 jogador.addSpeed( 0, -8);
118 jogador.onGround = false;
121 if (newY < -60 || (mesg->nunchuk_mesg.buttons & 1)) {//descer com botao Z ou para baixo
122 jogador.onGround = false;
123 jogador.bypass = true;
126 break;
129 free(e.user.data1);
130 free(e.user.data2);
131 break;
133 break;
134 case SDL_MOUSEMOTION: {
135 if (!connected)
136 jogador.setAim(e.motion.x+game->camera.x,e.motion.y+game->camera.y);
138 break;
139 case SDL_MOUSEBUTTONDOWN:
140 jogador.fire();
141 break;
142 case SDL_KEYUP:
143 switch (e.key.keysym.sym) {
144 case SDLK_LEFT:
145 jogador.addSpeed(3,0);
146 break;
147 case SDLK_RIGHT:
148 jogador.addSpeed(-3,0);
149 break;
150 default: break;
152 break;
153 case SDL_KEYDOWN:
154 switch (e.key.keysym.sym) {
155 case SDLK_DOWN:
156 jogador.onGround = false;
157 jogador.bypass = true;
158 break;
159 case SDLK_UP:
160 if (jogador.onGround) {
161 jogador.addSpeed( 0, -8);
162 jogador.onGround = false;
164 break;
165 case SDLK_LEFT:
166 jogador.addSpeed(-3, 0);
167 break;
168 case SDLK_RIGHT:
169 jogador.addSpeed( 3, 0);
170 break;
171 case SDLK_RETURN:
172 for (it = map->items.begin(); it != map->items.end(); ++it)
173 if (game->collisionManager->checkCollision(&jogador, (Thing*) *it)) {
174 jogador.equip((*it)->getWeapon());
175 pegou = true;
176 break;
178 if (pegou)
179 map->items.erase(it);
180 break;
181 default: break;
183 break;
187 #define toggle_bit(bf,b) \
188 (bf) = ((bf) & b) \
189 ? ((bf) & ~(b)) \
190 : ((bf) | (b))
192 cwiid_err_t err;
193 void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap)
195 if (wiimote) printf("%d:", cwiid_get_id(wiimote));
196 else printf("-1:");
197 vprintf(s, ap);
198 printf("\n");
201 void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count,
202 union cwiid_mesg mesg[], struct timespec *timestamp)
204 int i, j;
205 int valid_source;
209 for (i=0; i < mesg_count; i++)
211 SDL_Event event;
212 SDL_UserEvent userevent;
213 userevent.type = SDL_USEREVENT;
214 event.type = SDL_USEREVENT;
215 userevent.code = WIIMOTEEVENT;
216 cwiid_mesg_type *type = (cwiid_mesg_type*)malloc(sizeof(cwiid_mesg_type));
217 *type = mesg[i].type;
218 userevent.data1 = (void*)type;
219 union cwiid_mesg *m = (union cwiid_mesg *)malloc(sizeof(union cwiid_mesg));
220 *m = mesg[i];
221 userevent.data2 = m;
222 event.user = userevent;
223 SDL_PushEvent(&event);
224 /*switch (mesg[i].type) {
225 case CWIID_MESG_STATUS:
226 printf("Status Report: battery=%d extension=",
227 mesg[i].status_mesg.battery);
228 switch (mesg[i].status_mesg.ext_type) {
229 case CWIID_EXT_NONE:
230 printf("none");
231 break;
232 case CWIID_EXT_NUNCHUK:
233 printf("Nunchuk");
234 break;
235 case CWIID_EXT_CLASSIC:
236 printf("Classic Controller");
237 break;
238 default:
239 printf("Unknown Extension");
240 break;
242 printf("\n");
243 break;
244 case CWIID_MESG_BTN:
245 printf("Button Report: %.4X\n", mesg[i].btn_mesg.buttons);
246 break;
247 case CWIID_MESG_ACC:
248 printf("Acc Report: x=%d, y=%d, z=%d\n",
249 mesg[i].acc_mesg.acc[CWIID_X],
250 mesg[i].acc_mesg.acc[CWIID_Y],
251 mesg[i].acc_mesg.acc[CWIID_Z]);
252 break;
253 case CWIID_MESG_IR:
254 printf("IR Report: ");
255 valid_source = 0;
256 for (j = 0; j < CWIID_IR_SRC_COUNT; j++) {
257 if (mesg[i].ir_mesg.src[j].valid) {
258 valid_source = 1;
259 printf("(%d,%d) ", mesg[i].ir_mesg.src[j].pos[CWIID_X],
260 mesg[i].ir_mesg.src[j].pos[CWIID_Y]);
263 if (!valid_source) {
264 printf("no sources detected");
266 printf("\n");
267 break;
268 case CWIID_MESG_NUNCHUK: {
269 printf("Nunchuk Report: btns=%.2X stick=(%d,%d) acc.x=%d acc.y=%d "
270 "acc.z=%d\n", mesg[i].nunchuk_mesg.buttons,
271 mesg[i].nunchuk_mesg.stick[CWIID_X],
272 mesg[i].nunchuk_mesg.stick[CWIID_Y],
273 mesg[i].nunchuk_mesg.acc[CWIID_X],
274 mesg[i].nunchuk_mesg.acc[CWIID_Y],
275 mesg[i].nunchuk_mesg.acc[CWIID_Z]);
276 break;
278 case CWIID_MESG_CLASSIC:
279 printf("Classic Report: btns=%.4X l_stick=(%d,%d) r_stick=(%d,%d) "
280 "l=%d r=%d\n", mesg[i].classic_mesg.buttons,
281 mesg[i].classic_mesg.l_stick[CWIID_X],
282 mesg[i].classic_mesg.l_stick[CWIID_Y],
283 mesg[i].classic_mesg.r_stick[CWIID_X],
284 mesg[i].classic_mesg.r_stick[CWIID_Y],
285 mesg[i].classic_mesg.l, mesg[i].classic_mesg.r);
286 break;
287 case CWIID_MESG_ERROR:
288 if (cwiid_close(wiimote)) {
289 fprintf(stderr, "Error on wiimote disconnect\n");
290 exit(-1);
292 exit(0);
293 break;
294 default:
295 printf("Unknown Report");
296 break;
301 void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state)
303 if (cwiid_set_led(wiimote, led_state)) {
304 fprintf(stderr, "Error setting LEDs \n");
308 void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode)
310 if (cwiid_set_rpt_mode(wiimote, rpt_mode)) {
311 fprintf(stderr, "Error setting report mode\n");
315 bool ControleWii::ControleWii::initializeWiimote() {
316 mesg = 0;
317 led_state = 0;
318 rpt_mode = 0;
319 rumble = 0;
321 cwiid_set_err(err);
323 bdaddr = *BDADDR_ANY;
325 /* Connect to the wiimote */
326 printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
327 if (!(wiimote = cwiid_open(&bdaddr, 0))) {
328 fprintf(stderr, "Unable to connect to wiimote\n");
329 return false;
331 if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
332 fprintf(stderr, "Unable to set message callback\n");
333 return false;
336 toggle_bit(rpt_mode, CWIID_RPT_BTN); //abilita status report de botoes
337 //set_rpt_mode(wiimote, rpt_mode);
339 toggle_bit(rpt_mode, CWIID_RPT_NUNCHUK); //abilita status report de nunchuck
340 //set_rpt_mode(wiimote, rpt_mode);
342 toggle_bit(led_state, CWIID_LED4_ON); //liga quarto LED para indentificar ligaƧao com o NOSSO jogo e nao o wii
343 set_led_state(wiimote, led_state);
345 toggle_bit(rpt_mode, CWIID_RPT_IR); //abilita recebimento do infravermelho
346 set_rpt_mode(wiimote, rpt_mode);
348 if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
349 fprintf(stderr, "Error enabling messages\n");
350 return false;
352 connected = true;
353 return true;