home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 262.lha / BlitLab_v1.4 / bits.c < prev    next >
C/C++ Source or Header  |  1989-06-29  |  3KB  |  150 lines

  1. /*
  2.  *   This routine handles the display of the bit array in BlitLab.
  3.  */
  4. #include "structures.h"
  5. /*
  6.  *   This is the address of the real bits we operate on.
  7.  */
  8. extern short int *realbits ;
  9. extern long gvals[] ;
  10. static short safearr[192] ;
  11. static short osafearr[192] ;
  12. static int specbits ; /* should we leave the undo array alone? */
  13. allocbitmem() {
  14.    extern void *allocmem() ;
  15.  
  16.    realbits = (short *)(allocmem(1000L, MEMF_CHIP | MEMF_CLEAR)) + 150 ;
  17. }
  18. savebits(f)
  19. FILE *f ;
  20. {
  21.    int i ;
  22.  
  23.    for (i=0; i<192; i++)
  24.       fprintf(f, "%d\n", safearr[i]) ;
  25. }
  26. loadbits(f)
  27. FILE *f ;
  28. {
  29.    int i ;
  30.  
  31.    for (i=0; i<192; i++)
  32.       if (fscanf(f, "%d\n", realbits+i) != 1)
  33.          error("invalid bit in save file") ;
  34.    updatebits() ;
  35. }
  36. pdot(x, y, on)
  37. int x, y, on ;
  38. {
  39.    int off = (x >> 4) + y * 6 ;
  40.    int bit = 1 << (15 - (x & 15)) ;
  41.  
  42.    if (on) {
  43.       if ((realbits[off] & bit) == 0) {
  44.          realbits[off] |= bit ;
  45.          safearr[off] |= bit ;
  46.          color(WHITE) ;
  47.          fbox(x * 6 + HBITSTART, y * 3 + VBITSTART + 1, 4, 2) ;
  48.       }
  49.    } else {
  50.       if (realbits[off] & bit) {
  51.          realbits[off] &= ~bit ;
  52.          safearr[off] &= ~bit ;
  53.          color(BLACK) ;
  54.          fbox(x * 6 + HBITSTART, y * 3 + VBITSTART + 1, 4, 2) ;
  55.       }
  56.    }
  57. }
  58. preg(x1, y1, x2, y2, on)
  59. int x1, y1, x2, y2, on ;
  60. {
  61.    int i, j ;
  62.  
  63.    for (i=x1; i<=x2; i++)
  64.       for (j=y1; j<=y2; j++)
  65.          pdot(i, j, on) ;
  66. }
  67. /*
  68.  *   This routine writes out the new position to the screen.
  69.  */
  70. updatepos(x1, y1)
  71. int x1, y1 ;
  72. {
  73.    char outbuf[4] ;
  74.  
  75.    sprintf(outbuf, "%3d", ((x1 >> 3) & ~1) + y1 * 12) ;
  76.    drawtext(HLMGSTART+28, VLMG3+10, outbuf) ;
  77.    sprintf(outbuf, "%2d", x1 & 15) ;
  78.    drawtext(HLMGSTART+20, VLMG3+26, outbuf) ;
  79. }
  80. undobits() {
  81.    register short *p2 = osafearr, *p3 = realbits ;
  82.    int i = 192 ;
  83.  
  84.    while (i-- > 0)
  85.       *p3++ = *p2++ ;
  86.    updatebits() ;
  87. }
  88. updatebits() {
  89.    int x=HBITSTART, y=VBITSTART+1 ;
  90.    register short *p1 = realbits, *p2 = safearr, *p3 = osafearr ;
  91.    int i = 192 ;
  92.    int rc = 6 ;
  93.    int bit ;
  94.  
  95.    while (i-- > 0) {
  96.       *p3++ = *p2 ;
  97.       if (*p1 == *p2) {
  98.          p1++ ;
  99.          p2++ ;
  100.          x += 6 * 16 ;
  101.       } else {
  102.          if (!specbits) {
  103.             bit = 0x8000 ;
  104.             while (bit != 0) {
  105.                if ((*p2 & bit) != (*p1 & bit)) {
  106.                   color((*p1 & bit) ? WHITE : BLACK) ;
  107.                   fbox(x, y, 4, 2) ;
  108.                }
  109.                bit = (bit >> 1) & 0x7fff ;
  110.                x += 6 ;
  111.             }
  112.          } else
  113.             x += 6 * 16 ;
  114.          *p2++ = *p1++ ;
  115.       }
  116.       if (--rc == 0) {
  117.          rc = 6 ;
  118.          x = HBITSTART ;
  119.          y += 3 ;
  120.       }
  121.    }
  122.    specbits = 0 ;
  123. }
  124. /*
  125.  *   Here we update a single screen word, if it lies on the screen.
  126.  */
  127. screenupdate(where, what)
  128. short *where ;
  129. short what ;
  130. {
  131.    int i, bit, x, y ;
  132.  
  133.    specbits = 1 ;
  134.    if (where >= realbits && where < realbits + 192) {
  135.       i = where - realbits ;
  136.       y = VBITSTART + 1 + (i / 6) * 3 ;
  137.       x = HBITSTART + (i % 6) * 96 ;
  138.       for (bit=0x8000; bit != 0; bit = (bit >> 1) & 0x7fff, x += 6) {
  139.          if ((bit & what) != (bit & *where)) {
  140.             color((what & bit) ? WHITE : BLACK) ;
  141.             fbox(x, y, 4, 2) ;
  142.          }
  143.       }
  144.       *where = what ;
  145.    } else {
  146.       error("Dangerous assignment!") ;
  147.       Delay(25L) ;
  148.    }
  149. }
  150.