//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: LgDirectDrawVideo.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of LgDirectDrawVideo.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "LgDirectDrawVideo.h" CLgDirectDrawVideo::CLgDirectDrawVideo() { m_pSurface = NULL; memset( m_pText, 0, sizeof(m_pText) ); m_hFont = NULL; m_cColor = 0; } CLgDirectDrawVideo::~CLgDirectDrawVideo() { OnDestroy(); } void CLgDirectDrawVideo::OnDestroy() { __try { // delete old surface if( m_pSurface ) { delete m_pSurface; m_pSurface = NULL; } // destroy display objects m_Display.DestroyObjects(); } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::OnDestroy"), _T("Exception 0x%08X"), _exception_code() ); } } BOOL CLgDirectDrawVideo::OnSize( HWND hWnd ) { __try { // destroy old OnDestroy(); // get window sizes RECT rect; GetClientRect( hWnd, &rect ); int nWidth = rect.right - rect.left; int nHeight = rect.bottom - rect.top; // create direct draw HRESULT hr = m_Display.CreateWindowedDisplay( hWnd, nWidth, nHeight ); if( FAILED( hr ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDraw::OnSize") , _T("CreateWindowedDisplay failed with 0x%08X"), hr ); return FALSE; } m_Display.Clear( 0 ); // Create a video surface hr = m_Display.CreateSurface( &m_pSurface, nWidth, nHeight ); if( FAILED( hr ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::OnSize") , _T("CreateSurface failed with 0x%08X"), hr ); return FALSE; } m_pSurface->Clear( 0 ); } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::OnSize"), _T("Exception 0x%08X"), _exception_code() ); return FALSE; } return TRUE; } BOOL CLgDirectDrawVideo::OnPaint( BOOL bStretch ) { __try { // Update the screen if we need to refresh. This case occurs // when in windowed mode and the window is behind others. // The app will not be active, but it will be visible. UpdateDirectDrawVideoDIB(); // Display the new position of the sprite if( DisplayFrame( bStretch ) == DDERR_SURFACELOST ) { // If the surfaces were lost, then restore and try again HRESULT hr = RestoreSurfaces(); if( FAILED(hr) ) return FALSE; DisplayFrame( bStretch ); } } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::OnPaint"), _T("Exception 0x%08X"), _exception_code() ); return FALSE; } return TRUE; } //----------------------------------------------------------------------------- // Name: DisplayFrame() // Desc: Blts a the sprites to the back buffer, then it blts or flips the // back buffer onto the primary buffer. //----------------------------------------------------------------------------- HRESULT CLgDirectDrawVideo::DisplayFrame( BOOL bStretch ) { __try { HRESULT hr; // Fill the back buffer with black, ignoring errors until the flip // m_Display.Clear( 0 ); m_Display.UpdateBounds(); // Blt the help text on the backbuffer, ignoring errors until the flip if( bStretch ) m_Display.Blt( m_pSurface ); else { LPBITMAPINFOHEADER pBIH = GetDirectDrawVideoDIB(); if( pBIH ) { RECT wRect; ::GetClientRect( m_Display.GetHWnd(), &wRect ); SIZE vSize = { pBIH->biWidth, pBIH->biHeight }; SIZE wSize = { wRect.right - wRect.left, wRect.bottom - wRect.top }; RECT vRect; vRect.left = wRect.left + ( wSize.cx - vSize.cx ) / 2; vRect.top = wRect.top + ( wSize.cy - vSize.cy ) / 2; vRect.right = vRect.left + vSize.cx; vRect.bottom = vRect.top + vSize.cy; RECT sRect; ::IntersectRect( &sRect, &vRect, &wRect ); if( wRect.right - wRect.left > vSize.cx || wRect.bottom - wRect.top > vSize.cy ) m_Display.Blt( sRect.left, sRect.top, m_pSurface, NULL ); else m_Display.Blt( sRect.left, sRect.top, m_pSurface, &sRect ); } else { return S_OK; } } RECT rect; ::GetClientRect( m_Display.GetHWnd(), &rect ); m_Display.BackBufferDrawText( m_hFont, m_pText, &rect, DT_SINGLELINE | DT_CENTER | DT_BOTTOM, m_cColor ); // We are in windowed mode so perform a blt from the backbuffer // to the primary, returning any errors like DDERR_SURFACELOST if( FAILED( hr = m_Display.Present() ) ) return hr; } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::DisplayFrame"), _T("Exception 0x%08X"), _exception_code() ); return 0x80000000; } return S_OK; } //----------------------------------------------------------------------------- // Name: RestoreSurfaces() // Desc: Restore all the surfaces, and redraw the sprite surfaces. //----------------------------------------------------------------------------- HRESULT CLgDirectDrawVideo::RestoreSurfaces() { __try { HRESULT hr = m_Display.RestoreAllSurfaces(); if( FAILED( hr ) ) { OnSize( m_Display.GetHWnd() ); } UpdateDirectDrawVideoDIB(); } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::RestoreSurfaces"), _T("Exception 0x%08X"), _exception_code() ); } return S_OK; } void CLgDirectDrawVideo::UpdateDirectDrawVideoDIB() { __try { if( m_pSurface ) { SIZE size = m_pSurface->GetSize(); LPBITMAPINFOHEADER pBIH = GetDirectDrawVideoDIB(); if( pBIH ) { if( size.cx != pBIH->biWidth || size.cy != pBIH->biHeight ) { delete m_pSurface; m_pSurface = NULL; // Create a video surface HRESULT hr = m_Display.CreateSurface( &m_pSurface, pBIH->biWidth, pBIH->biHeight ); if( FAILED( hr ) || !m_pSurface ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::UpdateDirectDrawVideoDIB") , _T("CreateSurface failed with 0x%08X"), hr ); return; } } if( m_pSurface ) m_pSurface->DrawBitmap( &GetDirectDrawVideoDIB() ); } } } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::UpdateDirectDrawVideoDIB"), _T("Exception 0x%08X"), _exception_code() ); } } void CLgDirectDrawVideo::SetDirectDrawVideoText( LPCTSTR pText, HFONT hFont, COLORREF cColor ) { _tcsncpy( m_pText, pText, MAX_PATH ); m_hFont = hFont; m_cColor = cColor; }