/*  :ts=8 bk=0
 * In the tradition of stupid programs, Leo Schwab presents (yet again)
 * The Art Program.
 *
 * Modofied to do even neater things.
 * 8607.1
 */

#include <exec/types.h>
#include <graphics/gfxbase.h>
#include <intuition/intuition.h>

#define XSIZE           319
#define YSIZE           199

extern int rnd();

struct IntuitionBase    *IntuitionBase;
struct GfxBase          *GfxBase;
struct Screen   *scr, *OpenScreen();
struct Window   *win, *OpenWindow();
struct ViewPort *vp;

struct NewScreen scrdef = {
        0, 0, XSIZE+1, YSIZE+1,
        4,              /*  16 colors  */
        0, 1,           /*  detail/block pens  */
                        /*  "Automatically" set up screen format  */
        ((XSIZE>320) ? HIRES : 0) | ((YSIZE>200) ? LACE : 0),
        CUSTOMSCREEN,
        NULL, NULL, NULL, NULL  /* no special font, title, gadg, or bitmap */
};

struct NewWindow windef = {
        0, 0, XSIZE+1, YSIZE+1,
        0, 1,
        CLOSEWINDOW,
        WINDOWCLOSE | BACKDROP | BORDERLESS,
        NULL, NULL, NULL,       /* no special gadget, checkmark or title */
        NULL,                   /* screen pointer; will get set later */
        NULL,                   /* no custom bitmap */
        0, 0, 0, 0,             /* ignored */
        CUSTOMSCREEN
};

main ()
{
        register struct RastPort *rp;
        int i, dx1, dy1, dx2, dy2, pen = 16, flag = 0,
            xa1[150], ya1[150], xa2[150], ya2[150];
        register int x1, y1, x2, y2;

        openstuff ();
        rnd (-87634);
/*	rp = win -> RPort;    <-- This is slower because of layers    */
        rp = & (scr -> RastPort);
        SetDrMd (rp, COMPLEMENT);
        ShowTitle (scr, FALSE);
        SetRast (rp, 0);
        x1 = rnd (XSIZE+1);  y1 = rnd (YSIZE+1);
        x2 = rnd (XSIZE+1);  y2 = rnd (YSIZE+1);
        setdisp (&dx1, &dy1);
        setdisp (&dx2, &dy2);

       while (1) {
        for (i=0; i<150; i++) {
		rotate ();
                if (GetMsg (win -> UserPort)) {
                        closestuff ();
                        exit (TRUE);
                }
                if (rnd (12) == 3)
                        if (rnd (2))
                                setdisp (&dx1, &dy1);
                        else
                                setdisp (&dx2, &dy2);

                x1 += dx1;  y1 += dy1;
                if (x1 > XSIZE || x1 < 0) {
                        dx1 = -dx1;
                        x1 = x1<0 ? 0 : XSIZE;
                }
                if (y1 > YSIZE || y1 < 0) {
                        dy1 = -dy1;
                        y1 = y1<0 ? 0 : YSIZE;
                }
                x2 += dx2;  y2 += dy2;
                if (x2 > XSIZE || x2 < 0) {
                        dx2 = -dx2;
                        x2 = x2<0 ? 0 : XSIZE;
                }
                if (y2 > YSIZE || y2 < 0) {
                        dy2 = -dy2;
                        y2 = y2<0 ? 0 : YSIZE;
                }

                if (!--pen)
                        pen = 15;
                rp -> Mask = pen;       /* kludge; COMPLEMENT does not  */
                Move (rp, x1, y1);      /* perform as advertised  */
                Draw (rp, x2, y2);
                if (flag) {
                        Move (rp, xa1[i], ya1[i]);
                        Draw (rp, xa2[i], ya2[i]);
                }
                xa1[i] = x1;  ya1[i] = y1;
                xa2[i] = x2;  ya2[i] = y2;
        }
       flag = 1;
       }        /* End of the while */
}

openstuff ()
{
        if (!(IntuitionBase = (struct IntuitionBase *)
            OpenLibrary ("intuition.library", 1))) {
                printf ("Awright, where's Intuition?\n");
                exit (FALSE);
        }

        if (!(GfxBase = (struct GfxBase *)
            OpenLibrary ("graphics.library", 1))) {
                printf ("Graphics?  Heeeere graphics....\n");
                exit (FALSE);
        }

	if (!(scr = OpenScreen (&scrdef))) {
                printf ("Can't open your screen.\n");
                exit (FALSE);
        }

        windef.Screen = scr;
        if (!(win = OpenWindow (&windef))) {
                printf ("Window painted shut.\n");
                exit (FALSE);
        }
	vp = &(scr -> ViewPort);
	setcolors ();
}

closestuff ()
{
        CloseWindow (win);
        CloseScreen (scr);
        CloseLibrary (GfxBase);
        CloseLibrary (IntuitionBase);
}

setdisp (x, y)
register int *x, *y;
{
        *x = rnd (9) - 4;
        *y = rnd (9) - 4;
}

/*  What I really wanted was the HSL color wheel.  Oh well....  */
setcolors ()
{
	int r, g, b, i;

	for (i=1; i<16; i++) {
		if (i <= 5) {
			r = (6-i) * 15 / 5;
			g = (i-1) * 15 / 5;
			b = 0;
		} else if (i > 5 && i <= 10) {
			r = 0;
			g = (11-i) * 15 / 5;
			b = (i-5) * 15 / 5;
		} else if (i > 10) {
			r = (i-10) * 15 / 5;
			g = 0;
			b = (16-i) * 15 / 5;
		}
		SetRGB4 (vp, i, r, g, b);
	}
	SetRGB4 (vp, 0, 0, 0, 0);
}
