Click here to Skip to main content
11,582,849 members (68,232 online)
Click here to Skip to main content
Add your own
alternative version

MFC D3D Application: Direct3D Tutorial: Part I

, 9 Dec 2012 CPOL 93.6K 2.6K 114
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)

Share

About the Author

hector [.j.] rivas
Software Developer (Senior) Code Authority, Inc.
United States United States
Pride in craftsmanship is my new thing.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 9 Dec 2012
Article Copyright 2006 by hector [.j.] rivas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid