home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / RAYCAST.ZIP / GOBJECTS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-23  |  6.4 KB  |  259 lines

  1. #include <stddef.h>
  2. #include <dos.h>
  3. #include "gobjects.h"
  4.  
  5. struct pcx_header {
  6.   CHAR manufacturer;    // Always set to 0
  7.   CHAR version;         // Always 5 for 256-color files
  8.   CHAR encoding;        // Always set to 1
  9.   CHAR bits_per_pixel;  // Should be 8 for 256-color files
  10.   SHORT  xmin,ymin;       // Coordinates for top left corner
  11.   SHORT  xmax,ymax;       // Width and height of image
  12.   SHORT  hres;            // Horizontal resolution of image
  13.   SHORT  vres;            // Vertical resolution of image
  14.   CHAR palette16[48];   // EGA palette; not used for
  15.             //  256-color files
  16.   CHAR reserved;        // Reserved for future use
  17.   CHAR color_planes;    // Color planes
  18.   SHORT  bytes_per_line;  // Number of bytes in 1 line of
  19.             //  pixels
  20.   SHORT  palette_type;    // Should be 2 for color palette
  21.   CHAR filler[58];      // Nothing but junk
  22. };
  23.  
  24.  
  25. gobject::gobject(UCHAR imagenum) : inpcxfile(), outpcxfile()
  26. {
  27.   holdimage=FALSE;
  28.   image=new PUCHAR [imagenum];
  29.   ni=imagenum;
  30.   for (SHORT i=0; i<imagenum; i++)
  31.     image[i]=NULL;
  32. }
  33.  
  34. LONG gobject::assigninfile(PCHAR filename,ULONG locate)
  35. {
  36.   if (inpcxfile.is_open())
  37.     inpcxfile.close();
  38.   inpcxfile.open(filename,ios::binary | ios::in);
  39.   if  (inpcxfile.fail())
  40.     return -1;
  41.   else {inpcxfile.seekg(locate,ios::beg);
  42.      return 0;}
  43. }
  44.  
  45. LONG gobject::assignoutfile(PCHAR filename,ULONG locate)
  46. {
  47.   if (outpcxfile.is_open())
  48.     outpcxfile.close();
  49.   outpcxfile.open(filename,ios::binary | ios::out | ios::truncate);
  50.   if  (outpcxfile.fail())
  51.     return -1;
  52.   else {outpcxfile.seekp(locate,ios::beg);
  53.      return 0;}
  54. }
  55.  
  56. LONG gobject::setup()
  57. {
  58. if (!inpcxfile.is_open())
  59.   return -1;
  60. LONG seek=inpcxfile.tellg();
  61. inpcxfile.seekg(0L,ios::beg);
  62. pcx_header header;
  63. inpcxfile.read((PCHAR)&header,128);
  64. image_size=((LONG)header.bytes_per_line)*((LONG)header.color_planes)
  65. *((LONG)(1+header.ymax-header.ymin));
  66. width=1+header.xmax-header.xmin;
  67. height=1+header.ymax-header.ymin;
  68. inpcxfile.seekg(seek,ios::beg);
  69. return 0;
  70. }
  71.  
  72. LONG gobject::writesetup()
  73. {        
  74. if (!outpcxfile.is_open())
  75.   return -1;
  76. pcx_header header;
  77. header.manufacturer=10;
  78. header.version=5;
  79. header.encoding=1;
  80. header.bits_per_pixel=8;
  81. header.xmin=0;header.ymin=0;
  82. header.xmax=width-1;header.ymax=height-1;
  83. header.hres=300; header.vres=300;
  84. header.palette16[0]=1;
  85. header.palette16[1]=0;
  86. header.palette16[2]=1;
  87. header.palette16[3]=0;
  88. header.palette16[4]=1;
  89. header.reserved=0;
  90. header.color_planes=1;
  91. header.bytes_per_line=width;
  92. header.palette_type=1;
  93. outpcxfile.write((PCHAR)&header,sizeof(header));
  94. return 0;
  95. }
  96.  
  97. LONG gobject::writepcx(UCHAR imagenum)
  98. {
  99.    if (!outpcxfile.is_open())
  100.       return (-1);
  101.    length=0;
  102.    static UCHAR buffer[5*1024];
  103.    PUCHAR image_ptr=image[imagenum];
  104.    char mode=1;
  105.    static UCHAR writeout;
  106.    static UCHAR bytecount;
  107.    LONG bufptr=0;
  108.    LONG buflen=5*1024;
  109.    LONG i=0;
  110.    while (i<image_size) {
  111.        if (mode>0) {
  112.       if (bufptr>=buflen) {
  113.       length+=5*1024;
  114.          bufptr=0;
  115.          outpcxfile.write(buffer,5*1024);}
  116.       writeout=*(image_ptr++);
  117.       if (((image_size-i>1)&&(writeout==*(image_ptr)))||(writeout > 0xbf)) {
  118.          bytecount=1;
  119.          mode=0;
  120.       } else { buffer[bufptr++]=writeout; i++;}
  121.        }
  122.        else {
  123.        if ((image_size-i>1)&&
  124.         (bytecount<0x39)&&(*(image_ptr)==writeout)){
  125.         bytecount++;
  126.         image_ptr++;}
  127.       else {
  128.         buffer[bufptr++]=bytecount+0xc0;
  129.         if (bufptr>=buflen) {
  130.            length+=5*1024;
  131.            bufptr=0;
  132.            outpcxfile.write(buffer,buflen);}
  133.         buffer[bufptr++]=writeout;
  134.         mode=1;
  135.       }
  136.              i++;
  137.        }
  138.    }
  139. outpcxfile.write(buffer,bufptr);
  140. length+=bufptr;
  141. return 0;
  142. }
  143.      
  144. LONG gobject::loadpcx(UCHAR imagenum)
  145. {
  146.   length=0;
  147.   if (!inpcxfile.is_open())
  148.     return (-1);
  149.   if ((image[imagenum]=new UCHAR [image_size])==NULL){
  150.     delete image[imagenum];
  151.     return 1;}
  152.   LONG seek=inpcxfile.tellg();
  153.   inpcxfile.seekg(128L,ios::cur);
  154.   static UCHAR buffer[5*1024];
  155.   PUCHAR image_ptr=image[imagenum];
  156.   CHAR mode=1;
  157.   LONG bufptr=0;
  158.   LONG buflen=0;
  159.   static UCHAR readin;
  160.   static UCHAR bytecount;
  161.   for (LONG i=0;i<image_size;i++){
  162.     if (mode > 0)
  163.        {
  164.      if (bufptr>=buflen)
  165.         {bufptr=0;
  166.         inpcxfile.read(buffer,5*1024);
  167.                buflen=inpcxfile.gcount();
  168.                length+=buflen;
  169.         if (buflen==0)
  170.           break;} //if (bufptr>=buflen)
  171.      readin=buffer[bufptr++];
  172.      if (readin>0xbf)
  173.        {
  174.        bytecount= (LONG)((LONG)readin & 0x3f);
  175.        if (bufptr>=buflen)
  176.         {bufptr=0;
  177.                inpcxfile.read(buffer,5*1024);
  178.                buflen=inpcxfile.gcount();
  179.                length+=buflen;
  180.         if (buflen==0)
  181.           break;} //if (bufptr>=buflen)
  182.        readin=buffer[bufptr++];
  183.        if (--bytecount > 0) mode=0;
  184.        } //if (readin>0xbf)
  185.        }  //if (mode)
  186.     else if (--bytecount == 0) mode=1;
  187.     *image_ptr++=readin;
  188.   } //for loop
  189.   inpcxfile.clear();
  190.   inpcxfile.seekg(seek,ios::beg);
  191.   return 0;
  192. }
  193.  
  194. LONG gobject::load(UCHAR imagenum)
  195. {
  196.   if (imagenum>=ni)
  197.     return -1;
  198.   if (image[imagenum]!=NULL)
  199.     delete image[imagenum];
  200.   setup();
  201.   if (loadpcx(imagenum))
  202.     return -1;
  203.   return 0;
  204. }
  205.  
  206. LONG gobject::Write(UCHAR imagenum)
  207. {
  208.   if (imagenum>=ni)
  209.     return -1;
  210.   if (image[imagenum]==NULL)
  211.     return -1;
  212.   writesetup();
  213.   if (writepcx(imagenum))
  214.     return -1;
  215.   return 0;
  216. }
  217.  
  218. void gobject::show(PUCHAR screen, SHORT screen_width, USHORT x, USHORT y, UCHAR imagenum)
  219. {
  220. PUCHAR imageptr=image[imagenum];
  221. PUCHAR scrnptr=(screen+(long)screen_width*(long)y+(long)x);
  222. for (LONG i=0;i<height;i++) {
  223.   for (LONG j=0;j<width;j++)
  224.     *(scrnptr++)=*(imageptr++);
  225.   scrnptr+=(screen_width-width);
  226. }
  227. }
  228.  
  229. void gobject::transparent_show(PUCHAR screen, SHORT screen_width, USHORT x, USHORT y, UCHAR imagenum)
  230. {
  231.    PUCHAR imageptr=image[imagenum];
  232.    PUCHAR scrnptr=(screen+(long)screen_width*(long)y+(long)x);
  233.    UCHAR pixel;
  234.    for (LONG i=0; i<height; i++) {
  235.       for (LONG j=0; j<width; j++) {
  236.         pixel=*(imageptr++);
  237.         if (pixel) {
  238.            *(scrnptr)=pixel;
  239.         } /* endif */
  240.         scrnptr++;
  241.       } /* endfor */
  242.       scrnptr+=(screen_width-width);
  243.    } /* endfor */
  244. }
  245.  
  246. gobject::~gobject()
  247. {
  248.   if (!holdimage) {
  249.      for (short i=0; i<ni; i++)
  250.         delete image[i];
  251.      delete image;
  252.   }
  253.   if (inpcxfile.is_open()) 
  254.     inpcxfile.close();
  255.   if (outpcxfile.is_open())
  256.     outpcxfile.close();
  257. }
  258.  
  259.