home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / GLEN / KALEIDS.ZIP / KALEIDSQ.C < prev    next >
C/C++ Source or Header  |  1988-09-08  |  16KB  |  528 lines

  1. /*
  2.  *    **************************************************
  3.  *    *                                                *
  4.  *    *                   KALEDIS.C                    *
  5.  *    *                                                *
  6.  *    *              EGA/VGA Kaleidoscope              *
  7.  *    *                                                *
  8.  *    *              Version 3.1 09-08-88              *
  9.  *    *                                                *
  10.  *    *              Judson D. McClendon               *
  11.  *    *              329 37th Court N.E.               *
  12.  *    *              Birmingham, AL 35215              *
  13.  *    *                 205-853-8440                   *
  14.  *    *            Compuserve [74415,1003]             *
  15.  *    *                                                *
  16.  *    **************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *    **************************************************
  22.  *    *                                                *
  23.  *    *                    Includes                    *
  24.  *    *                                                *
  25.  *    **************************************************
  26.  */
  27.  
  28.  
  29. #include <conio.h>
  30. #include <ctype.h>
  31. #include <dos.h>
  32. #include <graph.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <sys\timeb.h>
  36.  
  37.  
  38.  
  39.  
  40. /*
  41.  *    **************************************************
  42.  *    *                                                *
  43.  *    *              Function Declarations             *
  44.  *    *                                                *
  45.  *    **************************************************
  46.  */
  47.  
  48. extern void main(void);
  49. static void Initialize(void);
  50.  
  51. static int  GetChar(void);
  52. static int  KeyPressed(void);
  53.  
  54. static void ScrClr(void);
  55. static void WriteAt(int row,int col,char *str);
  56.  
  57. static int  Random(int r);
  58. static long TimerTicks(void);
  59.  
  60.  
  61.  
  62.  
  63. /*
  64.  *    **************************************************
  65.  *    *                                                *
  66.  *    *                    Defines                     *
  67.  *    *                                                *
  68.  *    **************************************************
  69.  */
  70.  
  71. #define TRUE  1
  72. #define FALSE 0
  73. #define ESC  27
  74. #define MAXCOLORS   16           /* Color Palette size   */
  75. #define COLORSCREEN 0xB8000000L  /* Color buffer address */
  76.  
  77.  
  78.  
  79.  
  80. /*
  81.  *    **************************************************
  82.  *    *                                                *
  83.  *    *                  Data Types                    *
  84.  *    *                                                *
  85.  *    **************************************************
  86.  */
  87.  
  88. typedef struct {                 /* Screen char struct */
  89.    char
  90.       data,
  91.       attr;
  92.    } CHTYPE;
  93.  
  94. typedef struct {                 /* Screen memory structure */
  95.    CHTYPE ch[25][80];
  96.    } far *SCREENTYPE;
  97.  
  98.  
  99.  
  100.  
  101. /*
  102.  *    **************************************************
  103.  *    *                                                *
  104.  *    *                Global Variables                *
  105.  *    *                                                *
  106.  *    **************************************************
  107.  */
  108.  
  109. int X1, Y1, X2, Y2, XV1, YV1, XV2, YV2, HC, XA, YA, XB, YB, chr;
  110. int i, bc = 1, delay = 0;
  111. int wid = 4, hgt = 3;
  112. int CX = 320, CY = 240, M = 240;
  113.  
  114. long Pcol[MAXCOLORS];
  115. long Red, Green, Blue;
  116. long dummy = 0;
  117.  
  118. static SCREENTYPE                /* Screen pointer  */
  119.    scr;
  120.  
  121. static int
  122.    arow = 0,                     /* Absolute row    */
  123.    acol = 0;                     /* Absolute column */
  124.  
  125. static union REGS
  126.    cpu;                          /* CPU registers for int86() */
  127.  
  128. struct timeb TIME;               /* Time structure */
  129.  
  130. struct videoconfig VC;           /* Video configuration structure */
  131.  
  132.  
  133.  
  134.  
  135. /*
  136.  *    **************************************************
  137.  *    *                                                *
  138.  *    *                     Main                       *
  139.  *    *                                                *
  140.  *    **************************************************
  141.  */
  142.  
  143. extern void main()
  144. {
  145.  
  146.    Initialize();
  147.  
  148.    while (TRUE) {
  149.       Pcol[0] = 0L;
  150.  
  151.       Pcol[0] = 0L;
  152.       for (i = bc; i < MAXCOLORS; i++) {
  153.          Red = (long)Random(64);
  154.          Green = (long)Random(64);
  155.          Blue = (long)Random(64);
  156.          Pcol[i] = ((Blue << 8 | Green) << 8 | Red);
  157.          }
  158.  
  159.       _remapallpalette((long far *)&Pcol[0]);
  160.       _clearscreen(_GCLEARSCREEN);
  161.  
  162.       X1 = Random(M) + 1;
  163.       X2 = Random(M) + 1;
  164.       Y1 = Random(X1);
  165.       Y2 = Random(X2);
  166.  
  167.         while (Random(130) > 1) {
  168.             XV1 = Random(5) - 2;
  169.             XV2 = Random(5) - 2;
  170.             YV1 = Random(5) - 2;
  171.             YV2 = Random(5) - 2;
  172.          HC = Random(16);
  173.          _setcolor(HC);
  174.  
  175.             while (Random(30) > 1) {
  176.             XA = X1 * wid / hgt;
  177.             XB = X2 * wid / hgt;
  178.             YA = Y1 * wid / hgt;
  179.             YB = Y2 * wid / hgt;
  180.             _moveto( (CX+XA), (CY-Y1) ); _lineto( (CX+XB), (CY-Y2) );
  181.             _moveto( (CX-YA), (CY+X1) ); _lineto( (CX-YB), (CY+X2) );
  182.             _moveto( (CX-XA), (CY-Y1) ); _lineto( (CX-XB), (CY-Y2) );
  183.             _moveto( (CX-YA), (CY-X1) ); _lineto( (CX-YB), (CY-X2) );
  184.             _moveto( (CX-XA), (CY+Y1) ); _lineto( (CX-XB), (CY+Y2) );
  185.             _moveto( (CX+YA), (CY-X1) ); _lineto( (CX+YB), (CY-X2) );
  186.             _moveto( (CX+XA), (CY+Y1) ); _lineto( (CX+XB), (CY+Y2) );
  187.             _moveto( (CX+YA), (CY+X1) ); _lineto( (CX+YB), (CY+X2) );
  188.             X1 = (X1 + XV1) % M;
  189.             Y1 = (Y1 + YV1) % M;
  190.             X2 = (X2 + XV2) % M;
  191.             Y2 = (Y2 + YV2) % M;
  192.             if (delay) {
  193.                for (i = 0; i < delay; i++) {
  194.                   dummy += i * delay;
  195.                   dummy -= delay;
  196.                   }
  197.                }
  198.             }
  199.  
  200.          if (chr = KeyPressed()) {
  201.             if (chr == 'P') {
  202.                GetChar();
  203.                }
  204.             else if (chr == ESC) {
  205.                _clearscreen(_GCLEARSCREEN);
  206.                _setvideomode(_DEFAULTMODE);
  207.                exit(0);
  208.                }
  209.             else if (chr == 'C') {
  210.                break;
  211.                }
  212.             else if (chr == 'B') {
  213.                bc = 1 - bc;
  214.                break;
  215.                }
  216.             else if (isdigit(chr)) {
  217.                delay = ('9' - chr) * 3000;
  218.                }
  219.             }
  220.  
  221.          }  /* while (line) */
  222.  
  223.       }  /* while (screen) */
  224.  
  225.    _setvideomode(_DEFAULTMODE);
  226.    exit(0);
  227.  
  228. }
  229.  
  230.  
  231.  
  232.  
  233. /*
  234.  *    **************************************************
  235.  *    *                                                *
  236.  *    *                  Initialize                    *
  237.  *    *                                                *
  238.  *    **************************************************
  239.  */
  240.  
  241. void Initialize()
  242. {
  243.  
  244.    long endtime;
  245.  
  246.    cpu.h.ah = 15;                   /* Get current video mode */
  247.    int86(16, &cpu, &cpu);
  248.    if (cpu.h.al != 3) {             /* 80x25 color */
  249.       puts("Display must be in 80x25 color mode");
  250.       exit(1);
  251.       }
  252.    scr = (SCREENTYPE)((void far *)COLORSCREEN); /* Screen memory pointer */
  253.  
  254.    ftime(&TIME);                    /* Initialize random number generator */
  255.    srand(TIME.millitm);
  256.  
  257.    ScrClr();
  258.  
  259.    WriteAt( 1, 2, "KaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledis");
  260.    WriteAt( 2, 2, "KaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledis");
  261.    WriteAt( 3, 2, "KaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledis");
  262.    WriteAt( 4, 2, "KaledisKaledisKaledisKal┌───────────────────────────┐disKaledisKaledisKaledis");
  263.    WriteAt( 5, 2, "KaledisKaledisKaledisKal│  K A L E I D O S C O P E  │disKaledisKaledisKaledis");
  264.    WriteAt( 6, 2, "KaledisKaledisKaledisKal│                           │disKaledisKaledisKaledis");
  265.    WriteAt( 7, 2, "KaledisKaledisKaledisKal│  Version 3.1 for EGA/VGA  │disKaledisKaledisKaledis");
  266.    WriteAt( 8, 2, "KaledisKaledisKaledisKal│                           │disKaledisKaledisKaledis");
  267.    WriteAt( 9, 2, "KaledisKaledisKaledisKal│    Judson D. McClendon    │disKaledisKaledisKaledis");
  268.    WriteAt(10, 2, "KaledisKaledisKaledisKal│    329 37th Court N.E.    │disKaledisKaledisKaledis");
  269.    WriteAt(11, 2, "KaledisKaledisKaledisKal│    Birmingham, AL 35215   │disKaledisKaledisKaledis");
  270.    WriteAt(12, 2, "KaledisKaledisKaledisKal│       205-853-8440        │disKaledisKaledisKaledis");
  271.    WriteAt(13, 2, "KaledisKaledisKaledisKal│  Compuserve [74415,1003]  │disKaledisKaledisKaledis");
  272.    WriteAt(14, 2, "KaledisKaledisKaledisKal│                           │disKaledisKaledisKaledis");
  273.    WriteAt(15, 2, "KaledisKaledisKaledisKal│  Press: to:               │disKaledisKaledisKaledis");
  274.    WriteAt(16, 2, "KaledisKaledisKaledisKal│     'P' pause             │disKaledisKaledisKaledis");
  275.    WriteAt(17, 2, "KaledisKaledisKaledisKal│     'C' clear screen      │disKaledisKaledisKaledis");
  276.    WriteAt(18, 2, "KaledisKaledisKaledisKal│     'B' toggle background │disKaledisKaledisKaledis");
  277.    WriteAt(19, 2, "KaledisKaledisKaledisKal│ '0'-'9' set speed         │disKaledisKaledisKaledis");
  278.    WriteAt(20, 2, "KaledisKaledisKaledisKal│  ESCape exit              │disKaledisKaledisKaledis");
  279.    WriteAt(21, 2, "KaledisKaledisKaledisKal│ any key begin/resume...   │disKaledisKaledisKaledis");
  280.    WriteAt(22, 2, "KaledisKaledisKaledisKal└───────────────────────────┘disKaledisKaledisKaledis");
  281.    WriteAt(23, 2, "KaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledis");
  282.    WriteAt(24, 2, "KaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledis");
  283.    WriteAt(25, 2, "KaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledis");
  284.  
  285.    cpu.h.ah = 2;                    /* Move cursor to row 0, col 0 */
  286.    cpu.h.bh = 0;
  287.    cpu.x.dx = 0;
  288.    int86(16, &cpu, &cpu);
  289.    scr->ch[0][0].attr = 0;          /* Set cursor color to black on black */
  290.  
  291.    do {                             /* Kaleidoscope title */
  292.       for (arow = 0; arow < 25; arow++)
  293.          for (acol = 1; acol < 78; acol++)
  294.             if (arow < 6 || arow > 20 || acol < 26 || acol > 52)
  295.                if (!(--scr->ch[arow][acol].attr))
  296.                   scr->ch[arow][acol].attr = 15;
  297.  
  298.       endtime = TimerTicks() + 1;   /* Wait one tick */
  299.       while (TimerTicks() < endtime)
  300.          ;
  301.       } while (! (chr = KeyPressed()) );
  302.  
  303.    ScrClr();
  304.  
  305.    if (chr == ESC) {
  306.       exit(0);
  307.       }
  308.  
  309.    _getvideoconfig(&VC);            /* Save video configuration */
  310.  
  311.    if (! _setvideomode(_VRES16COLOR)) {   /* Try VGA 640x480x16 color */
  312.       CY = 175;
  313.       wid = 9;
  314.       hgt = 5;
  315.       if (! _setvideomode(_ERESCOLOR)) {  /* Try EGA 640x350x16 color */
  316.          puts("Display must be EGA or VGA");
  317.          _setvideomode(_DEFAULTMODE);
  318.          exit(1);
  319.          }
  320.       }
  321.    M = CY - 1;
  322.  
  323. }
  324.  
  325.  
  326.  
  327.  
  328. /*
  329.  *    **************************************************
  330.  *    *                                                *
  331.  *    *              Keyboard Routines                 *
  332.  *    *                                                *
  333.  *    **************************************************
  334.  */
  335.  
  336.  
  337. /*
  338.  *    **************************************************
  339.  *    *                                                *
  340.  *    *         Get decoded character from kbd         *
  341.  *    *                                                *
  342.  *    **************************************************
  343.  */
  344.  
  345. static int GetChar(void)
  346.  
  347. {
  348.  
  349.    int chr;
  350.  
  351.    chr = getch();
  352.    if (chr == 0)
  353.       chr = 1000 + getch();      /* Non ASCII character */
  354.    else
  355.       if (isascii(chr) )
  356.          chr = toupper(chr);
  357.  
  358.    return(chr);
  359.  
  360. }
  361.  
  362.  
  363.  
  364.  
  365. /*
  366.  *    **************************************************
  367.  *    *                                                *
  368.  *    *         Return character if key pressed        *
  369.  *    *                                                *
  370.  *    **************************************************
  371.  */
  372.  
  373. static int KeyPressed(void)
  374.  
  375. {
  376.  
  377.    if (kbhit())
  378.       return(GetChar());
  379.    else
  380.       return(0);
  381.  
  382. }
  383.  
  384.  
  385.  
  386.  
  387. /*
  388.  *    **************************************************
  389.  *    *                                                *
  390.  *    *                Screen Routines                 *
  391.  *    *                                                *
  392.  *    **************************************************
  393.  */
  394.  
  395.  
  396. /*
  397.  *    **************************************************
  398.  *    *                                                *
  399.  *    *                Clear the Screen                *
  400.  *    *                                                *
  401.  *    **************************************************
  402.  */
  403.  
  404. static void ScrClr(void)
  405.  
  406. {
  407.  
  408.    CHTYPE byte, far *pos, far *pend;
  409.  
  410.    pos = &scr->ch[0][0];
  411.    pend = pos + 2000;
  412.    byte.data = ' ';
  413.    byte.attr = 7;
  414.  
  415.    while (pos < pend)
  416.       *pos++ = byte;
  417.  
  418. }
  419.  
  420.  
  421.  
  422.  
  423. /*
  424.  *    **************************************************
  425.  *    *                                                *
  426.  *    *       Write String at row, col to Screen       *
  427.  *    *                                                *
  428.  *    **************************************************
  429.  */
  430.  
  431. static void WriteAt(int row, int col, char *str)
  432.  
  433. {
  434.  
  435.    int i, n, x, y, color, lst;
  436.  
  437.    arow = row - 1;
  438.    acol = col - 1;
  439.  
  440.    while (*str) {
  441.  
  442.       if (arow > 5 && arow < 21 && acol > 25 && acol < 53) {
  443.          color = 15;
  444.          }
  445.       else {
  446.          x = abs(39 - acol);
  447.          y = abs(12 - arow);
  448.          n = x*x + y*y;
  449.          lst = n >> 1;
  450.          for (i = 1; i < 5; i++) {
  451.             if (! lst) {
  452.                color = 0;
  453.                break;
  454.                }
  455.             color = (n / lst + lst) >> 1;
  456.             lst = color;
  457.             }
  458.          color = (color % 15) + 1;
  459.          }
  460.  
  461.       scr->ch[arow][acol].data = *(str++);
  462.       scr->ch[arow][acol].attr = (char)color;
  463.       if (++acol > 79) {
  464.          arow++;
  465.          acol = 0;
  466.          }
  467.       }
  468.  
  469. }
  470.  
  471.  
  472.  
  473.  
  474. /*
  475.  *    **************************************************
  476.  *    *                                                *
  477.  *    *            Miscellaneous Routines              *
  478.  *    *                                                *
  479.  *    **************************************************
  480.  */
  481.  
  482.  
  483. /*
  484.  *    **************************************************
  485.  *    *                                                *
  486.  *    *         Generate Random Number 0 to r          *
  487.  *    *                                                *
  488.  *    **************************************************
  489.  */
  490.  
  491. static int Random(int r)
  492.  
  493. {
  494.  
  495.    int s;
  496.    long t;
  497.  
  498.    s = rand();
  499.    t = (long) s * r / 32768;
  500.  
  501.    return((int) t);
  502.  
  503. }
  504.  
  505.  
  506.  
  507.  
  508. /*
  509.  *    **************************************************
  510.  *    *                                                *
  511.  *    *         Fetch Timer Ticks since Midnight       *
  512.  *    *                                                *
  513.  *    **************************************************
  514.  */
  515.  
  516. static long TimerTicks(void)
  517. {
  518.  
  519.    static long ticks;
  520.  
  521.    cpu.h.ah = 0;
  522.    int86(0x1A, &cpu, &cpu);
  523.  
  524.    ticks = (((long) cpu.x.cx) << 16) + ((long) cpu.x.dx);
  525.    return(ticks);
  526.  
  527. }
  528.