home *** CD-ROM | disk | FTP | other *** search
/ World of Graphics / WOGRAPH.BIN / 333.VSA_DEMO.C < prev    next >
C/C++ Source or Header  |  1992-12-23  |  12KB  |  379 lines

  1. /*..........................................................................*/
  2. /*                              VSA_DEMO.C              12-23-92            */
  3. /*                                                                          */
  4. /*  This is the "C" source code for the VSA_DEMO.EXE program.  This program */
  5. /*  demonstrates the usage of the VSA256 Graphics Library, Version 1.1b     */
  6. /*  functions.                                                              */
  7. /*                                                                          */
  8. /*            Copyright Spyro Gumas, 1992.  All Rights Reserved.            */
  9. /*..........................................................................*/
  10.  
  11. #include<stdlib.h>
  12. #include<math.h>
  13. #include<limits.h>
  14. #include<bios.h>
  15.  
  16. #include "vsa.h"             /* Required to support VSA256 Graphics Library */
  17.  
  18. #ifndef _MSC_VER
  19. /*.....                This line is for Borland C Only !               .....*/
  20. extern unsigned _stklen = 10000;
  21. #endif
  22.  
  23. void cube(int,int,int);
  24. void rainbow_lut();
  25. void special_lut();
  26. void color_bar(int,int);
  27. void banner(int,int);
  28. void image(int,int);
  29.  
  30. float SIN_LUT[1024];
  31.  
  32. void main()
  33. {
  34.     int i,x,y,size;
  35.     unsigned vmode,xx,yy,a,b,c,d;
  36.     unsigned char color,j,jj,row,col;
  37.     char text[100],your_name[80];
  38.     unsigned char color_array[768],save_color_array[768];
  39.     unsigned char red[256],green[256],blue[256];
  40.     srand(1);
  41. /*..........................................................................*/
  42. /*                      Initialize sin look up table.                       */
  43. /*  Index 'i' goes from 0 to 1023 and is equivalent to 0 to 360 degrees.    */
  44. /*..........................................................................*/
  45.     for(i=0;i<1024;i++)
  46.         SIN_LUT[i] = sin(i*6.28/1024.0);
  47. /*..........................................................................*/
  48. /*               Initialize video mode and VSA256 environment.              */
  49. /*               Valid modes are: 100h, 101h, 103h, and 105h.               */
  50. /*..........................................................................*/
  51.     printf("\n");
  52.     printf("Input VESA standard Video Mode (hex)\n");
  53.     printf("(100, 101, 103, or 105): ");
  54.     scanf("%x",&vmode);
  55.     if((i = vsa_init(vmode)) != 0)
  56.         {
  57.             printf("Error Initializing Requested Video Mode!\n");
  58.             if(i==1) printf("  - Did You Load Correct VESA Driver (TSR) ??\n");
  59.             if(i==2) printf("  - VESA BIOS Extensions (Driver) Not Loaded !!\n");
  60.             if(i==3) printf("  - Requested Video Mode Not Supported by this Card!\n");
  61.             if(i==4) printf("  - Mode Not an SVGA Mode Supported by this Card!\n");
  62.             if(i==5) printf("  - VESA Driver Not Returning Mode Information!\n");
  63.             if(i==6) printf("  - Text I/O Not Supported by your VESA BIOS TSR!\n");
  64.             return;
  65.         }
  66.     xx = XResolution;
  67.     yy = YResolution;
  68.     vsa_set_color(1);
  69.     vsa_move_to(0,0);
  70.     vsa_rect(xx-1,yy-1);
  71.  
  72. /*..........................................................................*/
  73. /*             Draw color look up table at bottom of screen.                */
  74. /*..........................................................................*/
  75.     rainbow_lut();
  76.     a = .125*xx;
  77.     b = .81*yy;
  78.     color_bar(a,b);
  79. /*..........................................................................*/
  80. /*            Draw "random" cubes enclosed by rectangle                     */
  81. /*..........................................................................*/
  82.     a = .6*xx;
  83.     b = .4*yy;
  84.     c = .88*xx;
  85.     d = .72*yy;
  86.     vsa_move_to(a,b);
  87.     vsa_set_color(180);
  88.     vsa_rect(c,d);
  89.     vsa_move_to(a+1,b+1);
  90.     vsa_set_color(20);
  91.     vsa_rect_fill(c-1,d-1);
  92.     for(i=0;i<16;i++)
  93.         {
  94.             x = a+5+.23*xx*(float)rand()/RAND_MAX;
  95.             y = b+5+.23*yy*(float)rand()/RAND_MAX;
  96.             size = .05*xx*(float)rand()/RAND_MAX;
  97.             color = (unsigned char) i;
  98.             vsa_set_color(color);
  99.             cube(x,y,size);
  100.         }
  101.     row = (.74*yy)/YCharSize;
  102.     col = (.64*xx)/XCharSize;
  103.     row = (d)/YCharSize + 1;
  104.     col = (a+(c-a)/2)/XCharSize - 12;
  105.     sprintf(text,"Lines using `vsa_line_to'");
  106.     vsa_write_string(row,col,250,text);
  107. /*..........................................................................*/
  108. /*                          Draw a banner                                   */
  109. /*..........................................................................*/
  110.     a = .55*xx;
  111.     b = .06*yy;
  112.     banner(a,b);
  113. /*..........................................................................*/
  114. /*                    Draw 2D sine-cosine image                             */
  115. /*..........................................................................*/
  116.     a = .08*xx;
  117.     b = .26*yy;
  118.     image(a,b);
  119. /*..........................................................................*/
  120. /*                        Using Text Cursor Mode 1.                         */
  121. /*..........................................................................*/
  122.     row = .1*yy/YCharSize;
  123.     col = .05*xx/XCharSize;
  124.     vsa_set_text_cursor_mode(1);
  125.     vsa_write_string(row,col,250,"Please Enter Your Name: ");
  126.     scanf("%s",&your_name);
  127.  
  128.     vsa_set_text_color(200);
  129.     vsa_set_text_cursor(row+1,col);
  130.     vsa_write_string_alt("Hello ");
  131.     vsa_write_string_alt(your_name);
  132.     vsa_write_string_alt(", Hit any key to bail.");
  133. /*..........................................................................*/
  134. /*    Now do sliding blue color effect, until someone presses a key.        */
  135. /*..........................................................................*/
  136.     vsa_read_color_block(251,5,save_color_array);
  137.     for(j=0;j<5;j++)
  138.         {
  139.             red[j+251]   = save_color_array[3*j];
  140.             green[j+251] = save_color_array[3*j+1];
  141.             blue[j+251]  = save_color_array[3*j+2];
  142.         }
  143. INFINITE_LOOP:
  144.     for(i=32;i<256;i++)
  145.         {
  146.             j = (unsigned char) i;
  147.             vsa_read_color_register(j,&red[j],&green[j],&blue[j]);
  148.             vsa_write_color_register(j,0,0,63);
  149.             if(j <= 36)
  150.                 jj = (unsigned char)(j-37);
  151.             else
  152.                 jj = (unsigned char)(j-5);
  153.             vsa_write_color_register(jj,red[jj],green[jj],blue[jj]);
  154. #ifdef _MSC_VER
  155. /*.....             For Microsoft C, Use this line.                    .....*/
  156.             if(_bios_keybrd(_KEYBRD_READY))
  157. #else
  158. /*.....             For Borland C, Use this line instead.              .....*/
  159.             if(bioskey(1))
  160. #endif
  161.             goto BAIL;
  162.         }
  163.     goto INFINITE_LOOP;
  164. /*..........................................................................*/
  165. /*           Restore text video mode and print information.                 */
  166. /*..........................................................................*/
  167. BAIL:
  168.     vsa_set_svga_mode(0x3);
  169.     vsa_about();
  170.     return;
  171. }
  172.  
  173. void cube(int x,int y,int size)
  174. {
  175.     int sizeb;
  176.     sizeb = size/2;
  177.     vsa_move_to(x,y);
  178.     vsa_line_to(x+size,y);
  179.     vsa_line_to(x+size,y+size);
  180.     vsa_line_to(x,y+size);
  181.     vsa_line_to(x,y);
  182.     vsa_line_to(x+sizeb,y+sizeb);
  183.     vsa_line_to(x+sizeb+size,y+sizeb);
  184.     vsa_line_to(x+sizeb+size,y+sizeb+size);
  185.     vsa_line_to(x+sizeb,y+sizeb+size);
  186.     vsa_line_to(x+sizeb,y+sizeb);
  187.     vsa_move_to(x+size,y);
  188.     vsa_line_to(x+sizeb+size,y+sizeb);
  189.     vsa_move_to(x+size,y+size);
  190.     vsa_line_to(x+sizeb+size,y+sizeb+size);
  191.     vsa_move_to(x,y+size);
  192.     vsa_line_to(x+sizeb,y+sizeb+size);
  193.     return;
  194. }
  195.  
  196. void rainbow_lut()
  197. {
  198.     int i,start,count;
  199.     unsigned char color_array[768];
  200.     for(i=0;i<224;i++)
  201.         {
  202.             color_array[3*i+2]=0;
  203.             color_array[3*i+1]=0;
  204.             color_array[3*i]=0;
  205.         }
  206. /*................................ RED .....................................*/
  207.     for(i=0;i<56;i++)
  208.         {
  209.                 color_array[3*i] = 63*sin((i*6.28)/112.0);
  210.         }
  211. /*............................... BLUE .....................................*/
  212.     for(i=0;i<126;i++)
  213.         {
  214.                 color_array[3*i+2] = 63*sin((i*6.28)/252.0);
  215.         }
  216. /*............................... GREEN ....................................*/
  217.     for(i=96;i<210;i++)
  218.         {
  219.                 color_array[3*i+1] = 63*sin(((i-90)*6.28)/252.0);
  220.         }
  221. /*................................ RED .....................................*/
  222.     for(i=140;i<224;i++)
  223.         {
  224.                 color_array[3*i]   = 63*sin(((i-140)*6.28)/280.0);
  225.         }
  226.     start = 32;
  227.     count = 224;
  228.     vsa_write_color_block(start,count,color_array);
  229.     return;
  230. }
  231.  
  232. void special_lut()
  233. {
  234.     int i,start,count;
  235.     unsigned char color_array[768];
  236.     for(i=0;i<256;i++)
  237.         {
  238.             color_array[3*i+2]=0;
  239.             color_array[3*i+1]=0;
  240.             color_array[3*i]=0;
  241.         }
  242. /*............................... BLUE .....................................*/
  243.     for(i=0;i<128;i++)
  244.         {
  245.                 color_array[3*i+2] = 63*sin((i*6.28)/256.0);
  246.         }
  247. /*............................... GREEN ....................................*/
  248.     for(i=48;i<240;i++)
  249.         {
  250.                 color_array[3*i+1] = 63*sin(((i-48)*6.28)/384.0);
  251.         }
  252. /*................................ RED .....................................*/
  253.     for(i=128;i<256;i++)
  254.         {
  255.                 color_array[3*i]   = 63*sin(((i-128)*6.28)/512.0);
  256.         }
  257.     start = 0;
  258.     count = 256;
  259.     vsa_write_color_block(start,count,color_array);
  260.     return;
  261. }
  262.  
  263. void color_bar(x0,y0)
  264. int x0,y0;
  265. {
  266.     char text[100];
  267.     unsigned char row,col;
  268.     int i;
  269.     unsigned xx,yy,a,b;
  270.     float c;
  271.     xx = XResolution;
  272.     yy = YResolution;
  273. /*..........................................................................*/
  274. /*     Draw outline for color bar.                                          */
  275. /*..........................................................................*/
  276.     vsa_set_color(15);
  277.     vsa_move_to(x0-1,y0-1);
  278.     a = .75*xx;
  279.     b = .065*yy;
  280.     vsa_rect(x0+a+1,y0+b+1);
  281.     c = (float)a/256;
  282.     for(i=0;i<256;i++)
  283.         {
  284.             vsa_set_color((unsigned char)i);
  285.             vsa_move_to(x0+(unsigned)(i*c),y0);
  286.             vsa_rect_fill(x0+(unsigned)(c+i*c),y0+b);
  287.         }
  288.     row = (y0+b+1)/YCharSize + 1;
  289.     col = (x0+a/2)/XCharSize - 31;
  290.     sprintf(text,"Color Look Up Table Manipulation using");
  291.     vsa_write_string(row,col,63,text);
  292.     sprintf(text," `vsa_write_color_block'");
  293.     vsa_write_string(row,col+38,63,text);
  294.     sprintf(text,"'vsa_read_color_register' and");
  295.     vsa_write_string(row+1,col,63,text);
  296.     sprintf(text,"`vsa_write_color_register'.");
  297.     vsa_write_string(row+1,col+31,63,text);
  298.     return;
  299. }
  300.  
  301. void banner(int x,int y)
  302. {
  303.     int x0,y0,x1,y1;
  304.     unsigned char row,col;
  305.     char text[100];
  306.     unsigned xx,yy,a,b;
  307.     xx = XResolution;
  308.     yy = YResolution;
  309.     a = .40*xx;
  310.     b = .17*yy;
  311.     vsa_move_to(x,y);
  312.     vsa_set_color(1);
  313.     vsa_rect_fill(x+a,y+b);
  314.     vsa_move_to(x+5,y+5);
  315.     vsa_set_color(0);
  316.     vsa_rect_fill(x+a-5,y+b-5);
  317.     row = (y+6+b/2)/YCharSize - 1;
  318.     col = (x+(a+5)/2)/XCharSize - 13;
  319.     sprintf(text,"VSA256 GRAPHICS LIBRARY");
  320.     vsa_write_string(row,col,200,text);
  321.     sprintf(text,"for C Programmers");
  322.     vsa_write_string(row+1,col,200,text);
  323.     sprintf(text,"Copyright Spyro Gumas 1992");
  324.     vsa_write_string(row+2,col,200,text);
  325.     vsa_set_text_color(250);
  326.     vsa_write_char(row+1,col+18,'V');
  327.     vsa_write_char(row+1,col+19,'1');
  328.     vsa_write_char(row+1,col+20,'.');
  329.     vsa_write_char(row+1,col+21,'1');
  330.     vsa_write_char(row+1,col+22,'b');
  331.     row = (y+b)/YCharSize + 1;
  332.     col = (x+(a+5)/2)/XCharSize - 13;
  333.     sprintf(text,"Character and Text Strings");
  334.     vsa_write_string(row,col,2,text);
  335.     sprintf(text,"using `vsa_write_char'");
  336.     vsa_write_string(row+1,col,2,text);
  337.     sprintf(text,"and `vsa_write_string'");
  338.     vsa_write_string(row+2,col,2,text);
  339.     return;
  340. }
  341.  
  342. void image(int x,int y)
  343. {
  344.     int i,j;
  345.     long ii,jj,z1,z2;
  346.     unsigned char array[1024],row,col;
  347.     char text[100];
  348.     unsigned xx,yy,a,b;
  349.     xx = XResolution;
  350.     yy = YResolution;
  351.     a = .4*xx;
  352.     b = .26*yy;
  353.     z1 = 2*1024L/a;
  354.     z2 = 1024L/b;
  355.     vsa_move_to(x-2,y-2);
  356.     vsa_set_color(250);
  357.     vsa_rect(x+a+1,y+b+1);
  358.     for(j=0;j<b;j++)
  359.         {
  360.             for(i=0;i<a;i++)
  361.                 {
  362.                     ii = (i*z1) & 0x000003ff;
  363.                     jj = (j*z2+256) & 0x000003ff;
  364. /*.....
  365.                     array[i] = 144+112*sin(i*6.28/c)*cos(j*6.28/c);
  366. .....*/
  367.                     array[i] = 144+112*SIN_LUT[ii]*SIN_LUT[jj];
  368.                 }
  369.             vsa_raster_line(x,x+a-1,y+j,array);
  370.         }
  371.     row = (y+b+1)/YCharSize + 1;
  372.     col = (x+a/2)/XCharSize - 17;
  373.     sprintf(text,"2D Images Using `vsa_raster_line()'");
  374.     vsa_write_string(row,col,100,text);
  375.     return;
  376. }
  377.  
  378.  
  379.