Hi all:
What I want to do is to update the frame every short interval (like 0.1s, that'll gimme 100FPS) while keeping the cpu usage low. I know how to satisfy the first goal, but have no idea on how to satisfy the senond at the same time. I'm using MFC, and someone told me I can override the CWinApp::Run() method to control how frequently my main rendering procedure is called, this is how I did it:
int H3DEffectApp::Run()
{
// from CWinApp::Run(), SRC can be found in \Microsoft Visual Studio .NET 2003\Vc7\atlmfc\src\mfc\appcore.cpp
if (m_pMainWnd == NULL && AfxOleGetUserCtrl())
{
// Not launched /Embedding or /Automation, but has no main window!
TRACE(traceAppMsg, 0, "Warning: m_pMainWnd is NULL in CWinApp::Run - quitting application.\n");
AfxPostQuitMessage(0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// from CWinThread::Run(), SRC can be found in \Microsoft Visual Studio .NET 2003\Vc7\atlmfc\src\mfc\thrdcore.cpp
ASSERT_VALID(this);
_AFX_THREAD_STATE* pState = AfxGetThreadState();
// for tracking the idle time state
BOOL bIdle = TRUE;
LONG lIdleCount = 0;
for (;;)
{
static DWORD lastTime = 0;
DWORD currTime = timeGetTime();
if((currTime - lastTime)>10)
{
PostMessage(renderWindowH, WM_PAINT, 0, 0);
lastTime = currTime;
}
// phase1: check to see if we can do idle work
while (bIdle && !::PeekMessage(&(pState->m_msgCur), NULL, NULL, NULL, PM_NOREMOVE))
{
if (!OnIdle(lIdleCount++)) // call OnIdle while in bIdle state
bIdle = FALSE; // assume "no idle" state
}
// phase2: pump messages while available
do
{
// pump message, but quit on WM_QUIT
if (!PumpMessage())
return ExitInstance();
// reset "no idle" state after pumping "normal" message
//if (IsIdleMessage(&m_msgCur))
if (IsIdleMessage(&(pState->m_msgCur)))
{
bIdle = TRUE;
lIdleCount = 0;
}
}
while (::PeekMessage(&(pState->m_msgCur), NULL, NULL, NULL, PM_NOREMOVE));
}
}
most of the code is borrowed from MFC's installed cpp, I just added the following:
static DWORD lastTime = 0;
DWORD currTime = timeGetTime();
if((currTime - lastTime)>10)
{
PostMessage(renderWindowH, WM_PAINT, 0, 0);
lastTime = currTime;
}
renderWindowH is the HWND of the window of which I want to re-render every 0.1s. The problem is that the rendering only happens when there's some windows message generated, such as mouse is moved, window is resized, etc. But if I remove the "if" statement, just use "PostMessage(renderWindowH, WM_PAINT, 0, 0);" instead, the window is re-rendered correctly(just very fast, I have no control on it), and the cpu usage is nearly 100%, as expected. Now can someone please tell me why the "if" test stops window from being re-rendered? Moreover, if anyone has better method to solve my problem from another perspective, I'd like to hear about it, thanks for reading.