11 #include "list/list.h"
16 #define MAX_X CWIID_IR_X_MAX
17 #define MAX_Y CWIID_IR_Y_MAX
25 /* Obtained from http://tog.acm.org/GraphicsGems/gemsiii/insectc.c */
26 /* Faster Line Segment Intersection */
27 /* Franklin Antonio */
28 static bool lines_intersect(const struct coord *start1,
29 const struct coord *start2,
30 const struct coord *start3,
31 const struct coord *start4,
32 struct coord *intersect)
34 double Ax,Bx,Cx,Ay,By,Cy,d,e,f,num;
37 Ax = start2->x - start1->x;
38 Bx = start3->x - start4->x;
40 if(Ax<0) { /* X bound box test*/
41 lo.x = start2->x; hi.x = start1->x;
43 hi.x = start2->x; lo.x = start1->x;
46 if(hi.x < start4->x || start3->x < lo.x) return false;
48 if(hi.x < start3->x || start4->x < lo.x) return false;
51 Ay = start2->y - start1->y;
52 By = start3->y - start4->y;
54 if(Ay<0) { /* Y bound box test*/
55 lo.y = start2->y; hi.y = start1->y;
57 hi.y = start2->y; lo.y = start1->y;
60 if(hi.y < start4->y || start3->y < lo.y) return false;
62 if(hi.y < start3->y || start4->y < lo.y) return false;
66 Cx = start1->x - start3->x;
67 Cy = start1->y - start3->y;
68 d = By*Cx - Bx*Cy; /* alpha numerator*/
69 f = Ay*Bx - Ax*By; /* both denominator*/
70 if (f>0) { /* alpha tests*/
71 if (d < 0 || d > f) return false;
73 if (d > 0 || d < f) return false;
76 e = Ax*Cy - Ay*Cx; /* beta numerator*/
77 if (f > 0) { /* beta tests*/
78 if (e < 0 || e > f) return false;
80 if (e > 0 || e < f) return false;
83 /*compute intersection coordinates*/
84 /* Don't worry about tiny values of f (< 1/2 pixel across screen) */
85 if (fabs(f) < 1.0 / (2.0 * MAX_X * MAX_Y))
88 num = d*Ax; /* numerator */
89 intersect->x = start1->x + num / f; /* intersection x */
92 intersect->y = start1->y + num / f; /* intersection y */
97 // A set of very useful macros that you will find in most
98 // code that I write whether I use them in a program or
101 #define abs(a) (((a)<0) ? -(a) : (a))
102 #define sign(a) (((a)<0) ? -1 : (a)>0 ? 1 : 0)
104 // The following code implements a Bresenham line drawing
105 // algorithm. There are 4 separate routines each optimized
106 // for one of the four pixel depths supported by SDL. SDL
107 // support many pixel formats, but it only support 8, 16,
108 // 24, and 32 bit pixels.
110 //----------------------------------------------------------
112 // Draw lines in 8 bit surfaces.
114 static void line8(SDL_Surface *s,
139 yOffset = sy * s->pitch;
144 lineAddr = ((Uint8 *)(s->pixels)) + (y * s->pitch);
150 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
151 *(lineAddr + x) = (Uint8)color;
172 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
173 *(lineAddr + x) = (Uint8)color;
191 //----------------------------------------------------------
193 // Draw lines in 16 bit surfaces. Note that this code will
194 // also work on 15 bit surfaces.
196 static void line16(SDL_Surface *s,
221 yOffset = sy * s->pitch;
226 lineAddr = ((Uint8 *)s->pixels) + (y * s->pitch);
232 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
233 *((Uint16 *)(lineAddr + (x << 1))) = (Uint16)color;
254 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
255 *((Uint16 *)(lineAddr + (x << 1))) = (Uint16)color;
273 //----------------------------------------------------------
275 // Draw lines in 24 bit surfaces. 24 bit surfaces require
276 // special handling because the pixels don't fall on even
277 // address boundaries. Instead of being able to store a
278 // single byte, word, or long you have to store 3
279 // individual bytes. As a result 24 bit graphics is slower
280 // than the other pixel sizes.
282 static void line24(SDL_Surface *s,
300 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
311 yOffset = sy * s->pitch;
316 lineAddr = ((Uint8 *)(s->pixels)) + (y * s->pitch);
322 Uint8 *p = (lineAddr + (x * 3));
323 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
324 memcpy(p, &color, 3);
345 Uint8 *p = (lineAddr + (x * 3));
346 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
347 memcpy(p, &color, 3);
365 //----------------------------------------------------------
367 // Draw lines in 32 bit surfaces. Note that this routine
368 // ignores alpha values. It writes them into the surface
369 // if they are included in the pixel, but does nothing
372 static void line32(SDL_Surface *s,
397 yOffset = sy * s->pitch;
402 lineAddr = ((Uint8 *)(s->pixels)) + (y * s->pitch);
408 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
409 *((Uint32 *)(lineAddr + (x << 2))) = (Uint32)color;
430 if (y >= 0 && y < MAX_Y && x >= 0 && x < MAX_X)
431 *((Uint32 *)(lineAddr + (x << 2))) = (Uint32)color;
449 //----------------------------------------------------------
451 // Examine the depth of a surface and select a line
452 // drawing routine optimized for the bytes/pixel of the
455 static void line(SDL_Surface *s,
460 switch (s->format->BytesPerPixel)
463 line8(s, x1, y1, x2, y2, color);
466 line16(s, x1, y1, x2, y2, color);
469 line24(s, x1, y1, x2, y2, color);
472 line32(s, x1, y1, x2, y2, color);
485 struct coord start, end;
486 struct list_node list;
488 struct timeval expires;
491 static void thick_line(SDL_Surface *s, const struct line_segment *l,
494 struct line_segment adj;
495 int x, y, minx = MAX_X, miny = MAX_Y, maxx = 0, maxy = 0;
500 for (x = -1; x < 2; x++) {
501 for (y = -1; y < 2; y++) {
502 adj.start.x = l->start.x + x;
503 adj.end.x = l->end.x + x;
504 adj.start.y = l->start.y + y;
505 adj.end.y = l->end.y + y;
507 if (adj.start.x < minx)
509 if (adj.start.x > maxx)
511 if (adj.start.y < miny)
513 if (adj.start.y > maxy)
515 if (adj.end.x < minx)
517 if (adj.end.x > maxx)
519 if (adj.end.y < miny)
521 if (adj.end.y > maxy)
524 line(s, adj.start.x, adj.start.y, adj.end.x, adj.end.y,
537 SDL_UpdateRect(s, minx, miny, maxx-minx, maxy-miny);
538 SDL_UnlockSurface(s);
541 static bool intersect(const struct coord *c1, const struct coord *c2,
542 const struct line_segment *seg, struct coord *intersect)
544 return lines_intersect(c1, c2, &seg->start, &seg->end, intersect);
547 static double dist(const struct coord *c1, const struct coord *c2)
549 double x = (c1->x - c2->x), y = (c1->y - c2->y);
550 return sqrt(x * x + y * y);
553 static unsigned rad_to_deg(double rad)
555 return ((unsigned)(rad / M_PI * 180) + 360) % 360;
558 static void bounce(struct ball *ball, const struct line_segment *line,
559 struct coord *move, double increase_speed)
561 double len, speed, lang, iang, oang;
562 struct coord isect, new;
564 new.x = ball->pos.x + move->x;
565 new.y = ball->pos.y + move->y;
567 /* How far were we supposed to move ball? */
568 len = sqrt(move->x * move->x + move->y * move->y);
570 /* How far is it to intersection? */
571 if (!intersect(&ball->pos, &new, line, &isect))
572 errx(1, "No intersection any more?\n");;
573 len -= dist(&ball->pos, &isect);
575 /* Move ball to intersection. */
577 printf("ball is now at %f,%f\n", ball->pos.x, ball->pos.y);
579 /* Outgoing angle = 2 * line angle - incident angle. */
580 lang = atan2(line->end.x - line->start.x, line->end.y - line->start.y);
581 iang = atan2(ball->move.x, ball->move.y);
582 oang = 2 * lang - iang, 2*M_PI;
583 printf("lang = %u, iang = %u, oang=%u\n",
588 /* Set new direction for ball, at slightly increased speed */
589 speed = sqrt(ball->move.x * ball->move.x + ball->move.y * ball->move.y);
590 speed += increase_speed;
591 ball->move.x = sin(oang) * speed;
592 ball->move.y = cos(oang) * speed;
595 move->x = sin(oang) * len;
596 move->y = cos(oang) * len;
597 printf("len = %f, move = %f,%f\n", len, move->x, move->y);
600 static struct line_segment border[] = {
601 { { 0, 0, }, { MAX_X-1, 0 } },
602 { { MAX_X-1, 0, }, { MAX_X-1, MAX_Y-1 } },
603 { { MAX_X-1, MAX_Y-1, }, { 0, MAX_Y-1 } },
604 { { 0, MAX_Y-1, }, { 0, 0 } },
607 static inline float deg_to_rad(float degrees)
609 return degrees * M_PI / 180;
612 static SDL_Surface *sub_surface(SDL_Surface *screen, int w, int h)
614 return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
615 screen->format->BitsPerPixel,
616 screen->format->Rmask,
617 screen->format->Gmask,
618 screen->format->Bmask,
619 screen->format->Amask);
622 static SDL_Surface *ball_surface(SDL_Surface *screen)
626 /* Just like the screen surface. */
627 ball = sub_surface(screen, 5, 5);
629 /* Lock the surface */
630 SDL_LockSurface(ball);
632 /* Black, white corners. */
633 memset(ball->pixels, 0x00, ball->pitch * 5);
634 line(ball, 0, 0, 0, 0, 0xFFFFFFFF);
635 line(ball, 4, 0, 4, 0, 0xFFFFFFFF);
636 line(ball, 0, 4, 0, 4, 0xFFFFFFFF);
637 line(ball, 4, 4, 4, 4, 0xFFFFFFFF);
638 SDL_UnlockSurface(ball);
643 static void clear_ignore(struct list_head *lines)
645 struct line_segment *i;
646 printf("Unignoring...\n");
647 list_for_each(lines, i, list)
651 int main(int argc, char *argv[])
654 SDL_Surface *screen, *ballsur, *savesur = NULL;
657 unsigned int i, time_since_last_ir = INT_MAX;
659 struct timeval line_life = { 3, 0 };
660 cwiid_wiimote_t *wiimote;
661 struct cwiid_state state;
662 bdaddr_t addr = *BDADDR_ANY;
664 struct cwiid_ir_src *ir, last_ir = { .valid = 0 };
666 videoflags = SDL_SWSURFACE;
668 if (argv[1] && streq(argv[1], "--fullscreen")) {
669 videoflags = SDL_SWSURFACE;
675 errx(1, "Usage: %s [--fullscreen] ballx bally ballangle ballspeed\n"
676 " Where ballangle is 0 for north, 90 for east, etc\n",
679 ball.pos.x = atol(argv[1]);
680 ball.pos.y = atol(argv[2]);
681 ball.move.x = sin(deg_to_rad(atof(argv[3]))) * atol(argv[4]);
682 ball.move.y = cos(deg_to_rad(atof(argv[3]))) * atol(argv[4]);
683 printf("ball move = %f,%f\n", ball.move.x, ball.move.y);
686 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
687 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
693 printf("Put Wiimote in discoverable mode (press 1+2)\n");
694 wiimote = cwiid_open(&addr, 0);
696 errx(1, "Can't find the Wiimote");
698 if (cwiid_set_rpt_mode(wiimote, CWIID_RPT_IR))
699 errx(1, "Can't set IR repeat mode");
701 if ( (screen=SDL_SetVideoMode(MAX_X,MAX_Y,video_bpp,videoflags)) == NULL ) {
702 errx(1, "Couldn't set %dx%dx%d video mode: %s",
703 MAX_X, MAX_Y,video_bpp, SDL_GetError());
706 /* Set the surface pixels and refresh! */
707 if ( SDL_LockSurface(screen) < 0 ) {
708 errx(1, "Couldn't lock the display surface: %s",
712 memset(screen->pixels, 0xFF, screen->pitch * screen->h);
714 SDL_UnlockSurface(screen);
715 SDL_UpdateRect(screen, 0, 0, 0, 0);
717 /* Draw borders, put them in list. */
718 for (i = 0; i < ARRAY_SIZE(border); i++) {
719 border[i].ignore = false;
720 border[i].expires.tv_sec = LONG_MAX;
721 border[i].expires.tv_usec = 0;
722 list_add(&lines, &border[i].list);
723 thick_line(screen, &border[i], 0);
726 ballsur = ball_surface(screen);
729 struct coord new, isect, move = ball.move;
730 struct line_segment *best_l, *l, *next;
734 bool ignored = false;
736 gettimeofday(&now, NULL);
739 list_for_each_safe(&lines, l, next, list) {
740 if (timercmp(&l->expires, &now, <)) {
741 printf("Deleting line %p\n", l);
743 /* FIXME: Undraw properly. */
744 thick_line(screen, l, 0xFFFFFFFF);
750 best_d = MAX_X + MAX_Y;
752 new.x = ball.pos.x + move.x;
753 new.y = ball.pos.y + move.y;
755 list_for_each(&lines, l, list) {
756 if (!l->ignore && intersect(&ball.pos, &new, l, &isect)) {
758 d = dist(&ball.pos, &isect);
767 printf("Ball bouncing off (%f,%f %f,%f)!\n",
768 best_l->start.x, best_l->start.y,
769 best_l->end.x, best_l->end.y);
770 /* Only increase speed on *first* bounce, to avoid
771 * mega speed increases. */
772 bounce(&ball, best_l, &move, ignored ? 0.0 : 0.1);
774 /* If we moved, stop ignoring lines. */
775 if (ignored && (move.x > 0.001 || move.y > 0.001))
776 clear_ignore(&lines);
778 /* Don't hit the same line twice. */
779 printf("Ignoring that line\n");
780 best_l->ignore = ignored = true;
784 if (ignored && (move.x > 0.001 || move.y > 0.001)) {
785 clear_ignore(&lines);
786 printf("Moving by %f,%f to %f,%f\n",
787 move.x, move.y, new.x, new.y);
790 /* Restore what was there before ball. */
792 SDL_BlitSurface(savesur, NULL, screen, &rect);
793 SDL_UpdateRect(screen, rect.x, rect.y, rect.w, rect.h);
795 savesur = sub_surface(screen, ballsur->w, ballsur->h);
798 /* Save away image under new ball. */
801 rect.x = new.x - ballsur->w/2;
802 rect.y = new.y - ballsur->h/2;
803 SDL_BlitSurface(screen, &rect, savesur, NULL);
806 SDL_BlitSurface(ballsur, NULL, screen, &rect);
807 SDL_UpdateRect(screen, rect.x, rect.y, rect.w, rect.h);
808 /* That SDL_BlitSurface can crop rect. */
809 rect.x = new.x - ballsur->w/2;
810 rect.y = new.y - ballsur->h/2;
814 if (SDL_PollEvent(&event)
815 && (event.type == SDL_QUIT
816 || (event.type == SDL_KEYDOWN
817 && event.key.keysym.sym == SDLK_ESCAPE))) {
822 if (cwiid_get_state(wiimote, &state))
823 errx(1, "No wii state");
825 /* Find biggest state. */
826 ir = &state.ir_src[0];
827 for (i = 0; i < CWIID_IR_SRC_COUNT; i++) {
828 if (!state.ir_src[i].valid)
830 if (!ir->valid || state.ir_src[i].size > ir->size)
831 ir = &state.ir_src[0];
835 /* Give it some slack for missing one or two... */
836 if (time_since_last_ir <= 3) {
837 struct line_segment *n = malloc(sizeof(*n));
838 /* Wiimote coordinates are backwards for us. */
839 n->start.x = MAX_X - last_ir.pos[0];
840 n->start.y = MAX_Y - last_ir.pos[1];
841 n->end.x = MAX_X - ir->pos[0];
842 n->end.y = MAX_Y - ir->pos[1];
844 timeradd(&now, &line_life, &n->expires);
845 list_add_tail(&lines, &n->list);
846 thick_line(screen, n, 0);
848 time_since_last_ir = 0;
851 time_since_last_ir++;