11 #include "list/list.h"
12 #include "talloc/talloc.h"
13 #include "container_of/container_of.h"
15 #include <SDL/SDL_image.h>
22 #define MAX_X CWIID_IR_X_MAX
23 #define MAX_Y CWIID_IR_Y_MAX
26 #define RIGHT_PLAYER 2
28 static LIST_HEAD(objects);
36 /* Obtained from http://tog.acm.org/GraphicsGems/gemsiii/insectc.c */
37 /* Faster Line Segment Intersection */
38 /* Franklin Antonio */
39 static bool lines_intersect(const struct coord *start1,
40 const struct coord *start2,
41 const struct coord *start3,
42 const struct coord *start4,
43 struct coord *intersect)
45 double Ax,Bx,Cx,Ay,By,Cy,d,e,f,num;
48 Ax = start2->x - start1->x;
49 Bx = start3->x - start4->x;
51 if(Ax<0) { /* X bound box test*/
52 lo.x = start2->x; hi.x = start1->x;
54 hi.x = start2->x; lo.x = start1->x;
57 if(hi.x < start4->x || start3->x < lo.x) return false;
59 if(hi.x < start3->x || start4->x < lo.x) return false;
62 Ay = start2->y - start1->y;
63 By = start3->y - start4->y;
65 if(Ay<0) { /* Y bound box test*/
66 lo.y = start2->y; hi.y = start1->y;
68 hi.y = start2->y; lo.y = start1->y;
71 if(hi.y < start4->y || start3->y < lo.y) return false;
73 if(hi.y < start3->y || start4->y < lo.y) return false;
77 Cx = start1->x - start3->x;
78 Cy = start1->y - start3->y;
79 d = By*Cx - Bx*Cy; /* alpha numerator*/
80 f = Ay*Bx - Ax*By; /* both denominator*/
81 if (f>0) { /* alpha tests*/
82 if (d < 0 || d > f) return false;
84 if (d > 0 || d < f) return false;
87 e = Ax*Cy - Ay*Cx; /* beta numerator*/
88 if (f > 0) { /* beta tests*/
89 if (e < 0 || e > f) return false;
91 if (e > 0 || e < f) return false;
94 /*compute intersection coordinates*/
95 /* Don't worry about tiny values of f (< 1/2 pixel across screen) */
96 if (fabs(f) < 1.0 / (2.0 * MAX_X * MAX_Y))
99 num = d*Ax; /* numerator */
100 intersect->x = start1->x + num / f; /* intersection x */
103 intersect->y = start1->y + num / f; /* intersection y */
108 // A set of very useful macros that you will find in most
109 // code that I write whether I use them in a program or
112 #define abs(a) (((a)<0) ? -(a) : (a))
113 #define sign(a) (((a)<0) ? -1 : (a)>0 ? 1 : 0)
115 // The following code implements a Bresenham line drawing
116 // algorithm. There are 4 separate routines each optimized
117 // for one of the four pixel depths supported by SDL. SDL
118 // support many pixel formats, but it only support 8, 16,
119 // 24, and 32 bit pixels.
121 //----------------------------------------------------------
123 // Draw lines in 8 bit surfaces.
125 static void line8(SDL_Surface *s,
150 yOffset = sy * s->pitch;
155 lineAddr = ((Uint8 *)(s->pixels)) + (y * s->pitch);
161 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
162 *(lineAddr + x) = (Uint8)color;
183 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
184 *(lineAddr + x) = (Uint8)color;
202 //----------------------------------------------------------
204 // Draw lines in 16 bit surfaces. Note that this code will
205 // also work on 15 bit surfaces.
207 static void line16(SDL_Surface *s,
232 yOffset = sy * s->pitch;
237 lineAddr = ((Uint8 *)s->pixels) + (y * s->pitch);
243 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
244 *((Uint16 *)(lineAddr + (x << 1))) = (Uint16)color;
265 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
266 *((Uint16 *)(lineAddr + (x << 1))) = (Uint16)color;
284 //----------------------------------------------------------
286 // Draw lines in 24 bit surfaces. 24 bit surfaces require
287 // special handling because the pixels don't fall on even
288 // address boundaries. Instead of being able to store a
289 // single byte, word, or long you have to store 3
290 // individual bytes. As a result 24 bit graphics is slower
291 // than the other pixel sizes.
293 static void line24(SDL_Surface *s,
311 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
322 yOffset = sy * s->pitch;
327 lineAddr = ((Uint8 *)(s->pixels)) + (y * s->pitch);
333 Uint8 *p = (lineAddr + (x * 3));
334 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
335 memcpy(p, &color, 3);
356 Uint8 *p = (lineAddr + (x * 3));
357 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
358 memcpy(p, &color, 3);
376 //----------------------------------------------------------
378 // Draw lines in 32 bit surfaces. Note that this routine
379 // ignores alpha values. It writes them into the surface
380 // if they are included in the pixel, but does nothing
383 static void line32(SDL_Surface *s,
408 yOffset = sy * s->pitch;
413 lineAddr = ((Uint8 *)(s->pixels)) + (y * s->pitch);
419 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
420 *((Uint32 *)(lineAddr + (x << 2))) = (Uint32)color;
441 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
442 *((Uint32 *)(lineAddr + (x << 2))) = (Uint32)color;
460 //----------------------------------------------------------
462 // Examine the depth of a surface and select a line
463 // drawing routine optimized for the bytes/pixel of the
466 static void line(SDL_Surface *s,
471 switch (s->format->BytesPerPixel)
474 line8(s, x1, y1, x2, y2, color);
477 line16(s, x1, y1, x2, y2, color);
480 line24(s, x1, y1, x2, y2, color);
483 line32(s, x1, y1, x2, y2, color);
488 /* Bounding box to be updated. */
489 static bool needs_update;
490 static struct coord start_update, end_update;
494 /* List of all objects. */
495 struct list_node list;
497 struct coord start, end;
499 void (*redraw)(SDL_Surface *s, struct object *me);
504 struct object object;
513 struct object object;
515 struct coord start, end;
516 struct list_node list;
518 struct timeval expires;
519 /* Does someone score if they hit this line ? */
525 struct object object;
532 static void thick_line(SDL_Surface *s, const struct line_segment *l,
535 struct line_segment adj;
536 int x, y, minx = MAX_X, miny = MAX_Y, maxx = 0, maxy = 0;
541 for (x = -1; x < 2; x++) {
542 for (y = -1; y < 2; y++) {
543 adj.start.x = l->start.x + x;
544 adj.end.x = l->end.x + x;
545 adj.start.y = l->start.y + y;
546 adj.end.y = l->end.y + y;
548 if (adj.start.x < minx)
550 if (adj.start.x > maxx)
552 if (adj.start.y < miny)
554 if (adj.start.y > maxy)
556 if (adj.end.x < minx)
558 if (adj.end.x > maxx)
560 if (adj.end.y < miny)
562 if (adj.end.y > maxy)
565 line(s, adj.start.x, adj.start.y, adj.end.x, adj.end.y,
578 SDL_UpdateRect(s, minx, miny, maxx-minx, maxy-miny);
579 SDL_UnlockSurface(s);
582 static bool intersect(const struct coord *c1, const struct coord *c2,
583 const struct line_segment *seg, struct coord *intersect)
585 return lines_intersect(c1, c2, &seg->start, &seg->end, intersect);
588 static double dist(const struct coord *c1, const struct coord *c2)
590 double x = (c1->x - c2->x), y = (c1->y - c2->y);
591 return sqrt(x * x + y * y);
594 static unsigned rad_to_deg(double rad)
596 return ((unsigned)(rad / M_PI * 180) + 360) % 360;
599 static void bounce(struct ball *ball, const struct line_segment *line,
600 struct coord *move, double increase_speed)
602 double len, speed, lang, iang, oang;
603 struct coord isect, new;
605 new.x = ball->pos.x + move->x;
606 new.y = ball->pos.y + move->y;
608 /* How far were we supposed to move ball? */
609 len = sqrt(move->x * move->x + move->y * move->y);
611 /* How far is it to intersection? */
612 if (!intersect(&ball->pos, &new, line, &isect))
613 errx(1, "No intersection any more?\n");;
614 len -= dist(&ball->pos, &isect);
616 /* Move ball to intersection. */
618 printf("ball is now at %f,%f\n", ball->pos.x, ball->pos.y);
620 /* Outgoing angle = 2 * line angle - incident angle. */
621 lang = atan2(line->end.x - line->start.x, line->end.y - line->start.y);
622 iang = atan2(ball->move.x, ball->move.y);
623 oang = 2 * lang - iang, 2*M_PI;
624 printf("lang = %u, iang = %u, oang=%u\n",
629 /* Set new direction for ball, at slightly increased speed */
630 speed = sqrt(ball->move.x * ball->move.x + ball->move.y * ball->move.y);
631 speed += increase_speed;
632 ball->move.x = sin(oang) * speed;
633 ball->move.y = cos(oang) * speed;
636 move->x = sin(oang) * len;
637 move->y = cos(oang) * len;
638 printf("len = %f, move = %f,%f\n", len, move->x, move->y);
641 static struct line_segment border[] = {
642 { .start = { 0, 0, }, .end = { MAX_X-1, 0 } },
643 { .start = { MAX_X-1, 0, }, .end = { MAX_X-1, MAX_Y-1 } },
644 { .start = { MAX_X-1, MAX_Y-1, }, .end = { 0, MAX_Y-1 } },
645 { .start = { 0, MAX_Y-1, }, .end = { 0, 0 } },
648 static inline float deg_to_rad(float degrees)
650 return degrees * M_PI / 180;
653 static SDL_Surface *sub_surface(SDL_Surface *screen, int w, int h)
655 return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
656 screen->format->BitsPerPixel,
657 screen->format->Rmask,
658 screen->format->Gmask,
659 screen->format->Bmask,
660 screen->format->Amask);
663 static SDL_Surface *ball_surface(SDL_Surface *screen)
667 /* Just like the screen surface. */
668 ball = sub_surface(screen, 5, 5);
670 /* Lock the surface */
671 SDL_LockSurface(ball);
673 /* Black, white corners. */
674 memset(ball->pixels, 0x00, ball->pitch * 5);
675 line(ball, 0, 0, 0, 0, 0xFFFFFFFF);
676 line(ball, 4, 0, 4, 0, 0xFFFFFFFF);
677 line(ball, 0, 4, 0, 4, 0xFFFFFFFF);
678 line(ball, 4, 4, 4, 4, 0xFFFFFFFF);
679 SDL_UnlockSurface(ball);
684 static void clear_ignore(struct list_head *lines)
686 struct line_segment *i;
687 printf("Unignoring...\n");
688 list_for_each(lines, i, list)
692 static uint16_t *find_valid_point(struct cwiid_state *state)
696 for (i = 0; i < CWIID_IR_SRC_COUNT; i++) {
697 if (state->ir_src[i].valid)
698 return state->ir_src[i].pos;
703 static void calibrate(SDL_Surface *screen,
704 const char *filename,
705 cwiid_wiimote_t *wiimote,
706 struct coord *calib, unsigned x, unsigned y)
708 SDL_Surface *img = IMG_Load(filename);
710 struct cwiid_state state;
711 int last_x = MAX_X, last_y = MAX_Y, count = 0;
712 SDL_Rect rect = { .x = screen->w/2 - img->w/2,
713 .y = screen->h/2 - img->h/2 };
715 SDL_BlitSurface(img, NULL, screen, &rect);
716 SDL_UpdateRect(screen, 0, 0, 0, 0);
717 SDL_FreeSurface(img);
719 /* Must see it for a full half second. */
723 if (cwiid_get_state(wiimote, &state))
724 errx(1, "No wii state");
726 pos = find_valid_point(&state);
731 /* Allow some jitter */
732 if (abs(pos[0]-last_x) < 3 && abs(pos[1]-last_y) < 3)
739 if (SDL_PollEvent(&event)
740 && (event.type == SDL_QUIT
741 || (event.type == SDL_KEYDOWN
742 && event.key.keysym.sym == SDLK_ESCAPE))) {
751 printf("Calibration point: %u,%u\n", last_x, last_y);
754 static bool map_coord(unsigned int x, unsigned int y,
755 const struct coord calib[],
758 struct coord line_start, line_end;
759 struct coord pos = { x, y }, intersect;
762 /* Calibration layout:
768 * We figure out the distance to each side, and then map using:
770 * x = d1 / (d1 + d2) * (rawend - rawstart) + rawstart
773 line_end.x = MAX_X-1;
774 line_start.y = line_end.y = y;
776 if (!lines_intersect(&calib[0], &calib[3], &line_start, &line_end,
779 d1 = dist(&pos, &intersect);
780 if (!lines_intersect(&calib[1], &calib[2], &line_start, &line_end,
783 d2 = dist(&pos, &intersect);
784 res->x = d1 / (d1 + d2) * MAX_X;
787 line_end.y = MAX_Y-1;
788 line_start.x = line_end.x = x;
790 if (!lines_intersect(&calib[0], &calib[1], &line_start, &line_end,
793 d1 = dist(&pos, &intersect);
794 if (!lines_intersect(&calib[3], &calib[2], &line_start, &line_end,
797 d2 = dist(&pos, &intersect);
798 res->y = d1 / (d1 + d2) * MAX_Y;
803 static void add_update(const struct object *obj)
806 start_update = obj->start;
807 end_update = obj->end;
811 if (obj->start.x < start_update.x)
812 start_update.x = obj->start.x;
813 if (obj->start.y < start_update.y)
814 start_update.y = obj->start.y;
815 if (obj->end.x > end_update.x)
816 end_update.x = obj->end.x;
817 if (obj->end.y > end_update.y)
818 end_update.y = obj->end.y;
821 static void destroy_object(struct object *object)
823 list_del(&object->list);
827 static void add_object(struct list_head *list,
829 unsigned int startx, unsigned int starty,
830 unsigned int width, unsigned int height,
831 void (*redraw)(SDL_Surface *s, struct object *me))
833 list_add_tail(list, &obj->list);
834 obj->start.x = startx;
835 obj->start.y = starty;
836 obj->end.x = startx + width;
837 obj->end.y = starty + height;
838 obj->redraw = redraw;
842 static void redraw_line(SDL_Surface *s, struct object *me)
844 struct line_segment *l = container_of(me, struct line_segment, object);
849 static int destroy_line(struct line_segment *l)
852 destroy_object(&l->object);
856 static void line_object_setup(struct list_head *lines,
857 struct list_head *objects,
858 struct line_segment *l)
860 unsigned int tmp, startx, endx, starty, endy;
862 list_add_tail(lines, &l->list);
869 /* Find bounding box */
880 /* Padding for thick lines (beware underflow) */
888 add_object(objects, &l->object, startx, starty,
889 endx - startx, endy - starty, redraw_line);
892 static void add_line(struct list_head *lines,
893 unsigned int startx, unsigned int starty,
894 unsigned int endx, unsigned int endy,
895 const struct coord calib[])
897 struct line_segment *l = talloc(NULL, struct line_segment);
900 if (!map_coord(startx, starty, calib, &l->start)
901 || !map_coord(endx, endy, calib, &l->end)) {
908 gettimeofday(&now, NULL);
910 l->expires.tv_sec += EXPIRY_SECS;
912 line_object_setup(lines, &objects, l);
913 talloc_set_destructor(l, destroy_line);
916 static void redraw_ball(SDL_Surface *s, struct object *me)
918 struct ball *b = container_of(me, struct ball, object);
919 SDL_Rect rect = { .x = b->pos.x - b->image->w/2,
920 .y = b->pos.y - b->image->h/2 };
922 SDL_BlitSurface(b->image, NULL, s, &rect);
925 static void redraw_score(SDL_Surface *s, struct object *me)
927 struct score *score = container_of(me, struct score, object);
928 SDL_Rect rect = { .x = score->object.start.x,
929 .y = score->object.start.y };
931 SDL_BlitSurface(score->image, NULL, s, &rect);
934 static SDL_Surface *score(struct score *s)
939 SDL_FreeSurface(s->image);
940 sprintf(filename, "images/%u.png", s->value);
941 s->image = IMG_Load(filename);
942 /* No more images? You won! */
946 add_update(&s->object);
950 /* Due to rounding, we exaggerate bounding box by 1 here. */
951 static void do_updates(struct list_head *objects, SDL_Surface *screen)
954 unsigned int y, startx, starty, endx, endy;
957 if (start_update.x <= 0)
960 startx = start_update.x - 1;
961 if (start_update.y <= 0)
964 starty = start_update.y - 1;
966 endx = end_update.x+1;
967 endy = end_update.y+1;
969 if (endx > screen->w)
971 if (endy > screen->h)
974 SDL_LockSurface(screen);
975 /* First we clear the area to white. */
976 for (y = starty; y < endy; y++) {
977 memset(screen->pixels + y * screen->pitch
978 + screen->format->BytesPerPixel * startx,
980 (endx - startx) * screen->format->BytesPerPixel);
982 SDL_UnlockSurface(screen);
984 /* Now redraw everything which overlaps it */
985 list_for_each(objects, i, list) {
986 if (i->end.x < startx)
988 if (i->end.y < starty)
990 if (i->start.x > endx)
992 if (i->start.y > endy)
994 i->redraw(screen, i);
997 SDL_UpdateRect(screen, startx, starty, endx - startx, endy - starty);
999 /* Reset bounding box */
1000 needs_update = false;
1003 static bool same_side(const struct ball *ball, unsigned int x)
1005 return (ball->pos.x >= MAX_X/2) == (x >= MAX_X/2);
1008 int main(int argc, char *argv[])
1011 SDL_Surface *screen;
1014 unsigned int i, time_since_last_ir = INT_MAX;
1015 cwiid_wiimote_t *wiimote;
1016 struct cwiid_state state;
1017 struct coord calib[4];
1018 bdaddr_t addr = *BDADDR_ANY;
1019 bool mouse = false, needs_calibration = true, drawing = false;
1021 struct cwiid_ir_src *ir, last_ir = { .valid = 0 };
1022 struct score left, right;
1024 videoflags = SDL_SWSURFACE;
1026 if (argv[1] && streq(argv[1], "--fullscreen")) {
1027 videoflags |= SDL_FULLSCREEN;
1032 if (argv[1] && strstarts(argv[1], "--calib=")) {
1033 needs_calibration = false;
1034 if (sscanf(argv[1], "--calib=%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
1035 &calib[0].x, &calib[0].y,
1036 &calib[1].x, &calib[1].y,
1037 &calib[2].x, &calib[2].y,
1038 &calib[3].x, &calib[3].y) != 8)
1039 errx(1, "Not enough calibration points");
1044 if (argv[1] && streq(argv[1], "--mouse")) {
1046 needs_calibration = false;
1047 calib[0].x = calib[0].y = 0;
1048 calib[1].x = MAX_X - 1;
1050 calib[2].x = MAX_X - 1;
1051 calib[2].y = MAX_Y - 1;
1053 calib[3].y = MAX_Y - 1;
1059 errx(1, "Usage: %s [--fullscreen] [--calib=x,y,x,y,x,y,x,y] [--mouse] speed\n",
1062 /* Initialize SDL */
1063 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
1064 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
1071 printf("Put Wiimote in discoverable mode (press 1+2)\n");
1072 wiimote = cwiid_open(&addr, 0);
1074 errx(1, "Can't find the Wiimote");
1076 if (cwiid_set_rpt_mode(wiimote, CWIID_RPT_IR))
1077 errx(1, "Can't set IR repeat mode");
1080 if ((screen=SDL_SetVideoMode(MAX_X,MAX_Y,video_bpp,videoflags)) == NULL) {
1081 errx(1, "Couldn't set %dx%dx%d video mode: %s",
1082 MAX_X, MAX_Y,video_bpp, SDL_GetError());
1085 /* Set the surface pixels and refresh! */
1086 if (SDL_LockSurface(screen) < 0) {
1087 errx(1, "Couldn't lock the display surface: %s",
1091 memset(screen->pixels, 0xFF, screen->pitch * screen->h);
1093 SDL_UnlockSurface(screen);
1094 SDL_UpdateRect(screen, 0, 0, 0, 0);
1096 if (needs_calibration) {
1098 calibrate(screen, "images/top-left.png",
1099 wiimote, &calib[0], 0, 0);
1100 calibrate(screen, "images/top-right.png",
1101 wiimote, &calib[1], MAX_X - 1, 0);
1102 calibrate(screen, "images/bottom-right.png",
1103 wiimote, &calib[2], MAX_X - 1, MAX_Y - 1);
1104 calibrate(screen, "images/bottom-left.png",
1105 wiimote, &calib[3], 0, MAX_Y - 1);
1108 /* Create borders, put them in list. */
1109 for (i = 0; i < ARRAY_SIZE(border); i++) {
1110 border[i].ignore = false;
1111 border[i].expires.tv_sec = LONG_MAX;
1112 border[i].expires.tv_usec = 0;
1113 line_object_setup(&lines, &objects, &border[i]);
1116 /* Set up scores. */
1117 left.value = right.value = 0;
1118 left.image = IMG_Load("images/0.png");
1119 left.won = IMG_Load("images/left-won.png");
1120 right.image = IMG_Load("images/0.png");
1121 right.won = IMG_Load("images/right-won.png");
1122 if (!left.image || !right.image || !left.won || !right.won)
1123 err(1, "Opening score images");
1125 add_object(&objects, &left.object,
1126 MAX_X/6 - left.image->w/2, 10,
1127 left.image->w, left.image->h, redraw_score);
1128 add_object(&objects, &right.object,
1129 MAX_X - MAX_X/6 - left.image->w/2, 10,
1130 left.image->w, left.image->h, redraw_score);
1132 /* Hit right border = score to left, and vice versa. */
1133 border[1].score = &left;
1134 border[3].score = &right;
1136 srandom(time(NULL));
1137 ball.pos.x = MAX_X/2;
1138 ball.pos.y = MAX_Y/2;
1139 ball.move.x = (random() % 2 ? -1 : 1) * atoi(argv[1]);
1141 ball.image = ball_surface(screen);
1142 add_object(&objects, &ball.object,
1143 ball.pos.x - ball.image->w/2,
1144 ball.pos.y - ball.image->h/2,
1145 ball.image->w, ball.image->h, redraw_ball);
1148 struct coord new, isect, move = ball.move;
1149 struct line_segment *best_l, *l, *next;
1153 bool ignored = false;
1155 gettimeofday(&now, NULL);
1158 list_for_each_safe(&lines, l, next, list) {
1159 if (timercmp(&l->expires, &now, <))
1164 best_d = MAX_X + MAX_Y;
1166 new.x = ball.pos.x + move.x;
1167 new.y = ball.pos.y + move.y;
1169 list_for_each(&lines, l, list) {
1170 if (!l->ignore && intersect(&ball.pos, &new, l, &isect)) {
1172 d = dist(&ball.pos, &isect);
1181 printf("Ball bouncing off (%f,%f %f,%f)!\n",
1182 best_l->start.x, best_l->start.y,
1183 best_l->end.x, best_l->end.y);
1184 /* Only increase speed on *first* bounce, to avoid
1185 * mega speed increases. */
1186 bounce(&ball, best_l, &move, ignored ? 0.0 : 0.1);
1188 /* If we moved, stop ignoring lines. */
1189 if (ignored && (move.x > 0.001 || move.y > 0.001))
1190 clear_ignore(&lines);
1192 /* Don't hit the same line twice. */
1193 printf("Ignoring that line\n");
1194 best_l->ignore = ignored = true;
1195 if (best_l->score) {
1196 SDL_Surface *won = score(best_l->score);
1200 rect.x = MAX_X/2 - won->w/2;
1201 rect.y = MAX_Y/2 - won->h/2;
1204 SDL_BlitSurface(won, NULL, screen,
1206 SDL_UpdateRect(screen,
1216 if (ignored && (move.x > 0.001 || move.y > 0.001)) {
1217 clear_ignore(&lines);
1218 printf("Moving by %f,%f to %f,%f\n",
1219 move.x, move.y, new.x, new.y);
1222 /* We also need to redraw under old ball. */
1223 add_update(&ball.object);
1227 ball.object.start.x = ball.pos.x - ball.image->w/2;
1228 ball.object.start.y = ball.pos.y - ball.image->h/2;
1229 ball.object.end.x = ball.pos.x + ball.image->w/2;
1230 ball.object.end.y = ball.pos.y + ball.image->h/2;
1232 /* Need to draw under new ball. */
1233 add_update(&ball.object);
1235 /* Clears, redraws and resets the updates */
1236 do_updates(&objects, screen);
1240 while (SDL_PollEvent(&event)) {
1241 if (event.type == SDL_QUIT
1242 || (event.type == SDL_KEYDOWN
1243 && event.key.keysym.sym == SDLK_ESCAPE)) {
1248 switch (event.type) {
1249 case SDL_MOUSEBUTTONDOWN:
1252 case SDL_MOUSEBUTTONUP:
1255 case SDL_MOUSEMOTION:
1257 !same_side(&ball, event.motion.x))
1260 add_line(&lines, event.motion.x,
1263 + event.motion.xrel,
1265 + event.motion.yrel,
1274 if (cwiid_get_state(wiimote, &state))
1275 errx(1, "No wii state");
1277 /* Find biggest state. */
1278 ir = &state.ir_src[0];
1279 for (i = 0; i < CWIID_IR_SRC_COUNT; i++) {
1280 if (!state.ir_src[i].valid)
1282 /* Only look at dots on same side as ball */
1283 if (!same_side(&ball, state.ir_src[i].pos[0]))
1285 if (!ir->valid || state.ir_src[i].size > ir->size)
1286 ir = &state.ir_src[0];
1290 /* Give it some slack for missing one or two... */
1291 if (time_since_last_ir <= 5
1292 && same_side(&ball, last_ir.pos[0])) {
1293 add_line(&lines, last_ir.pos[0],
1299 time_since_last_ir = 0;
1302 time_since_last_ir++;