Skip to content

Commit 559267f

Browse files
committed
check texture smart pointer, center window on monitor where mouse is
1 parent 6e84ea1 commit 559267f

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

grey/backends/win32dx11app.hpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,23 @@ namespace grey::backends {
240240
}
241241

242242
void get_screen_center(int width, int height, int& x, int& y) {
243-
int sw = ::GetSystemMetrics(SM_CXFULLSCREEN);
244-
int sh = ::GetSystemMetrics(SM_CYFULLSCREEN);
245-
246-
x = sw / 2 - width / 2;
247-
y = sh / 2 - height / 2;
243+
// get center of the screen where mouse cursor is, not just the primary monitor
244+
245+
// mouse cursor position
246+
POINT cursor;
247+
::GetCursorPos(&cursor);
248+
249+
// get monitor and it's info
250+
HMONITOR hMonitor = ::MonitorFromPoint(cursor, MONITOR_DEFAULTTONEAREST);
251+
MONITORINFO mi = {sizeof(mi)};
252+
::GetMonitorInfo(hMonitor, &mi);
253+
RECT& wa = mi.rcWork;
254+
255+
// calculate it
256+
int sw = wa.right - wa.left;
257+
int sh = wa.bottom - wa.top;
258+
x = wa.left + (sw - width) / 2;
259+
y = wa.top + (sh - height) / 2;
248260
}
249261

250262
void resize_main_viewport(int width, int height) {

grey/widgets.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ namespace grey::widgets {
808808
void image_rounded(app& app, const std::string& key, size_t width, size_t height, float rounding,
809809
float uv0_x, float uv0_y, float uv1_x, float uv1_y) {
810810
auto tex = app.get_texture(key);
811-
if (tex->data) {
811+
if (tex && tex->data) {
812812
ImDrawList* dl = ImGui::GetWindowDrawList();
813813
ImVec2 p_min = ImGui::GetCursorScreenPos();
814814
ImVec2 p_max = ImVec2(p_min.x + width, p_min.y + height);

0 commit comments

Comments
 (0)