home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / sphinx / examples / vga / plasma.c__ < prev    next >
Encoding:
Text File  |  1993-11-28  |  3.7 KB  |  180 lines

  1. /*
  2.     NAME:  PLASMA.C--
  3.     DESCRIPTION:  This program generates and colour cycles a plasma fractal
  4.                   in VGA mode 19.  80286+ and VGA required.
  5.                   If a command line parameter is given, an attempt will be
  6.                   made to save the screen the file name given as the first
  7.                   parameter.  The file will be in *.SCR format which can be
  8.                   used with TINYDRAW.
  9.     RUN FILE SIZE:  1465 bytes
  10. */
  11.  
  12.  
  13. ?parsecommandline TRUE
  14.  
  15. ?include "MATH.H--"
  16. ?include "VIDEO.H--"
  17. ?include "RANDOM.H--"
  18. ?include "WRITE.H--"
  19. ?include "VGA.H--"
  20. ?include "FILE.H--"
  21. ?include "DOS.H--"
  22. ?include "KEYCODES.H--"
  23.  
  24. ?define  ROUGHNESS   2     // the "roughness" of image, try 1, 2, 3 or 4
  25.  
  26. ?define  MINCOLOR    1
  27. ?define  MAXCOLOR  192
  28.  
  29. ?define PALSIZE MAXCOLOR+1*3
  30. byte palette[PALSIZE];
  31.  
  32.  
  33.  
  34. void adjust (int xa,ya,x,y,xb,yb)
  35. int d,pixel,average;
  36. {
  37. IF( byte getpixel19(x,y) )
  38.     return;
  39.  
  40. AX = ABS(xa-xb);
  41. BX = AX;
  42. AX = ABS(ya-yb);
  43. d = AX + BX;     
  44.  
  45. AL = getpixel19(xa,ya);
  46. AH = 0;
  47. SI = AX;
  48. AL = getpixel19(xb,yb);
  49. AH = 0;
  50. average = AX + SI * 64 / 2;   // average = pixel(xa,ya)+pixel(xb,yb)/2
  51.  
  52. AX = RAND() % 64 - 32;
  53. AX = AX * d * ROUGHNESS;
  54. pixel = AX + average / 64;  // v = random - 0.5 * d * ROUGHNESS + average;
  55.    
  56. IF( pixel < MINCOLOR )
  57.     pixel = MINCOLOR; 
  58. ELSE IF( pixel > MAXCOLOR )
  59.     pixel = MAXCOLOR; 
  60. putpixel19(x,y,pixel);  
  61. }
  62.  
  63.  
  64. void sub_divide (int x1,y1,x2,y2)
  65. int x,y;
  66. {
  67. IF( x2-x1 < 2 )
  68.     IF( y2-y1 < 2 )
  69.         return;
  70.  
  71. x = x1 + x2 / 2;
  72. y = y1 + y2 / 2;
  73.  
  74. adjust(x1,y1,x,y1,x2,y1);
  75. adjust(x2,y1,x2,y,x2,y2);
  76. adjust(x1,y2,x,y2,x2,y2);
  77. adjust(x1,y1,x1,y,x1,y2);
  78.  
  79. IF( byte getpixel19(x,y) == 0 )
  80.     {
  81.     AL = getpixel19(x1,y1);
  82.     AH = 0;
  83.     SI = AX;
  84.     AL = getpixel19(x2,y1);
  85.     AH = 0;
  86.     SI += AX;
  87.     AL = getpixel19(x2,y2);
  88.     AH = 0;
  89.     SI += AX;
  90.     AL = getpixel19(x1,y2);
  91.     AH = 0;
  92.     AX = AX + SI / 4;
  93.     SI = AX;
  94.     putpixel19(x,y,SI);
  95.     }
  96.  
  97. sub_divide(x1,y1,x,y);
  98. sub_divide(x,y1,x2,y);
  99. sub_divide(x,y,x2,y2);
  100. sub_divide(x1,y,x,y2);
  101. }
  102.  
  103.  
  104. void init_palette ()
  105. {
  106. palette[0] = 33;    //
  107. palette[1] = 10;    //  Set background colour
  108. palette[2] = 19;    //
  109.  
  110. BX = 0;
  111. do {
  112.     DI = BX+BX+BX;
  113.  
  114.     palette[DI+3] = BL;
  115.     palette[DI+3+1] = 63-BL;
  116.     palette[DI+3+2] = 0;
  117.  
  118.     palette[DI+195] = 63-BL;
  119.     palette[DI+195+1] = 0;
  120.     palette[DI+195+2] = BL;
  121.  
  122.     palette[DI+387] = 0;
  123.     palette[DI+387+1] = BL;
  124.     palette[DI+387+2] = 63-BL;
  125.  
  126.     BX++;
  127.     } while( BX < 64 );
  128.  
  129. rotate_palette();  // set the palette
  130. }
  131.  
  132.  
  133. main ()
  134. {
  135. IF( byte @ GETCPU() < 2 )    /* check if 80286 instructions available */
  136.     {WRITESTR("80286 or greater CPU required.");
  137.     EXIT(1);}
  138. SETVIDEOMODE( vid_320x200_256 );    /* set video mode */
  139. IF( @ GETVIDEOMODE() != vid_320x200_256 )  /* check if entered the mode */
  140.     {WRITESTR("Unable to enter 256 colour mode, VGA required.");
  141.     EXIT(1);}
  142. init_palette();
  143. @ RANDOMIZE();
  144.  
  145. putpixel19(0,0,RAND()%192+1);
  146. putpixel19(319,0,RAND()%192+1);
  147. putpixel19(319,199,RAND()%192+1);
  148. putpixel19(0,199,RAND()%192+1);
  149.  
  150. sub_divide(0,0,319,199);  // generate the image to cover the whole screen
  151.  
  152. IF( @ PARAMCOUNT() >= 1 )
  153.     IF( writefile(@PARAMSTR(0),VGA_SEG,0,64000) != 64000 )
  154.         @ BEEP();
  155.  
  156. do {
  157.     rotate_palette();
  158.     } while( KBHIT() == 0 );
  159.  
  160. BIOSREADKEY();
  161. SETVIDEOMODE(vid_text80c);
  162. }
  163.  
  164.  
  165. void rotate_palette ()   // rotate the palette one
  166. byte hold1,hold2,hold3;
  167. {
  168. hold1 = palette[3];
  169. hold2 = palette[4];
  170. hold3 = palette[5];
  171. @ COPYNEAR(#palette[3],#palette[6],PALSIZE-6);
  172. palette[PALSIZE-3] = hold1;
  173. palette[PALSIZE-2] = hold2;
  174. palette[PALSIZE-1] = hold3;
  175. @ WAITVSYNC();
  176. @ SETVGADAC(0, ,PALSIZE, , ,#palette);
  177. }
  178.  
  179.  
  180. /* end of PLASMA.C-- */