home *** CD-ROM | disk | FTP | other *** search
/ Windows Shareware GOLD / NuclearComputingVol3No1.cdr / _bbs2 / f1246.zip / TERMTOOL.C < prev   
C/C++ Source or Header  |  1990-09-20  |  9KB  |  384 lines

  1. //-------------------------------------------------------------------
  2. //
  3. //    Program:        Simple state machine simulation
  4. //    Filename:        TERMTOOL.C
  5. //    Description:
  6. //
  7. //        This program uses state machine theory to generate
  8. //        'termites'.  These termites move within the window
  9. //        using their individual state tables.
  10. //
  11. //    Author:            Hans D. Kellner
  12. //    Version:        1.0
  13. //    Notes:            none
  14. //
  15. //-------------------------------------------------------------------
  16.  
  17. #include <stdio.h>
  18.  
  19. #include "windows.h"
  20. #include "termite.h"
  21.  
  22.     /* These are the values to add for each direction index. */
  23.  
  24. int    xMove[4] = {0,0,1,-1};
  25. int    yMove[4] = {-1,1,0,0};
  26.  
  27.     // The following tables store the state information read from
  28.     // the input file.
  29.  
  30. int        stateTable[MAX_STATES][MAX_STATES],
  31.         colorTable[MAX_STATES][MAX_STATES],
  32.         motionTable[MAX_STATES][MAX_STATES];
  33.  
  34.     // These tables hold the current termites state values and
  35.     // locations on the screen.
  36.  
  37. int        miteX[MAX_MITES],
  38.         miteY[MAX_MITES],
  39.         direction[MAX_MITES],
  40.         state[MAX_MITES];
  41.  
  42. int        noFile = TRUE,            // True if no file has been loaded
  43.         pauseFlag = FALSE;        // True if user has paused termites
  44.  
  45. int        termiteCount;            // Current number of termites
  46.  
  47. DWORD    rgbColorTable[MAX_COLORS];
  48.  
  49.     // Local prototypes
  50.  
  51. int RGB2Color(DWORD);
  52. int moveMite(int *, int *, int *, int, int);
  53.  
  54. /*-----------------------------------------------------------------*/
  55. /*
  56. /*    Name:            RGB2Color
  57. /*    Description:
  58. /*
  59. /*        Convert an RGB value back into an array index.
  60. /*
  61. /*-----------------------------------------------------------------*/
  62.  
  63. int RGB2Color(rgbColor)
  64.     DWORD rgbColor;
  65. {
  66.     int    color;
  67.  
  68.     for ( color = 0; color < MAX_COLORS; color ++ )
  69.     {
  70.         if ( rgbColorTable[color] == rgbColor )
  71.             return color;
  72.     }
  73.  
  74.     return 0;
  75. }
  76.  
  77. /*-----------------------------------------------------------------*/
  78. /*
  79. /*    Name:            InitTermiteData
  80. /*    Description:
  81. /*
  82. /*        Do like the name says... Initialize the termites data.
  83. /*
  84. /*-----------------------------------------------------------------*/
  85.  
  86. InitTermiteData()
  87. {
  88.     int        index;
  89.     short    r,g,b;
  90.  
  91.     noFile = TRUE;
  92.     pauseFlag = FALSE;
  93.  
  94.     termiteCount = 1;
  95.  
  96.     /* Initialize the data for each Termite */
  97.  
  98.     for ( index = 0; index < termiteCount; index ++ )
  99.     {
  100.         state[index] = 0;                    /* Start in state 0 */
  101.  
  102.         direction[index] = NORTH;            /* heading north */
  103.  
  104.         miteX[index] = (int)xClient / 2;    /* and in the center of the window */
  105.         miteY[index] = (int)yClient / 2;
  106.     }
  107.  
  108.     /* Create some default colors for color table */
  109.  
  110.     rgbColorTable[0]  = RGB(    0,   0, 128 );
  111.     rgbColorTable[1]  = RGB(    0, 128,   0 );
  112.     rgbColorTable[2]  = RGB(  128,   0,   0 );
  113.     rgbColorTable[3]  = RGB(    0, 128, 128 );
  114.     rgbColorTable[4]  = RGB(  128,   0, 128 );
  115.     rgbColorTable[5]  = RGB(  128, 128,   0 );
  116.     rgbColorTable[6]  = RGB(  128, 128, 128 );
  117.     rgbColorTable[7]  = RGB(    0,   0, 255 );
  118.     rgbColorTable[8]  = RGB(    0, 255,   0 );
  119.     rgbColorTable[9]  = RGB(  255,   0,   0 );
  120.     rgbColorTable[10] = RGB(    0, 255, 255 );
  121.     rgbColorTable[11] = RGB(  255,   0, 255 );
  122.     rgbColorTable[12] = RGB(  255, 255,   0 );
  123.     rgbColorTable[13] = RGB(  255, 255, 255 );
  124.  
  125.     return 0;
  126. }
  127.  
  128. /*-----------------------------------------------------------------*/
  129. /*
  130. /*    Name:            ClipTermites
  131. /*    Description:
  132. /*
  133. /*        When the window size has changed this routine is called.
  134. /*        It moves through the list of termites and adjusts their
  135. /*        position if they no longer are within the window limits.
  136. /*
  137. /*-----------------------------------------------------------------*/
  138.  
  139. ClipTermites()
  140. {
  141.     int    index;
  142.  
  143.     for ( index = 0; index < termiteCount; index ++ )
  144.     {
  145.         if ( miteX[index] >= xClient )
  146.             miteX[index] = xClient / 2;
  147.  
  148.         if ( miteY[index] >= yClient )
  149.             miteY[index] = yClient / 2;
  150.     }
  151.  
  152.     return 0;
  153. }
  154.  
  155. /*-----------------------------------------------------------------*/
  156. /*
  157. /*    Name:            LoadTermiteTables
  158. /*    Description:
  159. /*
  160. /*        Using the filename given, load the termite data into
  161. /*        the program.
  162. /*
  163. /*-----------------------------------------------------------------*/
  164.  
  165. LoadTermiteTables( szFileName )
  166.     char *szFileName;
  167. {
  168.     int        row, col, rowCount, colCount;
  169.     FILE    *fp;
  170.  
  171.     InitTermiteData();
  172.  
  173.     /* Open the data file, exit if unable to open */
  174.  
  175.     fp = fopen( szFileName, "r" );
  176.  
  177.     if ( fp == NULL ) return( -1 );
  178.  
  179.     if ( fscanf( fp, "%d %d", &rowCount, &colCount ) != 2 )
  180.         goto loadError;
  181.  
  182.     if ( ( rowCount < 1 || rowCount > MAX_STATES ) ||
  183.          ( colCount < 1 || colCount > MAX_STATES ) )
  184.         goto loadError;
  185.  
  186.     /* Read the state table */
  187.  
  188.     for ( row = 0; row < rowCount; row ++ )
  189.         for ( col = 0; col < colCount; col ++ )
  190.             if ( fscanf( fp, "%d", &stateTable[row][col] ) != 1 )
  191.                 goto loadError;
  192.  
  193.     /* Read the color table */
  194.  
  195.     for ( row = 0; row < rowCount; row ++ )
  196.         for ( col = 0; col < colCount; col ++ )
  197.             if ( fscanf( fp, "%d", &colorTable[row][col] ) != 1 )
  198.                 goto loadError;
  199.  
  200.     /* Read the motion table */
  201.  
  202.     for ( row = 0; row < rowCount; row ++ )
  203.         for ( col = 0; col < colCount; col ++ )
  204.             if ( fscanf( fp, "%d", &motionTable[row][col] ) != 1 )
  205.                 goto loadError;
  206.  
  207.     fclose( fp );
  208.     noFile = FALSE;
  209.     return 0;
  210.  
  211. loadError:
  212.  
  213.     fclose( fp );
  214.     return -1;
  215. }
  216.  
  217. /*-----------------------------------------------------------------*/
  218. /*
  219. /*    Name:            InsertTermite
  220. /*    Description:
  221. /*
  222. /*        Add a new termite to the list.  Give it default values.
  223. /*
  224. /*-----------------------------------------------------------------*/
  225.  
  226. InsertTermite()
  227. {
  228.     if ( termiteCount < MAX_MITES )
  229.     {
  230.         state[termiteCount] = 0;                /* Start in state 0 */
  231.         direction[termiteCount] = NORTH;        /* heading north */
  232.  
  233.         miteX[termiteCount] = xClient / 2;        /* and in the center */
  234.         miteY[termiteCount] = yClient / 2;        /* of the window */
  235.  
  236.         termiteCount ++;
  237.     }
  238.  
  239.     return( termiteCount );
  240. }
  241.  
  242. /*-----------------------------------------------------------------*/
  243. /*
  244. /*    Name:            DeleteTermite
  245. /*    Description:
  246. /*
  247. /*        Just need to decrement the number of termites.  This
  248. /*        will cause the last termite in the list to be lost.
  249. /*
  250. /*-----------------------------------------------------------------*/
  251.  
  252. DeleteTermite()
  253. {
  254.     if ( termiteCount > 1 )
  255.         termiteCount --;
  256.  
  257.     return termiteCount;
  258. }
  259.  
  260. /*-----------------------------------------------------------------*/
  261. /*
  262. /*    Name:            moveMite
  263. /*    Description:
  264. /*
  265. /*        Moves the Termite depending on the direction stored in
  266. /*        the motion table.
  267. /*
  268. /*-----------------------------------------------------------------*/
  269.  
  270. moveMite(x, y, direction, state, color)
  271.     int *x, *y, *direction, state, color;
  272. {
  273.     /* Determine if the Termite is turning left or right. */
  274.  
  275.     switch ( motionTable[state][color] )
  276.     {
  277.         case LEFT:    /* Adjust 90' depending on current direction */
  278.  
  279.             switch ( *direction )
  280.             {
  281.                 case NORTH:
  282.                     *direction = WEST; break;
  283.                 case WEST:
  284.                     *direction = SOUTH; break;
  285.                 case SOUTH:
  286.                     *direction = EAST; break;
  287.                 default:
  288.                 case EAST:
  289.                     *direction = NORTH; break;
  290.             }
  291.  
  292.             break;
  293.  
  294.         case RIGHT:    /* Adjust -90' depending on current direction */
  295.  
  296.             switch ( *direction )
  297.             {
  298.                 case NORTH:
  299.                     *direction = EAST; break;
  300.                 case SOUTH:
  301.                     *direction = WEST; break;
  302.                 case EAST:
  303.                     *direction = SOUTH; break;
  304.                 default:
  305.                 case WEST:
  306.                     *direction = NORTH; break;
  307.             }
  308.  
  309.             break;
  310.     }
  311.  
  312.     *x = *x + xMove[*direction];        /* Move to new location */
  313.     *y = *y + yMove[*direction];
  314.  
  315.     /* Check for wrapping at window borders.  Adjust if needed. */
  316.  
  317.     if (*x < 0)
  318.         *x = (int)xClient-1;
  319.     else if (*x >= xClient)
  320.         *x = 0;
  321.  
  322.     if (*y < 0)
  323.         *y = (int)yClient-1;
  324.     else if (*y >= yClient)
  325.         *y = 0;
  326.  
  327.     return 0;
  328. }
  329.  
  330. /*-----------------------------------------------------------------*/
  331. /*
  332. /*    Name:            HandleTermites
  333. /*    Description:
  334. /*
  335. /*        This routine is called from the main message loop whenever
  336. /*        there was no message in the queue.  It loops through the
  337. /*        each termite and updates it position.
  338. /*
  339. /*-----------------------------------------------------------------*/
  340.  
  341. void HandleTermites( hWnd )
  342.     HWND    hWnd;
  343. {
  344.     int        index, color, newColor;
  345.     short    xPos, yPos, r, g, b;
  346.     DWORD    rgbColor;
  347.     HBRUSH    hBrush;
  348.     HDC        hDC;
  349.  
  350.  
  351.     if ( pauseFlag || noFile )
  352.         return;
  353.  
  354.     hDC = GetDC( hWnd );
  355.  
  356.     /* Move each Termite */
  357.  
  358.     for ( index = 0; index < termiteCount; index ++ )
  359.     {
  360.         /* Get the current color at the current position */
  361.  
  362.         rgbColor = GetPixel( hDC, miteX[index], miteY[index] );
  363.  
  364.         color = RGB2Color( rgbColor );
  365.  
  366.         /* Find new color from color table and set it at current position */
  367.  
  368.         newColor = colorTable[state[index]][color];
  369.  
  370.         SetPixel( hDC, miteX[index], miteY[index], rgbColorTable[newColor] );
  371.  
  372.         /* Now, move the Termite */
  373.  
  374.         moveMite( &miteX[index], &miteY[index], &direction[index], state[index], color );
  375.  
  376.         /* Get next state */
  377.  
  378.         state[index] = stateTable[state[index]][color];
  379.     }
  380.  
  381.     ReleaseDC( hWnd, hDC );
  382. }
  383.  
  384.