home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / GLEN / KALEIDS.ZIP / KALEIDST.C < prev    next >
Text File  |  1988-09-08  |  15KB  |  503 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 <graphics.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. void main(void);
  49. void Initialize(void);
  50.  
  51. int  GetChar(void);
  52. int  KeyPressed(void);
  53.  
  54. void ScrClr(void);
  55. void WriteAt(int row,int col,char *str);
  56.  
  57. long TimerTicks(void);
  58.  
  59.  
  60.  
  61.  
  62. /*
  63.  *    **************************************************
  64.  *    *                                                *
  65.  *    *                    Defines                     *
  66.  *    *                                                *
  67.  *    **************************************************
  68.  */
  69.  
  70. #define TRUE  1
  71. #define FALSE 0
  72. #define ESC  27
  73. #define COLORSCREEN 0xB8000000L  /* Color buffer address */
  74.  
  75.  
  76.  
  77.  
  78. /*
  79.  *    **************************************************
  80.  *    *                                                *
  81.  *    *                  Data Types                    *
  82.  *    *                                                *
  83.  *    **************************************************
  84.  */
  85.  
  86. typedef struct {                 /* Screen char struct */
  87.    char
  88.       data,
  89.       attr;
  90.    } CHTYPE;
  91.  
  92. typedef struct {                 /* Screen memory structure */
  93.    CHTYPE ch[25][80];
  94.    } far *SCREENTYPE;
  95.  
  96.  
  97.  
  98.  
  99. /*
  100.  *    **************************************************
  101.  *    *                                                *
  102.  *    *                Global Variables                *
  103.  *    *                                                *
  104.  *    **************************************************
  105.  */
  106.  
  107. int X1, Y1, X2, Y2, XV1, YV1, XV2, YV2, HC, XA, YA, XB, YB, chr;
  108. int i, bc = 1, dely = 0;
  109. int wid = 4, hgt = 3;
  110. int CX = 320, CY = 240, M = 240;
  111.  
  112. int Red[MAXCOLORS+1], Green[MAXCOLORS+1], Blue[MAXCOLORS+1];
  113. long dummy = 0;
  114.  
  115. SCREENTYPE                       /* Screen pointer  */
  116.    scr;
  117.  
  118. int
  119.    arow = 0,                     /* Absolute row    */
  120.    acol = 0;                     /* Absolute column */
  121.  
  122. union REGS
  123.    cpu;                          /* CPU registers for int86() */
  124.  
  125. struct timeb TIME;               /* Time structure */
  126.  
  127.  
  128.  
  129.  
  130. /*
  131.  *    **************************************************
  132.  *    *                                                *
  133.  *    *                     Main                       *
  134.  *    *                                                *
  135.  *    **************************************************
  136.  */
  137.  
  138. void main()
  139. {
  140.  
  141.  
  142.    Initialize();
  143.  
  144.    while (TRUE) {
  145.       cleardevice();
  146.  
  147.       if (bc)
  148.          setrgbpalette(0, 0, 0, 0);
  149.       for (i = bc; i <= MAXCOLORS; i++) {
  150.          Red[i] = random(64);
  151.          Green[i] = random(64);
  152.          Blue[i] = random(64);
  153.          setrgbpalette(i, Red[i], Green[i], Blue[i]);
  154.          }
  155.  
  156.       X1 = random(M) + 1;
  157.       X2 = random(M) + 1;
  158.       Y1 = random(X1);
  159.       Y2 = random(X2);
  160.  
  161.       while (random(130) > 1) {
  162.          XV1 = random(5) - 2;
  163.          XV2 = random(5) - 2;
  164.          YV1 = random(5) - 2;
  165.          YV2 = random(5) - 2;
  166.          HC = random(16);
  167.          setcolor(HC);
  168.  
  169.          while (random(30) > 1) {
  170.             XA = X1 * wid / hgt;
  171.             XB = X2 * wid / hgt;
  172.             YA = Y1 * wid / hgt;
  173.             YB = Y2 * wid / hgt;
  174.             line( (CX+XA), (CY-Y1), (CX+XB), (CY-Y2) );
  175.             line( (CX-YA), (CY+X1), (CX-YB), (CY+X2) );
  176.             line( (CX-XA), (CY-Y1), (CX-XB), (CY-Y2) );
  177.             line( (CX-YA), (CY-X1), (CX-YB), (CY-X2) );
  178.             line( (CX-XA), (CY+Y1), (CX-XB), (CY+Y2) );
  179.             line( (CX+YA), (CY-X1), (CX+YB), (CY-X2) );
  180.             line( (CX+XA), (CY+Y1), (CX+XB), (CY+Y2) );
  181.             line( (CX+YA), (CY+X1), (CX+YB), (CY+X2) );
  182.             X1 = (X1 + XV1) % M;
  183.             Y1 = (Y1 + YV1) % M;
  184.             X2 = (X2 + XV2) % M;
  185.             Y2 = (Y2 + YV2) % M;
  186.             if (dely) {
  187.                for (i = 1; i < dely; i++) {
  188.                   dummy += i * dely;
  189.                   dummy -= dely;
  190.                   }
  191.                }
  192.             }
  193.  
  194.          if (chr = KeyPressed()) {
  195.             if (chr == 'P') {
  196.                GetChar();
  197.                }
  198.             else if (chr == ESC) {
  199.                cleardevice();
  200.                closegraph();
  201.                exit(0);
  202.                }
  203.             else if (chr == 'C') {
  204.                break;
  205.                }
  206.             else if (chr == 'B') {
  207.                bc = 1 - bc;
  208.                break;
  209.                }
  210.             else if (isdigit(chr)) {
  211.                dely = ('9' - chr) * 1000;
  212.                }
  213.             }
  214.  
  215.          }  /* while (line) */
  216.  
  217.       }  /* while (screen) */
  218.  
  219.    closegraph();
  220.    exit(0);
  221.  
  222. }
  223.  
  224.  
  225.  
  226.  
  227. /*
  228.  *    **************************************************
  229.  *    *                                                *
  230.  *    *                  Initialize                    *
  231.  *    *                                                *
  232.  *    **************************************************
  233.  */
  234.  
  235. void Initialize()
  236. {
  237.  
  238.    int GraphDriver, GraphMode;
  239.    long endtime;
  240.  
  241.    cpu.h.ah = 15;                   /* Get current video mode */
  242.    int86(16, &cpu, &cpu);
  243.    if (cpu.h.al != 3) {             /* 80x25 color */
  244.       puts("Display must be 80x25 color");
  245.       exit(1);
  246.       }
  247.    scr = (SCREENTYPE)((void far *)COLORSCREEN); /* Screen memory pointer */
  248.  
  249.    ftime(&TIME);                    /* Initialize random number generator */
  250.    srand(TIME.millitm);
  251.  
  252.    ScrClr();
  253.  
  254.    WriteAt( 1, 2, "KaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledis");
  255.    WriteAt( 2, 2, "KaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledis");
  256.    WriteAt( 3, 2, "KaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledis");
  257.    WriteAt( 4, 2, "KaledisKaledisKaledisKal┌───────────────────────────┐disKaledisKaledisKaledis");
  258.    WriteAt( 5, 2, "KaledisKaledisKaledisKal│  K A L E I D O S C O P E  │disKaledisKaledisKaledis");
  259.    WriteAt( 6, 2, "KaledisKaledisKaledisKal│                           │disKaledisKaledisKaledis");
  260.    WriteAt( 7, 2, "KaledisKaledisKaledisKal│  Version 3.1 for EGA/VGA  │disKaledisKaledisKaledis");
  261.    WriteAt( 8, 2, "KaledisKaledisKaledisKal│                           │disKaledisKaledisKaledis");
  262.    WriteAt( 9, 2, "KaledisKaledisKaledisKal│    Judson D. McClendon    │disKaledisKaledisKaledis");
  263.    WriteAt(10, 2, "KaledisKaledisKaledisKal│    329 37th Court N.E.    │disKaledisKaledisKaledis");
  264.    WriteAt(11, 2, "KaledisKaledisKaledisKal│    Birmingham, AL 35215   │disKaledisKaledisKaledis");
  265.    WriteAt(12, 2, "KaledisKaledisKaledisKal│       205-853-8440        │disKaledisKaledisKaledis");
  266.    WriteAt(13, 2, "KaledisKaledisKaledisKal│  Compuserve [74415,1003]  │disKaledisKaledisKaledis");
  267.    WriteAt(14, 2, "KaledisKaledisKaledisKal│                           │disKaledisKaledisKaledis");
  268.    WriteAt(15, 2, "KaledisKaledisKaledisKal│  Press: to:               │disKaledisKaledisKaledis");
  269.    WriteAt(16, 2, "KaledisKaledisKaledisKal│     'P' pause             │disKaledisKaledisKaledis");
  270.    WriteAt(17, 2, "KaledisKaledisKaledisKal│     'C' clear screen      │disKaledisKaledisKaledis");
  271.    WriteAt(18, 2, "KaledisKaledisKaledisKal│     'B' toggle background │disKaledisKaledisKaledis");
  272.    WriteAt(19, 2, "KaledisKaledisKaledisKal│ '0'-'9' set speed         │disKaledisKaledisKaledis");
  273.    WriteAt(20, 2, "KaledisKaledisKaledisKal│  ESCape exit              │disKaledisKaledisKaledis");
  274.    WriteAt(21, 2, "KaledisKaledisKaledisKal│ any key begin/resume...   │disKaledisKaledisKaledis");
  275.    WriteAt(22, 2, "KaledisKaledisKaledisKal└───────────────────────────┘disKaledisKaledisKaledis");
  276.    WriteAt(23, 2, "KaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledis");
  277.    WriteAt(24, 2, "KaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledis");
  278.    WriteAt(25, 2, "KaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledisKaledis");
  279.  
  280.    cpu.h.ah = 2;                    /* Move cursor to row 0, col 0 */
  281.    cpu.h.bh = 0;
  282.    cpu.x.dx = 0;
  283.    int86(16, &cpu, &cpu);
  284.    scr->ch[0][0].attr = 0;          /* Set cursor color to black on black */
  285.  
  286.    do {                             /* Kaleidoscope title */
  287.       for (arow = 0; arow < 25; arow++)
  288.          for (acol = 1; acol < 78; acol++)
  289.             if (arow < 6 || arow > 20 || acol < 26 || acol > 52)
  290.                if (!(--scr->ch[arow][acol].attr))
  291.                   scr->ch[arow][acol].attr = 15;
  292.  
  293.       endtime = TimerTicks() + 1;   /* Wait one tick */
  294.       while (TimerTicks() < endtime)
  295.          ;
  296.       } while (! (chr = KeyPressed()) );
  297.  
  298.    ScrClr();
  299.  
  300.    if (chr == ESC) {
  301.       exit(0);
  302.       }
  303.  
  304.    registerbgidriver(EGAVGA_driver);
  305.    GraphDriver = VGA;
  306.    GraphMode = VGAHI;
  307.    initgraph(&GraphDriver, &GraphMode, "");     /* Try VGA 640x480x16 color */
  308.    if (graphresult()) {
  309.       CY = 175;
  310.       wid = 9;
  311.       hgt = 5;
  312.       GraphDriver = EGA;
  313.       GraphMode = EGAHI;
  314.       initgraph(&GraphDriver, &GraphMode, "");  /* Try EGA 640x350x16 color */
  315.       if (graphresult()) {
  316.          puts("Display must be EGA or VGA");
  317.          closegraph();
  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. 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. 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. 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. 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.  *    *         Fetch Timer Ticks since Midnight       *
  487.  *    *                                                *
  488.  *    **************************************************
  489.  */
  490.  
  491. long TimerTicks(void)
  492. {
  493.  
  494.    static long ticks;
  495.  
  496.    cpu.h.ah = 0;
  497.    int86(0x1A, &cpu, &cpu);
  498.  
  499.    ticks = (((long) cpu.x.cx) << 16) + ((long) cpu.x.dx);
  500.    return(ticks);
  501.  
  502. }
  503.