1 #include "controlewii.h"
2 #include "usereventtype.h"
5 ControleWii::ControleWii(Player
&p
) : Controle(p
) {
9 void ControleWii::handleOther() {
10 Uint8
*keystates
= SDL_GetKeyState( NULL
);
11 jogador
.bypass
= false;
12 if (keystates
[SDLK_DOWN
]) {
13 jogador
.onGround
= false;
14 jogador
.bypass
= true;
16 if(SDL_GetMouseState(NULL
, NULL
) & SDL_BUTTON(1)) { //botao esquerdo do mouse pressionado
21 void ControleWii::handleEvent(SDL_Event
&e
) {
24 switch (e
.user
.code
) {
26 cwiid_mesg_type
* type
= (cwiid_mesg_type
*)e
.user
.data1
;
27 union cwiid_mesg
*mesg
= (union cwiid_mesg
*)e
.user
.data2
;
29 case CWIID_MESG_NUNCHUK
: {
30 printf("Nunchuk Report: btns=%.2X stick=(%d,%d) acc.x=%d acc.y=%d "
31 "acc.z=%d\n", mesg
->nunchuk_mesg
.buttons
,
32 mesg
->nunchuk_mesg
.stick
[CWIID_X
],
33 mesg
->nunchuk_mesg
.stick
[CWIID_Y
],
34 mesg
->nunchuk_mesg
.acc
[CWIID_X
],
35 mesg
->nunchuk_mesg
.acc
[CWIID_Y
],
36 mesg
->nunchuk_mesg
.acc
[CWIID_Z
]);
42 /*case CWIID_MESG_NUNCHUK: {
43 userevent.code = FUNCTIONCALL;
44 userevent.data1 = (void*)mesg[i].type;
45 union cwiid_mesg *m = (union cwiid_mesg *)malloc(sizeof(union cwiid_mesg));
48 event.user = userevent;
49 SDL_PushEvent(&event);
50 printf("Nunchuk Report: btns=%.2X stick=(%d,%d) acc.x=%d acc.y=%d "
51 "acc.z=%d\n", mesg[i].nunchuk_mesg.buttons,
52 mesg[i].nunchuk_mesg.stick[CWIID_X],
53 mesg[i].nunchuk_mesg.stick[CWIID_Y],
54 mesg[i].nunchuk_mesg.acc[CWIID_X],
55 mesg[i].nunchuk_mesg.acc[CWIID_Y],
56 mesg[i].nunchuk_mesg.acc[CWIID_Z]);
62 case SDL_MOUSEMOTION
: {
63 jogador
.setAim(e
.motion
.x
,e
.motion
.y
);
66 case SDL_MOUSEBUTTONDOWN
:
70 switch (e
.key
.keysym
.sym
) {
72 jogador
.addSpeed(3,0);
75 jogador
.addSpeed(-3,0);
81 switch (e
.key
.keysym
.sym
) {
83 jogador
.onGround
= false;
84 jogador
.bypass
= true;
87 if (jogador
.onGround
) {
88 jogador
.addSpeed( 0, -8);
89 jogador
.onGround
= false;
93 jogador
.addSpeed(-3, 0);
96 jogador
.addSpeed( 3, 0);
104 #define toggle_bit(bf,b) \
110 void err(cwiid_wiimote_t
*wiimote
, const char *s
, va_list ap
)
112 if (wiimote
) printf("%d:", cwiid_get_id(wiimote
));
118 void cwiid_callback(cwiid_wiimote_t
*wiimote
, int mesg_count
,
119 union cwiid_mesg mesg
[], struct timespec
*timestamp
)
126 for (i
=0; i
< mesg_count
; i
++)
129 SDL_UserEvent userevent
;
130 userevent
.type
= SDL_USEREVENT
;
131 event
.type
= SDL_USEREVENT
;
132 userevent
.code
= WIIMOTEEVENT
;
133 cwiid_mesg_type
*type
= (cwiid_mesg_type
*)malloc(sizeof(cwiid_mesg_type
));
134 *type
= mesg
[i
].type
;
135 userevent
.data1
= (void*)type
;
136 union cwiid_mesg
*m
= (union cwiid_mesg
*)malloc(sizeof(union cwiid_mesg
));
139 event
.user
= userevent
;
140 SDL_PushEvent(&event
);
141 /*switch (mesg[i].type) {
142 case CWIID_MESG_STATUS:
143 printf("Status Report: battery=%d extension=",
144 mesg[i].status_mesg.battery);
145 switch (mesg[i].status_mesg.ext_type) {
149 case CWIID_EXT_NUNCHUK:
152 case CWIID_EXT_CLASSIC:
153 printf("Classic Controller");
156 printf("Unknown Extension");
162 printf("Button Report: %.4X\n", mesg[i].btn_mesg.buttons);
165 printf("Acc Report: x=%d, y=%d, z=%d\n",
166 mesg[i].acc_mesg.acc[CWIID_X],
167 mesg[i].acc_mesg.acc[CWIID_Y],
168 mesg[i].acc_mesg.acc[CWIID_Z]);
171 printf("IR Report: ");
173 for (j = 0; j < CWIID_IR_SRC_COUNT; j++) {
174 if (mesg[i].ir_mesg.src[j].valid) {
176 printf("(%d,%d) ", mesg[i].ir_mesg.src[j].pos[CWIID_X],
177 mesg[i].ir_mesg.src[j].pos[CWIID_Y]);
181 printf("no sources detected");
185 case CWIID_MESG_NUNCHUK: {
186 printf("Nunchuk Report: btns=%.2X stick=(%d,%d) acc.x=%d acc.y=%d "
187 "acc.z=%d\n", mesg[i].nunchuk_mesg.buttons,
188 mesg[i].nunchuk_mesg.stick[CWIID_X],
189 mesg[i].nunchuk_mesg.stick[CWIID_Y],
190 mesg[i].nunchuk_mesg.acc[CWIID_X],
191 mesg[i].nunchuk_mesg.acc[CWIID_Y],
192 mesg[i].nunchuk_mesg.acc[CWIID_Z]);
195 case CWIID_MESG_CLASSIC:
196 printf("Classic Report: btns=%.4X l_stick=(%d,%d) r_stick=(%d,%d) "
197 "l=%d r=%d\n", mesg[i].classic_mesg.buttons,
198 mesg[i].classic_mesg.l_stick[CWIID_X],
199 mesg[i].classic_mesg.l_stick[CWIID_Y],
200 mesg[i].classic_mesg.r_stick[CWIID_X],
201 mesg[i].classic_mesg.r_stick[CWIID_Y],
202 mesg[i].classic_mesg.l, mesg[i].classic_mesg.r);
204 case CWIID_MESG_ERROR:
205 if (cwiid_close(wiimote)) {
206 fprintf(stderr, "Error on wiimote disconnect\n");
212 printf("Unknown Report");
218 void set_led_state(cwiid_wiimote_t
*wiimote
, unsigned char led_state
)
220 if (cwiid_set_led(wiimote
, led_state
)) {
221 fprintf(stderr
, "Error setting LEDs \n");
225 void set_rpt_mode(cwiid_wiimote_t
*wiimote
, unsigned char rpt_mode
)
227 if (cwiid_set_rpt_mode(wiimote
, rpt_mode
)) {
228 fprintf(stderr
, "Error setting report mode\n");
232 bool ControleWii::ControleWii::initializeWiimote() {
240 bdaddr
= *BDADDR_ANY
;
242 /* Connect to the wiimote */
243 printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
244 if (!(wiimote
= cwiid_open(&bdaddr
, 0))) {
245 fprintf(stderr
, "Unable to connect to wiimote\n");
248 if (cwiid_set_mesg_callback(wiimote
, cwiid_callback
)) {
249 fprintf(stderr
, "Unable to set message callback\n");
253 toggle_bit(rpt_mode
, CWIID_RPT_BTN
); //abilita status report de botoes
254 set_rpt_mode(wiimote
, rpt_mode
);
256 toggle_bit(rpt_mode
, CWIID_RPT_NUNCHUK
); //abilita status report de nunchuck
257 set_rpt_mode(wiimote
, rpt_mode
);
259 toggle_bit(led_state
, CWIID_LED1_ON
); //liga primeiro LED por motivo nenhum
260 set_led_state(wiimote
, led_state
);
262 if (cwiid_enable(wiimote
, CWIID_FLAG_MESG_IFC
)) {
263 fprintf(stderr
, "Error enabling messages\n");