⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 childview.cpp

📁 C:Documents and SettingsAdministrator桌面VC++多媒体特效制作百例CHAR22Tearing
💻 CPP
字号:
// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "Tearing Demo.h"
#include "ChildView.h"
#include "DDExcept.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChildView

CChildView::CChildView() :
	m_iCurX(0),
	m_bFlip(FALSE)
{
}

CChildView::~CChildView()
{
}


BEGIN_MESSAGE_MAP(CChildView,CWnd )
	//{{AFX_MSG_MAP(CChildView)
	//}}AFX_MSG_MAP
	// Destruction
	ON_WM_CHAR()
	ON_WM_DESTROY()
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CChildView helpers

BOOL CChildView::InitDD()
{
	BOOL bReturn = TRUE;
	HRESULT hr = S_OK;
	try {
		// Create the DirectDraw object.  We won't need the
		// capabilities of IDirectDraw2, so there isn't
		// a need to QI.
		hr = DirectDrawCreate(NULL,&m_pIDirectDraw,NULL);
		if FAILED( hr ) {
			// Some error
			throw new CDDException(TRUE,"Error creating DirectDraw object");
		} // if

		// Set our cooperative level to be full screen/exclusive...
		hr = m_pIDirectDraw->SetCooperativeLevel(m_hWnd,
						DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
		if FAILED( hr ) {
			// Some error
			throw new CDDException(TRUE,"Error setting cooperative level");
		} // if

		// Set the display mode to 640 by 480, 256 colors.  Normally
		// you would query the driver to see if this mode is
		// supported, but this particular mode is always available. 
		hr =  m_pIDirectDraw->SetDisplayMode(640,480,8);
		if ( FAILED( hr ) ) {
			// Some error
			throw new CDDException(TRUE,"Error setting display mode");
		} // if

		// Create our primary surface
		DDSURFACEDESC ddsd;
		ZeroMemory(&ddsd,sizeof(ddsd));
		ddsd.dwSize = sizeof(ddsd);
		ddsd.dwFlags = DDSD_CAPS;
		ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
		if ( m_bFlip ) {
			// Add the enhancements for page flipping
			ddsd.dwFlags |= DDSD_BACKBUFFERCOUNT;
			ddsd.ddsCaps.dwCaps |= DDSCAPS_FLIP | 
								  DDSCAPS_COMPLEX |
								  DDSCAPS_VIDEOMEMORY;
			ddsd.dwBackBufferCount = NUMBUFFERS;
		} // if
    
		hr = m_pIDirectDraw->CreateSurface(&ddsd,&m_lpDDSPrimary,NULL);
		if ( FAILED( hr ) ) {
			// Some error
			throw new CDDException(TRUE,"Error creating primary surface");
		} // if

		// Check the surface format...will we need a palette?
		DDPIXELFORMAT ddpf;
		ZeroMemory(&ddpf,sizeof(ddpf));
		ddpf.dwSize = sizeof(ddpf);
		hr = m_lpDDSPrimary->GetPixelFormat(&ddpf);
		if ( FAILED( hr ) ) {
			// Some error
			throw new CDDException(TRUE,"Error retrieving pixel format");
		} // if

		if ( ddpf.dwFlags & DDPF_PALETTEINDEXED8 ) {
			// We are palettized, so create a palette and attach it to
			// the primary surface.
			hr = LoadDDPalette(IDB_CAN,&m_lpDDPalette);
			if ( FAILED( hr ) ) {
				// Some error
				throw new CDDException(TRUE,"Error establishing a new palette");
			} // if

			m_lpDDSPrimary->SetPalette(m_lpDDPalette);
			if ( FAILED( hr ) ) {
				// Some error
				throw new CDDException(TRUE,"Error assigning the new palette to the primary surface");
			} // if
		} // if

		// Load our can bitmap
		hr = LoadDDBitmap(IDB_CAN,&m_lpDDSCan);
		if ( FAILED( hr ) ) {
			// Some error
			throw new CDDException(TRUE,"Error loading table bitmap");
		} // if
		
		if ( m_bFlip ) {
			// Locate our secondary surface
			DDSCAPS ddscaps;
			ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
			hr = m_lpDDSPrimary->GetAttachedSurface(&ddscaps,&m_lpDDSSecondary);
			if ( FAILED( hr ) ) {
				// Some error
				throw new CDDException(TRUE,"Error retrieving secondary back buffer");
			} // if
		} // if
		else {
		// Create our back buffer
			hr = CreateSecondarySurface(640,480,&m_lpDDSSecondary);
			if ( FAILED( hr ) ) {
				// Some error
				throw new CDDException(TRUE,"Error creating secondary back buffer");
			} // if
		} // else
	} // try
	catch (CDDException* dde) {
		// Intercept our special exception and
		// tell user of error
		dde->ReportError(MB_OK | MB_ICONERROR,hr);

		// Delete the exception
		dde->Delete();

		// Return failure
		bReturn = FALSE;
	} // catch
	catch (...) {
		// Return failure
		bReturn = FALSE;
	} // catch

	// NOTE:  At this point the window has been
	// created.  If we threw an exception and are
	// returning -1 (to indicate failure during
	// creation), a WM_DESTROY message will still
	// be issued.  The net result is if we created
	// and DirectDraw objects prior to the
	// exception, they will automatically be
	// released at that time.  No special action is
	// required here.
	return bReturn;
}

void CChildView::ReleaseDDObjects()
{
	// Release our surfaces first.  The order in which
	// DirectDraw objects are released is important.
	// Surfaces first, then the palette, then finally
	// the DirectDraw object.
	if ( !m_bFlip ) {
		// When we're not page flipping, we release the
		// DirectDraw objects in the normal order.
		m_lpDDSPrimary = NULL;
		m_lpDDSSecondary = NULL;
	} // if
	else {
		// When we're page flipping, we reverse the order
		// of release for the primary and secondary 
		// surfaces.  This is because they really are
		// the SAME surface and must be released in an
		// order opposite to that which created them.
		m_lpDDSSecondary = NULL;
		m_lpDDSPrimary = NULL;
	} // else
	m_lpDDSCan = NULL;

	// Now the palette.  If we never created one,
	// this call will still succeed (thanks to
	// CComPtr).
	m_lpDDPalette = NULL;

	// Finally release our DirectDraw object
	m_pIDirectDraw = NULL;
}

LRESULT CChildView::AnimateFrame()
{
	// Calculate new coordinates
	static BOOL bMoveRight = TRUE;
	if ( bMoveRight ) {
		++m_iCurX;
		if ( m_iCurX >= (480-CANWIDTH-1) ) {
			// Bounce off of right-hand edge
			m_iCurX -= 2;
			bMoveRight = FALSE;
		} // if
	} // if
	else {
		--m_iCurX;
		if ( m_iCurX < 0 ) {
			// Bounce off of left-hand edge
			m_iCurX = 1;
			bMoveRight = TRUE;
		} // if
	} // else

	// Blt our bitmap
	Blt(m_iCurX);

	return 1;
}

/////////////////////////////////////////////////////////////////////////////
// CChildView message handlers

BOOL CChildView::Create(BOOL bEnableFlipping /*=FALSE*/) 
{
	// Register a new window class
	LPCSTR strClass = AfxRegisterWndClass(CS_DBLCLKS,
						::LoadCursor(NULL,IDC_ARROW),
						(HBRUSH)GetStockObject(WHITE_BRUSH),
						AfxGetApp()->LoadIcon(IDR_MAINFRAME));

	if ( !CreateEx(WS_EX_APPWINDOW,strClass,_T("Tearing Demo"),WS_SYSMENU | WS_POPUP | WS_VISIBLE,
		0,0,::GetSystemMetrics(SM_CXSCREEN),::GetSystemMetrics(SM_CYSCREEN),NULL,NULL,0) ) {
		// Failed to create the window
		return FALSE;
	} // if

	// Paint ourselves
	UpdateWindow();

	// Init DirectDraw
	m_bFlip = bEnableFlipping;
	return InitDD();
}

void CChildView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// Check for <Esc>
	if ( nChar == VK_ESCAPE ) {
		// User wants to quit...
		CTearingDemoApp* pApp = (CTearingDemoApp*)AfxGetApp();
		ASSERT(pApp != NULL);
		pApp->Stop();
	} // if

	CWnd::OnChar(nChar,nRepCnt,nFlags);
}

void CChildView::PostNcDestroy() 
{
	// Call our base class
	CWnd::PostNcDestroy();

	// Destroy our CWnd pointer
	delete this;
}

BOOL CChildView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	// No cursor
	::SetCursor(NULL);
	return TRUE;
}

void CChildView::OnDestroy() 
{
	// Release our DirectDraw objects (just making
	// sure they're gone...)
	ReleaseDDObjects();

	CWnd::OnDestroy();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -