/************************************************************************
 * meltviewport(struct ViewPort *,LONG);
 *    by Mark Nuiver 05/24/87
 *    most of the code is borrowed from Stephen Coy's "Melt.c"
 * program.
 *    The code is modified here into a general function to "melt"
 * any ViewPort.
 *    Function is called as meltviewport(vp,secs); with vp being a pointer
 * to a ViewPort struct and secs a LONG number of seconds to melt before
 * returning.
 *
 * COMPILER INFORMATION:
 *    Lattice C version 3.10
 *    AmigaDOS 1.2
 * LINKING INFORMATION:
 *    link with LIB:lcm.lib (first library)
 ***********************************************************************/

#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/tasks.h>
#include <exec/ports.h>
#include <exec/io.h>

#include <graphics/gfx.h>
#include <graphics/view.h>

#include <intuition/intuition.h>

#include <math.h>

extern struct IntuiMessage *GetMsg();
extern struct MsgPort      *CreatePort();

void meltviewport(vp,secs) struct ViewPort *vp;
                           LONG secs; {
   struct BitMap   *bitmap;
   struct timerequest tr;
   LONG   x, y,         /* start positions       */
          dx, dy,       /* offsets               */
          u, v;         /* size                  */
   LONG   TempA[32];    /* temp buffer           */
   UBYTE  mask;         /* bit-plane mask for blitter   */
   SHORT  width,height;
   UBYTE  depth;

/* create a reply port for the timer device */
   if ((tr.tr_node.io_Message.mn_ReplyPort = CreatePort(0,0)) == NULL)
      return;
/* open the timer device */
   if (OpenDevice(TIMERNAME,UNIT_VBLANK,&tr,0) != NULL) return;
/* send a time request */
   tr.tr_node.io_Command = TR_ADDREQUEST;
   tr.tr_node.io_Message.mn_Node.ln_Pri = 10;
   tr.tr_time.tv_secs = secs;
   tr.tr_time.tv_micro = 0;
   SendIO(&tr);

   bitmap = vp->RasInfo->BitMap;
   width = vp->DWidth;
   height = vp->DHeight;
   depth = bitmap->Depth;
/* do until the timerequest is returned */
   FOREVER {
     if(CheckIO(&tr) != NULL) {
         CloseDevice(&tr);
         return;
     }

     for(mask=1; mask<((1<<depth)-1); mask*=2) {
       u = (drand48() * (width - 3)) + 1;
       v = (drand48() * (height - 3)) + 1;
       x = (drand48() * (width - 1 - u)) + 1;
       y = (drand48() * (height - 2 - v)) + 1;
       dx = (drand48() * 3) - 1;
       dy = drand48() * 3;

       BltBitMap(bitmap, x, y,
                 bitmap, x+dx, y+dy,
                 u, v, 0x0c0, mask, TempA);

     }
   }     /* end of FOREVER */
}     /* end of meltviewport */
