home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 May / Game.EXE_05_2002.iso / Alawar / src / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-09  |  4.0 KB  |  180 lines

  1. #include <win32/windows.h>
  2.  
  3. #include <AutoPtr.h>
  4. #include <String.hpp>
  5. #include <Converter.h>
  6. #include <Log/LogPtr.h>
  7.  
  8. #include <2D/Interface2D.h>
  9. #include <2D/Hardware2DSoft.h>
  10. #include <2D/Hardware2DDirect.h>
  11.  
  12. #include <2D/Sequences.h>
  13. #include <safe_new.h>
  14.  
  15. static Hardware2D * volatile hardware = 0;
  16. static Interface2D * volatile inter = 0;
  17. static volatile HWND MainHWnd = NULL;
  18. static volatile bool end_game = false;
  19. static volatile HANDLE GameThread = NULL;
  20.  
  21. DWORD WINAPI GameThreadProc( void * param )
  22. {
  23.     int old_tick_count = GetTickCount();
  24.  
  25.     while( !end_game )
  26.     {
  27.         if( !inter )
  28.             break;
  29.         int dt = GetTickCount() - old_tick_count;
  30.         old_tick_count = GetTickCount();
  31.  
  32.         if( !inter->life_cycle( dt < 500 ? dt/1000.0 : 0.5 ) )
  33.             PostMessage( MainHWnd, WM_CLOSE, 0, 0 );
  34.         inter->render();
  35.         if( dt < 10 )
  36.             Sleep( 10 - dt );
  37. //        SetWindowText( MainHWnd, (const char *)inter->get_caption() );
  38.     }
  39.     return 0;
  40. }
  41.  
  42. void do_deinit()
  43. {
  44.     end_game = true;
  45.     DWORD result = WaitForSingleObject( GameThread, 1000 );
  46.     if( result != WAIT_OBJECT_0 )
  47.     {
  48.         LogPtr()->error("Game loop has not finished gracefully\n");
  49.     }
  50.     CloseHandle( GameThread ); GameThread = NULL;
  51.  
  52.     delete inter; inter = NULL;
  53.     delete hardware; hardware = NULL;
  54.     if( unsigned cc = Sequences::cached_count() )
  55.     {
  56.         LogPtr()->warning( String("in WinMain() Sequences::cached_count() == ") + Converter::convert( cc ) );
  57.     }
  58.  
  59.     LogPtr::destroy();
  60. }
  61.  
  62. LRESULT CALLBACK WindowProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
  63. {
  64.     int x_pos = LOWORD( lParam );
  65.     int y_pos = HIWORD( lParam );
  66.     switch (msg) 
  67.     {
  68.     case WM_ACTIVATE:
  69.         {
  70.             int active = LOWORD(wParam);
  71.             int fMinimized = (BOOL) HIWORD(wParam); // minimized flag 
  72.             switch( active )
  73.             {
  74.             case WA_ACTIVE:
  75.             case WA_CLICKACTIVE:
  76.                 ResumeThread( GameThread );
  77.                 break;
  78.             case WA_INACTIVE:
  79.                 SuspendThread( GameThread );
  80.                 break;
  81.             }
  82.         }
  83.         break;
  84.     case WM_KEYDOWN:
  85.         if ( inter )
  86.             inter->key_down( wParam );
  87.         break;
  88.     case WM_KEYUP:
  89.         if ( inter )
  90.             inter->key_up( wParam );
  91.         break;
  92.     case WM_PAINT:
  93.         {
  94.             PAINTSTRUCT paint;
  95.             HDC hdc = BeginPaint( hwnd, &paint );
  96.             
  97.             EndPaint( hwnd, &paint );
  98.         }
  99.         return 0;
  100.     case WM_CLOSE:
  101.         do_deinit();
  102.         DestroyWindow( hwnd );
  103.         break;
  104.     case WM_DESTROY:
  105.         PostQuitMessage(TRUE);
  106.         break;
  107.     }
  108.     return DefWindowProc( hwnd, msg, wParam, lParam);
  109. }
  110.  
  111. HWND do_init( HINSTANCE hInstance, int nCmdShow )
  112. {
  113.     WNDCLASS            wc;
  114.     wc.style = CS_HREDRAW | CS_VREDRAW;
  115.     wc.lpfnWndProc = WindowProc;
  116.     wc.cbClsExtra = 0;
  117.     wc.cbWndExtra = 0;
  118.     wc.hInstance = hInstance;
  119.     wc.hIcon = LoadIcon( hInstance, NULL);
  120.     wc.hCursor = NULL;
  121.     wc.hbrBackground = CreateSolidBrush( RGB( 88, 128, 192 ) );
  122.     wc.lpszMenuName = 0;
  123.     wc.lpszClassName = "Hrissan";
  124.     RegisterClass( &wc );
  125.     
  126.     HWND wnd = CreateWindow(
  127.         "Hrissan",
  128.         "Hrissan",
  129.         WS_OVERLAPPED | WS_SYSMENU,
  130.         0, 0,
  131.         CW_USEDEFAULT, CW_USEDEFAULT,
  132.         NULL, NULL, hInstance, NULL );
  133.     return wnd;
  134. }
  135.  
  136. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  137.     LPTSTR lpCmdLine, int nCmdShow )
  138. {
  139.     srand( GetTickCount() );
  140.  
  141.     MainHWnd = do_init( hInstance, nCmdShow );
  142.     if( !MainHWnd )
  143.     {
  144.         LogPtr()->error("WinMain() Failed to create winfow");
  145.         return 1;
  146.     }
  147.  
  148.     hardware = new Hardware2DSoft( MainHWnd );
  149. //    hardware = new Hardware2DDirect( MainHWnd );
  150.  
  151.     ShowWindow( MainHWnd, true );
  152.     UpdateWindow( MainHWnd );
  153.  
  154.     inter = create_game( hardware );
  155.  
  156.     SetWindowText( MainHWnd, inter->get_caption().c_str() );
  157.  
  158.     DWORD game_thread_id = 0;
  159.     GameThread = CreateThread( NULL, 0, GameThreadProc, 0, 0, &game_thread_id );
  160.     if( !GameThread )
  161.     {
  162.         LogPtr()->error("WinMain() Failed to create game thread");
  163.         return 0;
  164.     }
  165.  
  166.     MSG msg;
  167.     while( GetMessage( &msg, NULL, 0, 0 ) )
  168.     {
  169.         TranslateMessage(&msg);
  170.         DispatchMessage(&msg);
  171.     }
  172.  
  173.     return 0;
  174. }
  175.  
  176. int main(void) // ─δ  Ωε∞∩Φδ ≥ε≡α BCC5.5
  177. {
  178.     HINSTANCE h = GetModuleHandle(NULL);
  179.     return WinMain( h, NULL, GetCommandLine(), SW_SHOWNORMAL );
  180. }