From f545a15365688efc1b636c943aca5755661ce5b0 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Fri, 25 Jan 2019 15:33:02 +0800 Subject: [PATCH] high DPI resolution --- src/core/Application.cpp | 6 ++---- src/core/Input.cpp | 20 +++++--------------- src/core/Input.h | 4 +--- src/core/render.cpp | 7 ++++++- src/core/window.cpp | 40 ++++------------------------------------ src/core/window.h | 6 ------ 6 files changed, 18 insertions(+), 65 deletions(-) diff --git a/src/core/Application.cpp b/src/core/Application.cpp index 2e7f139d..40009204 100644 --- a/src/core/Application.cpp +++ b/src/core/Application.cpp @@ -83,8 +83,6 @@ namespace easy2d ThrowIfFailed( Input::Instance()->Init( hwnd, - Window::Instance()->GetContentScaleX(), - Window::Instance()->GetContentScaleY(), debug_ ) ); @@ -299,8 +297,8 @@ namespace easy2d { Event evt; - evt.mouse.x = GET_X_LPARAM(lparam) * Window::Instance()->GetContentScaleX(); - evt.mouse.y = GET_Y_LPARAM(lparam) * Window::Instance()->GetContentScaleY(); + evt.mouse.x = static_cast(GET_X_LPARAM(lparam)); + evt.mouse.y = static_cast(GET_Y_LPARAM(lparam)); evt.mouse.left_btn_down = !!(wparam & MK_LBUTTON); evt.mouse.left_btn_down = !!(wparam & MK_RBUTTON); diff --git a/src/core/Input.cpp b/src/core/Input.cpp index b8cbabe3..c421c080 100644 --- a/src/core/Input.cpp +++ b/src/core/Input.cpp @@ -26,8 +26,6 @@ namespace easy2d { Input::Input() : hwnd_(nullptr) - , scale_x_(1.f) - , scale_y_(1.f) { ZeroMemory(keys_, sizeof(keys_)); ZeroMemory(keys_cache_, sizeof(keys_cache_)); @@ -38,13 +36,11 @@ namespace easy2d E2D_LOG(L"Destroying input device"); } - HRESULT Input::Init(HWND hwnd, float scalex, float scaley, bool debug) + HRESULT Input::Init(HWND hwnd, bool debug) { E2D_LOG(L"Initing input device"); hwnd_ = hwnd; - scale_x_ = scalex; - scale_y_ = scaley; return S_OK; } @@ -52,7 +48,7 @@ namespace easy2d void Input::Update() { memcpy(keys_cache_, keys_, sizeof(keys_cache_)); - GetKeyboardState(keys_); + ::GetKeyboardState(keys_); } bool Input::IsDown(KeyCode code) @@ -91,18 +87,12 @@ namespace easy2d float Input::GetMouseX() { - POINT pos; - ::GetCursorPos(&pos); - ::ScreenToClient(hwnd_, &pos); - return pos.x * scale_x_; + return GetMousePos().x; } float Input::GetMouseY() { - POINT pos; - ::GetCursorPos(&pos); - ::ScreenToClient(hwnd_, &pos); - return pos.y * scale_y_; + return GetMousePos().y; } Point Input::GetMousePos() @@ -110,6 +100,6 @@ namespace easy2d POINT pos; ::GetCursorPos(&pos); ::ScreenToClient(hwnd_, &pos); - return Point{ pos.x * scale_x_, pos.y * scale_y_ }; + return Point{ static_cast(pos.x), static_cast(pos.y) }; } } \ No newline at end of file diff --git a/src/core/Input.h b/src/core/Input.h index fee3bfc3..96cb7e79 100644 --- a/src/core/Input.h +++ b/src/core/Input.h @@ -31,7 +31,7 @@ namespace easy2d E2D_DECLARE_SINGLETON(Input); public: - HRESULT Init(HWND hwnd, float scalex, float scaley, bool debug); + HRESULT Init(HWND hwnd, bool debug); // 检测键盘按键是否正被按下 bool IsDown( @@ -81,8 +81,6 @@ namespace easy2d protected: HWND hwnd_; - float scale_x_; - float scale_y_; BYTE keys_[256]; BYTE keys_cache_[256]; }; diff --git a/src/core/render.cpp b/src/core/render.cpp index 6b91ba33..09f3eaf7 100644 --- a/src/core/render.cpp +++ b/src/core/render.cpp @@ -506,7 +506,12 @@ namespace easy2d hr = Factory::Instance()->CreateHwndRenderTarget( render_target_, - D2D1::RenderTargetProperties(), + D2D1::RenderTargetProperties( + D2D1_RENDER_TARGET_TYPE_DEFAULT, + D2D1::PixelFormat(), + 96.f, + 96.f + ), D2D1::HwndRenderTargetProperties( hwnd, size, diff --git a/src/core/window.cpp b/src/core/window.cpp index 130120d9..0540055a 100644 --- a/src/core/window.cpp +++ b/src/core/window.cpp @@ -33,9 +33,7 @@ namespace easy2d { MONITORINFOEX GetMoniterInfoEx(HWND hwnd); - void GetContentScale(float* scalex, float* scaley); - - void AdjustWindow(UINT width, UINT height, DWORD style, float scalex, float scaley, UINT* win_width, UINT* win_height); + void AdjustWindow(UINT width, UINT height, DWORD style, UINT* win_width, UINT* win_height); void ChangeFullScreenResolution(int width, int height, WCHAR* device_name); @@ -46,8 +44,6 @@ namespace easy2d : handle_(nullptr) , width_(0) , height_(0) - , scalex_(1.f) - , scaley_(1.f) , device_name_(nullptr) { } @@ -105,8 +101,6 @@ namespace easy2d device_name_ = new WCHAR[len + 1]; lstrcpyW(device_name_, monitor_info_ex.szDevice); - GetContentScale(&scalex_, &scaley_); - int left = -1; int top = -1; @@ -133,8 +127,6 @@ namespace easy2d width, height, GetWindowStyle(), - scalex_, - scaley_, &win_width, &win_height ); @@ -282,7 +274,7 @@ namespace easy2d UINT screenh = info.rcWork.bottom - info.rcWork.top; UINT win_width, win_height; - AdjustWindow(width, height, GetWindowStyle(), scalex_, scaley_, &win_width, &win_height); + AdjustWindow(width, height, GetWindowStyle(), &win_width, &win_height); int left = screenw > win_width ? ((screenw - win_width) / 2) : 0; int top = screenh > win_height ? ((screenh - win_height) / 2) : 0; @@ -307,16 +299,6 @@ namespace easy2d return is_fullscreen_ ? (WINDOW_FULLSCREEN_STYLE) : (WINDOW_STYLE); } - float Window::GetContentScaleX() const - { - return scalex_; - } - - float Window::GetContentScaleY() const - { - return scaley_; - } - void Window::UpdateWindowRect() { if (!handle_) @@ -376,24 +358,10 @@ namespace easy2d return monitor_info; } - void GetContentScale(float* scalex, float* scaley) - { - const float DEFAULT_SCREEN_DPI = 96.f; - const HDC dc = GetDC(NULL); - float xdpi = static_cast(GetDeviceCaps(dc, LOGPIXELSX)); - float ydpi = static_cast(GetDeviceCaps(dc, LOGPIXELSY)); - ReleaseDC(NULL, dc); - - if (scalex) - *scalex = xdpi / DEFAULT_SCREEN_DPI; - if (scaley) - *scaley = ydpi / DEFAULT_SCREEN_DPI; - } - - void AdjustWindow(UINT width, UINT height, DWORD style, float scalex, float scaley, UINT* win_width, UINT* win_height) + void AdjustWindow(UINT width, UINT height, DWORD style, UINT* win_width, UINT* win_height) { RECT rc; - ::SetRect(&rc, 0, 0, (int)math::Ceil(width * scalex), (int)math::Ceil(height * scaley)); + ::SetRect(&rc, 0, 0, (int)width, (int)height); ::AdjustWindowRect(&rc, style, false); *win_width = rc.right - rc.left; diff --git a/src/core/window.h b/src/core/window.h index 74ef4dee..d875fc46 100644 --- a/src/core/window.h +++ b/src/core/window.h @@ -70,10 +70,6 @@ namespace easy2d DWORD GetWindowStyle() const; - float GetContentScaleX() const; - - float GetContentScaleY() const; - void UpdateWindowRect(); void SetActive(bool actived); @@ -90,8 +86,6 @@ namespace easy2d bool is_fullscreen_; int width_; int height_; - float scalex_; - float scaley_; WCHAR* device_name_; }; }