home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 April / Game.EXE_04_2002.iso / Alawar / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-02  |  3.4 KB  |  158 lines

  1. #include <windows.h>
  2.  
  3. #include "AutoPtr.h"
  4. #include "String.h"
  5. #include "Resource.h"
  6. #include "PictureFormat.h"
  7.  
  8. #include "ResourceManagerPtr.h"
  9. #include "PictureFormatManagerPtr.h"
  10. #include "LogPtr.h"
  11.  
  12. #include "Interface2D.h"
  13. #include "SoftwareHardware2D.h"
  14.  
  15. #include "Sequences.h"
  16. #include "safe_new.h"
  17.  
  18. static Hardware2D * hardware = 0;
  19. static Interface2D * inter = 0;
  20. static HWND MainHWnd = NULL;
  21.  
  22. void CALLBACK display(void)
  23. {
  24.     static int old_tick_count = GetTickCount();
  25.  
  26.     int dt = GetTickCount() - old_tick_count;
  27.     old_tick_count = GetTickCount();
  28.  
  29.     if( inter )
  30.     {
  31.         if( !inter->life_cycle( dt < 500 ? dt : 500 ) )
  32.                 PostMessage( MainHWnd, WM_CLOSE, 0, 0 );
  33.         inter->render();
  34.     }
  35.     if( dt < 10 )
  36.         Sleep( 10 - dt );
  37.     SetWindowText( MainHWnd, (const char *)inter->get_caption() );
  38. }
  39.  
  40. bool is_mouse_down = false;
  41.  
  42. LRESULT CALLBACK WindowProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
  43. {
  44.     int x_pos = LOWORD( lParam );
  45.     int y_pos = HIWORD( lParam );
  46.     switch (msg) 
  47.     {
  48.     case WM_PAINT:
  49.         {
  50.             PAINTSTRUCT paint;
  51.             HDC hdc = BeginPaint( hwnd, &paint );
  52.             
  53.             EndPaint( hwnd, &paint );
  54.         }
  55.         return 0;
  56.     case WM_CLOSE:
  57.         delete inter; inter = NULL;
  58.         delete hardware; hardware = NULL;
  59.         DestroyWindow( hwnd );
  60.         break;
  61.     case WM_DESTROY:
  62.         PostQuitMessage(TRUE);
  63.         break;
  64.     }
  65.     return DefWindowProc( hwnd, msg, wParam, lParam);
  66. }
  67.  
  68. HWND do_init( HINSTANCE hInstance, int nCmdShow )
  69. {
  70.     WNDCLASS            wc;
  71.     wc.style = CS_HREDRAW | CS_VREDRAW;
  72.     wc.lpfnWndProc = WindowProc;
  73.     wc.cbClsExtra = 0;
  74.     wc.cbWndExtra = 0;
  75.     wc.hInstance = hInstance;
  76.     wc.hIcon = LoadIcon( hInstance, NULL);
  77.     wc.hCursor = NULL;
  78.     wc.hbrBackground = CreateSolidBrush( RGB( 88, 128, 192 ) );
  79.     wc.lpszMenuName = 0;
  80.     wc.lpszClassName = "Hrissan";
  81.     RegisterClass( &wc );
  82.     
  83.     HWND wnd = CreateWindow(
  84.         "Hrissan",
  85.         "Hrissan",
  86.         WS_OVERLAPPED | WS_SYSMENU,
  87.         0, 0,
  88.         CW_USEDEFAULT, CW_USEDEFAULT,
  89.         NULL, NULL, hInstance, NULL );
  90.     return wnd;
  91. }
  92.  
  93. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  94.     LPTSTR lpCmdLine, int nCmdShow )
  95. {
  96.     srand( GetTickCount() );
  97.  
  98.     MainHWnd = do_init( hInstance, nCmdShow );
  99.     if( !MainHWnd )
  100.     {
  101.         LogPtr()->error("WinMain() Failed to create winfow");
  102.         return 1;
  103.     }
  104.  
  105.     hardware = new SoftwareHardware2D( MainHWnd );
  106.  
  107.     ShowWindow( MainHWnd, true );
  108.     UpdateWindow( MainHWnd );
  109.  
  110.     inter = create_game( hardware );
  111.  
  112.     unsigned frames_done = 0;
  113.     while( 1 )
  114.     {
  115.         MSG msg;
  116.         if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) ) 
  117.         {
  118.             if( !GetMessage( &msg, NULL, 0, 0 ) ) 
  119.             {
  120.                 break ;
  121.             }
  122.             TranslateMessage(&msg);
  123.             DispatchMessage(&msg);
  124.         } 
  125.         else
  126.         {
  127.             display();
  128.             ++frames_done;
  129.         }
  130.     }
  131.  
  132.     if( inter )
  133.     {
  134.         delete inter; inter = NULL;
  135.         LogPtr()->warning( "After Main Loop Interface2D is still valid" );
  136.     }
  137.     if( hardware )
  138.     {
  139.         delete hardware; hardware = NULL;
  140.         LogPtr()->warning( "After Main Loop Hardware2D is still valid" );
  141.     }
  142.  
  143.     if( unsigned cc = Sequences::cached_count() )
  144.     {
  145.         LogPtr()->warning( String("in WinMain() Sequences::cached_count() == ") + String::convert( cc ) );
  146.     }
  147.  
  148.     LogPtr::destroy();
  149.     ResourceManagerPtr::destroy();
  150.     PictureFormatManagerPtr::destroy();
  151.     return 0;
  152. }
  153.  
  154. int main(void) // ─δ  Ωε∞∩Φδ ≥ε≡α BCC5.5
  155. {
  156.     HINSTANCE h = GetModuleHandle(NULL);
  157.     return WinMain( h, NULL, GetCommandLine(), SW_SHOWNORMAL );
  158. }