home *** CD-ROM | disk | FTP | other *** search
/ Share Gallery 1 / share_gal_1.zip / share_gal_1 / GA / GA025.ZIP / KALEDIS.C next >
Text File  |  1988-04-08  |  6KB  |  229 lines

  1.  
  2. /*
  3.  *  KALEDIS   EGA/VGA Kaleidoscope
  4.  *
  5.  *     Judson D. McClendon
  6.  *     Sun Valley Systems
  7.  *     844 Sun Valley Road
  8.  *     Birmingham, AL 35215
  9.  *       (205) 853-8440
  10.  *    Compuserve [74415,1003]
  11.  */
  12.  
  13. /*
  14.  *  Compiled using Microsoft C 5.0 with option /Ox
  15.  */
  16.  
  17. #include <conio.h>
  18. #include <ctype.h>
  19. #include <dos.h>
  20. #include <graph.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <sys\timeb.h>
  24.  
  25. #define TRUE 1
  26. #define FALSE 0
  27. #define MAXCOLORS 16
  28. #define ESC  27
  29.  
  30. void main(void);                 /* Main */
  31. void curpos(int, int);           /* Position cursor to row, col */
  32. void printat(int, int, char *);  /* Print string at row, col */
  33. int getkey(void);                /* Get decoded keypress no echo */
  34. int random(int);                 /* Generate Random Number 0 <= r < n */
  35.  
  36. int X1, Y1, X2, Y2, XV1, YV1, XV2, YV2, HC, XA, YA, XB, YB, chr;
  37. int i, bc = 1, delay = 0;
  38. int wid = 4, hgt = 3;
  39. int CX = 320, CY = 240, M = 240;
  40.  
  41. long far Pcol[MAXCOLORS];
  42. long Red, Green, Blue;
  43. long dummy = 0;
  44.  
  45. struct timeb TIME;
  46. struct videoconfig VC;
  47.  
  48. enum {VGA,EGA} adapter = VGA;
  49.  
  50. void main()                      /* main */
  51. {
  52.    _getvideoconfig(&VC);
  53.    _clearscreen(_GCLEARSCREEN);
  54.  
  55.    printat( 3,25, "K A L E I D O S C O P E");
  56.    printat( 5,25, "Version 2.1 for EGA/VGA");
  57.    printat( 7,27, "Judson D. McClendon");
  58.    printat( 8,27, "844 Sun Valley Road");
  59.    printat( 9,27, "Birmingham, AL 35215");
  60.    printat(10,31, "205-853-8440");
  61.    printat(11,26, "Compuserve [74415,1003]");
  62.    printat(13,23, "Press 'P' to pause,");
  63.    printat(14,29, "'C' to clear screen,");
  64.    printat(15,29, "'B' to toggle background,");
  65.    printat(16,29, "'0'-'9' set speed,");
  66.    printat(17,29, "ESCape to exit,");
  67.    printat(18,29, "any key to begin/resume...");
  68.  
  69.    chr = getkey();
  70.    if (chr == ESC)
  71.      exit(0);
  72.  
  73.    ftime(&TIME);           /* Initialize random number generator */
  74.    srand(TIME.millitm);
  75.  
  76.    if (! _setvideomode(_VRES16COLOR)) {            /* VGA 640x480x16 color */
  77.       CY = 175;
  78.       wid = 9;
  79.       hgt = 5;
  80.       adapter = EGA;
  81.       if (! _setvideomode(_ERESCOLOR)) {           /* EGA 640x350x16 color */
  82.          printat(20,25, "Must have EGA or VGA!");
  83.          exit(1);
  84.          }
  85.       }
  86.    M = CY - 1;
  87.  
  88.    while (TRUE) {
  89.       Pcol[0] = 0L;
  90.  
  91.       Pcol[0] = 0L;
  92.       for (i = bc; i < MAXCOLORS; i++) {
  93.          Red = (long)random(64);
  94.          Green = (long)random(64);
  95.          Blue = (long)random(64);
  96.          Pcol[i] = ((Blue << 8 | Green) << 8 | Red);
  97.          }
  98.  
  99.       _remapallpalette(&Pcol[0]);
  100.       _clearscreen(_GCLEARSCREEN);
  101.  
  102.       X1 = random(M) + 1;
  103.       X2 = random(M) + 1;
  104.       Y1 = random(X1);
  105.       Y2 = random(X2);
  106.  
  107.       while (random(200) > 1) {
  108.          XV1 = random(11) - 5;
  109.          XV2 = random(11) - 5;
  110.          YV1 = random(11) - 5;
  111.          YV2 = random(11) - 5;
  112.          HC = random(16);
  113.          _setcolor(HC);
  114.  
  115.          while (random(10) > 1) {
  116.             XA = X1 * wid / hgt;
  117.             XB = X2 * wid / hgt;
  118.             YA = Y1 * wid / hgt;
  119.             YB = Y2 * wid / hgt;
  120.             _moveto( (CX+XA), (CY-Y1) ); _lineto( (CX+XB), (CY-Y2) );
  121.             _moveto( (CX-YA), (CY+X1) ); _lineto( (CX-YB), (CY+X2) );
  122.             _moveto( (CX-XA), (CY-Y1) ); _lineto( (CX-XB), (CY-Y2) );
  123.             _moveto( (CX-YA), (CY-X1) ); _lineto( (CX-YB), (CY-X2) );
  124.             _moveto( (CX-XA), (CY+Y1) ); _lineto( (CX-XB), (CY+Y2) );
  125.             _moveto( (CX+YA), (CY-X1) ); _lineto( (CX+YB), (CY-X2) );
  126.             _moveto( (CX+XA), (CY+Y1) ); _lineto( (CX+XB), (CY+Y2) );
  127.             _moveto( (CX+YA), (CY+X1) ); _lineto( (CX+YB), (CY+X2) );
  128.             X1 = (X1 + XV1) % M;
  129.             Y1 = (Y1 + YV1) % M;
  130.             X2 = (X2 + XV2) % M;
  131.             Y2 = (Y2 + YV2) % M;
  132.             if (delay) {
  133.                for (i = 1; i < delay; i++)
  134.                   dummy += i * delay;
  135.                   dummy -= delay;
  136.                   ;
  137.                }
  138.             };
  139.  
  140.          if (kbhit()) {
  141.             chr = getkey();
  142.             if (chr == 'P') {
  143.                getkey();
  144.                }
  145.             else if (chr == ESC) {
  146.                _clearscreen(_GCLEARSCREEN);
  147.                _setvideomode(_DEFAULTMODE);
  148.                exit(0);
  149.                }
  150.             else if (chr == 'C') {
  151.                break;
  152.                }
  153.             else if (chr == 'B') {
  154.                bc = 1 - bc;
  155.                break;
  156.                }
  157.             else if (isdigit(chr)) {
  158.                delay = ('9' - chr) * 3000;
  159.                }
  160.             }
  161.  
  162.          }  /* while (line) */
  163.  
  164.       }  /* while (screen) */
  165.  
  166.    _setvideomode(_DEFAULTMODE);
  167.    exit(0);
  168. }
  169.  
  170.  
  171.  
  172. /*
  173.  *       Position cursor to row, col
  174.  */
  175.  
  176. void curpos(int row, int col)
  177.  
  178. {
  179.    union REGS cpu;
  180.  
  181.    cpu.h.ah = 2;
  182.    cpu.h.dh = (char)row -1;
  183.    cpu.h.dl = (char)col -1;
  184.    cpu.h.bh = 0;
  185.    int86(16, &cpu, &cpu);
  186. }
  187.  
  188.  
  189.  
  190. /*
  191.  *       Position cursor to row, col
  192.  */
  193.  
  194. int getkey(void)                 /* Get decoded keypress no echo */
  195.  
  196. {
  197.    int chr;
  198.  
  199.    chr = getch();
  200.    if (chr) {
  201.       return(toupper(chr));
  202.       }
  203.    else {
  204.       return(1000 + getch());
  205.       }
  206. }
  207.  
  208.  
  209.                                  /* Print string at row, col */
  210. void printat(int row, int col, char *fld)
  211.  
  212. {
  213.    curpos(row, col);
  214.    cputs(fld);
  215. }
  216.  
  217.  
  218.  
  219. int random(int r)                /* Generate Random Number 0-r */
  220.  
  221. {
  222.    int s;
  223.    long t;
  224.  
  225.    s = rand();
  226.    t = (long) s * r / 32768;
  227.    return((int) t);
  228. }
  229.