home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2002 June / Game.EXE_06_2002.iso / Alawar / Lib / 2D / mainForGameExe.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-26  |  4.3 KB  |  191 lines

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