Click here to Skip to main content
15,883,749 members
Articles / Desktop Programming / MFC

Drawing with DirectDraw & GDI

Rate me:
Please Sign up or sign in to vote.
4.88/5 (11 votes)
24 May 2002 300.2K   13.8K   68  
Drawing Graphics fast with DirectDraw than with GDI
// TestView.cpp : implementation of the CTestView class
//

#include "stdafx.h"
#include "Test.h"

#include "TestDoc.h"
#include "TestView.h"
#include "include\Painter.h"
#include "mmsystem.h"


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

/////////////////////////////////////////////////////////////////////////////
// CTestView

IMPLEMENT_DYNCREATE(CTestView, CView)

BEGIN_MESSAGE_MAP(CTestView, CView)
//{{AFX_MSG_MAP(CTestView)
ON_WM_ERASEBKGND()
ON_COMMAND(ID_MODE_DDRAW, OnModeDdraw)
ON_COMMAND(ID_MODE_GDI, OnModeGdi)
ON_UPDATE_COMMAND_UI(ID_MODE_DDRAW, OnUpdateModeDdraw)
ON_UPDATE_COMMAND_UI(ID_MODE_GDI, OnUpdateModeGdi)
ON_WM_KEYDOWN()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_COMMAND(ID_VIEW_SAVEBITMAP, OnViewSavebitmap)
ON_UPDATE_COMMAND_UI(ID_VIEW_SAVEBITMAP, OnUpdateViewSavebitmap)
ON_COMMAND(ID_VIEW_ZOOMFIT, OnViewZoomfit)
ON_COMMAND(ID_VIEW_ZOOMIN, OnViewZoomin)
ON_COMMAND(ID_VIEW_ZOOMOUT, OnViewZoomout)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestView construction/destruction

CTestView::CTestView()
{
	// TODO: add construction code here
    m_mode = 1;
	m_scale = 64;
	m_nStep = 1;
	m_dwFrameCount = 0;
	m_dwFrames =0;
	m_dwFrameTime = timeGetTime();
	m_bSaveBmp = FALSE;
}

CTestView::~CTestView()
{ 
	
}

BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTestView drawing
#include "include\memdc.h"

void CTestView::OnDraw(CDC* pDC)
{   
	CTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CxImage& image = pDoc->m_image;
	
	//**************************
	
	DWORD time2;
	m_dwFrameCount++;
	time2 = timeGetTime() - m_dwFrameTime;
	if(time2 > 1000)
	{
		m_dwFrames = (m_dwFrameCount*1000)/time2;
		m_dwFrameTime = timeGetTime();
		m_dwFrameCount = 0;
		
	}
	
	//***************************
	
	CRect rc,client;
	GetClientRect(&client);
	CCanvas canvas1(this,m_mode);
	CCanvas canvas(this,m_mode,512,512);
	
	
	if(image.GetWidth()>0)
	{
		canvas.Blt(CRect(0, 0, 512, 512), 
		CRect(0, 0, image.GetWidth(), image.GetHeight()), 
			image.GetBits(), 
			(LPBITMAPINFO)image.hDib);
	}
	else
		canvas.Fill(RGB(200, 0, 0));
	int i= 200;
	
	rc.left = (client.left+client.right)/2-100;
	rc.top = (client.top+client.bottom)/2-50;
	rc.right = (client.left+client.right)/2+100;
	rc.bottom =(client.top+client.bottom)/2+50;
	{
		
		canvas1.Fill(RGB(0,0,255));
		canvas1.Circle(350, 100, 80, RGB(0, 255, i));
		
		CString Fps,dismode;
		Fps.Format("Fps is %d",m_dwFrames);
		if(!m_mode) 
			dismode.Format("Displaying  with GDI");
		else
			dismode.Format("Displaying with DirectDraw");
		LPSTR p = Fps.GetBuffer(20);
		CFont font;
		font.CreatePointFont(100, "Arial Black");
		HDC hDC = canvas.GetDC();
		CDC dc;
		dc.Attach(hDC);
		int oldmode = dc.SetBkMode(TRANSPARENT);
		int oldrop2 = dc.SetROP2(R2_NOT);
		CFont* pOldFont = dc.SelectObject(&font);		
		dc.TextOut(0,0,dismode);
		CRect r(CPoint(i%300, i%350), CSize(200, 200));
		canvas1.TextOut(client.right-200,client.bottom-20,RGB(i,0,0),p);
		Fps.ReleaseBuffer();
		dc.SetBkMode(oldmode);
		dc.SetROP2(oldrop2);
		dc.SelectObject(pOldFont);
		dc.Detach();
		canvas.ReleaseDC();		
		canvas.Rect(100,300,500,400,RGB(128,0,i));
		canvas1.FillRect(200,300,400,400,RGB(255,i,0));
		canvas.Line(50, i+100, 450, i+300 ,RGB(255-i*2,i*2,128+i*3));
		canvas1.Line(client.right,client.top,client.left,client.bottom,RGB(255,i,0));
		canvas.RoundedRect(300,200,500,300,40,RGB(255,0,i));
		
		RECT testrc;
		GetClientRect(&testrc);
		CRect rcSrc(256-4*m_scale, 256-4*m_scale, 256+4*m_scale, 256+4*m_scale);
		
		canvas1.Blt(&canvas,&testrc,&rcSrc);
		//		if(m_bSaveBmp)
		//		{
		//
		//			canvas1.SaveAsBMP("test.bmp");
		//			m_bSaveBmp = FALSE;
		//		}
		CPainter painter(&canvas1);
		
	}
	
}

