}
static void calibrate(SDL_Surface *screen,
+ const char *filename,
cwiid_wiimote_t *wiimote,
struct coord *calib, unsigned x, unsigned y)
{
+ SDL_Surface *img = IMG_Load(filename);
uint16_t *pos;
struct cwiid_state state;
- unsigned int last_x = MAX_X, last_y = MAX_Y, count = 0;
+ int last_x = MAX_X, last_y = MAX_Y, count = 0;
+ SDL_Rect rect = { .x = screen->w/2 - img->w/2,
+ .y = screen->h/2 - img->h/2 };
+
+ SDL_BlitSurface(img, NULL, screen, &rect);
+ SDL_UpdateRect(screen, 0, 0, 0, 0);
+ SDL_FreeSurface(img);
/* Must see it for a full half second. */
while (count < 20) {
errx(1, "No wii state");
pos = find_valid_point(&state);
- if (!pos || pos[0] != last_x || pos[1] != last_y)
- count = 0;
-
- if (pos) {
+ if (!pos) {
+ if (count)
+ count--;
+ } else {
+ /* Allow some jitter */
+ if (abs(pos[0]-last_x) < 3 && abs(pos[1]-last_y) < 3)
+ count++;
last_x = pos[0];
last_y = pos[1];
- count++;
}
SDL_Delay(25);
calib->x = last_x;
calib->y = last_y;
+ SDL_Delay(500);
printf("Calibration point: %u,%u\n", last_x, last_y);
}
if (needs_calibration) {
/* Calibration */
- calibrate(screen, wiimote, &calib[0], 0, 0);
- calibrate(screen, wiimote, &calib[1], MAX_X - 1, 0);
- calibrate(screen, wiimote, &calib[2], MAX_X - 1, MAX_Y - 1);
- calibrate(screen, wiimote, &calib[3], 0, MAX_Y - 1);
+ calibrate(screen, "images/top-left.png",
+ wiimote, &calib[0], 0, 0);
+ calibrate(screen, "images/top-right.png",
+ wiimote, &calib[1], MAX_X - 1, 0);
+ calibrate(screen, "images/bottom-right.png",
+ wiimote, &calib[2], MAX_X - 1, MAX_Y - 1);
+ calibrate(screen, "images/bottom-left.png",
+ wiimote, &calib[3], 0, MAX_Y - 1);
}
/* Create borders, put them in list. */
SDL_UpdateRect(screen,
rect.x, rect.y,
rect.w, rect.h);
- SDL_Delay(3000);
+ SDL_Delay(5000);
exit(0);
}
}