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

📄 directxdraw.cpp

📁 Conway s Game of life 算法程序
💻 CPP
字号:
// DirectXDraw.cpp: implementation of the CDirectXDraw class.
/*
 *  DirectXDraw.cpp  - Copyright (C) 2006 Jerry Jiang
 *
 **********************************************************************
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 
 **********************************************************************
 *
 */
#include "stdafx.h"
#include "DirectXDraw.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDirectXDraw::CDirectXDraw()
{
	m_pDD = NULL;
	m_pddsFrontBuffer = NULL;
	m_pddsStoreBuffer = NULL;
    pcClipper = NULL;
	m_bInitial = FALSE;

}

CDirectXDraw::~CDirectXDraw()
{
	Terminate();
}

// old DirectDraw Initialization stuff. Set a window mode DirectDraw Display.
BOOL CDirectXDraw::Init(HWND hWnd)
{
	HRESULT hRet;
	CRect rect;
	HRESULT hr;
	DDSURFACEDESC2 ddsd;


	this->hWnd = hWnd;
    GetClientRect(hWnd,rect);
    nFullClientWidth = rect.Width(); 
    nFullClientHeight= rect.Height();  
	hRet = DirectDrawCreateEx(NULL, (VOID**)&m_pDD, IID_IDirectDraw7, NULL);
	if(hRet != DD_OK)
	{
		AfxMessageBox("Failed to create directdraw object.");
		return FALSE;
	}
	hRet = m_pDD->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
	if(hRet != DD_OK)
	{
		AfxMessageBox("Failed to set directdraw display behavior.");
		return FALSE;
	}
    ZeroMemory( &ddsd, sizeof( ddsd ) );
    ddsd.dwSize         = sizeof( ddsd );
    ddsd.dwFlags        = DDSD_CAPS;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

    if(FAILED(hr = m_pDD->CreateSurface(&ddsd, &m_pddsFrontBuffer, NULL)))
	{
		AfxMessageBox("Failed to create primary surface.");
		return FALSE;		
	}

    // Create the backbuffer surface
    ddsd.dwFlags        = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;    
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
    ddsd.dwWidth        = nFullClientWidth;
    ddsd.dwHeight       = nFullClientHeight;

    if(FAILED(hr = m_pDD->CreateSurface(&ddsd, &m_pddsStoreBuffer, NULL)))
	{
		AfxMessageBox("Failed to create back buffer surface.");
		return FALSE;		
	}

    if(FAILED(hr = m_pDD->CreateClipper(0, &pcClipper, NULL)))
	{
		AfxMessageBox("Failed to create clipper.");
		return FALSE;		
	}

    if(FAILED(hr = pcClipper->SetHWnd(0, hWnd)))
    {
        pcClipper->Release();
		AfxMessageBox("Failed to create primary surface.");
		return FALSE;		
    }

    if(FAILED(hr = m_pddsFrontBuffer->SetClipper(pcClipper)))
    {
        pcClipper->Release();
		AfxMessageBox("Failed to create primary surface.");
		return FALSE;		
    }
    m_bInitial = TRUE;
	return TRUE;
}

// make sure all stuff are terminated. and set to NULL when application ends.
void CDirectXDraw::Terminate()
{
	if (m_pDD != NULL)
	{
		if (m_pddsFrontBuffer != NULL)
		{
			if (m_pddsStoreBuffer != NULL)
			{
				m_pddsStoreBuffer->Release();
				m_pddsStoreBuffer = NULL;
			}

			if (pcClipper != NULL)
			{
				pcClipper->Release();
				pcClipper = NULL;
			}

			m_pddsFrontBuffer->Release();
			m_pddsFrontBuffer = NULL;
		}
		m_pDD->Release();
		m_pDD = NULL;
	}
	m_bInitial = FALSE;
}

// clear both off csreen buffer and primary buffer.
void CDirectXDraw::Clear()
{
	HRESULT hRet;
	DDBLTFX fx;
	fx.dwSize = sizeof(fx);
	fx.dwFillColor = 0x000000;

	while (1)
	{
		hRet = m_pddsFrontBuffer->Blt(NULL, NULL, NULL, DDBLT_COLORFILL, &fx);
		if (hRet == DD_OK)
			break;
		else if (hRet == DDERR_SURFACELOST)
		{
			m_pddsFrontBuffer->Restore();
		}
		else if (hRet != DDERR_WASSTILLDRAWING)
			break;
	}

	while (1)
	{
		hRet = m_pddsStoreBuffer->Blt(NULL, NULL, NULL, DDBLT_COLORFILL, &fx);
		if (hRet == DD_OK)
			break;
		else if (hRet == DDERR_SURFACELOST)
		{
			m_pddsStoreBuffer->Restore();
		}
		else if (hRet != DDERR_WASSTILLDRAWING)
			break;
	}

}

