home *** CD-ROM | disk | FTP | other *** search
/ Adventures in Heaven 2 / adventuresinheaven2powergamesfordosandwindows.iso / dos / demos / scope / ascope.c next >
C/C++ Source or Header  |  1989-07-12  |  6KB  |  260 lines

  1. /*
  2. ascope.c, version 1.0
  3. by Aaron Contorer, 1989
  4.  
  5. This program draws a kaleidoscope in the VGA 16-color, 640x480 resolution
  6. graphics mode.  It works ONLY on the VGA, as it takes advantage of the
  7. large number of colors available on this graphics adapter.
  8. Written in Microsoft Quick C 1.0.
  9.  
  10. This program is PUBLIC DOMAIN, so you may use, distribute, or modify it
  11. as you wish.  Please send the author a copy of anything interesting you do
  12. with this program, and if you use some of this program in another program,
  13. an acknowledgement on the screen would be appreciated.  The author disclaims
  14. all liability of any sort and all warranties on this program.
  15.  
  16. You can contact the author on CompuServe (address 71571,1773) or by mail at:
  17.     Aaron Contorer
  18.     President, Contorer Computing
  19.     Post Office Box 5056
  20.     Champaign, IL 61825
  21.     United States of America
  22. */
  23.  
  24.  
  25. #include <graph.h>
  26. #include <stdlib.h>
  27. #include <conio.h>
  28. #include <time.h>
  29. #include <stdio.h>
  30.  
  31.  
  32. #define MINBRIGHT 7
  33. #define RANDOMTURN 300
  34.     /* How likely is a random turn-around, on a scale of 32767 */
  35. #define MAXLINE 50
  36.     /* how many lines on the screen at a time */
  37. #define COLORSTEP 7
  38.  
  39. int lastx=639;
  40. int lasty=479;
  41. int lastbigx = 599 << 4;
  42. int lastbigy = 439 << 4;
  43.  
  44. #define MAPSIZE 16
  45. long colormap[MAPSIZE];
  46.  
  47.  
  48.  
  49. long findnewcolor(char[]);
  50. /* Find a new color similar to the given color */
  51. long findnewcolor(char *old)
  52. {
  53.     int red = old[0];
  54.     int green = old[1];
  55.     int blue = old[2];
  56.     int r;
  57.  
  58.     r=rand();
  59.     if (r & 8) red += r & COLORSTEP;
  60.     else red -= r & COLORSTEP;
  61.     r >>= 4;
  62.     if (r & 8) green += r & COLORSTEP;
  63.     else green -= r & COLORSTEP;
  64.     r >>= 4;
  65.     if (r & 8) blue += r & COLORSTEP;
  66.     else blue -= r & COLORSTEP;
  67.  
  68.     if (blue>0x3f) blue=0x3f;
  69.     else if (blue<MINBRIGHT) blue=MINBRIGHT;
  70.     if (green>0x3f) green=0x3f;
  71.     else if (green<MINBRIGHT) green=MINBRIGHT;
  72.     if (red>0x3f) red=0x3f;
  73.     else if (red<MINBRIGHT) red=MINBRIGHT;
  74.  
  75.     return (((long)blue) << 16) | (green<<8) | red;
  76. }
  77.  
  78.  
  79.  
  80. void initcolormap(void);
  81. void initcolormap()
  82. {
  83.     int i;
  84.     colormap[0] = 0L; /* the "erase" color never changes */
  85.     colormap[1] = 0x3f3f3fL; /* a color to start with */
  86.     for (i=2; i<MAPSIZE; i++) colormap[i] = findnewcolor(&colormap[i-1]);
  87.     for (i=0; i<MAPSIZE; i++) _remappalette(i, colormap[i]);
  88. }
  89.  
  90.  
  91. void rotatecolormap(int);
  92. void rotatecolormap(int newcolor)
  93. {
  94.     int i;
  95.     int prevcolor;
  96.  
  97.     if (newcolor>1) prevcolor = newcolor-1;
  98.     else prevcolor = MAPSIZE-1;
  99.     colormap[newcolor] = findnewcolor(&colormap[prevcolor]);
  100.     _remappalette(newcolor, colormap[newcolor]);
  101. }
  102.  
  103.  
  104.  
  105. void mirrorline(int,int,int,int);
  106. void mirrorline(x0,y0,x1,y1)
  107. {
  108.     x0 >>= 4;
  109.     y0 >>= 4;
  110.     x1 >>= 4;
  111.     y1 >>= 4;
  112.  
  113.     _moveto(x0,y0);
  114.     _lineto(x1,y1);
  115.     _moveto(lastx-x0, y0);
  116.     _lineto(lastx-x1, y1);
  117.     y0 = lasty-y0;
  118.     y1 = lasty-y1;
  119.     _moveto(x0,y0);
  120.     _lineto(x1,y1);
  121.     _moveto(lastx-x0, y0);
  122.     _lineto(lastx-x1, y1);
  123. }
  124.  
  125.  
  126.  
  127. int randsign(void);
  128.  
  129. int randsign()
  130. {
  131.     if (rand() & 1) return 1;
  132.     return -1;
  133. }
  134.  
  135.  
  136.  
  137. #define isgn(i) (i<0 ? -1 : 1)
  138.     /* Signum macro */
  139.  
  140.  
  141.  
  142. #define randacc() ((rand() & 3) + 1)
  143.  
  144.  
  145.  
  146. void randomize(void);
  147. /* Initialize random number generator to ensure a different result every
  148. time the program is run. */
  149. void randomize()
  150. {
  151.     long temp;
  152.     time(&temp);
  153.     srand((unsigned)temp);
  154. }
  155.  
  156.  
  157. int main(void);
  158. main()
  159. {
  160.     int i;
  161.     int endpoint[MAXLINE][4]; /* circular buffer for endpoints */
  162.     int first=0;
  163.     int free=0;
  164.     int dx0,dy0,dx1,dy1; /* velocities of endpoints */
  165.     int ax0,ay0,ax1,ay1; /* accelerations of endpoints */
  166.     int x0,y0,x1,y1;
  167.     int colorcount, usecolor;
  168.  
  169.     if (!_setvideomode(_VRES16COLOR)) {
  170.         puts("Sorry, this program requres a VGA graphics card.");
  171.         exit(1);
  172.     }
  173.  
  174.     _settextposition(12,35);
  175.     _outtext("A-Scope 1.0");
  176.     _settextposition(14,30);
  177.      _outtext("by Aaron M. Contorer");
  178.     randomize();
  179.  
  180.     x0=rand() & 0x3fff;
  181.     y0=rand() & 0x3fff;
  182.     x1=rand() & 0x3fff;
  183.     y1=rand() & 0x3fff;
  184.  
  185.     dx0=randsign() * (rand() & 0x3f);
  186.     dx1=randsign() * (rand() & 0x3f);
  187.     dy0=randsign() * (rand() & 0x3f);
  188.     dy1=randsign() * (rand() & 0x3f);
  189.  
  190.     ax0=randsign() * randacc();
  191.     ay0=randsign() * randacc();
  192.     ax1=randsign() * randacc();
  193.     ay1=randsign() * randacc();
  194.  
  195.     colorcount = 1;
  196.     usecolor = 0;
  197.     initcolormap();
  198.  
  199.     while (!kbhit()) {
  200.  
  201.         if (--colorcount <= 0) {
  202.             colorcount = 4;
  203.             usecolor++;
  204.             if (usecolor>=MAPSIZE) usecolor = 1;
  205.             rotatecolormap(usecolor);
  206.         }
  207.  
  208.         x0 += dx0;
  209.         x1 += dx1;
  210.         y0 += dy0;
  211.         y1 += dy1;
  212.  
  213.         dx0 += ax0;
  214.         dy0 += ay0;
  215.         dx1 += ax1;
  216.         dy1 += ay1;
  217.  
  218.         if (rand()<RANDOMTURN || x0<0 || x0>lastbigx) if (isgn(ax0)==isgn(x0)) {
  219.             ax0 = -isgn(ax0) * randacc();
  220.             dx0=0; }
  221.         if (rand()<RANDOMTURN || x1<0 || x1>lastbigx) if (isgn(ax1)==isgn(x1)) {
  222.             ax1 = -isgn(ax1) * randacc();
  223.             dx1=1; }
  224.         if (rand()<RANDOMTURN || y0<0 || y0>lastbigy) if (isgn(ay0)==isgn(y0)) {
  225.             ay0 = -isgn(ay0) * randacc();
  226.             dy0=0; }
  227.         if (rand()<RANDOMTURN || y1<0 || y1>lastbigy) if (isgn(ay1)==isgn(y1)) {
  228.             ay1 = -isgn(ay1) * randacc();
  229.             dy1=1; }
  230.  
  231.         endpoint[free][0] = x0;
  232.         endpoint[free][1] = y0;
  233.         endpoint[free][2] = x1;
  234.         endpoint[free][3] = y1;
  235.         _setcolor(usecolor);
  236.         mirrorline(endpoint[free][0], endpoint[free][1],
  237.             endpoint[free][2], endpoint[free][3]);
  238.  
  239.         free++;
  240.         if (free >= MAXLINE) free = 0;
  241.         if (free == first) {
  242.             /* erase oldest line */
  243.             _setcolor(0);
  244.             mirrorline(endpoint[first][0], endpoint[first][1],
  245.                 endpoint[first][2], endpoint[first][3]);
  246.             first++;
  247.             if (first >= MAXLINE) first = 0;
  248.         }
  249.     }
  250.     getch();
  251.  
  252.     _setvideomode(_DEFAULTMODE);
  253.     puts("Thank you for using A-scope.  I hope you enjoyed this program!\n");
  254.     puts("Author: Aaron Contorer [71571,1773]");
  255.     puts("        President, Contorer Computing");
  256.     puts("        Box 5056, Champaign, IL 61825, USA");
  257.     return 0;
  258. }
  259.  
  260.