home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / msdos / fractal / fdesi313 / fdes13s / fdeshres.c < prev    next >
Text File  |  1990-01-17  |  6KB  |  195 lines

  1. /*
  2.         Hardware Routines for MCGA 256 color mode plotting
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <dos.h>
  7. #include <conio.h>
  8. #include <math.h>
  9. #include "fdesplot.h"
  10. #include "fdesmenu.h"
  11.  
  12. #define MAXGUN 63
  13. #define PI 3.14159265358979
  14. /*
  15.         Palette
  16. */
  17. int pixel_max;                          /* largest pixel put value since last
  18.                                            last 'set_palette' */
  19. int color_shift;                        /* color shift used in 256 color mode */
  20.  
  21. int vcard_type;                         /* video card type
  22.                                                 0 = undefined
  23.                                                 1 = ast/paradise pro
  24.                                                 2 = orchid/genoa
  25.                                         */
  26. /*
  27. popmenu vtype = {
  28.         3,
  29.         "AST/PARADISE", "GENOA/ORCHID", "Cancel"
  30.         };
  31.  
  32. int vcard_type_check(void)      /* returns 0 if cancelled, mode# if successful */
  33. {
  34. int select;
  35.         if (vcard_type == 0)
  36.         {
  37.                 select = popup(100,100,&vtype,WHITE,BLUE);
  38.         switch (select)
  39.                 {
  40.                         case 1:                 /* ast/paradise */
  41.                                 vcard_type = 1;
  42.                                 return(0x5f);
  43.                         case 2:                 /* genoa/orchid */
  44.                                 vcard_type = 2;
  45.                                 return(0x2e);
  46.                         case 3:
  47.                                 return(0);
  48.                 }
  49.     }
  50.     else
  51.     {
  52.         switch (vcard_type)
  53.                 {
  54.                         case 1:                 /* ast/paradise */
  55.                                 return(0x5f);
  56.                         case 2:                 /* genoa/orchid */
  57.                                 return(0x2e);
  58.                 }
  59.     }
  60.     return(0);
  61. }
  62.  
  63.  
  64. typedef struct {
  65.         unsigned char r,g,b;
  66.         } rgb;
  67.  
  68. void set_palette(int quantize)
  69. {
  70. FILE *tempfile;
  71. int i,j,level;
  72. struct REGPACK regs;
  73. rgb *palette;
  74. float theta,tx,ty,compg,compr,fphi,sinphi,phi;
  75.  
  76.         if (quantize == 256)
  77.         {
  78.         gotoxy(1,1); printf("maximum quantization");
  79.         }
  80.         pixel_max = 0;
  81.         palette = calloc(256,sizeof(rgb));
  82.         regs.r_ax = 0x1017;             /* read block of DAC registers */
  83.         regs.r_bx = 0;                  /* starting register */
  84.         regs.r_cx = 256;                /* how many to read */
  85.         regs.r_es = FP_SEG((void far *)palette);
  86.         regs.r_dx = FP_OFF((void far *)palette);
  87.         intr(0x10,®s);
  88.  
  89.         if (quantize < 0)
  90.         {
  91.                 quantize = -quantize;
  92.         /* color wheel */
  93.         /*
  94.            mapping from base { (1,0), (0,1) }
  95.            to base { (1,0), (cos(phi),sin(phi)) }
  96.                { red_vector, green_vector }
  97.         */
  98.         phi = 2.0*PI/3.0;
  99.         fphi = cos(phi)/sin(phi);
  100.         sinphi = sin(phi);
  101.  
  102.                 for (i=0; i<(quantize/3); i++)
  103.                 {
  104.             theta = (i/(quantize/3.0 - 1))*2*PI/3.0;
  105.             tx = 50.0*cos(theta);
  106.             ty = 50.0*sin(theta);
  107.  
  108.             compr = tx - fphi*ty;
  109.             compg = ty/sinphi;
  110.                         j = (i + color_shift) % quantize;
  111.                         palette[j+1].r = compr;
  112.                         palette[j+1].g = compg;
  113.                         palette[j+1].b = 0;
  114.                         j = (i + quantize/3 + color_shift) % quantize;
  115.                         palette[j+1].r = 0;
  116.                         palette[j+1].g = compr;
  117.                         palette[j+1].b = compg;
  118.             j = (i + (2*quantize)/3 + color_shift) % quantize;
  119.                         palette[j+1].r = compg;
  120.                         palette[j+1].g = 0;
  121.                         palette[j+1].b = compr;
  122.                 }
  123.         }
  124.         else
  125.         {
  126.                 /* grey levels */
  127.                 for (i = 0; i<256; i++)   /* adjust DAC registers */
  128.                 {
  129.                         if ((i%(quantize*2)) >= quantize)
  130.                                 level = quantize - 1 - (i%(quantize));
  131.                         else level = (i%quantize);
  132.                         palette[i].r = level*(64/quantize);
  133.                         palette[i].g = level*(64/quantize);
  134.                         palette[i].b = level*(64/quantize);
  135.  
  136.                 }
  137.     }
  138.  
  139. /*    tempfile = fopen("DAC","wt");
  140.     for (i=0; i<256; i++)
  141.     {
  142.         fprintf(tempfile,"\nDAC %3d %3d %3d %3d",i,palette[i].r,
  143.             palette[i].g,palette[i].b);
  144.     }
  145.     fclose(tempfile);
  146. */
  147.         /* perform a 'set block of DAC registers' */
  148.         regs.r_ax = 0x1012;             /* set block of DAC registers */
  149.         regs.r_bx = 0;                  /* starting register */
  150.         regs.r_cx = 256;                /* how many to read */
  151.     regs.r_es = FP_SEG((void far *)palette);
  152.     regs.r_dx = FP_OFF((void far *)palette);
  153.         intr(0x10,®s);
  154.         free(palette);
  155. }
  156. /*
  157.         Graphics Card initialize to 256 color mode (320x200)
  158. */
  159. void init256(unsigned char mode)
  160. {
  161. struct REGPACK regs;
  162.  
  163.         /* set video mode */
  164.         regs.r_ax = 0x0000 | mode;             /* function 0, mode 13h or 5fh */
  165.         intr(0x10,®s);
  166.  
  167. /*    set_palette(64); */
  168. }
  169.  
  170. void putpixel256(int col, int row, unsigned char color)
  171. {
  172. struct REGPACK regs;
  173.         if ((col > maxx) || (col < 0) || (row > maxy) || (row < 0)) return;
  174.         regs.r_ax = 0x0c00 | color;
  175.         regs.r_bx = 0;
  176.         regs.r_cx = col;
  177.         regs.r_dx = row;
  178.         intr(0x10,®s);
  179.  
  180.         pixel_max = max(pixel_max,color);
  181. }
  182.  
  183. unsigned char getpixel256(int col, int row)
  184. {
  185. struct REGPACK regs;
  186.         if ((col > maxx) || (col < 0) || (row > maxy) || (row < 0)) return(0);
  187.         regs.r_ax = 0x0d00;
  188.         regs.r_cx = col;
  189.         regs.r_dx = row;
  190.         intr(0x10,®s);
  191.         return(regs.r_ax & 0xff);
  192. }
  193. */
  194.  
  195.