Skip to content

Commit

Permalink
add refreshRate support
Browse files Browse the repository at this point in the history
  • Loading branch information
ColleagueRiley committed Feb 10, 2025
1 parent 38af1b4 commit 05f0b36
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
29 changes: 21 additions & 8 deletions RGFW.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ typedef RGFW_ENUM(u8, RGFW_gamepadCodes) {
/*! get the primary monitor */
RGFWDEF RGFW_monitor RGFW_getPrimaryMonitor(void);
/*! scale monitor to area */
RGFWDEF RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area);
RGFWDEF RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area, u32 refreshRate);
#endif

/* RGFW mouse loading */
Expand Down Expand Up @@ -1878,7 +1878,7 @@ void RGFW_window_center(RGFW_window* win) {

RGFW_bool RGFW_monitor_scaleToWindow(RGFW_monitor mon, RGFW_window* win) {
RGFW_ASSERT(win != NULL);
return RGFW_monitor_scale(mon, RGFW_AREA(win->r.w, win->r.h));
return RGFW_monitor_scale(mon, RGFW_AREA(win->r.w, win->r.h), 0);
}

RGFW_bool RGFW_window_shouldClose(RGFW_window* win) {
Expand Down Expand Up @@ -5030,7 +5030,13 @@ RGFW_monitor RGFW_getPrimaryMonitor(void) {
#endif
}

RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area) {
static u32 RGFW_XCalculateRefreshRate(XRRModeInfo mi) {
if (mi.hTotal == 0 || mi.vTotal == 0) return 0;

return (u32) RGFW_ROUND((double) mi.dotClock / ((double) mi.hTotal * (double) mi.vTotal));
}

RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area, u32 refreshRate) {
RGFW_GOTO_WAYLAND(1);
#ifdef RGFW_X11
#ifndef RGFW_NO_DPI
Expand All @@ -5044,7 +5050,8 @@ RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area) {
RRMode mode = None;
for (int index = 0; index < screenRes->nmode; index++) {
if (screenRes->modes[index].width == area.w &&
screenRes->modes[index].height == area.h) {
screenRes->modes[index].height == area.h &&
(refreshRate == 0 || refreshRate == RGFW_XCalculateRefreshRate(screenRes->modes[index]))) {
mode = screenRes->modes[index].id;
break;
}
Expand Down Expand Up @@ -6752,7 +6759,7 @@ RGFW_monitor RGFW_window_getMonitor(RGFW_window* win) {
return win32CreateMonitor(src);
}

RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area) {
RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area, u32 refreshRate) {
HMONITOR src = MonitorFromPoint((POINT) { mon.rect.x, mon.rect.y }, MONITOR_DEFAULTTOPRIMARY);

MONITORINFOEX monitorInfo;
Expand All @@ -6776,6 +6783,11 @@ RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area) {
dm.dmPelsWidth = area.w;
dm.dmPelsHeight = area.h;

if (refreshRate) {
dm.dmFields |= DM_DISPLAYFREQUENCY;
dm.dmDisplayFrequency = refreshRate;
}

if (ChangeDisplaySettingsEx(dd.DeviceName, &dm, NULL, CDS_TEST, NULL) == DISP_CHANGE_SUCCESSFUL) {
if (ChangeDisplaySettingsEx(dd.DeviceName, &dm, NULL, CDS_UPDATEREGISTRY, NULL) == DISP_CHANGE_SUCCESSFUL)
return RGFW_TRUE;
Expand Down Expand Up @@ -8897,7 +8909,7 @@ RGFW_monitor* RGFW_getMonitors(void) {
return RGFW_monitors;
}

RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area) {
RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area, u32 refreshRate) {
CGPoint point = { mon.rect.x, mon.rect.y };

CGDirectDisplayID display;
Expand All @@ -8913,7 +8925,8 @@ RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area) {

for (CFIndex i = 0; i < CFArrayGetCount(allModes); i++) {
CGDisplayModeRef mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(allModes, i);
if (CGDisplayModeGetWidth(mode) == area.w && CGDisplayModeGetHeight(mode) == area.h) {
if (CGDisplayModeGetWidth(mode) == area.w && CGDisplayModeGetHeight(mode) == area.h &&
(refreshRate == 0 || CGDisplayModeGetRefreshRate(mode) == refreshRate) {
CGError err = CGDisplaySetDisplayMode(display, mode, NULL);
if (err == kCGErrorSuccess) {
CFRelease(allModes);
Expand Down Expand Up @@ -9985,7 +9998,7 @@ void RGFW_window_setFullscreen(RGFW_window* win, RGFW_bool fullscreen) {
}

/* unsupported functions */
RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area) { RGFW_UNUSED(mon); RGFW_UNUSED(area); return RGFW_FALSE; }
RGFW_bool RGFW_monitor_scale(RGFW_monitor mon, RGFW_area area, u32 refreshRate) { RGFW_UNUSED(mon); RGFW_UNUSED(area); RGFW_UNUSED(refreshRate); return RGFW_FALSE; }
RGFW_monitor* RGFW_getMonitors(void) { return NULL; }
RGFW_monitor RGFW_getPrimaryMonitor(void) { return (RGFW_monitor){}; }
void RGFW_window_move(RGFW_window* win, RGFW_point v) { RGFW_UNUSED(win); RGFW_UNUSED(v); }
Expand Down
2 changes: 1 addition & 1 deletion examples/monitor/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main(void) {
RGFW_window_swapBuffers(win);
}

RGFW_monitor_scale(RGFW_window_getMonitor(win), RGFW_AREA(mon.rect.w, mon.rect.h));
RGFW_monitor_scale(RGFW_window_getMonitor(win), RGFW_AREA(mon.rect.w, mon.rect.h), 0);

RGFW_window_close(win);
return 0;
Expand Down

0 comments on commit 05f0b36

Please sign in to comment.