mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
Added love.window.setPosition(x, y [, displayindex]), love.window.getPosition, and optional x and y fields for t.window in love.conf and for the flags table in love.window.set/getMode (resolves issue #930.)
This commit is contained in:
@@ -124,6 +124,25 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
|
||||
sdlflags |= SDL_WINDOW_ALLOW_HIGHDPI;
|
||||
#endif
|
||||
|
||||
int x = f.x;
|
||||
int y = f.y;
|
||||
|
||||
if (f.useposition)
|
||||
{
|
||||
// The position needs to be in the global coordinate space.
|
||||
SDL_Rect displaybounds = {};
|
||||
SDL_GetDisplayBounds(f.display, &displaybounds);
|
||||
x += displaybounds.x;
|
||||
y += displaybounds.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (f.centered)
|
||||
x = y = SDL_WINDOWPOS_CENTERED_DISPLAY(f.display);
|
||||
else
|
||||
x = y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(f.display);
|
||||
}
|
||||
|
||||
graphics::Graphics *gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
|
||||
if (gfx != nullptr)
|
||||
gfx->unSetMode();
|
||||
@@ -156,9 +175,6 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
|
||||
}
|
||||
}
|
||||
|
||||
int centeredpos = SDL_WINDOWPOS_CENTERED_DISPLAY(f.display);
|
||||
int uncenteredpos = SDL_WINDOWPOS_UNDEFINED_DISPLAY(f.display);
|
||||
|
||||
if (!window)
|
||||
{
|
||||
created = false;
|
||||
@@ -167,9 +183,8 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
|
||||
setWindowGLAttributes(f.msaa, f.sRGB);
|
||||
|
||||
const char *title = windowTitle.c_str();
|
||||
int pos = f.centered ? centeredpos : uncenteredpos;
|
||||
|
||||
window = SDL_CreateWindow(title, pos, pos, width, height, sdlflags);
|
||||
window = SDL_CreateWindow(title, x, y, width, height, sdlflags);
|
||||
|
||||
if (!window && f.msaa > 0)
|
||||
{
|
||||
@@ -177,7 +192,7 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
|
||||
|
||||
window = SDL_CreateWindow(title, pos, pos, width, height, sdlflags);
|
||||
window = SDL_CreateWindow(title, x, y, width, height, sdlflags);
|
||||
f.msaa = 0;
|
||||
}
|
||||
|
||||
@@ -195,8 +210,8 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
|
||||
// Enforce minimum window dimensions.
|
||||
SDL_SetWindowMinimumSize(window, f.minwidth, f.minheight);
|
||||
|
||||
if (f.centered && !f.fullscreen)
|
||||
SDL_SetWindowPosition(window, centeredpos, centeredpos);
|
||||
if ((f.useposition || f.centered) && !f.fullscreen)
|
||||
SDL_SetWindowPosition(window, x, y);
|
||||
|
||||
SDL_RaiseWindow(window);
|
||||
|
||||
@@ -374,7 +389,8 @@ void Window::updateSettings(const WindowSettings &newsettings)
|
||||
curMode.settings.resizable = (wflags & SDL_WINDOW_RESIZABLE) != 0;
|
||||
curMode.settings.borderless = (wflags & SDL_WINDOW_BORDERLESS) != 0;
|
||||
curMode.settings.centered = newsettings.centered;
|
||||
curMode.settings.display = std::max(SDL_GetWindowDisplayIndex(window), 0);
|
||||
|
||||
getPosition(curMode.settings.x, curMode.settings.y, curMode.settings.display);
|
||||
|
||||
#if SDL_VERSION_ATLEAST(2,0,1)
|
||||
curMode.settings.highdpi = (wflags & SDL_WINDOW_ALLOW_HIGHDPI) != 0;
|
||||
@@ -543,6 +559,46 @@ void Window::getDesktopDimensions(int displayindex, int &width, int &height) con
|
||||
}
|
||||
}
|
||||
|
||||
void Window::setPosition(int x, int y, int displayindex)
|
||||
{
|
||||
if (!window)
|
||||
return;
|
||||
|
||||
displayindex = std::min(std::max(displayindex, 0), getDisplayCount() - 1);
|
||||
|
||||
SDL_Rect displaybounds = {};
|
||||
SDL_GetDisplayBounds(displayindex, &displaybounds);
|
||||
|
||||
// The position needs to be in the global coordinate space.
|
||||
x += displaybounds.x;
|
||||
y += displaybounds.y;
|
||||
|
||||
SDL_SetWindowPosition(window, x, y);
|
||||
|
||||
curMode.settings.useposition = true;
|
||||
}
|
||||
|
||||
void Window::getPosition(int &x, int &y, int &displayindex)
|
||||
{
|
||||
if (!window)
|
||||
{
|
||||
x = y = 0;
|
||||
displayindex = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
displayindex = std::max(SDL_GetWindowDisplayIndex(window), 0);
|
||||
|
||||
SDL_GetWindowPosition(window, &x, &y);
|
||||
|
||||
SDL_Rect displaybounds = {};
|
||||
SDL_GetDisplayBounds(displayindex, &displaybounds);
|
||||
|
||||
// The position needs to be in the monitor's coordinate space.
|
||||
x -= displaybounds.x;
|
||||
y -= displaybounds.y;
|
||||
}
|
||||
|
||||
bool Window::isCreated() const
|
||||
{
|
||||
return created;
|
||||
|
||||
@@ -60,6 +60,9 @@ public:
|
||||
|
||||
void getDesktopDimensions(int displayindex, int &width, int &height) const;
|
||||
|
||||
void setPosition(int x, int y, int displayindex);
|
||||
void getPosition(int &x, int &y, int &displayindex);
|
||||
|
||||
bool isCreated() const;
|
||||
|
||||
void setWindowTitle(const std::string &title);
|
||||
|
||||
Reference in New Issue
Block a user