Click here to Skip to main content
15,881,559 members
Articles / Programming Languages / C++

MFC D3D Application: Direct3D Tutorial: Part I

Rate me:
Please Sign up or sign in to vote.
4.99/5 (31 votes)
9 Dec 2012CPOL31 min read 149.6K   3.8K   117  
Yet another Direct3D framework, this time for MFC apps, with a step by step tutorial
// MFCD3DView.cpp : implementation of the CMFCD3DView class
//

#include "stdafx.h"
#include "MFCD3D.h"

#include "MFCD3DDoc.h"
#include "MFCD3DView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMFCD3DView

IMPLEMENT_DYNCREATE(CMFCD3DView, CFormView)

BEGIN_MESSAGE_MAP(CMFCD3DView, CFormView)
	//{{AFX_MSG_MAP(CMFCD3DView)
	ON_COMMAND(ID_VIEW_RENDER, OnRender)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMFCD3DView construction/destruction

CMFCD3DView::CMFCD3DView()
	: CFormView(CMFCD3DView::IDD)
{
	//{{AFX_DATA_INIT(CMFCD3DView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

	m_pD3DWnd = new CD3DWnd;
}

CMFCD3DView::~CMFCD3DView()
{
	delete m_pD3DWnd;
}

void CMFCD3DView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMFCD3DView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BOOL CMFCD3DView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CMFCD3DView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	// init D3D
	m_pD3DWnd->SubclassDlgItem(IDC_RENDER, this);

	if (FAILED(m_pD3DWnd->Initialize()))
		PostQuitMessage(1);

	// keep track of the main frame window
	m_pMainFrame = (CMainFrame*)GetTopLevelFrame();

	// update D3D device description status bar pane
	m_pMainFrame->UpdateStatusBar(ID_INDICATOR_DEVICE, m_pD3DWnd->GetDeviceStats());
}

/////////////////////////////////////////////////////////////////////////////
// CMFCD3DView diagnostics

#ifdef _DEBUG
void CMFCD3DView::AssertValid() const
{
	CFormView::AssertValid();
}

void CMFCD3DView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CMFCD3DView message handlers

void CMFCD3DView::OnRender() 
{
	m_pD3DWnd->RenderEnvironment();

	// update D3D fps status bar pane
	m_pMainFrame->UpdateStatusBar(ID_INDICATOR_FPS, m_pD3DWnd->GetFrameStats());
}

LRESULT CMFCD3DView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	if (m_pD3DWnd && m_pD3DWnd->IsActive())
		if (m_pD3DWnd->HandleMouseMessages(message, wParam, lParam))
			return 0L;
	
	return CFormView::WindowProc(message, wParam, lParam);
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Texas Capital Bank
United States United States
Professional software engineer with 30+ years of experience delivering systems across diverse industries, looking for the next opportunity to deliver cutting edge end-to-end technology solutions.

Avid reader, disciplined writer and enthusiastic tinkerer with a background in electronics, looking inside and thinking outside the box, genuinely passionate about robust, extensible, reusable and performant code.

Framework developer leading, coaching and learning about best practices, code quality, DevOps and software and data lifecycle management with an agile mindset to create the most elegant and sustainable solutions.

Comments and Discussions