/////////////////////////////////////////////////////////////////////////////
// CTestView printing

BOOL CTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTestView diagnostics

#ifdef _DEBUG
void CTestView::AssertValid() const
{
	CView::AssertValid();
}

void CTestView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTestDoc* CTestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc)));
	return (CTestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestView message handlers

BOOL CTestView::OnEraseBkgnd(CDC* pDC) 
{
	
	return TRUE;
}



void CTestView::OnModeDdraw() 
{
	m_mode = 1;
	Invalidate();
}

void CTestView::OnModeGdi() 
{
	m_mode = 0;
	Invalidate();
}

void CTestView::OnUpdateModeDdraw(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_mode);
}

void CTestView::OnUpdateModeGdi(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(!m_mode);
}

void CTestView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	switch(nChar)
	{
	case 107:   // KeyAdd	
		if((m_scale-1)>0)
			m_scale--;
		Invalidate();
		break;
	case 109:	//KeySubtract
		m_scale++;
		Invalidate();
		break;
	case 106:	//KeyMultiply
		m_scale = 64;
		Invalidate();
		break;
	default :
		break;
		
	}
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CTestView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	if(m_nStep == 1)
	{
		m_ptFirst = point;
		m_nStep++;
	}
	CView::OnLButtonDown(nFlags, point);
}

void CTestView::OnMouseMove(UINT nFlags, CPoint point) 
{
	if(m_nStep == 2)
	{
		CClientDC dc(this);
		dc.MoveTo(m_ptFirst);
		dc.LineTo(point);
	}
	CView::OnMouseMove(nFlags, point);
}

void CTestView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(m_nStep == 2)
		m_nStep--;
	CView::OnLButtonUp(nFlags, point);
}

void CTestView::OnViewSavebitmap() 
{
	m_bSaveBmp = !m_bSaveBmp;
	Invalidate();
}

void CTestView::OnUpdateViewSavebitmap(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_bSaveBmp);
}

void CTestView::OnViewZoomfit() 
{
	m_scale = 64;
	Invalidate();
	
}

void CTestView::OnViewZoomin() 
{
	if((m_scale-1)>0)
		m_scale--;
	Invalidate();
	
}

void CTestView::OnViewZoomout() 
{
	m_scale++;
	Invalidate();
	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions