1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-07-04 15:04:36 +00:00

Use refresh rate setting + attempt to improve window/fullscreen apply logic

This commit is contained in:
fgenesis 2017-01-13 13:06:31 +01:00
parent b781b45789
commit eb8ef1fdf7
4 changed files with 136 additions and 90 deletions

View file

@ -777,6 +777,8 @@ void DSQ::setVersionLabelText()
static bool sdlVideoModeOK(int disp, const int w, const int h, const int bpp) static bool sdlVideoModeOK(int disp, const int w, const int h, const int bpp)
{ {
if(!w && !h)
return true;
#ifdef BBGE_BUILD_SDL2 #ifdef BBGE_BUILD_SDL2
SDL_DisplayMode mode; SDL_DisplayMode mode;
const int modecount = SDL_GetNumDisplayModes(disp); const int modecount = SDL_GetNumDisplayModes(disp);
@ -869,7 +871,6 @@ This build is not yet final, and as such there are a couple things lacking. They
this->setBaseTextureDirectory("gfx/"); this->setBaseTextureDirectory("gfx/");
bool fullscreen = true;
voiceOversEnabled = true; voiceOversEnabled = true;
@ -878,7 +879,7 @@ This build is not yet final, and as such there are a couple things lacking. They
particleManager->setSize(user.video.numParticles); particleManager->setSize(user.video.numParticles);
fullscreen = user.video.full; bool fullscreen = user.video.full;
useFrameBuffer = user.video.fbuffer; useFrameBuffer = user.video.fbuffer;
if (isDeveloperKeys()) if (isDeveloperKeys())
@ -899,17 +900,32 @@ This build is not yet final, and as such there are a couple things lacking. They
SDL_Init(SDL_INIT_VIDEO); SDL_Init(SDL_INIT_VIDEO);
if (fullscreen && !sdlVideoModeOK(user.video.displayindex, user.video.resx, user.video.resy, user.video.bits)) if (fullscreen && !sdlVideoModeOK(user.video.displayindex, user.video.resx, user.video.resy, user.video.bits))
{ {
// maybe we can force a sane resolution if SetVideoMode is going to fail... SDL_DisplayMode mode, closest;
user.video.resx = 800; mode.format = 0;
user.video.resy = 600; mode.driverdata = 0;
user.video.hz = 60; mode.w = user.video.resx;
user.video.displayindex = 0; mode.h = user.video.resy;
if (!sdlVideoModeOK(0, user.video.resx, user.video.resy, user.video.bits)) mode.refresh_rate = user.video.hz;
fullscreen = false; // last chance. if(SDL_GetClosestDisplayMode(user.video.displayindex, &mode, &closest))
{
user.video.resx = closest.w;
user.video.resy = closest.h;
user.video.hz = closest.refresh_rate;
}
else
{
// maybe we can force a sane resolution if SetVideoMode is going to fail...
user.video.resx = 800;
user.video.resy = 600;
user.video.hz = 60;
user.video.displayindex = 0;
if (!sdlVideoModeOK(0, user.video.resx, user.video.resy, user.video.bits))
fullscreen = false; // last chance.
}
} }
debugLog("Init Graphics Library..."); debugLog("Init Graphics Library...");
initGraphicsLibrary(user.video.resx, user.video.resy, fullscreen, user.video.vsync, user.video.bits, user.video.displayindex); initGraphicsLibrary(user.video.resx, user.video.resy, fullscreen, user.video.vsync, user.video.bits, user.video.displayindex, user.video.hz);
debugLog("OK"); debugLog("OK");
debugLog("Init Sound Library..."); debugLog("Init Sound Library...");

View file

@ -3292,24 +3292,30 @@ void InGameMenu::onOptionsSave()
dsq->user.apply(); dsq->user.apply();
if (dsq->user.video.resx != dsq->user_backup.video.resx const UserSettings::Video& bv = dsq->user_backup.video;
|| dsq->user.video.resy != dsq->user_backup.video.resy UserSettings::Video& v = dsq->user.video;
|| dsq->user.video.bits != dsq->user_backup.video.bits
|| dsq->user.video.full != dsq->user_backup.video.full if (v.resx != bv.resx
|| dsq->user.video.vsync != dsq->user_backup.video.vsync) || v.resy != bv.resy
|| v.bits != bv.bits
|| v.full != bv.full
|| v.vsync != bv.vsync
|| v.hz != bv.hz
)
{ {
dsq->initGraphics(dsq->user.video.resx, dsq->user.video.resy, dsq->user.video.full); dsq->initGraphics(v.resx, v.resy, v.full, v.vsync, v.bits, -1, v.hz);
if (dsq->confirm("", "graphics", false, 10)) { if (dsq->confirm("", "graphics", false, 10)) {
} else { } else {
dsq->user.video.resx = dsq->user_backup.video.resx; v.resx = bv.resx;
dsq->user.video.resy = dsq->user_backup.video.resy; v.resy = bv.resy;
dsq->user.video.bits = dsq->user_backup.video.bits; v.bits = bv.bits;
dsq->user.video.full = dsq->user_backup.video.full; v.full = bv.full;
dsq->user.video.vsync = dsq->user_backup.video.vsync; v.vsync = bv.vsync;
v.hz = bv.hz;
dsq->user.apply(); dsq->user.apply();
dsq->initGraphics(dsq->user.video.resx, dsq->user.video.resy, dsq->user.video.full); dsq->initGraphics(v.resx, v.resy, v.full);
} }
} }
@ -3909,7 +3915,7 @@ void InGameMenu::toggleOptionsMenu(bool f, bool skipBackup, bool isKeyConfig)
if (resBox) if (resBox)
{ {
// Note: This adds one past the original list (core->screenModes) // Note: This adds one past the original list (core->screenModes)
ScreenMode m = core->isDesktopResolution() ? ScreenMode(0,0,0) : ScreenMode(core->width, core->height, 0); ScreenMode m = core->isDesktopResolution() ? ScreenMode(0,0,0) : ScreenMode(core->width, core->height, core->getRefreshRate());
std::string mstr = screenModeStr(m); std::string mstr = screenModeStr(m);
if (!resBox->setSelectedItem(mstr)) if (!resBox->setSelectedItem(mstr))
{ {

View file

@ -146,37 +146,32 @@ void Core::setup_opengl()
} }
void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int display) void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int display, int hz)
{ {
assert(lib_graphics); assert(lib_graphics);
const int oldDisplay = getDisplayIndex(); const int oldDisplay = getDisplayIndex();
assert(oldDisplay >= 0);
if(display == -1) if(display == -1)
display = oldDisplay; display = oldDisplay;
int screenw = 0, screenh = 0; SDL_DisplayMode displaymode;
if(display >= 0) if(SDL_GetDesktopDisplayMode(display, &displaymode) != 0)
{ {
SDL_DisplayMode desktop; // fail-safe
if(SDL_GetDesktopDisplayMode(display, &desktop) == 0) displaymode.w = 800;
{ displaymode.h = 600;
screenw = desktop.w; displaymode.driverdata = 0;
screenh = desktop.h; displaymode.refresh_rate = 0;
} displaymode.format = 0;
else // fail-safe display = oldDisplay;
{ }
screenw = 800;
screenh = 600;
display = oldDisplay;
}
// Move window to specified display if necessary // Move window to specified display if necessary
if(display != oldDisplay) if(display != oldDisplay)
{ {
SDL_Rect bounds; int center = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
SDL_GetDisplayBounds(display,&bounds); SDL_SetWindowPosition(gScreen, center, center);
SDL_SetWindowPosition(gScreen, bounds.x, bounds.y);
}
} }
const int oldw = width; const int oldw = width;
@ -186,6 +181,9 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di
const bool useDesktop = w == 0 || h == 0 || (oldw && w == -1 && oldh && h == -1 && _useDesktopResolution); const bool useDesktop = w == 0 || h == 0 || (oldw && w == -1 && oldh && h == -1 && _useDesktopResolution);
const bool wasFullscreen = _fullscreen; const bool wasFullscreen = _fullscreen;
if(hz == -1)
hz = _refreshRate;
if (fullscreen == -1) if (fullscreen == -1)
fullscreen = _fullscreen; fullscreen = _fullscreen;
@ -194,8 +192,8 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di
if(useDesktop) if(useDesktop)
{ {
w = screenw; w = displaymode.w;
h = screenh; h = displaymode.h;
} }
if (w == -1) if (w == -1)
@ -210,6 +208,8 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di
_vsync = vsync; _vsync = vsync;
_fullscreen = fullscreen; _fullscreen = fullscreen;
_bpp = bpp; _bpp = bpp;
_refreshRate = hz;
displaymode.refresh_rate = hz;
#ifdef BBGE_BUILD_SDL2 #ifdef BBGE_BUILD_SDL2
if(vsync) if(vsync)
@ -221,44 +221,54 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di
SDL_GL_SetSwapInterval(0); SDL_GL_SetSwapInterval(0);
// Record window position so we can properly restore it when leaving fullscreen // Record window position so we can properly restore it when leaving fullscreen
if(fullscreen && !wasFullscreen) //if(fullscreen && !wasFullscreen)
SDL_GetWindowPosition(gScreen, &winPosX, &winPosY); // SDL_GetWindowPosition(gScreen, &winPosX, &winPosY);
if(!!fullscreen != wasFullscreen) SDL_SetWindowFullscreen(gScreen, 0);
{
int screenflags = 0;
if(fullscreen)
{
screenflags |= useDesktop ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_FULLSCREEN;
}
SDL_SetWindowFullscreen(gScreen, screenflags);
if(!fullscreen && wasFullscreen)
{
// Need to do this; else the window ends up at (0, 0) with the title bar outside the screen area
SDL_SetWindowPosition(gScreen, winPosX, winPosY);
}
}
bool resize = true; bool resize = true;
bool reloadRes = false; bool reloadRes = false;
bool maximize = true;
if(useDesktop != _useDesktopResolution) if(useDesktop != _useDesktopResolution)
{ {
if(useDesktop) int usew = 0, useh = 0;
createWindow(oldw, oldh, true, fullscreen); bool maximize = !fullscreen;
else if(!useDesktop)
createWindow(w, h, false, fullscreen); {
usew = w;
useh = h;
maximize = false;
}
if(useDesktop && !fullscreen && wasFullscreen)
{
usew = oldw;
useh = oldh;
maximize = false;
}
createWindow(usew, useh, useDesktop, false);
reloadRes = true; reloadRes = true;
if(useDesktop)
{
SDL_MaximizeWindow(gScreen);
resize = false;
}
} }
// First time called + windowed mode --> maximize window
else if(!oldw && !oldh && useDesktop && !fullscreen) if(fullscreen)
{
int screenflags = useDesktop ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_FULLSCREEN;
displaymode.w = w;
displaymode.h = h;
// must not be already in fullscreen here, otherwise new display mode doesn't apply properly
SDL_SetWindowSize(gScreen, w, h);
SDL_SetWindowDisplayMode(gScreen, &displaymode);
SDL_SetWindowFullscreen(gScreen, screenflags);
maximize = false;
}
if(!fullscreen && wasFullscreen)
{
// Need to do this; else the window ends up at (0, 0) with the title bar outside the screen area
int c = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
SDL_SetWindowPosition(gScreen, c, c);
}
if(maximize)
{ {
SDL_MaximizeWindow(gScreen); SDL_MaximizeWindow(gScreen);
resize = false; resize = false;
@ -282,7 +292,8 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di
_useDesktopResolution = useDesktop; _useDesktopResolution = useDesktop;
SDL_GetWindowSize(gScreen, &w, &h); if(!resize)
SDL_GetWindowSize(gScreen, &w, &h);
updateWindowDrawSize(w, h); updateWindowDrawSize(w, h);
@ -501,6 +512,8 @@ Core::Core(const std::string &filesystem, const std::string& extraDataDir, int n
baseCullRadius = 1; baseCullRadius = 1;
width = height = 0; width = height = 0;
_fullscreen = false; _fullscreen = false;
_refreshRate = 0;
_useDesktopResolution = false;
afterEffectManagerLayer = 0; afterEffectManagerLayer = 0;
renderObjectLayers.resize(1); renderObjectLayers.resize(1);
invGlobalScale = 1.0; invGlobalScale = 1.0;
@ -655,12 +668,18 @@ bool Core::isDesktopResolution()
int Core::getDisplayIndex() int Core::getDisplayIndex()
{ {
#ifdef BBGE_BUILD_SDL2 #ifdef BBGE_BUILD_SDL2
return SDL_GetWindowDisplayIndex(gScreen); int display = SDL_GetWindowDisplayIndex(gScreen);
return display < 0 ? 0 : display; // if there's an error, assume primary display
#else #else
return 0; return 0;
#endif #endif
} }
int Core::getRefreshRate()
{
return _refreshRate;
}
bool Core::isShuttingDown() bool Core::isShuttingDown()
{ {
return shuttingDown; return shuttingDown;
@ -827,7 +846,7 @@ void Core::setClearColor(const Vector &c)
} }
bool Core::initGraphicsLibrary(int width, int height, bool fullscreen, bool vsync, int bpp, int display) bool Core::initGraphicsLibrary(int width, int height, bool fullscreen, bool vsync, int bpp, int display, int hz)
{ {
assert(!gScreen); assert(!gScreen);
@ -886,7 +905,7 @@ bool Core::initGraphicsLibrary(int width, int height, bool fullscreen, bool vsyn
enumerateScreenModes(); enumerateScreenModes();
initGraphics(width, height, fullscreen, vsync, bpp, display); initGraphics(width, height, fullscreen, vsync, bpp, display, hz);
_hasFocus = true; _hasFocus = true;
@ -896,11 +915,6 @@ bool Core::initGraphicsLibrary(int width, int height, bool fullscreen, bool vsyn
void Core::createWindow(int w, int h, bool resizable, bool fullscreen) void Core::createWindow(int w, int h, bool resizable, bool fullscreen)
{ {
if(w <= 0)
w = 640;
if(h <= 0)
h = 480;
#ifdef BBGE_BUILD_SDL2 #ifdef BBGE_BUILD_SDL2
if(gScreen) if(gScreen)
{ {
@ -916,8 +930,17 @@ void Core::createWindow(int w, int h, bool resizable, bool fullscreen)
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
if(resizable) if(resizable)
flags |= SDL_WINDOW_RESIZABLE; flags |= SDL_WINDOW_RESIZABLE;
//if(fullscreen) // Ignore fullscreen setting here, it's applied later if(fullscreen)
// flags |= SDL_WINDOW_FULLSCREEN; {
if(!w && !h)
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
else
flags |= SDL_WINDOW_FULLSCREEN;
}
if(w <= 0)
w = 640;
if(h <= 0)
h = 480;
gScreen = SDL_CreateWindow(appName.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags); gScreen = SDL_CreateWindow(appName.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
if (gScreen == NULL) if (gScreen == NULL)
{ {

View file

@ -278,7 +278,7 @@ public:
unsigned getTicks(); unsigned getTicks();
void initGraphics(int w, int h, int fullscreen=-1, int vsync=-1, int bpp=-1, int display=-1); // pass 0x0 for desktop resolution void initGraphics(int w, int h, int fullscreen=-1, int vsync=-1, int bpp=-1, int display=-1, int hz=-1); // pass 0x0 for desktop resolution
void updateWindowDrawSize(int w, int h); void updateWindowDrawSize(int w, int h);
Vector getGameCursorPosition(); Vector getGameCursorPosition();
@ -416,6 +416,7 @@ public:
bool isFullscreen(); bool isFullscreen();
bool isDesktopResolution(); bool isDesktopResolution();
int getDisplayIndex(); int getDisplayIndex();
int getRefreshRate();
int getVirtualOffX(); int getVirtualOffX();
int getVirtualOffY(); int getVirtualOffY();
@ -483,7 +484,7 @@ protected:
bool initSoundLibrary(const std::string &defaultDevice); bool initSoundLibrary(const std::string &defaultDevice);
bool initInputLibrary(); bool initInputLibrary();
void initJoystickLibrary(); void initJoystickLibrary();
bool initGraphicsLibrary(int w, int h, bool fullscreen, bool vsync, int bpp, int display); bool initGraphicsLibrary(int w, int h, bool fullscreen, bool vsync, int bpp, int display, int hz);
void createWindow(int w, int h, bool resizable, bool fullscreen); void createWindow(int w, int h, bool resizable, bool fullscreen);
void shutdownInputLibrary(); void shutdownInputLibrary();
void shutdownJoystickLibrary(); void shutdownJoystickLibrary();
@ -513,7 +514,7 @@ protected:
int nowTicks, thenTicks; int nowTicks, thenTicks;
int _vsync, _bpp; int _vsync, _bpp, _refreshRate;
bool _fullscreen, _useDesktopResolution; bool _fullscreen, _useDesktopResolution;
int winPosX, winPosY; // pre-fullscreen int winPosX, winPosY; // pre-fullscreen