void CDirectXDraw::CellDraw(char *m_cArray, COLORREF cellColor,CRect &rectView,BOOL bGrid)
{
	if(!m_bInitial)
		return;

	HRESULT hRet;
	HDC dc;
	hRet = m_pddsStoreBuffer->GetDC(&dc);
	if (hRet != DD_OK)
		return;

	int nCellWidth = rectView.Width()/CONST_INT_GRIDNUMBER;
	int nCellHeight = rectView.Height()/CONST_INT_GRIDNUMBER;
	int x,y;

	CDC *pDC = CDC::FromHandle(dc);
	CRect rect;
	CPen pen(PS_SOLID,1,RGB(192,192,192));
	m_redBrush.DeleteObject();
	m_redBrush.CreateSolidBrush(cellColor);
	pDC->FillSolidRect(&rectView, RGB(255,255,255));
	pDC->SelectObject(m_redBrush);
	pDC->SelectObject(&pen);

	rect.SetRect(0,0,nCellWidth*CONST_INT_GRIDNUMBER,nCellHeight*CONST_INT_GRIDNUMBER);

	pDC->SetWindowOrg(-(rectView.Width()-rect.Width())/2,-(rectView.Height()-rect.Height())/2);

	if(bGrid)
	{
		for(int i = 0;i<= CONST_INT_GRIDNUMBER;i++)
		{
			pDC->MoveTo(nCellWidth*i,0);
			pDC->LineTo(nCellWidth*i,rect.Height());
			pDC->MoveTo(0,nCellHeight*i);
			pDC->LineTo(rect.Width(),nCellHeight*i);
		}
	}

	for(int i=0;i<CONST_INT_GRIDNUMBER;i++)
	{
		for(int j=0;j<CONST_INT_GRIDNUMBER;j++)
		{	
			x = nCellWidth*i;
			y = nCellHeight*j;
			rect.top= y;
			rect.left = x;
			rect.bottom = y+nCellHeight;
			rect.right = x+nCellWidth;
			if(m_cArray[i*CONST_INT_GRIDNUMBER+j])
				pDC->Ellipse(&rect);
		}
	}
	m_pddsStoreBuffer->ReleaseDC(dc);
}


void CDirectXDraw::BarDraw(int *m_nNumberArray, COLORREF barColor,CRect &rectView)
{
	if(!m_bInitial)
		return;
	HRESULT hRet;
	HDC dc;
	hRet = m_pddsStoreBuffer->GetDC(&dc);
	
	if (hRet != DD_OK)
		return;

	int nBarWidthWithInterval = (rectView.Height()/CONST_INT_BARNUMBER);
	int nBarWidth = (int)((rectView.Height()/CONST_INT_BARNUMBER)*0.8);
	int y,cx,cy;

	CDC *pDC = CDC::FromHandle(dc);
	pDC->FillSolidRect(&rectView, RGB(255,255,255));

	for(int i=0;i<CONST_INT_BARNUMBER;i++)
	{	
		y = nBarWidthWithInterval*i;
		cx =(int)(rectView.Width()*((double)m_nNumberArray[i]/CONST_INT_BARNUMBER));
		cy = nBarWidth;
		pDC->FillSolidRect(0,y,cx,cy,barColor);
	}

	m_pddsStoreBuffer->ReleaseDC(dc);
}

// Load images from offscteen buffer to primary buffer and for display.
void CDirectXDraw::Display()
{
	if(!m_bInitial)
		return;

	HRESULT hRet;
	RECT rt;
	POINT p = {0, 0};

	ClientToScreen(hWnd, &p);
	rt.left = 0 + p.x; rt.top = 0 + p.y; rt.right = nFullClientWidth + p.x; rt.bottom = nFullClientHeight + p.y;
    while( 1 )
    {
		hRet = m_pddsFrontBuffer->Blt(&rt, m_pddsStoreBuffer, NULL, DDBLT_WAIT, NULL);
		if (hRet == DD_OK)
			break;
        else if(hRet == DDERR_SURFACELOST)
        {
            m_pddsFrontBuffer->Restore();
            m_pddsStoreBuffer->Restore();
        }
        else if(hRet != DDERR_WASSTILLDRAWING)
            return;
	}
}

⌨️ 快捷键说明

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