|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 04/03/2003
// PROJECT:
// SUBSYSTEM:
// FILE: DirectDrawWnd.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of DirectDrawWnd.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "..\LgLib\Version.h"
#include <DDraw.h>
#include "DirectDrawWnd.h"
CDirectDrawWnd::CDirectDrawWnd(){ ::CoInitializeEx( NULL, COINIT_MULTITHREADED );
m_pDD = NULL; m_pFrontBuffer = NULL; m_pBackBuffer = NULL;}
CDirectDrawWnd::~CDirectDrawWnd(){ FreeDirectDraw();
::CoUninitialize();}
BOOL CDirectDrawWnd::Create( HWND hWndParent, int x, int y, int nWidth, int nHeight ){ if( !CWnd::Create( _T("DirectDrawWndClass"), "", WS_CHILD, x, y, nWidth, nHeight, hWndParent, NULL, NULL ) ) return FALSE;
return InitDirectDraw();}
LRESULT CDirectDrawWnd::OnDestroy(){ FreeDirectDraw(); return 0;}
BOOL CDirectDrawWnd::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 CDirectDrawWnd::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 CDirectDrawWnd::Draw(){ 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;
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 CDirectDrawWnd::OnDraw( LPDIRECTDRAWSURFACE7 pBackBuffer ){
}
|