ogre1.7 cookbook_OgreMfc

新建一个单文档mfc应用OgreMfc,项目配置与win32相同,Engine.h, Engine.cpp也与win32 application相同。

OgreMfc.h中加:

public:
CEngine* m_pEngine;
void InitEngine(HWND hwnd);
void CloseEngine();

OgreMfc.cpp中的BOOL COgreMfcApp::InitInstance()的最后+:

InitEngine(m_pMainWnd->m_hWnd);

OgreMfc.cpp中的int COgreMfcApp::ExitInstance()+:

CloseEngine();

void COgreMfcApp::InitEngine(HWND hwnd)
{
	m_pEngine = new CEngine(hwnd);
}

void COgreMfcApp::CloseEngine()
{
	if (m_pEngine)
	{
		delete m_pEngine;
		m_pEngine = 0;
	}
}

  

OgreMfcView类添加OnPaint()消息响应函数;

void COgreMfcView::OnPaint()
{
    CPaintDC dc(this); // device context for painting
    // TODO: 在此处添加消息处理程序代码
    // 不为绘图消息调用 CView::OnPaint()

    CEngine* pEngine = ((COgreMfcApp*)AfxGetApp())->m_pEngine;
    if (pEngine && pEngine->m_Root)
    {
        pEngine->m_Root->renderOneFrame();
    }
}

可选的代码:(加不加都可以,窗口未显示渲染处的场景时,可以移动一下窗口就显示了。。)

BOOL COgreMfcDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;

// TODO: 在此添加重新初始化代码
// (SDI 文档将重用该文档)
UpdateAllViews(NULL);

return TRUE;
}

 

PS:mfc程序退出时,显示有大量的内存泄露。。。解决办法:

http://www.blogjava.net/wangle/archive/2008/11/18/124813.html

i) in the General tab, switch "Use MFC in a shared DLL" to "Use Standard Windows Libraries" 
ii) in the C/C++/Preprocessor tab, add _AFXDLL to the preprocessor definitions 
iii) in the Linker/Input tab, add mfc80d.lib anywhere before OgreMain_d.lib

另一种方法是,使用Ogre自己的MemoryManager,并且禁止调用MFC的DEBUG_NEW,这需要先

#define OGRE_DEBUG_MEMORY_MANAGER 1

然后删除cpp中的以下行

#ifdef _DEBUG 
#define new DEBUG_NEW 
#endif

这样Ogre中会使用自己的new/delete,而不是调用vccrt中的_heap_alloc_debug

我用的第一种方法,另外VS2010中,应该是mfc100d.lib.......

 

你可能感兴趣的:(mfc)