/*
 *   Initialization module of the blitlab program.
 */
#include "structures.h"
/*
 *   These are the externals we reference.
 */
extern struct Window *mywindow ;
extern struct GfxBase *GfxBase ;
extern struct IntuitionBase *IntuitionBase ;
extern struct RastPort *myrp ;
/*
 *   This is the humongous window we open on the standard
 *   workbench screen.
 */
static struct NewWindow mynewwindow = {
   HWINSTART, VWINSTART, HWINSIZE, VWINSIZE,
   -1, -1,
   MOUSEBUTTONS | MOUSEMOVE | GADGETDOWN | GADGETUP | CLOSEWINDOW,
   WINDOWDEPTH | WINDOWCLOSE | WINDOWDRAG | SMART_REFRESH | REPORTMOUSE
   | ACTIVATE,
   NULL,
   NULL,
   (UBYTE *)BANNER,
   NULL,
   NULL,
   0, 0, 0, 0,
   WBENCHSCREEN } ;
/*
 *   This is the main initialize routine, which gets everything started
 *   up.
 */
initialize() {
   int i, j ;
/*
 *   First, we try and open libraries and windows.
 */
   if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary(
      "intuition.library",0L))==NULL ||
       (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L))
      ==NULL)
      error("! Couldn't open libraries") ;
   if ((mywindow=OpenWindow(&mynewwindow))==NULL)
      error("! Couldn't open window") ;
   myrp = mywindow -> RPort ;
   allocbitmem() ;
   buildgadgets() ;
   drawlabels() ;
   parseall() ;
/*
 *   Here we draw the bits array, hopefully for easy reference.
 */
   color(BLACK) ;
   fbox(HBITSTART, VBITSTART, HBITSIZE, VBITSIZE) ;
   color(WHITE) ;
   box(HBITSTART, VBITSTART, HBITSIZE, VBITSIZE) ;
   color(ORANGE) ;
   for (i=1; i<24; i++)
      box(HBITSTART + i * 24 - 2, VBITSTART, 2, VBITSIZE) ;
   for (i=1; i<96; i++)
      for (j=1; j<32; j++)
         line(HBITSTART + i * 6 - 2, VBITSTART + j * 3, HBITSTART + i * 6 - 2,
              VBITSTART + j * 3) ;
   color(BLUE) ;
   box(HBITSTART, VBITSTART, HBITSIZE, VBITSIZE) ;
   for (i=1; i<7; i++)
      box(HBITSTART + i * 96 - 2, VBITSTART, 2, VBITSIZE) ;
   for (i=1; i<9; i++)
      line(HBITSTART, VBITSTART + i * 12, HBITSTART + HBITSIZE - 1,
           VBITSTART + i * 12) ;
   updatebits() ;
/*
 *   Now we draw boxes around the blitter register values and user
 *   settable values.
 */
   color(WHITE) ;
   box(HRVSTART, VRVSTART, HRVSIZE, VRVSIZE) ;
   line(HMVSTART, VRVSTART, HMVSTART, VRVSTART + VRVSIZE - 1) ;
}
/*
 *   This routine cleans up for exit.
 */
cleanup() {
   if (mywindow != NULL)
      CloseWindow(mywindow) ;
   if (GfxBase != NULL)
      CloseLibrary(GfxBase) ;
   if (IntuitionBase != NULL)
      CloseLibrary(IntuitionBase) ;
   freemem() ;
   exit(0) ;
}
/*
 *   drawlabels() draws several miscellaneous labels all over the
 *   screen.
 */
drawlabels() {
   drawtext(HLMGSTART+4, VLMG3-2, "Adrs:") ;
   drawtext(HLMGSTART+4, VLMG3+6, " M+") ;
   drawtext(HLMGSTART+4, VLMG4-2, "Shift:") ;
   drawtext(HRVC1 + 12, VRVL6, "Blitter Register Values") ;
   drawtext(HRVC7 + 4, VRVLL6, "DMA Channels") ;
   drawtext(HRVC1, VRVL1, "CON0") ;
   drawtext(HRVC1, VRVL2, "CON1") ;
   drawtext(HRVC1, VRVL3, "SIZE") ;
   drawtext(HRVC1, VRVL4, "AFWM") ;
   drawtext(HRVC1, VRVL5, "ALWM") ;
   drawtext(HRVC3, VRVL2, "A") ;
   drawtext(HRVC3, VRVL3, "B") ;
   drawtext(HRVC3, VRVL4, "C") ;
   drawtext(HRVC3, VRVL5, "D") ;
   drawtext(HRVC4 + 4, VRVL1, "PTH  PTL  MOD  DAT") ;
   drawtext(HRVC7, VRVLL2, "A") ;
   drawtext(HRVC7, VRVLL3, "B") ;
   drawtext(HRVC7, VRVLL4, "C") ;
   drawtext(HRVC7, VRVLL5, "D") ;
   drawtext(HRVC8, VRVL1, "USE   PT     MOD          DAT          SH") ;
}
