1 #include "controlewii.h"
2 #include "usereventtype.h"
5 ControleWii::ControleWii(Player
&p
) : Controle(p
) {
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
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
39 void ControleWii::handleEvent(SDL_Event
&e
) {
42 switch (e
.user
.code
) {
44 cwiid_mesg_type
* type
= (cwiid_mesg_type
*)e
.user
.data1
;
45 union cwiid_mesg
*mesg
= (union cwiid_mesg
*)e
.user
.data2
;
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
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,
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);
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;
95 case SDL_MOUSEMOTION
: {
96 jogador
.setAim(e
.motion
.x
,e
.motion
.y
);
99 case SDL_MOUSEBUTTONDOWN
:
103 switch (e
.key
.keysym
.sym
) {
105 jogador
.addSpeed(3,0);
108 jogador
.addSpeed(-3,0);
114 switch (e
.key
.keysym
.sym
) {
116 jogador
.onGround
= false;
117 jogador
.bypass
= true;
120 if (jogador
.onGround
) {
121 jogador
.addSpeed( 0, -8);
122 jogador
.onGround
= false;
126 jogador
.addSpeed(-3, 0);
129 jogador
.addSpeed( 3, 0);
137 #define toggle_bit(bf,b) \
143 void err(cwiid_wiimote_t
*wiimote
, const char *s
, va_list ap
)
145 if (wiimote
) printf("%d:", cwiid_get_id(wiimote
));
151 void cwiid_callback(cwiid_wiimote_t
*wiimote
, int mesg_count
,
152 union cwiid_mesg mesg
[], struct timespec
*timestamp
)
159 for (i
=0; i
< mesg_count
; i
++)
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
));
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) {
182 case CWIID_EXT_NUNCHUK:
185 case CWIID_EXT_CLASSIC:
186 printf("Classic Controller");
189 printf("Unknown Extension");
195 printf("Button Report: %.4X\n", mesg[i].btn_mesg.buttons);
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]);
204 printf("IR Report: ");
206 for (j = 0; j < CWIID_IR_SRC_COUNT; j++) {
207 if (mesg[i].ir_mesg.src[j].valid) {
209 printf("(%d,%d) ", mesg[i].ir_mesg.src[j].pos[CWIID_X],
210 mesg[i].ir_mesg.src[j].pos[CWIID_Y]);
214 printf("no sources detected");
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]);
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);
237 case CWIID_MESG_ERROR:
238 if (cwiid_close(wiimote)) {
239 fprintf(stderr, "Error on wiimote disconnect\n");
245 printf("Unknown Report");
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() {
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");
281 if (cwiid_set_mesg_callback(wiimote
, cwiid_callback
)) {
282 fprintf(stderr
, "Unable to set message callback\n");
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");