/*
 *   This routine handles the display of the bit array in BlitLab.
 */
#include "structures.h"
/*
 *   This is the address of the real bits we operate on.
 */
extern short int *realbits ;
static int safearr[192] ;
allocbitmem() {
   extern void *allocmem() ;

   realbits = (short *)(allocmem(1000L, MEMF_CHIP | MEMF_CLEAR)) + 150 ;
}
pdot(x, y, on)
int x, y, on ;
{
   int off = (x >> 4) + y * 6 ;
   int bit = 1 << (15 - (x & 15)) ;

   if (on) {
      if ((realbits[off] & bit) == 0) {
         realbits[off] |= bit ;
         safearr[off] |= bit ;
         color(WHITE) ;
         fbox(x * 6 + HBITSTART, y * 3 + VBITSTART + 1, 4, 2) ;
      }
   } else {
      if (realbits[off] & bit) {
         realbits[off] &= ~bit ;
         safearr[off] &= ~bit ;
         color(BLACK) ;
         fbox(x * 6 + HBITSTART, y * 3 + VBITSTART + 1, 4, 2) ;
      }
   }
}
preg(x1, y1, x2, y2, on)
int x1, y1, x2, y2, on ;
{
   int i, j ;

   for (i=x1; i<=x2; i++)
      for (j=y1; j<=y2; j++)
         pdot(i, j, on) ;
}
/*
 *   This routine writes out the new position to the screen.
 */
updatepos(x1, y1)
int x1, y1 ;
{
   char outbuf[4] ;

   sprintf(outbuf, "%3d", ((x1 >> 3) & ~1) + y1 * 12) ;
   drawtext(HLMGSTART+28, VLMG3+6, outbuf) ;
   sprintf(outbuf, "%2d", x1 & 15) ;
   drawtext(HLMGSTART+12, VLMG4+6, outbuf) ;
}
updatebits() {
   int x=HBITSTART, y=VBITSTART+1 ;
   register short *p1 = realbits, *p2 = safearr ;
   int i = 192 ;
   int rc = 6 ;
   int bit ;

   while (i-- > 0) {
      if (*p1 == *p2) {
         p1++ ;
         p2++ ;
         x += 6 * 16 ;
      } else {
         bit = 0x8000 ;
         while (bit != 0) {
            if ((*p2 & bit) != (*p1 & bit)) {
               color((*p1 & bit) ? WHITE : BLACK) ;
               fbox(x, y, 4, 2) ;
            }
            bit = (bit >> 1) & 0x7fff ;
            x += 6 ;
         }
         *p2++ = *p1++ ;
      }
      if (--rc == 0) {
         rc = 6 ;
         x = HBITSTART ;
         y += 3 ;
      }
   }
}
