I have an app that uses SDL2 where you can change screen mode and resolution in runtime.
No problems with initial screen mode and resolution but there are troubles after changing to Fullscreen in runtime.
When i go from from SDL_WINDOW_FULLSCREEN to SDL_WINDOW_FULLSCREEN_DESKTOP or Windowed (with decorations) result is not as i expect.
What i expect:
FullscreenBorderless or Windowed at the center of the screen.
What i get:
Windowed at left top corner with resolution that differs from one was set and AlwaysOnTop that i can’t turn off.
Here is some code:
void ScreenResolution(const crx::TPoint& p, NScreenMode smd) {
int curW = 0;
int curH = 0;
SDL_GetWindowSize(m_pWin, &curW, &curH);
if( curW==p.x && curH==p.y && smd==ScreenMode() ) return;
ScreenMode(smd);
if(smd==NScreenMode::Windowed) {
SDL_SetWindowSize(m_pWin, p.x, p.y);
SDL_SetWindowPosition(m_pWin, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
} else if(smd==NScreenMode::FullScreen) {
auto modesNum = SDL_GetNumDisplayModes(0);
SDL_DisplayMode mode;
for(int i=0; i<modesNum; ++i) {
if( SDL_GetDisplayMode(0, i, &mode)!=0 || mode.w!=p.x || mode.h!=p.y) continue;
if( SDL_SetWindowDisplayMode(m_pWin, &mode)!=0 ) _ERROR( SDL_GetError() );
break;
}
}
}
void ScreenMode(NScreenMode mode) {
if( ScreenMode()==mode ) return;
BeforeChangeScreenMode(mode);
SDL_SetWindowFullscreen( m_pWin, ScreenModeToSDLWindowMode(mode) );
if(mode==NScreenMode::Windowed) {
SDL_SetWindowBordered(m_pWin, SDL_TRUE);
} else if(mode==NScreenMode::WindowedFullScreen) {
// Hardfix for troubles that happen on switch from Fullscreen with not max Resolution to WindowedFullScreen
SDL_MinimizeWindow(m_pWin);
SDL_MaximizeWindow(m_pWin);
}
}
int ScreenModeToSDLWindowMode(NScreenMode newMode) {
// 0 for Windowed
int md = 0;
if(newMode==NScreenMode::FullScreen) {
md = SDL_WINDOW_FULLSCREEN;
} else if(newMode==NScreenMode::WindowedFullScreen) {
md = SDL_WINDOW_FULLSCREEN_DESKTOP;
}
return md;
}
For SDL_WINDOW_FULLSCREEN_DESKTOP i found solution in SDL_MinimizeWindow & SDL_MaximizeWindow but that not working for Windowed mode.
Also i have troubles with SDL_GetDesktopDisplayMode when desktop resolution is lower that was set in Fullscreen mode in runtime.
For ex. when desktop is FullHD and in Fullscreen was set 4K i receive 4K instead if FullHD.
Code:
TPoint GetDesktopSize() {
SDL_DisplayMode dm;
SDL_GetDesktopDisplayMode(0, &dm);
return { dm.w, dm.h };
}
Also sometimes have wrong resolution and Fullscreen after switching to Windowed and back.
FullHD Fullscreen → FullHD Windowed → HD Windowed → FullHD Fullscreen (when set HD Fullscreen)
Am i doing something wrong?