I am new to Direct2D and recently I've found a wierd problem. When external power supply is plugged, my program runs at a steady FPS of 60, which I know may be a result of VSync; but after unplugging the external power supply for a while, my program drops to a steady FPS of 30 (I outputed the time interval between every two renderings and it shows 32ms).
And even if I plugged the power supply again, it still remains at 30 FPS until I reboot the computer.
Is it because my laptop shut down something when powered by battery which cuts the FPS to its half? If true, what can I do for it?
My laptop's OS is Windows 8.1.
Here are some code that may be helpful.
HRESULT hr = S_OK;
if (!m_pRenderTarget)
{
RECT rc;
GetClientRect(m_hwnd, &rc);
D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top);
D2D1_HWND_RENDER_TARGET_PROPERTIES render_target_properties = D2D1::HwndRenderTargetProperties(m_hwnd, size);
//render_target_properties.presentOptions = D2D1_PRESENT_OPTIONS_IMMEDIATELY;
//↑ Tried this and it doesn't work, don't know why
// Create a Direct2D render target.
hr = m_pDirect2dFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
render_target_properties,
&m_pRenderTarget
);
}
return hr;
The mainloop looks like this:
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,0,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
now_time = timeGetTime();
if(now_time - last_time >= 1000/MAX_FPS)
{
OutputDebugPrintf("%lf\n", now_time - last_time);
application->Update(now_time - last_time);
application->OnRender();
last_time = now_time;
}
}
I'm sure it takes little time in Update()
and it can run at a steady frame rate of 60, so there seems to be no problem in OnRender()
.
Thank you!