Pequena correção no Makefile
[Projeto-PCG.git] / controlewii.cpp
blob035449886f5973b43b65c9c6a140f8aa90d13416
1 #include "controlewii.h"
2 #include "usereventtype.h"
5 ControleWii::ControleWii(Player &p) : Controle(p) {
6 initializeWiimote();
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
17 jogador.fire();
21 void ControleWii::handleEvent(SDL_Event &e) {
22 switch( e.type ) {
23 case SDL_USEREVENT:
24 switch (e.user.code) {
25 case WIIMOTEEVENT:
26 cwiid_mesg_type* type = (cwiid_mesg_type*)e.user.data1;
27 union cwiid_mesg *mesg = (union cwiid_mesg*)e.user.data2;
28 switch (*type) {
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]);
38 break;
40 free(e.user.data1);
41 free(e.user.data2);
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));
46 *m = mesg[i];
47 userevent.data2 = m;
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]);
57 break;
58 }*/
59 break;
61 break;
62 case SDL_MOUSEMOTION: {
63 jogador.setAim(e.motion.x,e.motion.y);
65 break;
66 case SDL_MOUSEBUTTONDOWN:
67 jogador.fire();
68 break;
69 case SDL_KEYUP:
70 switch (e.key.keysym.sym) {
71 case SDLK_LEFT:
72 jogador.addSpeed(3,0);
73 break;
74 case SDLK_RIGHT:
75 jogador.addSpeed(-3,0);
76 break;
77 default: break;
79 break;
80 case SDL_KEYDOWN:
81 switch (e.key.keysym.sym) {
82 case SDLK_DOWN:
83 jogador.onGround = false;
84 jogador.bypass = true;
85 break;
86 case SDLK_UP:
87 if (jogador.onGround) {
88 jogador.addSpeed( 0, -8);
89 jogador.onGround = false;
91 break;
92 case SDLK_LEFT:
93 jogador.addSpeed(-3, 0);
94 break;
95 case SDLK_RIGHT:
96 jogador.addSpeed( 3, 0);
97 break;
98 default: break;
100 break;
104 #define toggle_bit(bf,b) \
105 (bf) = ((bf) & b) \
106 ? ((bf) & ~(b)) \
107 : ((bf) | (b))
109 cwiid_err_t err;
110 void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap)
112 if (wiimote) printf("%d:", cwiid_get_id(wiimote));
113 else printf("-1:");
114 vprintf(s, ap);
115 printf("\n");
118 void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count,
119 union cwiid_mesg mesg[], struct timespec *timestamp)
121 int i, j;
122 int valid_source;
126 for (i=0; i < mesg_count; i++)
128 SDL_Event event;
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));
137 *m = mesg[i];
138 userevent.data2 = m;
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) {
146 case CWIID_EXT_NONE:
147 printf("none");
148 break;
149 case CWIID_EXT_NUNCHUK:
150 printf("Nunchuk");
151 break;
152 case CWIID_EXT_CLASSIC:
153 printf("Classic Controller");
154 break;
155 default:
156 printf("Unknown Extension");
157 break;
159 printf("\n");
160 break;
161 case CWIID_MESG_BTN:
162 printf("Button Report: %.4X\n", mesg[i].btn_mesg.buttons);
163 break;
164 case CWIID_MESG_ACC:
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]);
169 break;
170 case CWIID_MESG_IR:
171 printf("IR Report: ");
172 valid_source = 0;
173 for (j = 0; j < CWIID_IR_SRC_COUNT; j++) {
174 if (mesg[i].ir_mesg.src[j].valid) {
175 valid_source = 1;
176 printf("(%d,%d) ", mesg[i].ir_mesg.src[j].pos[CWIID_X],
177 mesg[i].ir_mesg.src[j].pos[CWIID_Y]);
180 if (!valid_source) {
181 printf("no sources detected");
183 printf("\n");
184 break;
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]);
193 break;
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);
203 break;
204 case CWIID_MESG_ERROR:
205 if (cwiid_close(wiimote)) {
206 fprintf(stderr, "Error on wiimote disconnect\n");
207 exit(-1);
209 exit(0);
210 break;
211 default:
212 printf("Unknown Report");
213 break;
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() {
233 mesg = 0;
234 led_state = 0;
235 rpt_mode = 0;
236 rumble = 0;
238 cwiid_set_err(err);
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");
246 return false;
248 if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
249 fprintf(stderr, "Unable to set message callback\n");
250 return false;
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");
264 return false;