home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / msdos / convrtrs / hgrpic / cga.c < prev    next >
C/C++ Source or Header  |  1991-05-02  |  2KB  |  139 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_cgfx(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.