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

  1. //-------------------------------------------------------------------
  2. //
  3. //    Program:        Simple state machine simulation
  4. //    Filename:        TERMITE.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 "windows.h"
  18. #include "termite.h"
  19. #include "fileopen.h"
  20.  
  21. #include <string.h>
  22.  
  23. HANDLE    hInst;
  24.  
  25. short    xClient, yClient;
  26.  
  27. char    szFileName[128] = "Default.mit";
  28.  
  29. char    szProgramName[] = "Termite";
  30.  
  31.  
  32. /*-----------------------------------------------------------------*/
  33. /*
  34. /*    Name:            WinMain
  35. /*    Description:
  36. /*
  37. /*-----------------------------------------------------------------*/
  38.  
  39. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  40.     HANDLE hInstance;
  41.     HANDLE hPrevInstance;
  42.     LPSTR lpCmdLine;
  43.     int nCmdShow;
  44. {
  45.     MSG        msg;
  46.     HWND    hWnd;
  47.     HANDLE    hAcc;
  48.     short    xScreen, yScreen;
  49.  
  50.  
  51.     if ( !hPrevInstance )
  52.         if ( !InitApplication( hInstance ) )
  53.             return FALSE;
  54.  
  55.     hInst = hInstance;            /* Save instance handle */
  56.  
  57.     // Create a window for the termite to live in.  Make it
  58.     // half the height and width of the screen.
  59.  
  60.     xScreen = GetSystemMetrics( SM_CXSCREEN );
  61.     yScreen = GetSystemMetrics( SM_CYSCREEN );
  62.  
  63.     hWnd = CreateWindow(
  64.         "TermiteWClass",
  65.         szProgramName,
  66.         WS_OVERLAPPEDWINDOW,
  67.         xScreen/4, yScreen/4,
  68.         xScreen/2, yScreen/2,
  69.         NULL, NULL, hInstance, NULL
  70.     );
  71.  
  72.     if ( !hWnd )
  73.         return FALSE;
  74.  
  75.     ShowWindow( hWnd, nCmdShow );
  76.     UpdateWindow( hWnd );
  77.  
  78.     // Load the accelerators for this program.
  79.  
  80.     hAcc = LoadAccelerators( hInstance, "TermiteAcc" );
  81.  
  82.     // Start main message loop.  Note that the loop uses
  83.     // PeekMessage to check for messages.  If no message
  84.     // is found the termites are updated.  Otherwise,
  85.     // normal message processing occurs...
  86.  
  87.     while ( TRUE )
  88.     {
  89.         if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  90.         {
  91.             if ( msg.message == WM_QUIT )
  92.                 break;
  93.  
  94.             if ( !TranslateAccelerator( hWnd, hAcc, &msg ) )
  95.             {
  96.                 TranslateMessage( &msg );
  97.                 DispatchMessage( &msg );
  98.             }
  99.         }
  100.         else
  101.             HandleTermites( hWnd );
  102.     }
  103.  
  104.     return ( msg.wParam );
  105. }
  106.  
  107. /*-----------------------------------------------------------------*/
  108. /*
  109. /*    Name:            InitApplication
  110. /*    Description:
  111. /*
  112. /*-----------------------------------------------------------------*/
  113.  
  114. BOOL InitApplication(hInstance)
  115.     HANDLE hInstance;
  116. {
  117.     WNDCLASS    wc;
  118.  
  119.     wc.style            = CS_HREDRAW | CS_VREDRAW;
  120.     wc.lpfnWndProc        = MainWndProc;
  121.     wc.cbClsExtra        = 0;
  122.     wc.cbWndExtra        = 0;
  123.     wc.hInstance        = hInstance;
  124.     wc.hIcon            = LoadIcon( hInst, szProgramName );
  125.     wc.hCursor            = LoadCursor( NULL, IDC_ARROW );
  126.     wc.hbrBackground    = GetStockObject( BLACK_BRUSH );
  127.     wc.lpszMenuName        = "TermiteMenu";
  128.     wc.lpszClassName    = "TermiteWClass";
  129.  
  130.     return ( RegisterClass( &wc ) );
  131. }
  132.  
  133. /*-----------------------------------------------------------------*/
  134. /*
  135. /*    Name:            MainWndProc
  136. /*    Description:
  137. /*
  138. /*-----------------------------------------------------------------*/
  139.  
  140. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  141.     HWND hWnd;
  142.     unsigned message;
  143.     WORD wParam;
  144.     LONG lParam;
  145. {
  146.     HPEN        hPenLgrey, hPenDgrey, hOldPen;
  147.     FARPROC        lpProcAbout, lpOpenDlg;
  148.     char        szText[128];
  149.     HMENU        hMenu;
  150.     RECT        rect;
  151.     HDC            hDC;
  152.     PAINTSTRUCT ps;
  153.  
  154.  
  155.     switch ( message )
  156.     {
  157.         case WM_CREATE:
  158.  
  159.             GetClientRect( hWnd, &rect );
  160.  
  161.             xClient = rect.right - rect.left;
  162.             yClient = rect.bottom - rect.top;
  163.  
  164.             LoadTermiteTables( szFileName );
  165.  
  166.             if ( LoadTermiteTables( szFileName ) == 0 )
  167.                  wsprintf( szText, "%s - %s", (LPSTR)szProgramName, (LPSTR)szFileName );
  168.             else
  169.                 wsprintf( szText, "%s - <No File>", (LPSTR)szProgramName );
  170.  
  171.             SetWindowText( hWnd, szText );
  172.  
  173.             break;
  174.  
  175.         case WM_SIZE:
  176.  
  177.             xClient = LOWORD(lParam);
  178.             yClient = HIWORD(lParam);
  179.  
  180.             ClipTermites();
  181.  
  182.             InvalidateRect(hWnd,NULL,TRUE);
  183.  
  184.             break;
  185.  
  186.         case WM_COMMAND:
  187.  
  188.             switch ( wParam )
  189.             {
  190.                 case IDM_OPEN:
  191.  
  192.                     if ( GetFileName( hInst, hWnd, "*.mit", szText ) )
  193.                     {
  194.                         strcpy( szFileName, szText );
  195.  
  196.                         InvalidateRect( hWnd, NULL, TRUE );
  197.                         SendMessage( hWnd, WM_CREATE, 0, 0L );
  198.                     }
  199.  
  200.                     break;
  201.  
  202.                 case IDM_INS:
  203.  
  204.                     InsertTermite();
  205.                     break;
  206.  
  207.                 case IDM_DEL:
  208.  
  209.                     DeleteTermite();
  210.                     break;
  211.  
  212.                 case IDM_RESTART:
  213.  
  214.                     InvalidateRect( hWnd, NULL, TRUE );
  215.                     SendMessage( hWnd, WM_CREATE, 0, 0L );
  216.                     break;
  217.  
  218.                 case IDM_PAUSE:
  219.  
  220.                     hMenu = GetMenu( hWnd );
  221.  
  222.                     if ( pauseFlag )
  223.                         CheckMenuItem( hMenu, IDM_PAUSE, MF_UNCHECKED );
  224.                     else
  225.                         CheckMenuItem( hMenu, IDM_PAUSE, MF_CHECKED );
  226.  
  227.                     pauseFlag = !pauseFlag;
  228.  
  229.                     break;
  230.  
  231.                 case IDM_ABOUT:
  232.  
  233.                     lpProcAbout = MakeProcInstance( AboutDlgWndProc, hInst );
  234.  
  235.                     DialogBox( hInst, "AboutBox", hWnd, lpProcAbout );
  236.  
  237.                     FreeProcInstance( lpProcAbout );
  238.  
  239.                     break;
  240.  
  241.                 case IDM_EXIT:
  242.  
  243.                     SendMessage( hWnd, WM_DESTROY, 0, 0L );
  244.  
  245.                     break;
  246.             }
  247.  
  248.             break;
  249.  
  250.         case WM_DESTROY:
  251.  
  252.             PostQuitMessage( 0 );
  253.  
  254.             break;
  255.  
  256.         default:
  257.  
  258.             return DefWindowProc( hWnd, message, wParam, lParam );
  259.     }
  260.  
  261.     return 0L;
  262. }
  263.  
  264. /*-----------------------------------------------------------------*/
  265. /*
  266. /*    Name:            AboutDlgWndProc
  267. /*    Description:
  268. /*
  269. /*-----------------------------------------------------------------*/
  270.  
  271. BOOL FAR PASCAL AboutDlgWndProc(hDlg, message, wParam, lParam)
  272.     HWND hDlg;
  273.     unsigned message;
  274.     WORD wParam;
  275.     LONG lParam;
  276. {
  277.     switch ( message )
  278.     {
  279.         case WM_INITDIALOG:
  280.  
  281.             return TRUE;
  282.  
  283.         case WM_COMMAND:
  284.  
  285.         if ( wParam == IDOK || wParam == IDCANCEL )
  286.         {
  287.             EndDialog( hDlg, TRUE );
  288.             return TRUE;
  289.         }
  290.  
  291.         break;
  292.     }
  293.  
  294.     return FALSE;
  295. }
  296.  
  297.