home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / GRAPHICS / MISC / PCXPIC10.ZIP / CGA.C next >
Encoding:
C/C++ Source or Header  |  1991-05-07  |  2.9 KB  |  213 lines

  1. /*
  2.  * CGA.C -- A collection of CGA screen manipulation routines, as used
  3.  *          by A2B.
  4.  */
  5.  
  6. /* INCLUDES */
  7.  
  8. #include <stdio.h>
  9. #include <alloc.h>
  10. #include <mem.h>
  11. #include <dos.h>
  12.  
  13. #include "cga.h"
  14.  
  15.  
  16.  
  17.  
  18. /* GLOBALS */
  19.  
  20. static long int   sadr[200];    /* screen addresses of each line */
  21.  
  22.  
  23.  
  24.  
  25. /* FUNCTIONS */
  26.  
  27. void mode(WORD vmode)
  28. {
  29.  _AH = 0;
  30.  _AL = (UBYTE)vmode;
  31.  
  32.  geninterrupt(0x10);
  33. }
  34.  
  35.  
  36.  
  37. void cls(void)
  38. {
  39.  register int line, byte;
  40.  
  41.  
  42.  for(line=0;line<200;line++)
  43.   for(byte=0;byte<80;byte++)
  44.    *((UBYTE far *)(sadr[line]+byte)) = 0x00;
  45. }
  46.  
  47.  
  48.  
  49. void init_cga(UBYTE vmode)
  50. {
  51.  int    i;
  52.  long int tmp;
  53.  
  54.  
  55.  for (i = 0; i < 200; i++)
  56.   {
  57.   if (i % 2 == 0)
  58.    tmp = ((i / 2) * 0x50);
  59.   else
  60.    tmp = (((i / 2) * 0x50) + 0x2000);
  61.  
  62.   sadr[i] = (long int)(0xB8000000L + (long int)tmp);
  63.   }
  64.  
  65.  mode(vmode);
  66. }
  67.  
  68.  
  69.  
  70. void put_pixel(int x, int y, int color)
  71. {
  72.  UBYTE far *mem_addr, clr[] = { 0x00, 0x55, 0xAA, 0xFF };
  73.  int   mask;
  74.  
  75.  
  76.  mem_addr = (UBYTE far *)(sadr[y] + (ULONG)x/4L);
  77.  
  78.  mask = 0xC0 >> ((x % 4) * 2);
  79.  
  80.  *mem_addr = (clr[color] & mask) | (*mem_addr & ~mask);
  81. }
  82.  
  83.  
  84.  
  85. void save_pic(const char *name)
  86. {
  87.  FILE *fh;
  88.  UBYTE far *data;
  89.  UWORD dseg, doff;
  90.  
  91.  
  92.  data = (UBYTE far *)calloc(16384, 1);
  93.  
  94.  dseg = FP_SEG(data);
  95.  doff = FP_OFF(data);
  96.  
  97.  movedata(0xB800, 0x0000, dseg, doff, 16384);
  98.  
  99.  if((fh=fopen(name,"wb")) != NULL)
  100.   {
  101.   fprintf(fh,"%c%c%c%c%c%c%c",0xFD,0x00,0xB8,0x00,0x00,0x00,0x40);
  102.  
  103.   fwrite((void *)data, 1, 16384, fh);
  104.  
  105.   fclose(fh);
  106.  
  107.   free((void *)data);
  108.   }
  109.  else
  110.   printf("ERROR:  Unable to open %s\n",name);
  111. }
  112.  
  113.  
  114.  
  115. void fload_pic(const char *name)
  116. {
  117.  register int i;
  118.  UBYTE    *s, *tmp, far *pic = (UBYTE far *)MK_FP(0xB800,0x0000);
  119.  FILE     *fh;
  120.  
  121.  
  122.  if((fh=fopen(name, "rb")) != NULL)
  123.   {
  124.   tmp = s = (UBYTE *)calloc(16392, 1);
  125.  
  126.   fseek(fh, 8, SEEK_SET);
  127.  
  128.   fread(s,1,16377,fh);
  129.   fclose(fh);
  130.  
  131.   for(i=0;i<16378;i++)
  132.    *pic++ = *tmp++;
  133.  
  134.   free((void *)s);
  135.   }
  136.  else
  137.   printf("ERROR:  Unable to open %s\n", name);
  138. }
  139.  
  140.  
  141.  
  142. int fload_pcx(const char *file_name)
  143. {
  144.  FILE  *fsave;
  145.  UBYTE  ch, ch1;
  146.  int    i, j, k, m, line_end, pass, x1, y1, x2, y2;
  147.  
  148.  
  149.  if((fsave = fopen(file_name, "rb")) == NULL)
  150.   return -1;
  151.  
  152.  fseek(fsave, 4L, SEEK_SET);
  153.  
  154.  x1 = getw(fsave);
  155.  y1 = getw(fsave);
  156.  x2 = getw(fsave);
  157.  y2 = getw(fsave);
  158.  
  159.  fseek(fsave, 128L, SEEK_SET);
  160.  
  161.  for(k=y1; k<y2; k++)
  162.   {
  163.   i = (x1>>2);
  164.  
  165.   line_end = (x2>>2) + 1;
  166.  
  167.   j = 0;
  168.  
  169.   while(j < 1)
  170.    {
  171.    ch1 = fgetc(fsave);
  172.  
  173.    if((ch1 & 0xC0) != 0xC0)
  174.     {
  175.     *((UBYTE far *)(sadr[k] + i)) = ch1;
  176.  
  177.     i++;
  178.  
  179.     if(i >= line_end)
  180.      {
  181.      j++;
  182.  
  183.      i = (x1>>2);
  184.      }
  185.     }
  186.    else
  187.     {
  188.     ch1 &= 0x3F;
  189.     pass = ch1;
  190.     ch   = fgetc(fsave);
  191.  
  192.     for(m=0; m<pass; m++)
  193.      {
  194.      *((UBYTE far *)(sadr[k] + i)) = ch;
  195.  
  196.      i++;
  197.  
  198.      if(i >= line_end)
  199.       {
  200.       j++;
  201.  
  202.       i = (x1>>2);
  203.       }
  204.      }
  205.     }
  206.    }
  207.   }
  208.  
  209.  fclose(fsave);
  210.  
  211.  return 1;
  212. }
  213.