home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 6.ddi / SWAT.ZIP / SWAT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  13.1 KB  |  550 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2. //
  3. // swat.cpp
  4.  
  5. #include <owl.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #include "swat.h"
  11.  
  12. const int MissedPoints    = -2;
  13. const int HitPoints    = 5;
  14. const int MissedCritter    = -1;
  15. const int CritterSize    = 72;
  16. const int MaxPop    = 35;
  17. const int MaxLiveTime    = 30;
  18.  
  19. POINT Holes[ 5 ] = { { 10, 10 }, { 200, 10 },
  20.     { 100, 100 }, { 10, 200 }, { 200, 200 } };
  21.  
  22. class TApp : public TApplication
  23. {
  24. public:
  25.     TApp( LPSTR name, HINSTANCE hInstance,
  26.           HINSTANCE hPrevInstance, LPSTR lpCmd,
  27.           int nCmdShow)
  28.             : TApplication(name, hInstance,
  29.                    hPrevInstance, lpCmd, nCmdShow) {};
  30.     virtual void InitMainWindow( void );
  31. };
  32.  
  33.  
  34. struct THole {
  35.     WORD Time;
  36.     BOOL Dead;
  37. };
  38.  
  39. class TGameWindow;
  40. typedef TGameWindow* PGameWindow;
  41.  
  42. class TGameWindow : public TWindow
  43. {
  44. private:
  45.     HBITMAP Live, Dead, GameOver, ScoreBoard;
  46.     HCURSOR CursorDown, CursorUp;
  47.     int Counter, Score, LiveTime, Frequence, GameTime;
  48.     int Hits, Miss, Escaped;
  49.     BOOL IsGameOver, IsPause;
  50.     THole HoleInfo[ 5 ];
  51. public:
  52.     TGameWindow( PTWindowsObject AParent, LPSTR Title );
  53.     virtual void About( TMessage& Message ) = [ CM_FIRST + IDM_ABOUT ];
  54.     void DrawBMP( HDC DC, int X, int Y, HBITMAP BitMap );
  55.     void DrawGameOver( HDC DC );
  56.     void DrawCritter( HDC DC, int CritterNumber );
  57.     void DrawScoreBoard( HDC DC );
  58.     virtual void GetWindowClass( WNDCLASS& WndClass );
  59.     virtual void Options( TMessage& Message ) = [ CM_FIRST + IDM_OPTION ];
  60.     virtual void Paint( HDC PaintDC, PAINTSTRUCT& PaintInfo );
  61.     virtual void Pause( TMessage& Message ) = [ CM_FIRST + IDM_PAUSE ];
  62.     virtual void ResetGame( TMessage& Message ) =  [ CM_FIRST + IDM_RESET ];
  63.     virtual void SetupWindow( void );
  64.     virtual void Stop( TMessage& Message ) = [ CM_FIRST + IDM_STOP ];
  65.     void StopGame( void );
  66.     virtual void WMDestroy( TMessage& Message ) = [ WM_FIRST + WM_DESTROY ];
  67.     virtual void WMLButtonDown( TMessage& Message ) = [ WM_FIRST + WM_LBUTTONDOWN ];
  68.     virtual void WMLButtonUp( TMessage& Message ) = [ WM_FIRST + WM_LBUTTONUP ];
  69.     virtual void WMTimer( TMessage& Message ) = [ WM_FIRST + WM_TIMER ];
  70.     virtual void WMSize( TMessage& Message ) = [ WM_FIRST + WM_SIZE ];
  71.     void WriteScore( HDC DC );
  72.  
  73.     friend class TOptionDialog;
  74. };
  75.  
  76. class TOptionDialog : public TDialog
  77. {
  78. public:
  79.     TOptionDialog( PTWindowsObject AParent, LPSTR AName ) :
  80.         TDialog( AParent, AName ) {}
  81.     virtual BOOL CanClose(void);
  82.     virtual void SetupWindow( void );
  83.     virtual void WMHScroll( TMessage& Message ) = [ WM_FIRST + WM_HSCROLL ];
  84. };
  85.  
  86. // --------------- TOptionDialog ---------------
  87.  
  88. void TOptionDialog::SetupWindow( void )
  89. {
  90.     char str[ 10 ];
  91.  
  92.     TWindowsObject::SetupWindow();
  93.     SetScrollRange( GetDlgItem( HWindow, IDD_LIVETIMESB ), SB_CTL, 1,
  94.         MaxLiveTime, FALSE );
  95.     SetScrollRange( GetDlgItem( HWindow, IDD_POPSB ), SB_CTL, 1,
  96.         MaxPop, FALSE );
  97.     SetScrollPos( GetDlgItem( HWindow, IDD_LIVETIMESB ), SB_CTL,
  98.         MaxLiveTime + 1 - PGameWindow( Parent )->LiveTime, TRUE );
  99.     SetScrollPos( GetDlgItem( HWindow, IDD_POPSB ), SB_CTL,
  100.         MaxPop + 6 - PGameWindow( Parent )->Frequence, TRUE );
  101.     itoa( PGameWindow( Parent )->GameTime / 10, str, 10 );
  102.     SetDlgItemText( HWindow, IDD_INPUTEDITBOX, str );
  103. }
  104.  
  105. void TOptionDialog::WMHScroll( TMessage& Message )
  106. {
  107.     int Pos;
  108.     HWND Scroll;
  109.     const int PageStep = 10;
  110.  
  111.     Scroll = (HWND)Message.LP.Hi;
  112.     Pos = GetScrollPos( Scroll, SB_CTL );
  113.     switch ( Message.WParam )
  114.     {
  115.         case SB_LINEUP:
  116.             Pos--;
  117.             break;
  118.         case SB_LINEDOWN:
  119.             Pos++;
  120.             break;
  121.         case SB_PAGEUP:
  122.             Pos -= PageStep;
  123.             break;
  124.         case SB_PAGEDOWN:
  125.             Pos += PageStep;
  126.             break;
  127.         case SB_THUMBPOSITION:
  128.         case SB_THUMBTRACK:
  129.             Pos = Message.LP.Lo;
  130.             break;
  131.     }
  132.     SetScrollPos( Scroll, SB_CTL, Pos, TRUE );
  133. }
  134.  
  135. BOOL TOptionDialog::CanClose()
  136. {
  137.     BOOL NoError;
  138.     int Time;
  139.  
  140.     PGameWindow( Parent )->LiveTime = MaxLiveTime + 1 -
  141.         GetScrollPos( GetDlgItem( HWindow, IDD_LIVETIMESB ), SB_CTL );
  142.     PGameWindow( Parent )->Frequence = MaxPop + 1 -
  143.         GetScrollPos( GetDlgItem( HWindow, IDD_POPSB ), SB_CTL ) + 5;
  144.  
  145.     Time = GetDlgItemInt( HWindow, IDD_INPUTEDITBOX, &NoError, FALSE ) * 10;
  146.     if ( ( NoError ) && ( Time > 0 ) )
  147.     {
  148.         PGameWindow( Parent )->GameTime = Time;
  149.         return TRUE;
  150.     }
  151.     else
  152.     {
  153.         MessageBox( HWindow,
  154.             "Game Time must be a number greater than 0!",
  155.             "Error",
  156.             MB_OK );
  157.         return FALSE;
  158.     }
  159. }
  160.  
  161. // --------------- TGameWindow -----------------
  162.  
  163. TGameWindow::TGameWindow( PTWindowsObject AParent, LPSTR Title ) :
  164.     TWindow( AParent, Title )
  165. {
  166.     Attr.W = 282;
  167.     Attr.H = 400;
  168.     Attr.Style = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
  169.     randomize();
  170. };
  171.  
  172. void TGameWindow::About( TMessage& )
  173. {
  174.         GetApplication()->ExecDialog(new TDialog(this, "About"));
  175. }
  176.  
  177. void TGameWindow::DrawBMP( HDC DC, int X, int Y, HBITMAP BitMap )
  178. {
  179.     HDC MemDC;
  180.     BITMAP bm;
  181.     BOOL MadeDC;
  182.  
  183.     if ( DC == 0 )
  184.     {
  185.         DC = GetDC( HWindow );
  186.         MadeDC = TRUE;
  187.     }
  188.     else
  189.         MadeDC = FALSE;
  190.     MemDC = CreateCompatibleDC( DC );
  191.     SelectObject( MemDC, BitMap );
  192.     GetObject( GameOver, sizeof( bm ), ( LPSTR ) &bm );
  193.     BitBlt( DC, X, Y, bm.bmWidth, bm.bmHeight, MemDC, 0, 0, SRCCOPY );
  194.     DeleteDC( MemDC );
  195.     if ( MadeDC )
  196.         ReleaseDC( HWindow, DC );
  197. }
  198.  
  199. void TGameWindow::DrawGameOver( HDC DC )
  200. {
  201.     DrawBMP( DC, 10, 70, GameOver );
  202. }
  203.  
  204. void TGameWindow::DrawCritter( HDC DC, int CritterNumber )
  205. {
  206.     BOOL MadeDC;
  207.     HDC MemDC;
  208.  
  209.     if ( DC == 0 )
  210.     {
  211.         DC = GetDC( HWindow );
  212.         MadeDC = TRUE;
  213.     }
  214.     else
  215.         MadeDC = FALSE;
  216.  
  217.     if ( HoleInfo[ CritterNumber ].Time != 0 )
  218.     {
  219.         MemDC = CreateCompatibleDC( DC );
  220.         if ( HoleInfo[ CritterNumber ].Dead )
  221.             SelectObject( MemDC, Dead );
  222.         else
  223.             SelectObject( MemDC, Live );
  224.         BitBlt( DC, Holes[ CritterNumber ].x,
  225.             Holes[ CritterNumber ].y,
  226.             CritterSize, CritterSize, MemDC, 0, 0, SRCCOPY );
  227.         DeleteDC( MemDC );
  228.     }
  229.     else
  230.     {
  231.         SelectObject( DC, GetStockObject( GRAY_BRUSH ) );
  232.         SelectObject( DC, GetStockObject( NULL_PEN ) );
  233.         Rectangle( DC, Holes[ CritterNumber ].x,
  234.             Holes[ CritterNumber ].y,
  235.             Holes[ CritterNumber ].x + CritterSize + 1,
  236.             Holes[ CritterNumber ].y + CritterSize + 1 );
  237.     }
  238.     if ( MadeDC )
  239.         ReleaseDC( HWindow, DC );
  240. }
  241.  
  242. void TGameWindow::DrawScoreBoard( HDC DC )
  243. {
  244.     DrawBMP( DC, 11, 214, ScoreBoard );
  245. }
  246.  
  247. void TGameWindow::GetWindowClass( WNDCLASS& WndClass )
  248. {
  249.     TWindow::GetWindowClass( WndClass );
  250.     CursorUp = LoadCursor( GetApplication()->hInstance, "Malet" );
  251.     WndClass.style = 0;
  252.     WndClass.hCursor = CursorUp;
  253.     WndClass.hbrBackground = (HBRUSH)GetStockObject( GRAY_BRUSH );
  254.     WndClass.lpszMenuName = "SWATMENU";
  255.     WndClass.hIcon = LoadIcon( GetApplication()->hInstance, "Critter" );
  256. }
  257.  
  258. void TGameWindow::Options( TMessage& )
  259. {
  260.         GetApplication()->ExecDialog(new TOptionDialog(this, "OptionDlg"));
  261. }
  262.  
  263. void TGameWindow::Paint( HDC PaintDC, PAINTSTRUCT& )
  264. {
  265.     DrawScoreBoard( PaintDC );
  266.     WriteScore( PaintDC );
  267.     if ( IsGameOver )
  268.         DrawGameOver( PaintDC );
  269.     else {
  270.         for( int i = 0; i < 5; i++ )
  271.             DrawCritter( PaintDC, i );
  272.     }
  273. }
  274.  
  275. void TGameWindow::Pause( TMessage& )
  276. {
  277.     if ( IsGameOver )
  278.         return;
  279.     if ( IsPause )
  280.     {
  281.         IsPause = FALSE;
  282.         ModifyMenu( GetMenu( HWindow ), IDM_PAUSE, MF_BYCOMMAND,
  283.             IDM_PAUSE, "&Pause" );
  284.         DrawMenuBar( HWindow );
  285.         if ( SetTimer( HWindow, 1, 100, NULL ) == 0 )
  286.         {
  287.             MessageBox( HWindow, "No Timers Left", "Error", MB_OK );
  288.             exit( 1 );
  289.         }
  290.     }
  291.     else
  292.     {
  293.         IsPause = TRUE;
  294.         KillTimer( HWindow, 1 );
  295.         ModifyMenu( GetMenu( HWindow ), IDM_PAUSE, MF_BYCOMMAND,
  296.             IDM_PAUSE, "&Continue" );
  297.         DrawMenuBar( HWindow );
  298.     }
  299. }
  300.  
  301. void TGameWindow::ResetGame( TMessage& )
  302. {
  303.     ModifyMenu( GetMenu( HWindow ), IDM_OPTION, MF_BYCOMMAND | MF_GRAYED,
  304.         IDM_OPTION, "&Options" );
  305.     ModifyMenu( GetMenu( HWindow ), IDM_PAUSE, MF_BYCOMMAND,
  306.         IDM_PAUSE, "&Pause" );
  307.     ModifyMenu( GetMenu( HWindow ), IDM_STOP, MF_BYCOMMAND,
  308.         IDM_STOP, "&Stop" );
  309.     DrawMenuBar( HWindow );
  310.     InvalidateRect( HWindow, FALSE, TRUE );
  311.     if ( SetTimer( HWindow, 1, 100, NULL ) == 0 )
  312.     {
  313.         MessageBox( HWindow, "No Timers Left", "Error", MB_OK );
  314.         exit( 1 );
  315.     }
  316.     memset( HoleInfo, 0, sizeof( HoleInfo ) );
  317.     Counter = 0;
  318.     Score = 0;
  319.     Hits = 0;
  320.     Miss = 0;
  321.     Escaped = 0;
  322.     IsGameOver = FALSE;
  323.     if ( IsPause )
  324.     {
  325.         IsPause = FALSE;
  326.         ModifyMenu( GetMenu( HWindow ), IDM_PAUSE, MF_BYCOMMAND,
  327.             IDM_PAUSE, "&Pause" );
  328.         DrawMenuBar( HWindow );
  329.     }
  330. }
  331.  
  332. void TGameWindow::SetupWindow( void )
  333. {
  334.     CursorDown = LoadCursor( GetApplication()->hInstance, "MaletDown" );
  335.     Live = LoadBitmap( GetApplication()->hInstance, "Live" );
  336.     Dead = LoadBitmap( GetApplication()->hInstance, "Dead" );
  337.     GameOver = LoadBitmap( GetApplication()->hInstance, "GameOver" );
  338.     ScoreBoard = LoadBitmap( GetApplication()->hInstance, "Board" );
  339.     IsGameOver = TRUE;
  340.     IsPause = FALSE;
  341.     LiveTime = 10;
  342.     Frequence = 20;
  343.     Counter = 0;
  344.     Score = 0;
  345.     Hits = 0;
  346.     Miss = 0;
  347.     Escaped = 0;
  348.     GameTime = 150;        // fifteen seconds
  349. }
  350.  
  351. void TGameWindow::Stop( TMessage& )
  352. {
  353.     StopGame();
  354. }
  355.  
  356. void TGameWindow::StopGame( void )
  357. {
  358.     KillTimer( HWindow, 1 );
  359.     ModifyMenu( GetMenu( HWindow ), IDM_OPTION, MF_BYCOMMAND,
  360.         IDM_OPTION, "&Options" );
  361.     ModifyMenu( GetMenu( HWindow ), IDM_PAUSE, MF_BYCOMMAND | MF_GRAYED,
  362.         IDM_PAUSE, "&Pause" );
  363.     ModifyMenu( GetMenu( HWindow ), IDM_STOP, MF_BYCOMMAND | MF_GRAYED,
  364.         IDM_STOP, "&Stop" );
  365.     IsPause = FALSE;
  366.     DrawMenuBar( HWindow );
  367.     IsGameOver = TRUE;
  368.     InvalidateRect( HWindow, FALSE, TRUE );
  369.     Counter = GameTime;
  370. }
  371.  
  372. void TGameWindow::WMDestroy( TMessage& Message )
  373. {
  374.     DeleteObject( Live );
  375.     DeleteObject( Dead );
  376.     DeleteObject( GameOver );
  377.     DeleteObject( ScoreBoard );
  378.     KillTimer( HWindow, 1 );
  379.     TWindow::WMDestroy( Message );
  380. }
  381.  
  382. void TGameWindow::WMLButtonDown( TMessage& Message )
  383. {
  384.     POINT Point;
  385.     RECT R;
  386.     BOOL Hit;
  387.  
  388.     SetClassWord( HWindow, GCW_HCURSOR, (WORD)CursorDown );
  389.     GetCursorPos( &Point );
  390.     SetCursorPos( Point.x, Point.y );
  391.     if ( IsGameOver || IsPause )
  392.         return;
  393.     Hit = FALSE;
  394.     for( int i = 0; i < 5; i++ )
  395.     {
  396.         if (!( ( HoleInfo[ i ].Dead ) || ( HoleInfo[ i ].Time == 0 ) ) )
  397.         {
  398.             R.top = Holes[ i ].x;
  399.             R.left = Holes[ i ].y;
  400.             R.bottom = R.top + CritterSize;
  401.             R.right = R.left + CritterSize;
  402.             Point.x = Message.LP.Hi;
  403.             Point.y = Message.LP.Lo;
  404. #pragma warn -stv
  405.             if ( PtInRect( &R, Point ) )
  406. #pragma warn +stv
  407.             {
  408.                 Score += HitPoints;
  409.                 HoleInfo[ i ].Dead = TRUE;
  410.                 HoleInfo[ i ].Time = Counter + 2 * LiveTime;
  411.                 Hits++;
  412.                 Hit = TRUE;
  413.                 DrawCritter( 0, i );
  414.             }
  415.         }
  416.     }
  417.     if ( !Hit )
  418.     {
  419.         Score += MissedPoints;
  420.         Miss++;
  421.     }
  422.     WriteScore( 0 );
  423. }
  424.  
  425. void TGameWindow::WMLButtonUp( TMessage& )
  426. {
  427.     POINT Point;
  428.  
  429.     SetClassWord( HWindow, GCW_HCURSOR, (WORD)CursorUp );
  430.     GetCursorPos( &Point );
  431.     SetCursorPos( Point.x, Point.y );
  432. }
  433.  
  434. void TGameWindow::WMTimer( TMessage& )
  435. {
  436.     int i;
  437.  
  438.     Counter++;
  439.     i = random( Frequence );
  440.     if ( i < 5 )
  441.     {
  442.         if ( HoleInfo[ i ].Time == 0 )
  443.         {
  444.             HoleInfo[ i ].Time = Counter + LiveTime;
  445.             HoleInfo[ i ].Dead = FALSE;
  446.             DrawCritter( 0, i );
  447.         }
  448.     }
  449.     for ( i = 0; i < 5; i++ )
  450.     {
  451.         if ( ( Counter > HoleInfo[ i ].Time ) &&
  452.             ( HoleInfo[ i ].Time != 0 ) )
  453.         {
  454.             HoleInfo[ i ].Time = 0;
  455.             if ( ! HoleInfo[ i ].Dead )
  456.             {
  457.                 Score += MissedCritter;
  458.                 Escaped++;
  459.             }
  460.             DrawCritter( 0, i );
  461.         }
  462.     }
  463.     WriteScore( 0 );
  464.     if ( Counter >= GameTime )
  465.         StopGame();
  466. }
  467.  
  468. void TGameWindow::WMSize( TMessage& )
  469. {
  470.     if ( IsGameOver )
  471.         return;
  472.     if ( IsIconic( HWindow ) )
  473.         KillTimer( HWindow, 1 );
  474.     else
  475.         if ( ! IsPause )
  476.             if ( SetTimer( HWindow, 1, 100, NULL ) == 0 )
  477.             {
  478.                 MessageBox( HWindow, "No Timers Left",
  479.                     "Error", MB_OK );
  480.                 exit( 1 );
  481.             }
  482. }
  483.  
  484. void TGameWindow::WriteScore( HDC DC )
  485. {
  486.     char s[ 21 ];
  487.     BOOL MadeDC;
  488.  
  489.     if ( DC == 0 )
  490.     {
  491.         MadeDC = TRUE;
  492.         DC = GetDC( HWindow );
  493.     }
  494.     else
  495.         MadeDC = FALSE;
  496.     SelectObject( DC, CreateSolidBrush( 0x8080 ) );
  497.     SelectObject( DC, GetStockObject( NULL_PEN ) );
  498.     SetBkMode( DC, TRANSPARENT );
  499.  
  500.     // Timer
  501.     Rectangle( DC, 130, 252, 163, 275 );
  502.     sprintf( s, "%3.3d", GameTime - Counter );
  503.     s[ 3 ] = s[ 2 ];
  504.     s[ 2 ]= '.';
  505.     TextOut( DC, 130, 252, s, 4 );
  506.  
  507.     // Hits
  508.     Rectangle( DC, 40, 310, 71, 329 );
  509.     sprintf( s, "%3.3d", Hits );
  510.     TextOut( DC, 40, 310, s, strlen( s ) );
  511.  
  512.     // Misses
  513.     Rectangle( DC, 77, 310, 117, 329 );
  514.     sprintf( s, "%3.3d", Miss );
  515.     TextOut( DC, 77, 310, s, strlen( s ) );
  516.  
  517.     // Escaped
  518.     Rectangle( DC, 133, 310, 174, 329 );
  519.     sprintf( s, "%3.3d", Escaped );
  520.     TextOut( DC, 133, 310, s, strlen( s ) );
  521.  
  522.     // Total
  523.     Rectangle( DC, 203, 310, 239, 328 );
  524.     sprintf( s, "%3.3d", Score );
  525.     TextOut( DC, 203, 310, s, strlen( s ) );
  526.  
  527.     DeleteObject( SelectObject( DC, GetStockObject( GRAY_BRUSH ) ) );
  528.     SelectObject( DC, GetStockObject( NULL_PEN ) );
  529.     if ( MadeDC )
  530.         ReleaseDC( HWindow, DC );
  531. }
  532.  
  533. // --------------- TApp ------------------------
  534.  
  535. void TApp::InitMainWindow( void )
  536. {
  537.     MainWindow = new TGameWindow( NULL, "Swat!" );
  538. }
  539.  
  540. // -------------Main Program--------------------
  541.  
  542. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  543.            LPSTR lpCmd, int nCmdShow)
  544. {
  545.     TApp App( "SwatGame", hInstance, hPrevInstance,
  546.         lpCmd, nCmdShow);
  547.     App.Run();
  548.     return ( App.Status );
  549. }
  550.