Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 6ee8c78

Browse files
committed
Add function to Monitors
Add function to define display monitor
1 parent 98a0f22 commit 6ee8c78

4 files changed

Lines changed: 107 additions & 3 deletions

File tree

module/includes/window/Monitors.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@
1010

1111
#include "GLFW/glfw3.h"
1212

13+
#include "Vector2.hpp"
14+
1315
namespace sw::Monitors
1416
{
1517
int GetCurrentMonitorIndex();
18+
GLFWmonitor *GetMonitor(int index);
19+
int GetMonitorsCount();
20+
sw::Vector2i GetMonitorPos(int index);
21+
std::string GetMonitorName(int index);
22+
sw::Vector2i GetMonitorPhysicalSize(int index);
23+
int GetMonitorFreshRate(int index);
1624
}
1725

1826
#endif //SHIPWRECK_ENGINE_MONITORS_HPP

module/includes/window/Window.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ namespace sw
7474
static void SetSize(sw::Vector2i size);
7575
static void SetSize(int with, int height);
7676
static void SetVisibleCursor(bool visible);
77+
static void SetMonitor(int index);
78+
static void SetClipboardText(std::string text);
7779

7880
static bool IsFullScreen();
7981
static bool IsReady();
@@ -83,6 +85,7 @@ namespace sw
8385
static Vector2i GetSize();
8486
static std::string GetTitle();
8587
static Vector2i GetPosition();
88+
static std::string GetClipBoardText();
8689
static bool HasFlag(WindowFlags flags);
8790
};
8891

module/sources/window/Monitors.cpp

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
** Monitors.cpp
66
*/
77

8+
#include "Window.hpp"
89
#include "Monitors.hpp"
9-
#include "Error.hpp"
1010

1111
int sw::Monitors::GetCurrentMonitorIndex()
1212
{
@@ -16,6 +16,78 @@ int sw::Monitors::GetCurrentMonitorIndex()
1616

1717
if (monitorCount == 1)
1818
return 0;
19-
// TODO: Handle more device
20-
throw sw::Error("The Engine handle one monitor device!", "");
19+
if (sw::Window::IsFullScreen()) {
20+
monitor = glfwGetWindowMonitor(sw::Window::GetWindow());
21+
for (int i = 0; i < monitorCount; i++)
22+
if (monitors[i] == monitor)
23+
return i;
24+
return 0;
25+
} else {
26+
int winX = 0;
27+
int winY = 0;
28+
29+
glfwGetWindowPos(sw::Window::GetWindow(), &winX, &winY);
30+
for (int i = 0; i < monitorCount; i++)
31+
{
32+
int mx = 0;
33+
int my = 0;
34+
int width = 0;
35+
int height = 0;
36+
37+
monitor = monitors[i];
38+
glfwGetMonitorWorkarea(monitor, &mx, &my, &width, &height);
39+
if (winX >= mx && winX <= (mx + width) && winY >= my && winY <= (my + height))
40+
return i;
41+
}
42+
}
43+
return (0);
44+
}
45+
46+
GLFWmonitor *sw::Monitors::GetMonitor(int index)
47+
{
48+
int count = 0;
49+
GLFWmonitor **monitors = glfwGetMonitors(&count);
50+
51+
if (index > count)
52+
throw std::out_of_range("Monitor index doesn't exist");
53+
return (monitors[index]);
54+
55+
}
56+
57+
int sw::Monitors::GetMonitorsCount()
58+
{
59+
int count = 0;
60+
glfwGetMonitors(&count);
61+
62+
return (count);
63+
}
64+
65+
sw::Vector2i sw::Monitors::GetMonitorPos(int index)
66+
{
67+
int height;
68+
int width;
69+
70+
glfwGetMonitorPos(GetMonitor(index), &width, &height);
71+
return (sw::Vector2i(width, height));
72+
}
73+
74+
std::string sw::Monitors::GetMonitorName(int index)
75+
{
76+
return (std::string(glfwGetMonitorName(GetMonitor(index))));
77+
}
78+
79+
sw::Vector2i sw::Monitors::GetMonitorPhysicalSize(int index)
80+
{
81+
int height;
82+
int width;
83+
84+
glfwGetMonitorPhysicalSize(GetMonitor(index), &width, &height);
85+
return (sw::Vector2i(width, height));
86+
}
87+
88+
int sw::Monitors::GetMonitorFreshRate(int index)
89+
{
90+
const GLFWvidmode *videoMode = glfwGetVideoMode(GetMonitor(index));
91+
92+
return (videoMode->refreshRate);
2193
}

module/sources/window/Window.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,22 @@ void sw::Window::SetVisibleCursor(bool visible)
166166
glfwSetInputMode(m_window, GLFW_CURSOR, (visible ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_HIDDEN));
167167
}
168168

169+
void sw::Window::SetMonitor(int index)
170+
{
171+
int count = 0;
172+
GLFWmonitor **monitors = glfwGetMonitors(&count);
173+
174+
if (count > index)
175+
throw std::out_of_range("Monitor not found");
176+
const GLFWvidmode *mode = glfwGetVideoMode(monitors[index]);
177+
glfwSetWindowMonitor(m_window, monitors[index], 0, 0, mode->width, mode->height, mode->refreshRate);
178+
}
179+
180+
void sw::Window::SetClipboardText(std::string text)
181+
{
182+
glfwSetClipboardString(m_window, text.c_str());
183+
}
184+
169185
bool sw::Window::HasFlag(sw::WindowFlags flags)
170186
{
171187
return (m_flags & flags);
@@ -186,6 +202,11 @@ sw::Vector2i sw::Window::GetPosition()
186202
return (m_position);
187203
}
188204

205+
std::string sw::Window::GetClipBoardText()
206+
{
207+
return (std::string(glfwGetClipboardString(m_window)));
208+
}
209+
189210
bool sw::Window::IsFullScreen()
190211
{
191212
return (m_fullScreen);

0 commit comments

Comments
 (0)