[net.sources.mac] Saddle.c

tdn@cmu-cs-spice.ARPA (Thomas Newton) (03/27/85)

This is the source code for the Saddle desk accessory, written in C for the
Megamax C compiler (v2.0, mixed-case toolbox names).

                                        -- Thomas Newton
                                           Thomas.Newton@cmu-cs-spice.ARPA


/*
 * Saddle desk accessory
 *
 * Copyright (c) 1985 Thomas Newton
 * May be reproduced freely for personal use
 *     but not for commercial use, provided
 *     that this notice remains intact.
 *
 */
 
#include <desk.h>
#include <acc.h>
#include <qd.h>
#include <event.h>
#include <file.h>
#include <win.h>
#include <device.h>

ACC(0x2400,      /* Enable control, need time */
    2,           /* Thirty times per second */
    0x0140,      /* Activate, update events */
    0,           /* No menu items */
    7, "Saddle ")  /* Length and text of title */

#define NIL 0
#define TRUE 1
#define abs(x) ((x)<0?-(x):(x))

/* Constants related to window size */
#define xSize 256
#define ySize 256
#define minXRange 128			/* xSize/2 */
#define minYRange 128			/* ySize/2 */
#define n 36				/* (xSize + YSize)/14 */

/* Globals */
WindowPtr window; /* our window */
Rect windowRect; /* its size */
int x11, y11, x12, y12;
int x21, y21, x22, y22;
int x1, y1, x2, y2, deltaX1, deltaY1, deltaX2, deltaY2;
int i, delay = 0;
int update_lock = 0;
int oldi;
char title[] = "Saddle";

/* Driver routines */

accopen(dctl, pb)
dctlentry *dctl;
paramblockrec *pb;
{
    if (!dctl->dCtlWindow) createWindow(dctl);
    return 0;
}

accclose(dctl, pb)
dctlentry *dctl;
paramblockrec *pb;
{
    DisposeWindow(window);
    dctl->dCtlWindow = NIL;
    return 0;
}

accctl(dctl, pb)
dctlentry *dctl;
paramblockrec *pb;
{   SetPort(window);
    switch (pb->paramunion.CntrlParam.CSCode) {
        case accEvent:
            handleEvent((EventRecord *)  /* We really have an event */
	                pb->paramunion.CntrlParam.csParam.diskBuff);
            break;
        case accRun:
            update_lock += 1;
            if (update_lock == 1) updateSaddle();
            update_lock -= 1;
            break;
    }
    return 0;
}

accprime()
{
}

accstatus()
{
}


/* Real routines */

/* Create our window */

createWindow(dctl)
dctlentry *dctl;
{
    SetRect(&windowRect, 0, 0, xSize, ySize);
    OffsetRect(&windowRect, 128, 50);
    window = NewWindow((WindowPeek) NIL, &windowRect, title, TRUE,
        RDocProc, (WindowPtr) -1, TRUE, 0);
    dctl->dCtlWindow = window;
    OffsetRect(&windowRect, -128, -50);

    SetPort(window);
    ((WindowPeek) window)->windowKind = dctl->dCtlRefNum;
}

handleEvent(event)
EventRecord *event;
{
    switch (event->what) {
        case updateEvt:
            updateEvent();
            break;
    }
}

updateEvent()
{
    update_lock += 1;
    if (update_lock == 1) {
        BeginUpdate(window);
        SetPort(window);

        if ((i == 0) && (delay > 0)) {
            i = n;        /* Redraw entire previous saddle */
        }

        if ((i > 0) && (i <= n)) {
            oldi = i;
            i = 0;
            while (i < oldi) {
                DrawSaddle();
            }
        }
        EndUpdate(window);
    }
    update_lock -= 1;
}

/* Predeclare functions used by updateSaddle() */
int RandomMod();
int min();
int max();

updateSaddle()
{   int flag = NIL;

    SetPort(window);
    if (i == 0) {
        if (delay == 0) {
            EraseRect(&windowRect);
            flag = NIL;
            while (flag == NIL) {
                x11 = RandomMod(xSize);
                y11 = RandomMod(ySize);
                x12 = RandomMod(xSize);
                y12 = RandomMod(ySize);
                x21 = RandomMod(xSize);
                y21 = RandomMod(ySize);
                x22 = RandomMod(xSize);
                y22 = RandomMod(ySize);
                if ((range(x11, x12, x21, x22) >= minXRange) &&
                    (range(y11, y12, y21, y22) >= minYRange)) {
                    flag = TRUE;
                    }
            }
            delay = 30;             /* reset delay for next clear screen */
            DrawSaddle();
        }
        else
            delay = delay - 1;     /* pause between drawings */
    }
    else
        DrawSaddle();
}

int RandomMod(wrap)
int wrap;
{   int temp1;

    temp1 = Random();
    if (temp1 == -32768) {
        temp1 = -32767;
        }
    temp1 = abs(temp1);
    temp1 = temp1 - (wrap * (temp1 / wrap));
    return(temp1);
}

DrawSaddle()
{
    if (i == 0) {
        /* calculate drawing increments */
        deltaX1 = (x12 - x11) / n;
        deltaY1 = (y12 - y11) / n;
        deltaX2 = (x22 - x21) / n;
        deltaY2 = (y22 - y21) / n;

        /* initialize Saddle line endpoints */
        x1 = x11;
        y1 = y11;
        x2 = x21;
        y2 = y21;
    }

    /* draw one line of Saddle itself */
    MoveTo(x1, y1);
    LineTo(x2, y2);

    x1 = x1 + deltaX1;
    y1 = y1 + deltaY1;

    x2 = x2 + deltaX2;
    y2 = y2 + deltaY2;

    i = i + 1;
    if (i > n) {
        i = 0;
    }
}

int min (i1, i2, i3, i4)
int i1, i2, i3, i4;
{   int t;

    t = i1;
    if (i2 < t) {
        t = i2;
    }
    if (i3 < t) {
        t = i3;
    }
    if (i4 < t) {
        t = i4;
    }
    return(t);
}

int max (i1, i2, i3, i4)
int i1, i2, i3, i4;
{   int t;

    t = i1;
    if (i2 > t) {
        t = i2;
    }
    if (i3 > t) {
        t = i3;
    }
    if (i4 > t) {
        t = i4;
    }
    return(t);
}

int range (i1, i2, i3, i4)
int i1, i2, i3, i4;
{
    return(max(i1, i2, i3, i4) - min(i1, i2, i3, i4));
}