home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ISO_SRC.ZIP / LIBRARY.ZIP / BLOCK.C next >
Encoding:
C/C++ Source or Header  |  1996-06-08  |  1.8 KB  |  114 lines

  1. /*
  2.   WORKS REGARDLESS OF COMPILER_ SETTING
  3. */
  4. char *block_clip(char *screen);
  5. char *block_clip2(char *screen,signed short int x,signed short int y,char w,char h);
  6.  
  7. char *block_clip(char *screen)
  8. {
  9.   signed short int _top,_left,_bottom,_right,x,y;
  10.   unsigned short int i;
  11.  
  12.   _left   = 319;
  13.   _right  = 0;
  14.   _top    = 199;
  15.   _bottom = 0;
  16.  
  17.   x=0;
  18.   y=0;
  19.   for(i=0;i<64000;i++) {
  20.     if(screen[i]) {
  21.       if(y < _top)
  22.         _top = y;
  23.       if(y > _bottom)
  24.         _bottom = y;
  25.       if(x < _left)
  26.         _left = x;
  27.       if(x > _right)
  28.         _right = x;
  29.     }
  30.     if((x++)==320) {
  31.       x=0;
  32.       y++;
  33.     }
  34.   }
  35.  
  36.   return((char*)block_clip2(screen,x,y,_right-_left+1,_bottom-_top+1));
  37. }
  38.  
  39. char *block_clip2(char *screen,signed short int x,signed short int y,char w,char h)
  40. {
  41.   char *ptr,*p;
  42.   unsigned short int size,i;
  43.  
  44.   size = w*h+2;
  45.   if((ptr=malloc(size))==NULL)
  46.     return(NULL);
  47.  
  48.   ptr[0] = w;
  49.   ptr[1] = h;
  50.   p=&ptr[2];
  51.  
  52.   for(i=0;i<h;i++) {
  53.     memcpy(p,&screen[320*(y+i)+x],w);
  54.     p+=w;
  55.   }
  56.  
  57.   return(ptr);
  58. }
  59.  
  60. block_draw(char *buf,signed short int x,signed short int y,char *screen)
  61. {
  62.   signed short int i,j,w,h;
  63.   signed short int xoff,xlen;
  64.   signed short int yoff,ylen;
  65.   char color;
  66.  
  67.   w = *buf++;
  68.   h = *buf++;
  69.  
  70.   x-=w;
  71.   y-=h;
  72.  
  73.   // clip the block to fit within screen
  74.   xlen = w;
  75.   xoff = 0;
  76.  
  77.   if(x<0) {
  78.     xlen+=x;
  79.     xoff=-x;
  80.     x=0;
  81.   }
  82.  
  83.   if(x+w > 319)
  84.     xlen = 320-x;
  85.  
  86.   ylen = h;
  87.   yoff = 0;
  88.   if(y<0) {
  89.     ylen+=y;
  90.     yoff=-y;
  91.     y=0;
  92.   }
  93.  
  94.   if(y+h > 199)
  95.     ylen = 200-y;
  96.  
  97.   if((xlen<1) || (ylen<1))
  98.     return;
  99.  
  100.   // setup the pointers
  101.   screen+=(y<<8)+(y<<6)+x;
  102.   buf+=yoff*w+xoff;
  103.  
  104.   for(i=0;i<ylen;i++) {
  105.     for(j=0;j<xlen;j++) {
  106.       color = buf[j];
  107.       if(color)
  108.         screen[j] = color;
  109.     }
  110.     screen+=320;
  111.     buf+=w;
  112.   }
  113. }
  114.