Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 2e5c64d

Browse files
authored
Use SDL_GetWindowSize and SDL_WarpMouseInWindow in Windows (#443)
Add few WindowSize and WarpMouseInWindow to class OSWindow Remove few related #ifdef _WIN32
1 parent 7579e3a commit 2e5c64d

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

src/libs/window/include/os_window.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
namespace storm
88
{
9+
struct WindowSize
10+
{
11+
int width{};
12+
int height{};
13+
};
14+
915
//! Abstract window
1016
class OSWindow
1117
{
@@ -38,6 +44,8 @@ class OSWindow
3844
virtual int Width() const = 0;
3945
//! Current height of window
4046
virtual int Height() const = 0;
47+
//! Current window size
48+
virtual WindowSize GetWindowSize() const = 0;
4149
//! Is window fullscreen
4250
virtual bool Fullscreen() const = 0;
4351
//! Window title
@@ -47,6 +55,8 @@ class OSWindow
4755
virtual void SetFullscreen(bool fullscreen) = 0;
4856
//! Resize window
4957
virtual void Resize(int width, int height) = 0;
58+
//! Warp mouse
59+
virtual void WarpMouseInWindow(int x, int y) = 0;
5060
//! Set window title
5161
virtual void SetTitle(const std::string &title) = 0;
5262
//! Set window gamma

src/libs/window/src/sdl_window.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ int SDLWindow::Height() const
5555
return h;
5656
}
5757

58+
WindowSize SDLWindow::GetWindowSize() const
59+
{
60+
int w, h;
61+
SDL_GetWindowSize(window_.get(), &w, &h);
62+
return {w, h};
63+
}
64+
5865
bool SDLWindow::Fullscreen() const
5966
{
6067
return fullscreen_;
@@ -76,6 +83,11 @@ void SDLWindow::Resize(int width, int height)
7683
SDL_SetWindowSize(window_.get(), width, height);
7784
}
7885

86+
void SDLWindow::WarpMouseInWindow(int x, int y)
87+
{
88+
SDL_WarpMouseInWindow(window_.get(), x, y);
89+
}
90+
7991
void SDLWindow::SetTitle(const std::string &title)
8092
{
8193
SDL_SetWindowTitle(window_.get(), title.c_str());

src/libs/window/src/sdl_window.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class SDLWindow : public OSWindow
1818

1919
int Width() const override;
2020
int Height() const override;
21+
WindowSize GetWindowSize() const override;
2122
bool Fullscreen() const override;
2223
std::string Title() const override;
2324

2425
void SetFullscreen(bool fullscreen) override;
2526
void Resize(int width, int height) override;
27+
void WarpMouseInWindow(int x, int y) override;
2628
void SetTitle(const std::string &title) override;
2729
void SetGamma(const uint16_t (&red)[256], const uint16_t (&green)[256], const uint16_t (&blue)[256]) override;
2830

src/libs/xinterface/src/xinterface.cpp

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,20 +1067,12 @@ void XINTERFACE::LoadIni()
10671067
if (!ini)
10681068
throw std::runtime_error("ini file not found!");
10691069

1070-
int sdlScreenWidth, sdlScreenHeight;
1071-
#ifdef _WIN32 // FIX_LINUX GetWindowRect
1072-
RECT Screen_Rect;
1073-
GetWindowRect(static_cast<HWND>(core.GetWindow()->OSHandle()), &Screen_Rect);
1074-
sdlScreenWidth = Screen_Rect.right - Screen_Rect.left;
1075-
sdlScreenHeight = Screen_Rect.bottom - Screen_Rect.top;
1076-
#else
1077-
SDL_GetWindowSize(reinterpret_cast<SDL_Window *>(core.GetWindow()->OSHandle()), &sdlScreenWidth, &sdlScreenHeight);
1078-
#endif
1070+
auto windowSize = core.GetWindow()->GetWindowSize();
10791071

10801072
fScale = 1.0f;
10811073
const auto screenSize = core.GetScreenSize();
10821074
dwScreenHeight = screenSize.height;
1083-
dwScreenWidth = sdlScreenWidth * dwScreenHeight / sdlScreenHeight;
1075+
dwScreenWidth = windowSize.width * dwScreenHeight / windowSize.height;
10841076
if (dwScreenWidth < screenSize.width)
10851077
dwScreenWidth = screenSize.width;
10861078
GlobalScreenRect.top = 0;
@@ -1092,7 +1084,7 @@ void XINTERFACE::LoadIni()
10921084
bool sectionFound = false;
10931085
if (ini->GetSectionName(platform, sizeof(platform) - 1))
10941086
{
1095-
float windowRatio = (float)sdlScreenWidth / (float)sdlScreenHeight;
1087+
float windowRatio = (float)windowSize.width / (float)windowSize.height;
10961088
float iniRatio;
10971089
char splitPlatform[23], *platformW, *platformH;
10981090
do
@@ -1163,13 +1155,7 @@ void XINTERFACE::LoadIni()
11631155
sscanf(param, "%[^,],%d,size:(%d,%d),pos:(%d,%d)", param2, &m_lMouseSensitive, &MouseSize.x, &MouseSize.y,
11641156
&m_lXMouse, &m_lYMouse);
11651157
m_idTex = pRenderService->TextureCreate(param2);
1166-
// RECT Screen_Rect;
1167-
// GetWindowRect(core.GetAppHWND(), &Screen_Rect);
1168-
#ifdef _WIN32 // FIX_LINUX Cursor
1169-
lock_x = Screen_Rect.left + (Screen_Rect.right - Screen_Rect.left) / 2;
1170-
lock_y = Screen_Rect.top + (Screen_Rect.bottom - Screen_Rect.top) / 2;
1171-
SetCursorPos(lock_x, lock_y);
1172-
#endif
1158+
core.GetWindow()->WarpMouseInWindow(windowSize.width / 2, windowSize.height / 2);
11731159
fXMousePos = static_cast<float>(dwScreenWidth / 2);
11741160
fYMousePos = static_cast<float>(dwScreenHeight / 2);
11751161
for (int i = 0; i < 4; i++)

0 commit comments

Comments
 (0)