//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: LgUIMapBackgroundThread.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of LgUIMapBackgroundThread.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "..\Version.h" #include "LgUIMapBackgroundThread.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif CLgUIMapBackgroundThread::CLgUIMapBackgroundThread() { m_hMutexData = NULL; m_hMutexDraw = NULL; m_hEventDraw = NULL; m_WndRect.SetRectEmpty(); m_DstRect.SetRectEmpty(); m_hWnd = NULL; // log info LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgUIMapBackgroundThread::CLgUIMapBackgroundThread" ); } CLgUIMapBackgroundThread::~CLgUIMapBackgroundThread() { Destroy(); // log info LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgUIMapBackgroundThread::~CLgUIMapBackgroundThread" ); } DWORD CLgUIMapBackgroundThread::ThreadProc() { HANDLE hEvents[] = { CLgThread::GetThreadStopEvent(), m_hEventDraw }; CRect NewWndRect(0,0,0,0); CRect WndRect = NewWndRect; CRect DstRect = NewWndRect; while( true ) { switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, INFINITE ) ) { default: // log info LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, "CLgUIMapBackgroundThread::ThreadProc", "WaitForMultipleObjects returns unexpected value" ); case WAIT_OBJECT_0: // nessesary to delete in the context of this thread m_MemoryDC.DeleteDC(); return 0; case WAIT_OBJECT_0+1: break; } ::WaitForSingleObject( m_hMutexData, INFINITE ); NewWndRect = m_WndRect; DstRect = m_DstRect; ::ReleaseMutex( m_hMutexData ); ::WaitForSingleObject( m_hMutexDraw, INFINITE ); if( NewWndRect.Width() != WndRect.Width() || NewWndRect.Height() != WndRect.Height() ) { WndRect = NewWndRect; m_MemoryDC.Create( WndRect.Width(), WndRect.Height() ); } CLgUIMapBackground *pBackground = *this; pBackground->Draw( &m_MemoryDC, &DstRect ); CRect rect[4]; rect[0].SetRect( WndRect.left, WndRect.top, DstRect.left, WndRect.bottom ); rect[1].SetRect( DstRect.left, WndRect.top, DstRect.right, DstRect.top ); rect[2].SetRect( DstRect.right, WndRect.top, WndRect.right, WndRect.bottom ); rect[3].SetRect( DstRect.left, DstRect.bottom, DstRect.right, WndRect.bottom ); for( int i = 0; i < 4; i++ ) if( rect[i].Width() > 0 && rect[i].Height() > 0 ) m_MemoryDC.FillSolidRect( rect[i], RGB( 0,0,0 ) ); ::GdiFlush(); ::ReleaseMutex( m_hMutexDraw ); ::PostMessage( m_hWnd, WM_BACKGROUND_DRAW_THREAD, 0, 0 ); // log info LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgUIMapBackgroundThread::ThreadProc", "background filled" ); } LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgUIMapBackgroundThread::ThreadProc"), _T("Finished") ); return 0; } BOOL CLgUIMapBackgroundThread::InitDraw( LPRECT pWndRect, LPRECT pDstRect ) { LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgUIMapBackgroundThread::InitDraw") ); if( ::WaitForSingleObject( m_hMutexData, INFINITE ) != WAIT_OBJECT_0 ) return FALSE; m_WndRect = pWndRect; m_DstRect = pDstRect; ::ReleaseMutex( m_hMutexData ); return ::SetEvent( m_hEventDraw ); } BOOL CLgUIMapBackgroundThread::Draw( CDC *pDC ) { LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgUIMapBackgroundThread::Draw") ); if( !pDC ) return FALSE; if( ::WaitForSingleObject( m_hMutexDraw, 0UL ) != WAIT_OBJECT_0 ) return FALSE; pDC->BitBlt( 0, 0, m_WndRect.right - m_WndRect.left, m_WndRect.bottom - m_WndRect.top, &m_MemoryDC, 0, 0, SRCCOPY ); return ::ReleaseMutex( m_hMutexDraw ); } BOOL CLgUIMapBackgroundThread::Create( CWnd *pWnd, CLgUIMapBackground *pBackground ) { Destroy(); if( !pBackground ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("pBackground is NULL") ); return FALSE; } Link( pBackground ); if( !pWnd ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("pWnd is NULL") ); return FALSE; } m_hWnd = pWnd->GetSafeHwnd(); if( !m_hWnd ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("pWnd->GetSafeHwnd is NULL") ); return FALSE; } m_hMutexData = ::CreateMutex( NULL, FALSE, NULL ); if( !m_hMutexData ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("CreateMutex 1 failed") ); return FALSE; } m_hMutexDraw = ::CreateMutex( NULL, FALSE, NULL ); if( !m_hMutexDraw ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("CreateMutex 2 failed") ); return FALSE; } m_hEventDraw = ::CreateEvent( NULL, FALSE, FALSE, NULL ); if( !m_hEventDraw ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("CreateEvent failed") ); return FALSE; } if( !CLgThread::Start( FALSE ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("CLgThread::Start failed") ); return FALSE; } LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgUIMapBackgroundThread::Create", "Succeeded" ); return TRUE; } void CLgUIMapBackgroundThread::Destroy() { CLgThread::Stop( TRUE ); if( m_hMutexData ) { ::CloseHandle( m_hMutexData ); m_hMutexData = NULL; } if( m_hMutexDraw ) { ::CloseHandle( m_hMutexDraw ); m_hMutexDraw = NULL; } if( m_hEventDraw ) { ::CloseHandle( m_hEventDraw ); m_hEventDraw = NULL; } m_WndRect.SetRectEmpty(); m_DstRect.SetRectEmpty(); Unlink(); m_hWnd = NULL; }