home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / progrmng / stk110.lzh / STKSRC.COM / SPR_MISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-25  |  5.6 KB  |  160 lines

  1. /**********************************************************************
  2. * spr_misc.c
  3. * Miscellaneous (internal) utility routines for the sprite support
  4. **********************************************************************
  5.                     This file is part of
  6.  
  7.           STK -- The sprite toolkit -- version 1.1
  8.  
  9.               Copyright (C) Jari Karjala 1991
  10.  
  11. The sprite toolkit (STK) is a FreeWare toolkit for creating high
  12. resolution sprite graphics with PCompatible hardware. This toolkit 
  13. is provided as is without any warranty or such thing. See the file
  14. COPYING for further information.
  15.  
  16. **********************************************************************/
  17.  
  18.  
  19. #include "sprP.h"
  20.  
  21.  
  22. /**********************************************************************
  23. * Shift the given shape and mask bitmaps given number of bits to the
  24. * right and combine the result bytewise as required in sprite data
  25. * buffer.
  26. * shape   The original bitmap
  27. * mask    The original mask
  28. * step    The size of the step to the right in pixels (0-7)
  29. * shift   If zero then shape and mask are as wide as w (ie destination)
  30. * w       The width of the destination bitmap
  31. * h       The height of the bitmap
  32. * mask    Pad the result with ones if this is non-zero
  33. * bp      Pointer into the destination buffer
  34. **********************************************************************/
  35. static void shift_combine(BITMAP shape, BITMAP mask, 
  36.                           BYTE step, BYTE shift, BYTE w, BYTE h, FARMAP bp)
  37. {
  38.     WORD i,j;
  39.     BYTE mask1;
  40.     
  41.     mask1 = 0xFF << (8-step);
  42.  
  43.     if (shift)  /** shape/mask width is one less than given w **/
  44.         w--;
  45.     
  46.     for (j=0; j < h; j++) {         /* shift all scanlines */
  47.         *bp++ = *shape >> step;                 /* first shape byte */ 
  48.         *bp++ = (*mask >> step) | mask1;        /* first mask byte */
  49.         for (i=0; i < w-1; i++, shape++, mask++) {   /* next bytes */
  50.             *bp++ = (*shape << (8-step)) | (*(shape+1) >> step);
  51.             *bp++ = (*mask << (8-step)) | (*(mask+1) >> step);
  52.         }
  53.         if (shift) {    /** fix the shift overflow byte **/
  54.             *bp++ = (*shape << (8-step));
  55.             *bp++ = (*mask << (8-step)) | (0xFF >> step);
  56.         }
  57.         shape++;
  58.         mask++;
  59.     }
  60. }
  61.  
  62. /**********************************************************************
  63. * Create the shape data from the given shape and mask bitmaps
  64. * spr     The sprite used
  65. * shape   The shape bitmap
  66. * mask    The mask bitmap
  67. * res     The resolution used (1,2,4 or 8)
  68. **********************************************************************/
  69. void spr_misc_create_data(SPRITE spr, BITMAP shape, BITMAP mask)
  70. {
  71.     WORD size = spr->size, i;
  72.  
  73.     if (spr->res==1)
  74.         shift_combine(shape, mask, 0, 0, spr->w, spr->hp, spr->data);
  75.     else
  76.         for(i=0; i < spr->res; i++)
  77.             shift_combine(shape, mask, 
  78.                           (8/spr->res)*i,1, spr->w,spr->hp, spr->data+size*i);
  79. }
  80.  
  81. /**********************************************************************
  82. * Shift the given shape into the destination buffer word aligned.
  83. * shape   The original bitmap
  84. * step    The size of the step to the right in pixels (0-7)
  85. * shift   If zero then shape and mask are as wide as w (ie destination)
  86. * w       The width of the destination bitmap
  87. * h       The height of the bitmap
  88. * mask    Pad the result with ones if this is non-zero
  89. * bp      Pointer into the destination buffer
  90. **********************************************************************/
  91. static void xor_shift(BITMAP shape,
  92.                       BYTE step, BYTE shift, BYTE w, BYTE h, FARMAP bp)
  93. {
  94.     WORD i,j;
  95.  
  96.     if (shift)  /** shape width is one less than given w **/
  97.         w--;
  98.     
  99.     for (j=0; j < h; j++) {         /* shift all scanlines */
  100.         *bp++ = *shape >> step;                 /* first shape byte */ 
  101.         for (i=0; i < w-1; i++, shape++)        /* next bytes */
  102.             *bp++ = (*shape << (8-step)) | (*(shape+1) >> step);
  103.  
  104.         if (shift)     /** fix the shift overflow byte **/
  105.             *bp++ = (*shape << (8-step));
  106.  
  107.         shape++;
  108.     }
  109. }
  110.  
  111. /**********************************************************************
  112. * Create the XOR shape data from the given shape bitmas
  113. * spr     The sprite used
  114. * shape   The shape bitmap
  115. * mask    The mask bitmap
  116. * res     The resolution used (1,2,4 or 8)
  117. **********************************************************************/
  118. void spr_misc_create_xor_data(SPRITE spr, BITMAP shape)
  119. {
  120.     WORD size = spr->size, i;
  121.  
  122.     if (spr->res==1)
  123.         xor_shift(shape, 0, 0, spr->w, spr->hp, spr->data);
  124.     else
  125.         for(i=0; i < spr->res; i++)
  126.             xor_shift(shape,  
  127.                       (8/spr->res)*i,1, spr->w,spr->hp, spr->data+size*i);
  128. }
  129.  
  130. /**********************************************************************
  131. * Delete the given sprite from the display list for the given page.
  132. * Set the coordinates of the sprite to (WORD)-1,(WORD)-1
  133. * spr       The sprite to delete
  134. * spr_page  The page number (0/1)
  135. **********************************************************************/
  136. void spr_misc_delete(SPRITE spr, int spr_page)
  137. {
  138.     SPRITE s;
  139.     
  140.     spr->x = spr->y = (WORD)-1;
  141.     
  142.     s = spr_sprites[spr_page];
  143.     if (s==spr) /** it was the first one **/
  144.         spr_sprites[spr_page] = spr_sprites[spr_page]->next[spr_page];
  145.     else
  146.     while (s!=NULL) {
  147.         if (s->next[spr_page]!=NULL && s->next[spr_page]==spr) {
  148.             s->next[spr_page] = s->next[spr_page]->next[spr_page];
  149.             break;
  150.         }
  151.         s = s->next[spr_page];
  152.     }
  153. }
  154.