CHANGE: Added window API function glfwGetMonitorFromWindow. The function returns the monitor which has the largest area of intersection with the area of the specified window.

This commit is contained in:
Carsten Tewes 2022-11-20 14:20:30 +01:00
parent dd8a678a66
commit 577f9f9e62
2 changed files with 110 additions and 0 deletions

View File

@ -3890,6 +3890,28 @@ GLFWAPI void glfwFocusWindow(GLFWwindow* window);
*/
GLFWAPI void glfwRequestWindowAttention(GLFWwindow* window);
/*! @brief Returns the monitor with the largest intersection area of the window
*
* This function returns the handle of the monitor which has the largest area of
* intersection with the area of the specified window
*
* @param[in] window The window to query.
* @return The monitor, or `NULL`
* [error](@ref error_handling) occurred.
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref window_monitor
* @sa @ref glfwGetWindowMonitor
*
* @since Added in version 3.4.
*
* @ingroup window
*/
GLFWAPI GLFWmonitor* glfwGetMonitorFromWindow(GLFWwindow* window);
/*! @brief Returns the monitor that the window uses for full screen mode.
*
* This function returns the handle of the monitor that the specified window is

View File

@ -954,6 +954,94 @@ GLFWAPI void glfwSetWindowAttrib(GLFWwindow* handle, int attrib, int value)
_glfwInputError(GLFW_INVALID_ENUM, "Invalid window attribute 0x%08X", attrib);
}
typedef struct _rect { int x, y; int w, h; } _rect;
#define RECT_INTERSECTS(ra, rb) \
((ra)->x <= ((rb)->x + (rb)->w) && ((ra)->x + (ra)->w) >= (rb)->x && \
(ra)->y <= ((rb)->y + (rb)->h) && ((ra)->y + (ra)->h) >= (rb)->y)
static _rect _get_intersection(_rect* ra, _rect* rb)
{
_rect result = { 0, 0, 0, 0 };
if (RECT_INTERSECTS(ra, rb))
{
result.x = _glfw_max(ra->x, rb->x);
result.w = _glfw_min((ra->x + ra->w), (rb->x + rb->w)) - result.x;
result.y = _glfw_max(ra->y, rb->y);
result.h = _glfw_min((ra->y + ra->h), (rb->y + rb->h)) - result.y;
}
return result;
}
GLFWAPI GLFWmonitor* glfwGetMonitorFromWindow(GLFWwindow* window)
{
assert(window != NULL);
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
GLFWmonitor* result = NULL;
int monitorCount;
GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
if (monitorCount == 1)
{
result = monitors[0];
}
else if (monitorCount > 1)
{
_rect windowRect;
glfwGetWindowPos(window, &windowRect.x, &windowRect.y);
int windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
windowRect.w = windowWidth;
windowRect.h = windowHeight;
int windowFrameLeft, windowFrameTop, windowFrameRight, windowFrameBottom;
glfwGetWindowFrameSize(window, &windowFrameLeft, &windowFrameTop,
&windowFrameRight, &windowFrameBottom);
windowRect.x -= windowFrameLeft;
windowRect.y -= windowFrameTop;
windowRect.w += windowFrameLeft + windowFrameRight;
windowRect.h += windowFrameTop + windowFrameBottom;
_rect currentRect = { 0, 0, 0, 0 };
_rect overlapRect = { 0, 0, 0, 0 };
unsigned int currentDim, overlapDim;
int overlapMonitor = -1;
int i;
for (i = 0; i < monitorCount; i++)
{
_rect monitorRect;
glfwGetMonitorPos(monitors[i], &monitorRect.x, &monitorRect.y);
const GLFWvidmode* vidmode = glfwGetVideoMode(monitors[i]);
monitorRect.w = vidmode->width;
monitorRect.h = vidmode->height;
currentRect = _get_intersection(&windowRect, &monitorRect);
currentDim = currentRect.w * currentRect.h;
overlapDim = overlapRect.w * overlapRect.h;
if (currentDim > 0 && currentDim > overlapDim)
{
overlapRect = currentRect;
overlapMonitor = i;
}
}
if (overlapMonitor >= 0)
{
result = monitors[overlapMonitor];
}
}
return result;
}
GLFWAPI GLFWmonitor* glfwGetWindowMonitor(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;