1 #include "controlewii.h"
2 #include "usereventtype.h"
3 #include "weaponitem.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
) {
44 std::list
<WeaponItem
*>::iterator it
;
48 switch (e
.user
.code
) {
50 cwiid_mesg_type
* type
= (cwiid_mesg_type
*)e
.user
.data1
;
51 union cwiid_mesg
*mesg
= (union cwiid_mesg
*)e
.user
.data2
;
57 for (int j
= 0; j
< CWIID_IR_SRC_COUNT
; j
++) {
58 if (cyclesSinceSeen
[j
] != -1)
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) {
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);*/
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
);
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
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,
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());
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;
134 case SDL_MOUSEMOTION
: {
136 jogador
.setAim(e
.motion
.x
+game
->camera
.x
,e
.motion
.y
+game
->camera
.y
);
139 case SDL_MOUSEBUTTONDOWN
:
143 switch (e
.key
.keysym
.sym
) {
145 jogador
.addSpeed(3,0);
148 jogador
.addSpeed(-3,0);
154 switch (e
.key
.keysym
.sym
) {
156 jogador
.onGround
= false;
157 jogador
.bypass
= true;
160 if (jogador
.onGround
) {
161 jogador
.addSpeed( 0, -8);
162 jogador
.onGround
= false;
166 jogador
.addSpeed(-3, 0);
169 jogador
.addSpeed( 3, 0);
172 for (it
= map
->items
.begin(); it
!= map
->items
.end(); ++it
)
173 if (game
->collisionManager
->checkCollision(&jogador
, (Thing
*) *it
)) {
174 jogador
.equip((*it
)->getWeapon());
179 map
->items
.erase(it
);
187 #define toggle_bit(bf,b) \
193 void err(cwiid_wiimote_t
*wiimote
, const char *s
, va_list ap
)
195 if (wiimote
) printf("%d:", cwiid_get_id(wiimote
));
201 void cwiid_callback(cwiid_wiimote_t
*wiimote
, int mesg_count
,
202 union cwiid_mesg mesg
[], struct timespec
*timestamp
)
209 for (i
=0; i
< mesg_count
; i
++)
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
));
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) {
232 case CWIID_EXT_NUNCHUK:
235 case CWIID_EXT_CLASSIC:
236 printf("Classic Controller");
239 printf("Unknown Extension");
245 printf("Button Report: %.4X\n", mesg[i].btn_mesg.buttons);
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]);
254 printf("IR Report: ");
256 for (j = 0; j < CWIID_IR_SRC_COUNT; j++) {
257 if (mesg[i].ir_mesg.src[j].valid) {
259 printf("(%d,%d) ", mesg[i].ir_mesg.src[j].pos[CWIID_X],
260 mesg[i].ir_mesg.src[j].pos[CWIID_Y]);
264 printf("no sources detected");
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]);
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);
287 case CWIID_MESG_ERROR:
288 if (cwiid_close(wiimote)) {
289 fprintf(stderr, "Error on wiimote disconnect\n");
295 printf("Unknown Report");
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() {
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");
331 if (cwiid_set_mesg_callback(wiimote
, cwiid_callback
)) {
332 fprintf(stderr
, "Unable to set message callback\n");
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");