Click here to Skip to main content
15,893,722 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 150.2K   3.8K   117  
Yet another Direct3D framework, this time for MFC apps, with a step by step tutorial
// MainFrm.cpp : implementation of the CMainFrame class
//

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

#include "MainFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code !
	ON_WM_CREATE()
	//}}AFX_MSG_MAP

	ON_UPDATE_COMMAND_UI(ID_INDICATOR_DEVICE, OnUpdateDeviceStats)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_FPS,    OnUpdateFrameStats)	
	
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_DEVICE,
	ID_INDICATOR_FPS
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// size the status bar panels
	m_wndStatusBar.SetPaneInfo(m_wndStatusBar.CommandToIndex(ID_INDICATOR_DEVICE),
							   ID_INDICATOR_DEVICE,
							   SBPS_NORMAL,
							   428);

	m_wndStatusBar.SetPaneInfo(m_wndStatusBar.CommandToIndex(ID_INDICATOR_FPS),
							   ID_INDICATOR_FPS,
							   SBPS_NORMAL,
							   50);

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;

	cs.style &= ~FWS_ADDTOTITLE;

	return TRUE;
}

// status bar pane updater
void CMainFrame::UpdateStatusBar(DWORD dwPaneID, LPCTSTR lpszText)
{
	CString* psText;

	// force UI updates...
	switch (dwPaneID)
	{
	case ID_INDICATOR_DEVICE:	psText = &m_sDeviceStats;	break;
	case ID_INDICATOR_FPS:		psText = &m_sFrameStats;	break;
	
	default:
		return;
	}
	
	// with any extra formatting here.
	psText->Format(" %s", lpszText);

	// uncomment the next section for dynamic (content-based) pane resizing; this
	// saves guessing the required pane sizes, but it can be annoying if the text
	// extent of psText varies frequently on broad ranges...

/*	CFont* pFont	= m_wndStatusBar.GetFont();
	CFont* pOldFont = NULL;
	
	// get a client DC for dynamic resizing
	CClientDC dc((CWnd*)&m_wndStatusBar);
	
	if (pFont != NULL)
		pOldFont = dc.SelectObject(pFont);

	// get the text extent
	CSize size = dc.GetTextExtent(*psText);

	// done with DC objects and the DC
	dc.SelectObject(pOldFont);
	ReleaseDC(&dc);

	// actual horizontal resize, with some extra trailing space
	m_wndStatusBar.SetPaneInfo(m_wndStatusBar.CommandToIndex(dwPaneID), dwPaneID, SBPS_NORMAL, size.cx + 3);
*/
}


/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnUpdateDeviceStats(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(); pCmdUI->SetText(m_sDeviceStats);
}

void CMainFrame::OnUpdateFrameStats(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(); pCmdUI->SetText(m_sFrameStats);
}

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