修复高DPI下修改窗口大小异常BUG

This commit is contained in:
Nomango 2018-07-24 22:11:16 +08:00
parent 58af967968
commit 649eaa6ee6
2 changed files with 16 additions and 18 deletions

View File

@ -54,36 +54,28 @@ void e2d::Game::start()
Window::getInstance()->poll(); Window::getInstance()->poll();
// ¿ªÊ¼ÓÎÏ· // ¿ªÊ¼ÓÎÏ·
Duration interval;
int wait = 0; int wait = 0;
Duration interval;
_quit = false; _quit = false;
_last = _now = Time::now(); _last = _now = Time::now();
while (!_quit) while (!_quit)
{ {
_now = Time::now(); _now = Time::now();
interval = _now - _last;
if (_config.isVSyncEnabled()) if (_config.isVSyncEnabled() || _frameInterval < interval)
{ {
_last += _frameInterval;
__update(); __update();
} }
else else
{ {
interval = _now - _last; wait = (_frameInterval - interval).milliseconds() - 1;
if (wait > 1)
if (_frameInterval < interval)
{ {
_last = _now; std::this_thread::sleep_for(milliseconds(wait));
__update();
}
else
{
wait = (_frameInterval - interval).milliseconds() - 1;
if (wait > 1)
{
std::this_thread::sleep_for(milliseconds(wait));
}
} }
} }
} }
@ -139,6 +131,7 @@ void e2d::Game::setConfig(const Config& config)
if (_config.isVSyncEnabled() != config.isVSyncEnabled()) if (_config.isVSyncEnabled() != config.isVSyncEnabled())
{ {
Renderer::getInstance()->discardDeviceResources(); Renderer::getInstance()->discardDeviceResources();
_last = _now;
} }
_config = config; _config = config;

View File

@ -219,6 +219,11 @@ void e2d::Window::setSize(int width, int height)
this->_size = Size(width, height); this->_size = Size(width, height);
if (_hWnd) if (_hWnd)
{ {
float dpiScaleX = 0.f, dpiScaleY = 0.f;
Renderer::getFactory()->GetDesktopDpi(&dpiScaleX, &dpiScaleY);
width = static_cast<int>(ceil(width * dpiScaleX / 96.f));
height = static_cast<int>(ceil(height * dpiScaleY / 96.f));
// 计算窗口大小 // 计算窗口大小
DWORD dwStyle = WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX &~WS_THICKFRAME; DWORD dwStyle = WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX &~WS_THICKFRAME;
RECT wr = { 0, 0, static_cast<LONG>(width), static_cast<LONG>(height) }; RECT wr = { 0, 0, static_cast<LONG>(width), static_cast<LONG>(height) };
@ -426,8 +431,8 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPar
if (wParam == SIZE_RESTORED) if (wParam == SIZE_RESTORED)
{ {
UINT ret = ::GetDpiForWindow(hWnd); UINT dpi = ::GetDpiForWindow(hWnd);
_instance->_size = Size(width * 96.0 / ret, height * 96.0 / ret); _instance->_size = Size(width * 96.0 / dpi, height * 96.0 / dpi);
} }
// 如果程序接收到一个 WM_SIZE 消息,这个方法将调整渲染 // 如果程序接收到一个 WM_SIZE 消息,这个方法将调整渲染