|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: DDWnd.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of DDWnd.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
//#include "chicago.h"
#include <DDraw.h>
#include "DDWnd.h"
#include "..\..\LgLib\Version.h"
BEGIN_MESSAGE_MAP(CDDWnd, CWnd) //{{AFX_MSG_MAP(CDDWnd)
ON_WM_PAINT() ON_WM_SIZE() ON_WM_DEVMODECHANGE() //}}AFX_MSG_MAP
END_MESSAGE_MAP()
CDDWnd::CDDWnd(){ ::CoInitializeEx( NULL, COINIT_MULTITHREADED );
m_pDD = NULL; m_pFrontBuffer = NULL; m_pBackBuffer = NULL;}
CDDWnd::~CDDWnd(){ FreeDirectDraw();
::CoUninitialize();}
BOOL CDDWnd::Create( CWnd *pParentWnd, int x, int y, int nWidth, int nHeight ){ CString strClassName = ::AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, ::LoadCursor( NULL, IDC_ARROW ), NULL, NULL );
CRect rect( x, y, x + nWidth, y + nHeight );
if( !CWnd::Create( strClassName, "", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, rect, pParentWnd, 0, NULL ) ) return FALSE;
return InitDirectDraw();}
BOOL CDDWnd::InitDirectDraw(){ FreeDirectDraw();
if( FAILED( ::DirectDrawCreateEx(NULL, (void**)&m_pDD, IID_IDirectDraw7, NULL ) ) ) return FALSE;
if( FAILED( m_pDD->SetCooperativeLevel( GetSafeHwnd(), DDSCL_NORMAL ) ) ) return FALSE;
// Create the primary surface
DDSURFACEDESC2 ddsd; ZeroMemory( &ddsd, sizeof( ddsd ) ); ddsd.dwSize = sizeof( ddsd ); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
if( FAILED( m_pDD->CreateSurface( &ddsd, &m_pFrontBuffer, NULL ) ) ) return FALSE;
RECT rect; if( !::GetClientRect( GetSafeHwnd(), &rect ) ) return FALSE;
// Create the backbuffer surface
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE; ddsd.dwWidth = rect.right - rect.left; ddsd.dwHeight = rect.bottom - rect.top;
if( FAILED( m_pDD->CreateSurface( &ddsd, &m_pBackBuffer, NULL ) ) ) return FALSE;
LPDIRECTDRAWCLIPPER pClipper; if( FAILED( m_pDD->CreateClipper( 0, &pClipper, NULL ) ) ) return FALSE;
if( FAILED( pClipper->SetHWnd( 0, GetSafeHwnd() ) ) ) { pClipper->Release(); return FALSE; }
if( FAILED( m_pFrontBuffer->SetClipper( pClipper ) ) ) { pClipper->Release(); return E_FAIL; }
// Done with clipper
pClipper->Release();
return TRUE;}
void CDDWnd::FreeDirectDraw(){ if( m_pBackBuffer ) { m_pBackBuffer->Release(); m_pBackBuffer = NULL; } if( m_pFrontBuffer ) { m_pFrontBuffer->Release(); m_pFrontBuffer = NULL; }
if( m_pDD ) { m_pDD->Release(); m_pDD = NULL; }}
void CDDWnd::Draw(){ if( !m_pDD || !m_pBackBuffer || !m_pFrontBuffer ) return;
HRESULT hr;
// Check the cooperative level before rendering
if( FAILED( hr = m_pDD->TestCooperativeLevel() ) ) { switch( hr ) { case DDERR_EXCLUSIVEMODEALREADYSET: // Do nothing because some other app has exclusive mode
return;
case DDERR_WRONGMODE: // The display mode changed on us. Update the
// DirectDraw surfaces accordingly
InitDirectDraw(); return; } return; }
OnDraw( m_pBackBuffer );
RECT rect; if( !::GetClientRect( GetSafeHwnd(), &rect ) ) return;
::MapWindowPoints( GetSafeHwnd(), HWND_DESKTOP, (LPPOINT)&rect, 2 );
while( 1 ) { hr = m_pFrontBuffer->Blt( &rect, m_pBackBuffer, NULL, DDBLT_WAIT, NULL );
if( hr == DDERR_SURFACELOST ) { m_pFrontBuffer->Restore(); m_pBackBuffer->Restore(); }
if( hr != DDERR_WASSTILLDRAWING ) break; }
if( FAILED( hr ) ) { if( hr != DDERR_SURFACELOST ) return;
// The surfaces were lost so restore them
m_pDD->RestoreAllSurfaces(); }}
void CDDWnd::OnDraw( LPDIRECTDRAWSURFACE7 pBackBuffer ){ // Erase the background
DDBLTFX ddbltfx; ::ZeroMemory( &ddbltfx, sizeof(ddbltfx) ); ddbltfx.dwSize = sizeof(ddbltfx); ddbltfx.dwFillColor = 0;
pBackBuffer->Blt( NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx );}
BOOL CDDWnd::DestroyWindow() { FreeDirectDraw();
return CWnd::DestroyWindow();}
void CDDWnd::OnPaint() { CPaintDC dc(this);
Draw();}
void CDDWnd::OnSize(UINT nType, int cx, int cy) { CWnd::OnSize( nType, cx, cy ); InitDirectDraw();}
void CDDWnd::OnDevModeChange(LPTSTR lpDeviceName) { CWnd::OnDevModeChange(lpDeviceName); InitDirectDraw();}
|