Nunchuck + botoes funcionando, falta mira.
[Projeto-PCG.git] / controlewii.cpp
blob3c94ae55e9ffb50c68570c475cdf06748a46d702
1 #include "controlewii.h"
2 #include "usereventtype.h"
5 ControleWii::ControleWii(Player &p) : Controle(p) {
6 initializeWiimote();
7 stickX = 0xffff;
8 stickY = 0xffff;
9 buttonsNunchuck = 0;
10 buttonsWii = 0;
13 void ControleWii::handleOther() {
14 Uint8 *keystates = SDL_GetKeyState( NULL );
15 if (keystates[SDLK_DOWN]) {
16 jogador.onGround = false;
17 jogador.bypass = true;
19 if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(1)) { //botao esquerdo do mouse pressionado
20 jogador.fire();
22 if (jogador.onGround &&
23 buttonsNunchuck & 2) { //pulo com botao C ou para cima
24 jogador.addSpeed( 0, -8);
25 jogador.onGround = false;
28 if (buttonsNunchuck & 1) {//descer com botao Z ou para baixo
29 jogador.onGround = false;
30 jogador.bypass = true;
33 if (buttonsWii & 4) { //R atira
34 jogador.fire();
39 void ControleWii::handleEvent(SDL_Event &e) {
40 switch( e.type ) {
41 case SDL_USEREVENT:
42 switch (e.user.code) {
43 case WIIMOTEEVENT:
44 cwiid_mesg_type* type = (cwiid_mesg_type*)e.user.data1;
45 union cwiid_mesg *mesg = (union cwiid_mesg*)e.user.data2;
46 switch (*type) {
47 case CWIID_MESG_BTN: {
48 //printf("Button Report: %.4X\n", mesg->btn_mesg.buttons);
49 buttonsWii = mesg->btn_mesg.buttons;
50 if (mesg->btn_mesg.buttons & 4) { //R atira
51 jogador.fire();
54 break;
55 case CWIID_MESG_NUNCHUK: {
56 int newX = mesg->nunchuk_mesg.stick[CWIID_X] - 120;
57 int newY = mesg->nunchuk_mesg.stick[CWIID_Y] - 131;
59 printf("Nunchuk Report: btns=%.2X stick=(%d,%d) acc.x=%d acc.y=%d "
60 "acc.z=%d\n", mesg->nunchuk_mesg.buttons,
61 newX,
62 newY,
63 mesg->nunchuk_mesg.acc[CWIID_X],
64 mesg->nunchuk_mesg.acc[CWIID_Y],
65 mesg->nunchuk_mesg.acc[CWIID_Z]);*/
67 if (stickX != 0xffff) { //para calibrar
68 jogador.addSpeed(-3.0*stickX/128,0);
69 jogador.addSpeed(3.0*newX/128,0);
71 stickX = newX;
72 stickY = newY;
73 buttonsNunchuck = mesg->nunchuk_mesg.buttons;
75 if (jogador.onGround &&
76 ((mesg->nunchuk_mesg.buttons & 2) ||
77 newY > 60)) { //pulo com botao C ou para cima
78 jogador.addSpeed( 0, -8);
79 jogador.onGround = false;
82 if (newY < -60 || (mesg->nunchuk_mesg.buttons & 1)) {//descer com botao Z ou para baixo
83 jogador.onGround = false;
84 jogador.bypass = true;
87 break;
90 free(e.user.data1);
91 free(e.user.data2);
92 break;
94 break;
95 case SDL_MOUSEMOTION: {
96 jogador.setAim(e.motion.x,e.motion.y);
98 break;
99 case SDL_MOUSEBUTTONDOWN:
100 jogador.fire();
101 break;
102 case SDL_KEYUP:
103 switch (e.key.keysym.sym) {
104 case SDLK_LEFT:
105 jogador.addSpeed(3,0);
106 break;
107 case SDLK_RIGHT:
108 jogador.addSpeed(-3,0);
109 break;
110 default: break;
112 break;
113 case SDL_KEYDOWN:
114 switch (e.key.keysym.sym) {
115 case SDLK_DOWN:
116 jogador.onGround = false;
117 jogador.bypass = true;
118 break;
119 case SDLK_UP:
120 if (jogador.onGround) {
121 jogador.addSpeed( 0, -8);
122 jogador.onGround = false;
124 break;
125 case SDLK_LEFT:
126 jogador.addSpeed(-3, 0);
127 break;
128 case SDLK_RIGHT:
129 jogador.addSpeed( 3, 0);
130 break;
131 default: break;
133 break;
137 #define toggle_bit(bf,b) \
138 (bf) = ((bf) & b) \
139 ? ((bf) & ~(b)) \
140 : ((bf) | (b))
142 cwiid_err_t err;
143 void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap)
145 if (wiimote) printf("%d:", cwiid_get_id(wiimote));
146 else printf("-1:");
147 vprintf(s, ap);
148 printf("\n");
151 void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count,
152 union cwiid_mesg mesg[], struct timespec *timestamp)
154 int i, j;
155 int valid_source;
159 for (i=0; i < mesg_count; i++)
161 SDL_Event event;
162 SDL_UserEvent userevent;
163 userevent.type = SDL_USEREVENT;
164 event.type = SDL_USEREVENT;
165 userevent.code = WIIMOTEEVENT;
166 cwiid_mesg_type *type = (cwiid_mesg_type*)malloc(sizeof(cwiid_mesg_type));
167 *type = mesg[i].type;
168 userevent.data1 = (void*)type;
169 union cwiid_mesg *m = (union cwiid_mesg *)malloc(sizeof(union cwiid_mesg));
170 *m = mesg[i];
171 userevent.data2 = m;
172 event.user = userevent;
173 SDL_PushEvent(&event);
174 /*switch (mesg[i].type) {
175 case CWIID_MESG_STATUS:
176 printf("Status Report: battery=%d extension=",
177 mesg[i].status_mesg.battery);
178 switch (mesg[i].status_mesg.ext_type) {
179 case CWIID_EXT_NONE:
180 printf("none");
181 break;
182 case CWIID_EXT_NUNCHUK:
183 printf("Nunchuk");
184 break;
185 case CWIID_EXT_CLASSIC:
186 printf("Classic Controller");
187 break;
188 default:
189 printf("Unknown Extension");
190 break;
192 printf("\n");
193 break;
194 case CWIID_MESG_BTN:
195 printf("Button Report: %.4X\n", mesg[i].btn_mesg.buttons);
196 break;
197 case CWIID_MESG_ACC:
198 printf("Acc Report: x=%d, y=%d, z=%d\n",
199 mesg[i].acc_mesg.acc[CWIID_X],
200 mesg[i].acc_mesg.acc[CWIID_Y],
201 mesg[i].acc_mesg.acc[CWIID_Z]);
202 break;
203 case CWIID_MESG_IR:
204 printf("IR Report: ");
205 valid_source = 0;
206 for (j = 0; j < CWIID_IR_SRC_COUNT; j++) {
207 if (mesg[i].ir_mesg.src[j].valid) {
208 valid_source = 1;
209 printf("(%d,%d) ", mesg[i].ir_mesg.src[j].pos[CWIID_X],
210 mesg[i].ir_mesg.src[j].pos[CWIID_Y]);
213 if (!valid_source) {
214 printf("no sources detected");
216 printf("\n");
217 break;
218 case CWIID_MESG_NUNCHUK: {
219 printf("Nunchuk Report: btns=%.2X stick=(%d,%d) acc.x=%d acc.y=%d "
220 "acc.z=%d\n", mesg[i].nunchuk_mesg.buttons,
221 mesg[i].nunchuk_mesg.stick[CWIID_X],
222 mesg[i].nunchuk_mesg.stick[CWIID_Y],
223 mesg[i].nunchuk_mesg.acc[CWIID_X],
224 mesg[i].nunchuk_mesg.acc[CWIID_Y],
225 mesg[i].nunchuk_mesg.acc[CWIID_Z]);
226 break;
228 case CWIID_MESG_CLASSIC:
229 printf("Classic Report: btns=%.4X l_stick=(%d,%d) r_stick=(%d,%d) "
230 "l=%d r=%d\n", mesg[i].classic_mesg.buttons,
231 mesg[i].classic_mesg.l_stick[CWIID_X],
232 mesg[i].classic_mesg.l_stick[CWIID_Y],
233 mesg[i].classic_mesg.r_stick[CWIID_X],
234 mesg[i].classic_mesg.r_stick[CWIID_Y],
235 mesg[i].classic_mesg.l, mesg[i].classic_mesg.r);
236 break;
237 case CWIID_MESG_ERROR:
238 if (cwiid_close(wiimote)) {
239 fprintf(stderr, "Error on wiimote disconnect\n");
240 exit(-1);
242 exit(0);
243 break;
244 default:
245 printf("Unknown Report");
246 break;
251 void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state)
253 if (cwiid_set_led(wiimote, led_state)) {
254 fprintf(stderr, "Error setting LEDs \n");
258 void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode)
260 if (cwiid_set_rpt_mode(wiimote, rpt_mode)) {
261 fprintf(stderr, "Error setting report mode\n");
265 bool ControleWii::ControleWii::initializeWiimote() {
266 mesg = 0;
267 led_state = 0;
268 rpt_mode = 0;
269 rumble = 0;
271 cwiid_set_err(err);
273 bdaddr = *BDADDR_ANY;
275 /* Connect to the wiimote */
276 printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
277 if (!(wiimote = cwiid_open(&bdaddr, 0))) {
278 fprintf(stderr, "Unable to connect to wiimote\n");
279 return false;
281 if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
282 fprintf(stderr, "Unable to set message callback\n");
283 return false;
286 toggle_bit(rpt_mode, CWIID_RPT_BTN); //abilita status report de botoes
287 set_rpt_mode(wiimote, rpt_mode);
289 toggle_bit(rpt_mode, CWIID_RPT_NUNCHUK); //abilita status report de nunchuck
290 set_rpt_mode(wiimote, rpt_mode);
292 toggle_bit(led_state, CWIID_LED1_ON); //liga primeiro LED por motivo nenhum
293 set_led_state(wiimote, led_state);
295 if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
296 fprintf(stderr, "Error enabling messages\n");
297 return false;