1 #include "controlewii.h"
2 #include "usereventtype.h"
6 ControleWii::ControleWii(Player
&p
) : Controle(p
) {
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
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
43 void ControleWii::handleEvent(SDL_Event
&e
) {
46 switch (e
.user
.code
) {
48 cwiid_mesg_type
* type
= (cwiid_mesg_type
*)e
.user
.data1
;
49 union cwiid_mesg
*mesg
= (union cwiid_mesg
*)e
.user
.data2
;
55 for (int j
= 0; j
< CWIID_IR_SRC_COUNT
; j
++) {
56 if (cyclesSinceSeen
[j
] != -1)
58 if (mesg
->ir_mesg
.src
[j
].valid
) {
59 cyclesSinceSeen
[j
] = 0;
60 lastSeenAt
[j
].x
= mesg
->ir_mesg
.src
[j
].pos
[CWIID_X
];
61 lastSeenAt
[j
].y
= mesg
->ir_mesg
.src
[j
].pos
[CWIID_Y
];
64 for (int i
= 0; i
< CWIID_IR_SRC_COUNT
; i
++)
65 if (cyclesSinceSeen
[i
] != -1) {
72 double angle = (y / 738.0) * PI;
73 y = jogador.getY() + jogador.pescoco().y - jogador.game->camera.y - 100 * cos(angle);
74 x = jogador.getX() + jogador.pescoco().x - jogador.game->camera.x + 100 * sin(angle);
75 jogador.setAim(x,y);*/
78 x
= jogador
.game
->config
->screen
["width"] - (x
/ 1024.0) * jogador
.game
->config
->screen
["width"];
79 y
= (y
/ 738.0) * jogador
.game
->config
->screen
["height"];
80 jogador
.setAim(x
+game
->camera
.x
,y
+game
->camera
.y
);
84 case CWIID_MESG_BTN
: {
85 //printf("Button Report: %.4X\n", mesg->btn_mesg.buttons);
86 buttonsWii
= mesg
->btn_mesg
.buttons
;
87 if (mesg
->btn_mesg
.buttons
& 4) { //R atira
92 case CWIID_MESG_NUNCHUK
: {
93 int newX
= mesg
->nunchuk_mesg
.stick
[CWIID_X
] - 120;
94 int newY
= mesg
->nunchuk_mesg
.stick
[CWIID_Y
] - 131;
96 printf("Nunchuk Report: btns=%.2X stick=(%d,%d) acc.x=%d acc.y=%d "
97 "acc.z=%d\n", mesg->nunchuk_mesg.buttons,
100 mesg->nunchuk_mesg.acc[CWIID_X],
101 mesg->nunchuk_mesg.acc[CWIID_Y],
102 mesg->nunchuk_mesg.acc[CWIID_Z]);*/
104 if (stickX
!= 0xffff) { //para calibrar
105 //jogador.addSpeed(-3.0*stickX/128,0);
106 jogador
.setSpeed(3.0*newX
/128,jogador
.getSpeedY());
110 buttonsNunchuck
= mesg
->nunchuk_mesg
.buttons
;
112 if (jogador
.onGround
&&
113 ((mesg
->nunchuk_mesg
.buttons
& 2) ||
114 newY
> 60)) { //pulo com botao C ou para cima
115 jogador
.addSpeed( 0, -8);
116 jogador
.onGround
= false;
119 if (newY
< -60 || (mesg
->nunchuk_mesg
.buttons
& 1)) {//descer com botao Z ou para baixo
120 jogador
.onGround
= false;
121 jogador
.bypass
= true;
132 case SDL_MOUSEMOTION
: {
134 jogador
.setAim(e
.motion
.x
+game
->camera
.x
,e
.motion
.y
+game
->camera
.y
);
137 case SDL_MOUSEBUTTONDOWN
:
141 switch (e
.key
.keysym
.sym
) {
143 jogador
.addSpeed(3,0);
146 jogador
.addSpeed(-3,0);
152 switch (e
.key
.keysym
.sym
) {
154 jogador
.onGround
= false;
155 jogador
.bypass
= true;
158 if (jogador
.onGround
) {
159 jogador
.addSpeed( 0, -8);
160 jogador
.onGround
= false;
164 jogador
.addSpeed(-3, 0);
167 jogador
.addSpeed( 3, 0);
175 #define toggle_bit(bf,b) \
181 void err(cwiid_wiimote_t
*wiimote
, const char *s
, va_list ap
)
183 if (wiimote
) printf("%d:", cwiid_get_id(wiimote
));
189 void cwiid_callback(cwiid_wiimote_t
*wiimote
, int mesg_count
,
190 union cwiid_mesg mesg
[], struct timespec
*timestamp
)
197 for (i
=0; i
< mesg_count
; i
++)
200 SDL_UserEvent userevent
;
201 userevent
.type
= SDL_USEREVENT
;
202 event
.type
= SDL_USEREVENT
;
203 userevent
.code
= WIIMOTEEVENT
;
204 cwiid_mesg_type
*type
= (cwiid_mesg_type
*)malloc(sizeof(cwiid_mesg_type
));
205 *type
= mesg
[i
].type
;
206 userevent
.data1
= (void*)type
;
207 union cwiid_mesg
*m
= (union cwiid_mesg
*)malloc(sizeof(union cwiid_mesg
));
210 event
.user
= userevent
;
211 SDL_PushEvent(&event
);
212 /*switch (mesg[i].type) {
213 case CWIID_MESG_STATUS:
214 printf("Status Report: battery=%d extension=",
215 mesg[i].status_mesg.battery);
216 switch (mesg[i].status_mesg.ext_type) {
220 case CWIID_EXT_NUNCHUK:
223 case CWIID_EXT_CLASSIC:
224 printf("Classic Controller");
227 printf("Unknown Extension");
233 printf("Button Report: %.4X\n", mesg[i].btn_mesg.buttons);
236 printf("Acc Report: x=%d, y=%d, z=%d\n",
237 mesg[i].acc_mesg.acc[CWIID_X],
238 mesg[i].acc_mesg.acc[CWIID_Y],
239 mesg[i].acc_mesg.acc[CWIID_Z]);
242 printf("IR Report: ");
244 for (j = 0; j < CWIID_IR_SRC_COUNT; j++) {
245 if (mesg[i].ir_mesg.src[j].valid) {
247 printf("(%d,%d) ", mesg[i].ir_mesg.src[j].pos[CWIID_X],
248 mesg[i].ir_mesg.src[j].pos[CWIID_Y]);
252 printf("no sources detected");
256 case CWIID_MESG_NUNCHUK: {
257 printf("Nunchuk Report: btns=%.2X stick=(%d,%d) acc.x=%d acc.y=%d "
258 "acc.z=%d\n", mesg[i].nunchuk_mesg.buttons,
259 mesg[i].nunchuk_mesg.stick[CWIID_X],
260 mesg[i].nunchuk_mesg.stick[CWIID_Y],
261 mesg[i].nunchuk_mesg.acc[CWIID_X],
262 mesg[i].nunchuk_mesg.acc[CWIID_Y],
263 mesg[i].nunchuk_mesg.acc[CWIID_Z]);
266 case CWIID_MESG_CLASSIC:
267 printf("Classic Report: btns=%.4X l_stick=(%d,%d) r_stick=(%d,%d) "
268 "l=%d r=%d\n", mesg[i].classic_mesg.buttons,
269 mesg[i].classic_mesg.l_stick[CWIID_X],
270 mesg[i].classic_mesg.l_stick[CWIID_Y],
271 mesg[i].classic_mesg.r_stick[CWIID_X],
272 mesg[i].classic_mesg.r_stick[CWIID_Y],
273 mesg[i].classic_mesg.l, mesg[i].classic_mesg.r);
275 case CWIID_MESG_ERROR:
276 if (cwiid_close(wiimote)) {
277 fprintf(stderr, "Error on wiimote disconnect\n");
283 printf("Unknown Report");
289 void set_led_state(cwiid_wiimote_t
*wiimote
, unsigned char led_state
)
291 if (cwiid_set_led(wiimote
, led_state
)) {
292 fprintf(stderr
, "Error setting LEDs \n");
296 void set_rpt_mode(cwiid_wiimote_t
*wiimote
, unsigned char rpt_mode
)
298 if (cwiid_set_rpt_mode(wiimote
, rpt_mode
)) {
299 fprintf(stderr
, "Error setting report mode\n");
303 bool ControleWii::ControleWii::initializeWiimote() {
311 bdaddr
= *BDADDR_ANY
;
313 /* Connect to the wiimote */
314 printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
315 if (!(wiimote
= cwiid_open(&bdaddr
, 0))) {
316 fprintf(stderr
, "Unable to connect to wiimote\n");
319 if (cwiid_set_mesg_callback(wiimote
, cwiid_callback
)) {
320 fprintf(stderr
, "Unable to set message callback\n");
324 toggle_bit(rpt_mode
, CWIID_RPT_BTN
); //abilita status report de botoes
325 //set_rpt_mode(wiimote, rpt_mode);
327 toggle_bit(rpt_mode
, CWIID_RPT_NUNCHUK
); //abilita status report de nunchuck
328 //set_rpt_mode(wiimote, rpt_mode);
330 toggle_bit(led_state
, CWIID_LED4_ON
); //liga quarto LED para indentificar ligaƧao com o NOSSO jogo e nao o wii
331 set_led_state(wiimote
, led_state
);
333 toggle_bit(rpt_mode
, CWIID_RPT_IR
); //abilita recebimento do infravermelho
334 set_rpt_mode(wiimote
, rpt_mode
);
336 if (cwiid_enable(wiimote
, CWIID_FLAG_MESG_IFC
)) {
337 fprintf(stderr
, "Error enabling messages\n");