home *** CD-ROM | disk | FTP | other *** search
/ Game Developers Magazine 4 / GDM004.ZIP / SOURCE / PLASMA / PLASMA.C next >
C/C++ Source or Header  |  1994-06-06  |  4KB  |  148 lines

  1. /*
  2.  
  3.    PLASMA.C - Written by Phil Inch for Game Developers Magazine (Issue 4)
  4.  
  5.    This is actually a translation of a Pascal version which I found on
  6.    a local BBS, but regrettably no author details were included so I am
  7.    unable to credit the original author.
  8.  
  9.    I have tried to re-use the same variable and function names in the hope
  10.    that someone recognises the code and can let me know who the author was.
  11.  
  12.    This version, as was the original Pascal version, is contributed to the
  13.      public domain.
  14.  
  15.    This program written and compiled with Borland C++ v3.1
  16.    Compatibility with other compilers is not guaranteed.
  17.  
  18.    Usage of this program is subject to the disclaimer printed
  19.    in the magazine.  You assume all risks associated with the use
  20.    of this program.
  21.  
  22. */
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <conio.h>
  27. #include <stdlib.h>
  28. #include <dos.h>
  29. #include <time.h>
  30. #include "vga.h"
  31.  
  32. #define MAX_PALETTE 192
  33. #define MAX_X 319
  34. #define MAX_Y 199
  35.  
  36. unsigned char palette[MAX_PALETTE][3];
  37.  
  38. const double F = 4.0;
  39.  
  40. void adjust(int xa, int ya, int x, int y, int xb, int yb ) {
  41.     unsigned char a,b;
  42.   int d;
  43.   double r,v;
  44.  
  45.   GetPoint(x,y,&a); if (a) return;
  46.  
  47.   r = (double)rand()/RAND_MAX - 0.5;
  48.   d = abs(xa-xb)+abs(ya-yb);
  49.  
  50.   GetPoint(xa,ya,&a);
  51.   GetPoint(xb,yb,&b);
  52.  
  53.   v = (a+b)/2+d*r*F;
  54.  
  55.   if ( v < 1 ) v = 1;
  56.   if ( v > MAX_PALETTE ) v = MAX_PALETTE;
  57.  
  58.   SetPoint( x, y, (unsigned char)v );
  59. }
  60.  
  61. void split_box( int x1, int y1, int x2, int y2 ) {
  62.   unsigned char c1, c2, c3, c4;
  63.   int diffx, diffy;
  64.   int x,y;
  65.  
  66.     diffx = x2-x1;
  67.   diffy = y2-y1;
  68.   if ( diffx < 2 && diffy < 2 ) return;
  69.  
  70.   x = (x1+x2)/2;
  71.   y = (y1+y2)/2;
  72.  
  73.   adjust(x1,y1,x,y1,x2,y1);
  74.   adjust(x2,y1,x2,y,x2,y2);
  75.   adjust(x1,y2,x,y2,x2,y2);
  76.   adjust(x1,y1,x1,y,x1,y2);
  77.  
  78.   GetPoint( x, y, &c1 );
  79.   if ( c1 == 0 ) {
  80.       GetPoint( x1, y1, &c1 );    /* top left */
  81.       GetPoint( x2, y1, &c2 );  /* top right */
  82.       GetPoint( x1, y2, &c3 );  /* bot left */
  83.       GetPoint( x2, y2, &c4 );  /* bot right */
  84.     SetPoint( x, y, (c1+c2+c3+c4)/4 );
  85.     }
  86.  
  87.   split_box(x1,y1,x,y);
  88.   split_box(x,y1,x2,y);
  89.   split_box(x,y,x2,y2);
  90.   split_box(x1,y,x,y2);
  91. }
  92.  
  93. void set_palette(void) {
  94.     unsigned char c;
  95.  
  96.   for ( c = 0; c < 64; c++ ) {
  97.     palette[c][0] = c;
  98.     palette[c][1] = 63-c;
  99.     palette[c][2] = 0;
  100.  
  101.     palette[c+64][0] = 63-c;
  102.     palette[c+64][1] = 0;
  103.     palette[c+64][2] = c;
  104.  
  105.     palette[c+128][0] = 0;
  106.     palette[c+128][1] = c;
  107.     palette[c+128][2] = 63-c;
  108.     }
  109.   SetBlock( 1, &palette[0][0], MAX_PALETTE );
  110. }
  111.  
  112. void main(void) {
  113.   int c;
  114.   char c1, c2, c3, c4;
  115.  
  116.     SetGraphicsMode();
  117.   set_palette();
  118.      randomize();
  119.  
  120.   /* Set the starting points */
  121.   SetPoint( 0, 0, 1+(rand()%MAX_PALETTE) );
  122.   SetPoint( MAX_X, 0, 1+(rand()%MAX_PALETTE) );
  123.   SetPoint( 0, MAX_Y, 1+(rand()%MAX_PALETTE) );
  124.   SetPoint( MAX_X, MAX_Y, 1+(rand()%MAX_PALETTE) );
  125.  
  126.   /* Generate the plasma pattern */
  127.   split_box( 0, 0, MAX_X, MAX_Y );
  128.  
  129.   /* Cycle the palette until a key is pressed */
  130.   while ( !kbhit() ) {
  131.     c1 = palette[0][0];
  132.     c2 = palette[0][1];
  133.     c3 = palette[0][2];
  134.     for ( c = 0; c < MAX_PALETTE-1; c++ ) {
  135.             palette[c][0] = palette[c+1][0];
  136.             palette[c][1] = palette[c+1][1];
  137.             palette[c][2] = palette[c+1][2];
  138.       }
  139.     palette[MAX_PALETTE-1][0] = c1;
  140.     palette[MAX_PALETTE-1][1] = c2;
  141.     palette[MAX_PALETTE-1][2] = c3;
  142.       SetBlock( 1, &palette[0][0], MAX_PALETTE );
  143.     delay(10);
  144.     }
  145.  
  146.   SetTextMode();
  147. }
  148.