home *** CD-ROM | disk | FTP | other *** search
/ CD Direkt: Spezial 1 / CDD_SPIELE_.ISO / wingames / life / life.c < prev    next >
C/C++ Source or Header  |  1994-03-15  |  35KB  |  963 lines

  1. /****************************************************************************
  2. *
  3. *  FILE:          LIFE.C
  4. *
  5. *  DESCRIPTION:   Windows 3.0 Version of the classic simulation of "LIFE".
  6. *                 This program incorporates a minor twist in the basic
  7. *                 algorithm by utilizing color to represent various
  8. *                 life "phases".  The final phase of any life cycle is
  9. *                 always death!
  10. *
  11. *                 This software is hereby placed in the public domain.
  12. *                 Use and abuse it at your own risk!
  13. *
  14. *
  15. *  AUTHOR:        Tom Wheeler
  16. *                 31294 Morlock
  17. *                 Livonia, Mich.  48152
  18. *                 [72037,1742]
  19. *
  20. *****************************************************************************
  21. *
  22. *     DATE        VER                     DESCRIPTION
  23. *  ----------     ---     ---------------------------------------------------
  24. *   05/20/91      1.0     Initial Development 1.0
  25. *   05/22/91      1.1     Added Color Support
  26. *
  27. ****************************************************************************/
  28.  
  29. #include <windows.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include "life.h"
  35.  
  36. #define CLASSNAME       "LifeClass"
  37. #define APPNAME         "Life"
  38.  
  39. #define DEAD              0         /* state: cell is dead                 */
  40. #define ALIVE           0x1         /* state: cell is alive                */
  41. #define CHILD           0x2         /* state: cell is a child              */
  42. #define ADOLESCENT      0x4         /* state: cell is an adolescent        */
  43. #define ADULT           0x8         /* state: cell is an adult             */
  44. #define MIDAGED        0x10         /* state: cell is middle aged          */
  45. #define RETIRED        0x20         /* state: cell is retirement age       */
  46. #define SENIOR         0x40         /* state: cell is a senior citizen     */
  47. #define METHUSULA      0x80         /* state: cell is to be terminated     */
  48. #define AGECYCLE         10         /* # of cycles till next age           */
  49.  
  50. #define MAX_XCELL       100         /* max number of horizontal cells      */
  51. #define MAX_YCELL       100         /* max number of vertical cells        */
  52. #define DEFAULT_WIDTH    15         /* default number of cells to display  */
  53. #define DEFAULT_HEIGHT   15         /* default number of cells to display  */
  54. #define CYCLE_TIMER       1
  55.  
  56. /* Macro to get a random integer within a specified range */
  57. #define getrandom( min, max ) ((rand() % (int)(((max)+1) - (min))) + (min))
  58.  
  59. typedef unsigned char UCHAR;
  60.  
  61. /* MATRIX defines the cell matrix used to represent the grid of entities.
  62.  * Life states are represented as the low byte of the matrix entry, the high
  63.  * byte is used to is used to keep a count representing the age of the
  64.  * individual entities.
  65.  */
  66. typedef int MATRIX[MAX_XCELL+2][MAX_YCELL+2];
  67.  
  68. typedef struct _Life {
  69.    int CellX;                       /* individual cell width               */
  70.    int CellY;                       /* individual cell height              */
  71.    int TopCellX;                    /* horiz. id of first cell displayed   */
  72.    int TopCellY;                    /* vert. id of frist cell displayed    */
  73.    int CellsPerPageX;               /* num of visible X cells on screen    */
  74.    int CellsPerPageY;               /* num of visible Y cells on screen    */
  75.    BOOL Grid;                       /* grid on/off state                   */
  76.    BOOL Run;                        /* run state of matrix                 */
  77.    MATRIX Matrix;                   /* state matrix                        */
  78. }LIFE;
  79. typedef LIFE *PLIFE;
  80.  
  81. MATRIX Scratch;                     /* temporary cell matrix               */
  82. HWND hInst = NULL;                    /* instance variable                   */
  83. HMENU hMenuFrame = NULL;               /* instance menu handle                */
  84. HWND hWndFrame = NULL;              /* instance window handle              */
  85. HANDLE hAccelFrame = NULL;          /* instance keyboard accelerator       */
  86. HANDLE hMatrix = NULL;              /* handle to cell matrix structure     */
  87.  
  88. HPEN hBluePen = NULL;               /* Blue drawing pen                    */
  89. HPEN hGreenPen = NULL;              /* Green drawing pen                   */
  90. HPEN hCyanPen = NULL;               /* Cyan drawing pen                    */
  91. HPEN hRedPen = NULL;                /* Red drawing pen                     */
  92. HPEN hMagentaPen = NULL;            /* Magenta drawing pen                 */
  93. HPEN hBrownPen = NULL;              /* Brown drawing pen                   */
  94. HPEN hGrayPen = NULL;               /* Gray drawing pen                    */
  95. HPEN hLightRedPen = NULL;           /* Light Red drawing pen               */
  96. HPEN hWhitePen = NULL;              /* White drawing pen                   */
  97.  
  98. HBRUSH hBlueBrush = NULL;           /* Solid Blue brush                    */
  99. HBRUSH hGreenBrush = NULL;          /* Solid Green brush                   */
  100. HBRUSH hCyanBrush = NULL;           /* Solid Cyan brush                    */
  101. HBRUSH hRedBrush = NULL;            /* Solid Red brush                     */
  102. HBRUSH hMagentaBrush = NULL;        /* Solid Magenta brush                 */
  103. HBRUSH hBrownBrush = NULL;          /* Solid Brown brush                   */
  104. HBRUSH hGrayBrush = NULL;           /* Solid Gray brush                    */
  105. HBRUSH hLightRedBrush = NULL;       /* Solid Light Red brush               */
  106. HBRUSH hWhiteBrush = NULL;          /* Solid White brush                   */
  107.  
  108. PLIFE pLife = NULL;                 /* pointer to cell life structure      */
  109. int iSmall;                          /* size in pixels of a small cell      */
  110. int iNorm;                           /* size in pixels of a normal cell     */
  111. int iLarge;                         /* size in pixels of a large cell      */
  112. int iCycleTime = 250;               /* default timer (milliseconds)        */
  113. WORD wCycleTimer = 0;               /* timer used for life cycling         */
  114.  
  115. /****************************************************************************
  116. *  void DrawGridLines(HDC hDC,RECT *rect)
  117. *
  118. *  Description:   DrawsGridLines on the Life Display Window
  119. *
  120. *  Input:         hDC   - Device Context to draw on
  121. *                 rect  - pointer to client rectangle
  122. *
  123. *  Output:        N/A
  124. ****************************************************************************/
  125. void DrawGridLines(HDC hDC,RECT *rect)
  126. {
  127.    int iX,iY;
  128.  
  129.    SelectObject(hDC,hGrayPen);
  130.    for(iX = pLife->CellX; iX < rect->right; iX += pLife->CellX) {
  131.       MoveTo(hDC,iX,0);
  132.       LineTo(hDC,iX,rect->bottom);
  133.    }
  134.    for(iY = pLife->CellY; iY < rect->bottom; iY += pLife->CellY) {
  135.       MoveTo(hDC,0,iY);
  136.       LineTo(hDC,rect->right,iY);
  137.    }
  138. }
  139.  
  140. /****************************************************************************
  141. *  void DrawCell(HDC hMouseMoveDC,int iXPos,int iYPos,UCHAR ucState)
  142. *
  143. *  Description:   Draws the cell at the indicated grid position in the color
  144. *                 appropriate to its current state.
  145. *
  146. *  Input:         hDC       - Device Context to draw on
  147. *                 iXPos
  148. *                 iYPos    - Screen Matrix position to draw at
  149. *                 ucState  - State to draw
  150. *
  151. *  Output:        N/A
  152. ****************************************************************************/
  153. void DrawCell(HDC hDC,int iXPos,int iYPos,UCHAR ucState)
  154. {
  155.    int iX,iY;
  156.    HPEN hPen;
  157.    HBRUSH hBrush;
  158.  
  159.    iX = iXPos * pLife->CellX;
  160.    iY = iYPos * pLife->CellY;
  161.    if(ucState & ALIVE) {
  162.       if(ucState & SENIOR) {
  163.          hPen = hBluePen;
  164.          hBrush = hBlueBrush;
  165.       }
  166.       else if(ucState & RETIRED) {
  167.          hPen = hBrownPen;
  168.          hBrush = hBrownBrush;
  169.       }
  170.       else if(ucState & MIDAGED) {
  171.          hPen = hMagentaPen;
  172.          hBrush = hMagentaBrush;
  173.       }
  174.       else if(ucState & ADULT) {
  175.          hPen = hRedPen;
  176.          hBrush = hRedBrush;
  177.       }
  178.       else if(ucState & ADOLESCENT) {
  179.          hPen = hCyanPen;
  180.          hBrush = hCyanBrush;
  181.       }
  182.       else if(ucState & CHILD) {
  183.          hPen = hGreenPen;
  184.          hBr