/*
 *      DMWAD\GFX.C        by RobNkill !!
 *      ===========
 *
 *      Graphics bit for dmwad.
 *
 *      This is actually getting pretty cool so I'd better say that
 *      this file is Copyright (C) 1997 Tom Robinson.
 *      If you want to use it, fine, but credit the following people:
 *              Tom Robinson - wad code.
 *              PS Jones - very low level graphics code.
 *              Ken Silverman - more graphics code, the PROPER interrupts
 *                              for setting the palette.
 *              Matt Fell - doom specs.
 */

 /* include */
 #include "gfx.h"

/********************************************************* GLOBALS *********/

char *scrptr;                           //pointer to screen data for the
                                          //FAST screen write/read
pal palette[256];                       // ... and for to set it!

char screenbuffer[64000];               //this is for my buffer

union REGS r;                           //how am I supposed to twiddle
                                        //any frobs without this? :)
int graphicsmode=0;

/********************************************************* MAIN PROGRAM ****/

void initraw()
{
      __djgpp_nearptr_enable();
        scrptr = (char*) (__djgpp_conventional_base + 0xa0000);
}

void setpalette(pal *palptr)
{
        short i;
        outp(0x3c8,0);
        for (i=0; i<256; i++)
        {
                outp(0x3c9, palptr[i].red>>2);   // >>2 sets a 0-255 pal to
                outp(0x3c9, palptr[i].green>>2); // a 0-63 pal.
                outp(0x3c9, palptr[i].blue>>2);
        }
}

void loadpal(char *filen)
{
        FILE *fstream;
        char str[50];
        int count;

        fstream=fopen(filen,"r");

        fgets(str,50,fstream);   // JASC-PAL
        fgets(str,50,fstream);   // 255
        fgets(str,50,fstream);   // 0

        for(count=0;count<256;count++)
        {
                fgets(str,50,fstream);   // JASC-PAL
                sscanf(str,"%i %i %i\n",&palette[count].red, &palette[count].green,
                               &palette[count].blue);
        }
        setpalette(palette);
}

/*
        SWITCH TO VGA
*/

void set320x200(void)
{
   r.h.ah=0;
   r.h.al=0x13;
   int86(0x10, &r, &r);
   graphicsmode=1;
}

/*
        SWITCH BACK TO NORMAL
*/

void settextmode(void)
{
   r.h.ah=0;
   r.h.al=0x03;
   int86(0x10, &r, &r);
   graphicsmode=0;
}

/*
        THIS LOT SETS PIXELS ON THE SCREEN, IN THE BUFFER AND COPIES THE
        BUFFER TO THE SCREEN (THANKS PS JONES!)
*/

inline void setpixel(long x, long y, char col)
{
        *(scrptr + (y<<8)+(y<<6) + x) = col;
}

inline void setbpixel(long x, long y, char col)
{
        *((char*) screenbuffer + (y<<8)+(y<<6) + x) = col;
}

inline void buffertoscreen(void)
{
        memcpy(scrptr, screenbuffer, 64000);    
}

void blitchar(char c, int x, int y)
{
        char *charset=(char *)(__djgpp_conventional_base+0xf0000);
        unsigned char z, x1, y1, z2;
        unsigned char bitmask;
        unsigned long n;

        n=(8*c)+0xfa6e;

        for(y1=0;y1<8;y1++)
        {
                z=*(charset+n);
                bitmask=0x80;
                for(x1=0;x1<8;x1++)
                {
                        z2=(z & bitmask);
                        if(z2) setbpixel(x+x1,y+y1,96);
                        bitmask=bitmask/2;
                }
                n++;
        }
}

void blitstr(char *s,int x, int y)
{
        int count;

        if(x==-1)
        {
               x=(320-(strlen(s)*8))/2;
        }
        if(y==-1)
        {
               y=96;
        }

        for(count=0;count<strlen(s);count++)
        {
               blitchar(s[count],x,y);
               x+=8;
        }
}

void melt()
{
       int worms[320];
       int count;
       char tempstr[50];
       int dones=0,x, y;
       char oldscreen[320][200];
       char newscreen[320][200];

       for(count=0;count<320;count++) worms[count]=0;

       for(x=0;x<320;x++)
               for(y=0;y<200;y++)
               {
                     oldscreen[x][y]=*(scrptr+(y*320)+x);
                     newscreen[x][y]=*(screenbuffer+(y*320)+x);
               }

       while(dones<320)
       {
               dones=0;
               for(count=0;count<320;count++)
               {
                     if(worms[count]>=200)
                     {
                           dones++; continue;
                     }
                     worms[count]+=rand()/(RAND_MAX/32);
                     if(worms[count]>=200) worms[count]=200;
                     for(y=0;y<200;y++)
                     {
                            if(y<worms[count])
                                setbpixel(count,y,newscreen[count][y]);
                            else
                                setbpixel(count,y,oldscreen[count][y-worms[count]]);
                     }
               }
               buffertoscreen();
       }
}

/* END OF FILE */


