home *** CD-ROM | disk | FTP | other *** search
- #include <win32/windows.h>
-
- #include <AutoPtr.h>
- #include <String.hpp>
- #include <Converter.h>
- #include <Log/LogPtr.h>
-
- #include <2D/Interface2D.h>
- #include <2D/Hardware2DSoft.h>
- #include <2D/Hardware2DDirect.h>
-
- #include <2D/Sequences.h>
- #include <safe_new.h>
-
- static Hardware2D * volatile hardware = 0;
- static Interface2D * volatile inter = 0;
- static volatile HWND MainHWnd = NULL;
- static volatile bool end_game = false;
- static volatile HANDLE GameThread = NULL;
-
- DWORD WINAPI GameThreadProc( void * param )
- {
- int old_tick_count = GetTickCount();
-
- while( !end_game )
- {
- if( !inter )
- break;
- int dt = GetTickCount() - old_tick_count;
- old_tick_count = GetTickCount();
-
- if( !inter->life_cycle( dt < 500 ? dt/1000.0 : 0.5 ) )
- PostMessage( MainHWnd, WM_CLOSE, 0, 0 );
- inter->render();
- if( dt < 10 )
- Sleep( 10 - dt );
- // SetWindowText( MainHWnd, (const char *)inter->get_caption() );
- }
- return 0;
- }
-
- void do_deinit()
- {
- end_game = true;
- DWORD result = WaitForSingleObject( GameThread, 1000 );
- if( result != WAIT_OBJECT_0 )
- {
- LogPtr()->error("Game loop has not finished gracefully\n");
- }
- CloseHandle( GameThread ); GameThread = NULL;
-
- delete inter; inter = NULL;
- delete hardware; hardware = NULL;
- if( unsigned cc = Sequences::cached_count() )
- {
- LogPtr()->warning( String("in WinMain() Sequences::cached_count() == ") + Converter::convert( cc ) );
- }
-
- LogPtr::destroy();
- }
-
- LRESULT CALLBACK WindowProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
- {
- int x_pos = LOWORD( lParam );
- int y_pos = HIWORD( lParam );
- switch (msg)
- {
- case WM_ACTIVATE:
- {
- int active = LOWORD(wParam);
- int fMinimized = (BOOL) HIWORD(wParam); // minimized flag
- switch( active )
- {
- case WA_ACTIVE:
- case WA_CLICKACTIVE:
- ResumeThread( GameThread );
- break;
- case WA_INACTIVE:
- SuspendThread( GameThread );
- break;
- }
- }
- break;
- case WM_KEYDOWN:
- if ( inter )
- inter->key_down( wParam );
- break;
- case WM_KEYUP:
- if ( inter )
- inter->key_up( wParam );
- break;
- case WM_PAINT:
- {
- PAINTSTRUCT paint;
- HDC hdc = BeginPaint( hwnd, &paint );
-
- EndPaint( hwnd, &paint );
- }
- return 0;
- case WM_CLOSE:
- do_deinit();
- DestroyWindow( hwnd );
- break;
- case WM_DESTROY:
- PostQuitMessage(TRUE);
- break;
- }
- return DefWindowProc( hwnd, msg, wParam, lParam);
- }
-
- HWND do_init( HINSTANCE hInstance, int nCmdShow )
- {
- WNDCLASS wc;
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = WindowProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon( hInstance, NULL);
- wc.hCursor = NULL;
- wc.hbrBackground = CreateSolidBrush( RGB( 88, 128, 192 ) );
- wc.lpszMenuName = 0;
- wc.lpszClassName = "Hrissan";
- RegisterClass( &wc );
-
- HWND wnd = CreateWindow(
- "Hrissan",
- "Hrissan",
- WS_OVERLAPPED | WS_SYSMENU,
- 0, 0,
- CW_USEDEFAULT, CW_USEDEFAULT,
- NULL, NULL, hInstance, NULL );
- return wnd;
- }
-
- int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine, int nCmdShow )
- {
- srand( GetTickCount() );
-
- MainHWnd = do_init( hInstance, nCmdShow );
- if( !MainHWnd )
- {
- LogPtr()->error("WinMain() Failed to create winfow");
- return 1;
- }
-
- hardware = new Hardware2DSoft( MainHWnd );
- // hardware = new Hardware2DDirect( MainHWnd );
-
- ShowWindow( MainHWnd, true );
- UpdateWindow( MainHWnd );
-
- inter = create_game( hardware );
-
- SetWindowText( MainHWnd, inter->get_caption().c_str() );
-
- DWORD game_thread_id = 0;
- GameThread = CreateThread( NULL, 0, GameThreadProc, 0, 0, &game_thread_id );
- if( !GameThread )
- {
- LogPtr()->error("WinMain() Failed to create game thread");
- return 0;
- }
-
- MSG msg;
- while( GetMessage( &msg, NULL, 0, 0 ) )
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
-
- return 0;
- }
-
- int main(void) // ─δ Ωε∞∩Φδ ≥ε≡α BCC5.5
- {
- HINSTANCE h = GetModuleHandle(NULL);
- return WinMain( h, NULL, GetCommandLine(), SW_SHOWNORMAL );
- }