home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / graf / fract5.zip / DISKVID.C < prev    next >
C/C++ Source or Header  |  1989-09-22  |  10KB  |  326 lines

  1. /*
  2.     "Disk-Video" (and RAM-Video and Expanded-Memory Video) routines
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <dos.h>
  7. #include "fractint.h"
  8.  
  9. extern    unsigned char diskline[2049];
  10. extern    int xdots, ydots, colors;
  11. extern    int diskisactive, diskvideo;
  12. extern    int diskprintfs;        /* 0 = supress diskprintfs */
  13.  
  14. static unsigned int pixelsperbyte, pixelshift, pixeloffset;
  15. static unsigned int pixeloffsetmask;
  16. static unsigned int pixelbitshift[8], pixelmask[8], notpixelmask[8];
  17.  
  18. static int bytesperline, oldy, linehaschanged, diskisgood;
  19. static FILE *fp;
  20.  
  21. static unsigned char huge *memoryvideo;
  22. static unsigned char far  *expmemoryvideo;
  23. static long memorypointer;
  24. static int exppages, oldexppage;
  25. static unsigned int emmhandle;
  26.  
  27. startdisk()
  28. {
  29. unsigned int i;
  30. long longi;
  31.  
  32. if (!diskisactive) return(0);
  33.  
  34. pixelsperbyte = 8;            /* calculate pixel/bits stuff */
  35. pixelshift    = 3;
  36. for (i = 0; i < pixelsperbyte; i++) {
  37.     pixelbitshift[i] = i;
  38.     pixelmask[i] = 1 << pixelbitshift[i];
  39.     }
  40. if (colors >  2) {
  41.     pixelsperbyte = 4;
  42.     pixelshift    = 2;
  43.     for (i = 0; i < pixelsperbyte; i++) {
  44.         pixelbitshift[i] = 2*i;
  45.         pixelmask[i] = 3 << pixelbitshift[i];
  46.         }
  47.     }
  48. if (colors >  4) {
  49.     pixelsperbyte = 2;
  50.     pixelshift    = 1;
  51.     for (i = 0; i < pixelsperbyte; i++) {
  52.         pixelbitshift[i] = 4*i;
  53.         pixelmask[i] = 15 << pixelbitshift[i];
  54.         }
  55.     }
  56. if (colors > 16) {
  57.     pixelsperbyte = 1;
  58.     pixelshift    = 0;
  59.     pixelbitshift[0] = 0;
  60.     pixelmask[0]  = 0xff;
  61.     }
  62. for (i = 0; i < pixelsperbyte; i++)
  63.     notpixelmask[i] = 255 - pixelmask[i] ;
  64. pixeloffsetmask = pixelsperbyte-1;
  65.  
  66. if (diskprintfs) {
  67.    printf("Clearing the 'screen'...\n\n");
  68.    printf("\n\n'Disk-Video' (using the disk as your 'screen') has been activated.\n\n");
  69.    printf("   Your 'screen' resolution is %d x %d x %d colors\n",
  70.     xdots, ydots, colors);
  71.    }
  72.  
  73. diskisgood = 0;            /* first claim disk file is no good */
  74. emmhandle = 0;            /* and claim no expanded memory */
  75.  
  76. memorypointer = xdots;  memorypointer *= ydots;    /* try RAM first */
  77. memorypointer >>= pixelshift;        /* adjust for pixels / byte */
  78. if ((memoryvideo = (unsigned char huge *)farmemalloc(memorypointer)) != NULL) {
  79.     if (diskprintfs) 
  80.         printf("\n(Using your Memory)\n");
  81.     for (longi = 0; longi < memorypointer; longi++)    /* clear the RAM */
  82.         memoryvideo[longi] = 0;
  83.     diskvideo = 0;            /* using regular RAM */
  84.     diskisgood = 1;            /* but the video IS good */
  85.     oldy = -1;            /* there is no current line */
  86.     return(0);            /* and bail out */
  87.     }
  88.  
  89. memorypointer = xdots; memorypointer *= ydots;        /* try exp memory */
  90. memorypointer >>= pixelshift;        /* adjust for pixels / byte */
  91. exppages = (memorypointer + 16383) >> 14;
  92. if ((expmemoryvideo = emmquery()) != NULL && 
  93.     (emmhandle = emmallocate(exppages)) != 0) {
  94.     if (diskprintfs) 
  95.         printf("\n(Using your Expanded Memory)\n");
  96.     for (oldexppage = 0; oldexppage < exppages; oldexppage++)
  97.         emmclearpage(oldexppage, emmhandle);/* clear the "video" */
  98.     diskvideo = 1;            /* using Expanded RAM */
  99.     diskisgood = 1;            /* the video IS good */
  100.     oldy = -1;            /* there is no current line */
  101.     return(0);            /* and bail out */
  102.     }
  103.  
  104. if (diskprintfs) 
  105.     printf("\n(Using your Disk Drive)\n");
  106.  
  107. if ((fp = fopen("FRACTINT.DSK","w+b")) == NULL) { /* else try the disk */
  108.     buzzer(2);
  109.     if (diskprintfs) 
  110.         printf("\n((OOPS - Couldn't create FRACTINT.DSK))");
  111.     diskprintfs = 1;        /* re-enable status msgs */
  112.     return(-1);
  113.     }
  114.  
  115. bytesperline = (xdots >> pixelshift);        /* length of a "line" */
  116. if (xdots & pixelmask[0] != 0) bytesperline++;    /* check for odd bits */
  117. for (i = 0; i < bytesperline; i++)    /* clear the line to black */
  118.     diskline[i] = 0;
  119. for (i = 0; i < ydots; i++)    /* "clear the screen" (write to the disk) */
  120.     if (fwrite(diskline,1,bytesperline,fp) < bytesperline) {
  121.         buzzer(2);
  122.     if (diskprintfs) 
  123.             printf("\n((OOPS - ran out of disk space))\n");
  124.         fclose(fp);
  125.         remove("FRACTINT.DSK");
  126.         diskprintfs = 1;    /* re-enable status msgs */
  127.         return(-1);
  128.         }
  129.  
  130. diskvideo = 2;            /* using your disk drive */
  131. diskisgood = 1;            /* now note that the file is intact */
  132. oldy = -1;            /* there is no current line */
  133. linehaschanged = 0;        /* current line has not changed */
  134.  
  135. return(0);
  136.  
  137. }
  138.  
  139. enddisk()
  140. {
  141. unsigned int i;
  142.  
  143. diskprintfs = 1;            /* re-enable status msgs */
  144.  
  145. if (memoryvideo != NULL) {        /* RAM video? */
  146.     farmemfree((void far*)memoryvideo);/* then free it */
  147.     memoryvideo = NULL;        /* signal same */
  148.     return(0);            /* we done */
  149.     }
  150.  
  151. if (emmhandle != 0) {            /* Expanded memory video? */
  152.     emmdeallocate(emmhandle);    /* clear the memory */
  153.     emmhandle = 0;            /* signal same */
  154.     return(0);            /* we done */
  155.     }
  156.  
  157. if (linehaschanged) diskwriteline();    /* flush the line if need be */
  158. fclose(fp);
  159. remove("FRACTINT.DSK");
  160.  
  161. }
  162.  
  163. readdisk(x,y)
  164. int x, y;
  165. {
  166. long start;
  167. unsigned int page, offset;
  168.  
  169. if (x < 0 || x >= xdots || y < 0 || y >= ydots) return(0);
  170.  
  171. if (memoryvideo != NULL) {        /* RAM video? */
  172.     if (++oldy == xdots) {            /* need to get a new line? */
  173.         if (diskprintfs) {
  174.             home();            /* show a progress report */
  175.             printf("Reading Line %4d...     ",y);
  176.             }
  177.         oldy = -1;
  178.         }
  179.     start = y;  start = (start * xdots) + x;    /* then read it! */
  180.     pixeloffset = start & pixeloffsetmask;    /* get pixel offset */
  181.         start >>= pixelshift;        /* adjust for pixels / byte */
  182.     return(diskgetcolor(memoryvideo[start]));    /* and return */
  183.     }
  184.  
  185. if (emmhandle != 0) {                /* expanded memory video? */
  186.     if (oldy != y) {            /* need to get a new line? */
  187.         if (diskprintfs) {
  188.             home();            /* show a progress report */
  189.             printf("Reading Line %4d...     ",y);
  190.         }
  191.         oldy = y;            /* reset oldy */
  192.         }
  193.     start = y; start = (start * xdots) + x;    /* figure out where it is */
  194.     pixeloffset = start & pixeloffsetmask;    /* get pixel offset */
  195.         start >>= pixelshift;        /* adjust for pixels / byte */
  196.     page = start >> 14;  offset = start & 0x3fff;
  197.     if (page != oldexppage) {        /* time to get a new page? */
  198.         oldexppage = page;
  199.         emmgetpage(page,emmhandle);
  200.         }
  201.     return(diskgetcolor(expmemoryvideo[offset]));    /* and return */
  202.     }
  203.  
  204. if (! diskisgood) return(0);        /* bail out if the open failed */
  205.  
  206. if (oldy != y) {            /* need to get a new line? */
  207.     if (linehaschanged)         /* write the old line out, if need be */
  208.         diskwriteline();
  209.     start = bytesperline;  start *= oldy;    /* locate start-of-line */
  210.     fseek(fp,start,SEEK_SET);    /* seek to start of line */
  211.     fread(diskline,1,bytesperline,fp);    /* read the line */
  212.     oldy = y;            /* reset oldy */
  213.     if (diskprintfs) {
  214.         home();                /* show a progress report */
  215.         printf("Reading Line %4d...     ",y);
  216.         }
  217.     }
  218.  
  219. pixeloffset = x & pixeloffsetmask;    /* get pixel offset */
  220. start = x >> pixelshift;        /* adjust for pixels / byte */
  221.  
  222. return(diskgetcolor(diskline[start]));    /* return with the "pixel" "color" */
  223. }
  224.  
  225. writedisk(x,y,color)
  226. int x, y, color;
  227. {
  228. long start;
  229. unsigned int page, offset;
  230.  
  231. if (x < 0 || x >= xdots || y < 0 || y >= ydots) return(0);
  232.  
  233. if (memoryvideo != NULL) {        /* RAM video? */
  234.     if (++oldy == xdots) {            /* need to get a new line? */
  235.         if (diskprintfs) {
  236.             home();            /* show a progress report */
  237.             printf("Writing Line %4d...     ",y);
  238.             }
  239.         oldy = -1;
  240.         }
  241.     start = y;  start = (start * xdots) + x;    /* then write it! */
  242.     pixeloffset = start & pixeloffsetmask;    /* get pixel offset */
  243.         start >>= pixelshift;        /* adjust for pixels / byte */
  244.     memoryvideo[start] = diskputcolor(memoryvideo[start], color);
  245.     return(0);            /* and bail out */
  246.     }
  247.  
  248. if (emmhandle != 0) {                /* expanded memory video? */
  249.     if (oldy != y) {            /* need to get a new line? */
  250.         if (diskprintfs) {
  251.             home();            /* show a progress report */
  252.             printf("Writing Line %4d...     ",y);
  253.             }
  254.         oldy = y;            /* reset oldy */
  255.         }
  256.     start = y; start = (start * xdots) + x;    /* figure out where it is */
  257.     pixeloffset = start & pixeloffsetmask;    /* get pixel offset */
  258.         start >>= pixelshift;        /* adjust for pixels / byte */
  259.     page = start >> 14;  offset = start & 0x3fff;
  260.     if (page != oldexppage) {        /* time to get a new page? */
  261.         oldexppage = page;
  262.         emmgetpage(page, emmhandle);
  263.         }
  264.     expmemoryvideo[offset] = diskputcolor(expmemoryvideo[offset], color);
  265.     return(0);                /* and return */
  266.     }
  267.  
  268. if (! diskisgood) return(0);        /* bail out if the open failed */
  269.  
  270. if (oldy != y) {            /* need to get a new line? */
  271.     if (linehaschanged)         /* write the old line out, if need be */
  272.         diskwriteline();
  273.     start = xdots;  start *= y;    /* locate start-of-line */
  274.     fseek(fp,start,SEEK_SET);    /* seek to start of line */
  275.     fread(diskline,1,bytesperline,fp);    /* read the line */
  276.     oldy = y;            /* reset oldy */
  277.     if (diskprintfs) {
  278.         home();            /* show a progress report */
  279.         printf("Writing Line %4d...     ",y);
  280.         }
  281.     }
  282.  
  283. pixeloffset = x & pixeloffsetmask;    /* get pixel offset */
  284. start = x >> pixelshift;        /* adjust for pixels / byte */
  285.  
  286. diskline[start] = diskputcolor(diskline[start], color);    /* "display" the "pixel" */
  287.  
  288. linehaschanged = 1;            /* note that the line has changed */
  289. return(0);
  290.  
  291. }
  292.  
  293. diskwriteline()
  294. {
  295. long start;
  296.  
  297. if (! linehaschanged)             /* double-check: bail out if no need */
  298.     return(0);
  299.  
  300. start = bytesperline;  start *= oldy;    /* locate start-of-line */
  301. fseek(fp,start,SEEK_SET);        /* seek to start of line */
  302. fwrite(diskline,1,bytesperline,fp);        /* write the line */
  303.  
  304. linehaschanged = 0;            /* indicate line has not changed */
  305. return(0);
  306.  
  307. }
  308.  
  309. /* disk-based versions of 'getcolor/putcolor' - includes bit-shifting */
  310.  
  311. diskgetcolor(unsigned char byte)
  312. {
  313. if (colors > 16) return(byte);
  314.  
  315. return((byte & pixelmask[pixeloffset]) >> pixelbitshift[pixeloffset]);
  316. }
  317.  
  318. diskputcolor(unsigned char byte, unsigned int color)
  319. {
  320. if (colors > 16) return(color);
  321.  
  322. return((byte & notpixelmask[pixeloffset]) |
  323.     ((color & pixelmask[0]) << pixelbitshift[pixeloffset]));
  324. }
  325.  
  326.