Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion native/cocos/application/BaseGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ int BaseGame::init() {
_windowInfo.height = _windowInfo.height == -1 ? 600 : _windowInfo.height;
_windowInfo.flags = _windowInfo.flags == -1 ? cc::ISystemWindow::CC_WINDOW_SHOWN |
cc::ISystemWindow::CC_WINDOW_RESIZABLE |
cc::ISystemWindow::CC_WINDOW_INPUT_FOCUS
cc::ISystemWindow::CC_WINDOW_INPUT_FOCUS |
cc::ISystemWindow::CC_WINDOW_ALLOW_HIGHDPI
: _windowInfo.flags;
std::call_once(_windowCreateFlag, [&]() {
ISystemWindowInfo info;
Expand Down
22 changes: 19 additions & 3 deletions native/cocos/platform/SDLHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ SDLHelper::~SDLHelper() {
}

int SDLHelper::init() {
#if CC_PLATFORM == CC_PLATFORM_WINDOWS
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
#endif
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
// Display error message
CC_LOG_ERROR("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
Expand Down Expand Up @@ -195,20 +198,30 @@ void SDLHelper::dispatchWindowEvent(uint32_t windowId, const SDL_WindowEvent &we
break;
}
case SDL_WINDOWEVENT_SIZE_CHANGED: {
auto *screen = BasePlatform::getPlatform()->getInterface<IScreen>();
CC_ASSERT(screen != nullptr);
ev.type = WindowEvent::Type::SIZE_CHANGED;
#if CC_PLATFORM == CC_PLATFORM_WINDOWS
ev.width = wevent.data1;
ev.height = wevent.data2;
#else
auto *screen = BasePlatform::getPlatform()->getInterface<IScreen>();
CC_ASSERT(screen != nullptr);
ev.width = wevent.data1 * screen->getDevicePixelRatio();
ev.height = wevent.data2 * screen->getDevicePixelRatio();
#endif
events::WindowEvent::broadcast(ev);
break;
}
case SDL_WINDOWEVENT_RESIZED: {
ev.type = WindowEvent::Type::RESIZED;
#if CC_PLATFORM == CC_PLATFORM_WINDOWS
ev.width = wevent.data1;
ev.height = wevent.data2;
#else
auto *screen = BasePlatform::getPlatform()->getInterface<IScreen>();
CC_ASSERT(screen != nullptr);
ev.type = WindowEvent::Type::RESIZED;
ev.width = wevent.data1 * screen->getDevicePixelRatio();
ev.height = wevent.data2 * screen->getDevicePixelRatio();
#endif
events::WindowEvent::broadcast(ev);
break;
}
Expand Down Expand Up @@ -315,20 +328,23 @@ void SDLHelper::dispatchSDLEvent(uint32_t windowId, const SDL_Event &sdlEvent) {
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::ENDED;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
touch.windowId = event.windowID;
events::Touch::broadcast(touch);
break;
}
case SDL_FINGERDOWN: {
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::BEGAN;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
touch.windowId = event.windowID;
events::Touch::broadcast(touch);
break;
}
case SDL_FINGERMOTION: {
const SDL_TouchFingerEvent &event = sdlEvent.tfinger;
touch.type = TouchEvent::Type::MOVED;
touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)};
touch.windowId = event.windowID;
events::Touch::broadcast(touch);
break;
}
Expand Down
19 changes: 13 additions & 6 deletions native/cocos/platform/win32/modules/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,26 @@
namespace cc {

int Screen::getDPI() const {
// 参考:https://learn.microsoft.com/zh-cn/windows/win32/api/wingdi/nf-wingdi-getdevicecaps
static int dpi = -1;
if (dpi == -1) {

dpi = 96;
HDC hScreenDC = GetDC(nullptr);
int PixelsX = GetDeviceCaps(hScreenDC, HORZRES);
int MMX = GetDeviceCaps(hScreenDC, HORZSIZE);
ReleaseDC(nullptr, hScreenDC);
dpi = static_cast<int>(254.0f * PixelsX / MMX / 10);
if (hScreenDC) {
// LOGPIXELSX 对应水平方向每逻辑英寸的像素点数
dpi = GetDeviceCaps(hScreenDC, LOGPIXELSX);
ReleaseDC(nullptr, hScreenDC);
}
// win10 1607 以上
// HWND hDesktop = GetDesktopWindow();
// dpi = GetDpiForWindow(hDesktop);
Comment on lines +45 to +47

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Remove or document this commented alternative implementation. If GetDpiForWindow is preferred for Windows 10 1607+, consider implementing version detection and using the appropriate API.

Suggested change
// win10 1607 以上
// HWND hDesktop = GetDesktopWindow();
// dpi = GetDpiForWindow(hDesktop);

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: native/cocos/platform/win32/modules/Screen.cpp
Line: 45:47

Comment:
**style:** Remove or document this commented alternative implementation. If `GetDpiForWindow` is preferred for Windows 10 1607+, consider implementing version detection and using the appropriate API.

```suggestion
```

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +45 to +47

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code or convert to a version-detection strategy if GetDpiForWindow is needed for Windows 10 1607+.

Suggested change
// win10 1607 以上
// HWND hDesktop = GetDesktopWindow();
// dpi = GetDpiForWindow(hDesktop);
// Note: GetDpiForWindow is available on Windows 10 1607+ and provides per-window DPI
// but requires runtime OS version detection. Current implementation uses GetDeviceCaps
// for broader compatibility.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: native/cocos/platform/win32/modules/Screen.cpp
Line: 45:47

Comment:
Remove commented code or convert to a version-detection strategy if `GetDpiForWindow` is needed for Windows 10 1607+.

```suggestion
        // Note: GetDpiForWindow is available on Windows 10 1607+ and provides per-window DPI
        // but requires runtime OS version detection. Current implementation uses GetDeviceCaps
        // for broader compatibility.
```

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +45 to +47

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code - it creates clutter and is tracked in version control history if needed

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: native/cocos/platform/win32/modules/Screen.cpp
Line: 45:47

Comment:
Remove commented code - it creates clutter and is tracked in version control history if needed

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

}
return dpi;
}

float Screen::getDevicePixelRatio() const {
return 1;
return getDPI() / 96.0f;
}

void Screen::setKeepScreenOn(bool value) {
Expand Down Expand Up @@ -73,4 +80,4 @@ void Screen::setDisplayStats(bool isShow) {
se::ScriptEngine::getInstance()->evalString(commandBuf);
}

} // namespace cc
} // namespace cc
16 changes: 10 additions & 6 deletions native/cocos/platform/win32/modules/SystemWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "engine/EngineEvents.h"
#include "platform/SDLHelper.h"
#include "platform/win32/WindowsPlatform.h"
#include "platform/interfaces/modules/IScreen.h"

namespace cc {
SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle)
Expand All @@ -45,27 +46,30 @@ SystemWindow::~SystemWindow() {

bool SystemWindow::createWindow(const char *title,
int w, int h, int flags) {
_window = SDLHelper::createWindow(title, w, h, flags);
float dpr = cc::BasePlatform::getPlatform()->getInterface<cc::IScreen>()->getDevicePixelRatio();
_width = w * dpr;
_height = h * dpr;
_window = SDLHelper::createWindow(title, _width, _height, flags);
if (!_window) {
return false;
}

_width = w;
_height = h;
_windowHandle = SDLHelper::getWindowHandle(_window);
return true;
}

bool SystemWindow::createWindow(const char *title,
int x, int y, int w,
int h, int flags) {
_window = SDLHelper::createWindow(title, x, y, w, h, flags);
float dpr = cc::BasePlatform::getPlatform()->getInterface<cc::IScreen>()->getDevicePixelRatio();
_width = w * dpr;
_height = h * dpr;

_window = SDLHelper::createWindow(title, x, y, _width, _height, flags);
if (!_window) {
return false;
}

_width = w;
_height = h;
_windowHandle = SDLHelper::getWindowHandle(_window);

return true;
Expand Down
14 changes: 8 additions & 6 deletions native/cocos/ui/edit-box/EditBox-win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "cocos/bindings/manual/jsb_global.h"
#include "cocos/platform/interfaces/modules/ISystemWindow.h"
#include "cocos/platform/interfaces/modules/ISystemWindowManager.h"
#include "platform/interfaces/modules/IScreen.h"

#include <stdlib.h>
#include <windows.h>
Expand Down Expand Up @@ -214,16 +215,17 @@ void EditBox::show(const EditBox::ShowInfo &showInfo) {
g_prevMainWindowProc = (WNDPROC)SetWindowLongPtr(parent, GWLP_WNDPROC, (LONG_PTR)mainWindowProc);
g_prevEditWindowProc = (WNDPROC)SetWindowLongPtr(g_hwndEditBox, GWLP_WNDPROC, (LONG_PTR)editWindowProc);
}

auto *screen = BasePlatform::getPlatform()->getInterface<IScreen>();
float dpr = screen->getDevicePixelRatio();
::SendMessageW(g_hwndEditBox, EM_LIMITTEXT, showInfo.maxLength, 0);

// SendMessage(g_hwndEditBox, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
SetWindowPos(g_hwndEditBox,
HWND_NOTOPMOST,
showInfo.x,
windowHeight - showInfo.y - showInfo.height,
showInfo.width,
showInfo.height,
showInfo.x * dpr,
windowHeight - showInfo.y * dpr - showInfo.height * dpr,
showInfo.width * dpr,
showInfo.height * dpr,
SWP_NOZORDER);

::SetWindowTextW(g_hwndEditBox, str2ws(showInfo.defaultValue).c_str());
Expand All @@ -243,7 +245,7 @@ void EditBox::show(const EditBox::ShowInfo &showInfo) {
RECT rect;

GetWindowRect(getCurrentWindowHwnd(), &rect);
float WindowRatio = (float)(rect.bottom - rect.top) / (float)CC_GET_MAIN_SYSTEM_WINDOW()->getViewSize().height;
float WindowRatio = (float)(rect.bottom - rect.top) / (float)CC_GET_MAIN_SYSTEM_WINDOW()->getViewSize().height * dpr;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WindowRatio calculation appears incorrect. GetWindowRect returns physical pixels, while getViewSize().height now also returns physical pixels (already scaled by dpr in SystemWindow.cpp:50). Multiplying by dpr again would double-scale the ratio.

Suggested change
float WindowRatio = (float)(rect.bottom - rect.top) / (float)CC_GET_MAIN_SYSTEM_WINDOW()->getViewSize().height * dpr;
float WindowRatio = (float)(rect.bottom - rect.top) / (float)CC_GET_MAIN_SYSTEM_WINDOW()->getViewSize().height;
Prompt To Fix With AI
This is a comment left during a code review.
Path: native/cocos/ui/edit-box/EditBox-win32.cpp
Line: 248:248

Comment:
The `WindowRatio` calculation appears incorrect. `GetWindowRect` returns physical pixels, while `getViewSize().height` now also returns physical pixels (already scaled by dpr in `SystemWindow.cpp:50`). Multiplying by dpr again would double-scale the ratio.

```suggestion
    float WindowRatio = (float)(rect.bottom - rect.top) / (float)CC_GET_MAIN_SYSTEM_WINDOW()->getViewSize().height;
```

How can I resolve this? If you propose a fix, please make it concise.

float JsFontRatio = float(showInfo.fontSize) / 5;
/** A probale way to calculate the increase of font size
* OriginalSize + Increase = OriginalSize * Ratio_of_js_fontSize * Ratio_of_window
Expand Down
4 changes: 4 additions & 0 deletions native/tools/simulator/frameworks/runtime-src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ else()
add_executable(${LIB_NAME} ${PROJ_SOURCES} ${PROJ_EXTRA_SOURCE})
endif()

if(WINDOWS AND MSVC)
set_property(TARGET ${LIB_NAME} PROPERTY VS_DPI_AWARE "PerMonitor")
endif()

target_link_libraries(${LIB_NAME} ${ENGINE_NAME} simulator)
target_include_directories(${LIB_NAME} PRIVATE
Classes
Expand Down
10 changes: 8 additions & 2 deletions pal/input/native/mouse-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ export class MouseInputSource {
private _getLocation (event: jsb.MouseEvent): Vec2 {
const window = this._windowManager.getWindow(event.windowId);
const windowSize = window.getViewSize();
const dpr = screenAdapter.devicePixelRatio;
let dpr = screenAdapter.devicePixelRatio;
if (systemInfo.os === OS.WINDOWS) { // 在windows下DPI变化时下发的是实际坐标
dpr = 1;
Comment on lines +120 to +121

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Touch input in touch-input.ts doesn't have the same Windows-specific DPR handling. Touch coordinates will be incorrectly scaled by DPR on Windows while mouse coordinates won't, causing inconsistent behavior.

Suggested change
if (systemInfo.os === OS.WINDOWS) { // 在windows下DPI变化时下发的是实际坐标
dpr = 1;
if (systemInfo.os === OS.WINDOWS) {
dpr = 1; // Native Windows coordinates are already in physical pixels after DPI awareness is enabled
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: pal/input/native/mouse-input.ts
Line: 120:121

Comment:
**logic:** Touch input in `touch-input.ts` doesn't have the same Windows-specific DPR handling. Touch coordinates will be incorrectly scaled by DPR on Windows while mouse coordinates won't, causing inconsistent behavior.

```suggestion
        if (systemInfo.os === OS.WINDOWS) {
            dpr = 1; // Native Windows coordinates are already in physical pixels after DPI awareness is enabled
        }
```

How can I resolve this? If you propose a fix, please make it concise.

}
const x = event.x * dpr;
const y = windowSize.height - event.y * dpr;
return new Vec2(x, y);
Expand Down Expand Up @@ -187,7 +190,10 @@ export class MouseInputSource {
const eventMouse = new EventMouse(eventType, false, this._preMousePos, mouseEvent.windowId);
eventMouse.setLocation(location.x, location.y);
eventMouse.setButton(button);
const dpr = screenAdapter.devicePixelRatio;
let dpr = screenAdapter.devicePixelRatio;
if (systemInfo.os === OS.WINDOWS) { // 在windows下DPI变化时下发的是实际坐标
dpr = 1;
}
Comment on lines +194 to +196

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Duplicated DPR adjustment logic - consider extracting to a helper method to avoid duplication and potential inconsistency

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: pal/input/native/mouse-input.ts
Line: 194:196

Comment:
**style:** Duplicated DPR adjustment logic - consider extracting to a helper method to avoid duplication and potential inconsistency

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

eventMouse.movementX = typeof mouseEvent.xDelta === 'undefined' ? 0 : mouseEvent.xDelta * dpr;
eventMouse.movementY = typeof mouseEvent.yDelta === 'undefined' ? 0 : mouseEvent.yDelta * dpr;
// update previous mouse position.
Expand Down
13 changes: 11 additions & 2 deletions pal/input/native/touch-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
*/

import { screenAdapter } from 'pal/screen-adapter';
import { systemInfo } from 'pal/system-info';
import { Size, Vec2 } from '../../../cocos/core/math';
import { EventTarget } from '../../../cocos/core/event';
import { EventTouch, Touch as CCTouch } from '../../../cocos/input/types';
import { touchManager } from '../touch-manager';
import { InputEventType } from '../../../cocos/input/types/event-enum';
import { OS } from '../../system-info/enum-type';

export type TouchCallback = (res: EventTouch) => void;

Expand Down Expand Up @@ -114,7 +116,11 @@ export class TouchInputSource {
private _dispatchEvent (eventType: InputEventType, changedTouches: Touch[], windowId: number): void {
const handleTouches: CCTouch[] = [];
const length = changedTouches.length;
const windowSize = this._windowManager.getWindow(windowId).getViewSize() as Size;
const window = this._windowManager.getWindow(windowId);
if (window === null) {
return;
}
const windowSize = window.getViewSize() as Size;
for (let i = 0; i < length; ++i) {
const changedTouch = changedTouches[i];
const touchID = changedTouch.identifier;
Expand Down Expand Up @@ -144,7 +150,10 @@ export class TouchInputSource {
}

private _getLocation (touch: globalThis.Touch, windowSize: Size): Vec2 {
const dpr = screenAdapter.devicePixelRatio;
let dpr = screenAdapter.devicePixelRatio;
if (systemInfo.os === OS.WINDOWS) { // 在windows下DPI变化时下发的是实际坐标
dpr = 1;
}
const x = touch.clientX * dpr;
const y = windowSize.height - touch.clientY * dpr;
return new Vec2(x, y);
Expand Down
2 changes: 1 addition & 1 deletion templates/compatibility-info.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"native": {
"default": ">=3.6.0"
"default": ">=3.8.0"
}
}
1 change: 1 addition & 0 deletions templates/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ cc_windows_before_target(${EXECUTABLE_NAME})
add_executable(${EXECUTABLE_NAME}
${CC_ALL_SOURCES}
)
set_property(TARGET ${EXECUTABLE_NAME} PROPERTY VS_DPI_AWARE "PerMonitor")
cc_windows_after_target(${EXECUTABLE_NAME})
Loading