Skip to content

Commit 89a0c5f

Browse files
committed
Added a monitor refresh rate getter.
Removed superfluous get and setters for Window framerate cap.
1 parent ccf8a95 commit 89a0c5f

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

source/Application.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void Application::simulation_loop(uint16_t physics_ticks_per_second, uint16_t re
5555
duration_since_last_input_tick += duration_since_last_frame;
5656

5757
// Check for dynamic framerate cap changes from the window
58-
uint16_t current_framerate_cap = m_window.get_framerate_cap();
58+
uint16_t current_framerate_cap = m_window.m_framerate_cap;
5959
render_rate_unlimited = current_framerate_cap == 0;
6060
render_timestep = render_rate_unlimited ? Duration::zero() : std::chrono::microseconds{1s} / current_framerate_cap;
6161

source/Platform/Window.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ namespace Platform
2222
, m_last_size_windowed{0, 0} // init in body
2323
, m_fullscreen{false}
2424
, m_VSync{false}
25-
, m_framerate_cap{0} // 0 = unlimited
2625
, m_close_requested{false}
2726
, m_handle{nullptr}
2827
, m_input{p_input_state}
28+
, m_framerate_cap{get_primary_monitor_refresh_rate()}
2929
, m_show_menu_bar{false}
3030
{
3131
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
@@ -160,11 +160,6 @@ namespace Platform
160160
m_VSync = p_enabled;
161161
}
162162

163-
void Window::set_framerate_cap(uint16_t p_framerate_cap)
164-
{
165-
m_framerate_cap = p_framerate_cap;
166-
}
167-
168163
void Window::toggle_fullscreen()
169164
{
170165
m_fullscreen = !m_fullscreen;
@@ -266,4 +261,16 @@ namespace Platform
266261
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
267262
return {static_cast<unsigned int>(mode->width), static_cast<unsigned int>(mode->height)};
268263
}
264+
uint16_t Window::get_primary_monitor_refresh_rate()
265+
{
266+
if (auto monitor = glfwGetPrimaryMonitor())
267+
if (auto video_mode = glfwGetVideoMode(monitor))
268+
{
269+
int refresh_rate = video_mode->refreshRate;
270+
if (refresh_rate > 0 && refresh_rate <= std::numeric_limits<decltype(refresh_rate)>::max())
271+
return static_cast<uint16_t>(refresh_rate);
272+
}
273+
274+
return 0;
275+
}
269276
} // namespace Platform

source/Platform/Window.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ namespace Platform
1616

1717
bool m_fullscreen;
1818
bool m_VSync; // Whether VSync is on for the window.
19-
uint16_t m_framerate_cap; // Target framerate cap (0 = unlimited).
2019
bool m_close_requested;
2120
GLFWwindow* m_handle;
2221
Input& m_input; // Window requires access to Input to use it in GLFW callbacks from glfwGetWindowUserPointer.
2322

2423
public:
24+
uint16_t m_framerate_cap; // Target framerate cap (0 = unlimited).
25+
2526
// Creates a OS window of p_width and p_height.
2627
// Takes an Input and sets its GLFW callback functions. Input depends on a Window.
2728
// Window construction requires GLFW and ImGui to be initialised before.
@@ -31,9 +32,6 @@ namespace Platform
3132
void set_VSync(bool p_enabled);
3233
bool get_VSync() const { return m_VSync; };
3334

34-
void set_framerate_cap(uint16_t p_framerate_cap);
35-
uint16_t get_framerate_cap() const { return m_framerate_cap; };
36-
3735
glm::uvec2 size() const;
3836
void on_size_callback(const glm::uvec2& p_new_size);
3937
void set_size(const glm::uvec2& p_new_size);
@@ -55,6 +53,7 @@ namespace Platform
5553

5654
// Get the max resolution of the primary monitor.
5755
static glm::uvec2 get_max_resolution();
56+
static uint16_t get_primary_monitor_refresh_rate();
5857

5958
bool m_show_menu_bar;
6059
};

source/UI/Editor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -631,12 +631,12 @@ namespace UI
631631
if (ImGui::Checkbox("VSync", &VSync))
632632
m_window.set_VSync(VSync);
633633

634-
int framerate_cap = static_cast<int>(m_window.get_framerate_cap());
634+
int framerate_cap = m_window.m_framerate_cap;
635635
if (ImGui::InputInt("Framerate cap (0 = unlimited)", &framerate_cap))
636636
{
637-
if (framerate_cap < 0)
637+
if (framerate_cap < 0 || framerate_cap > std::numeric_limits<decltype(m_window.m_framerate_cap)>::max())
638638
framerate_cap = 0;
639-
m_window.set_framerate_cap(static_cast<uint16_t>(framerate_cap));
639+
m_window.m_framerate_cap = static_cast<uint16_t>(framerate_cap);
640640
}
641641

642642
ImGui::SeparatorText("Renderer");
@@ -647,7 +647,7 @@ namespace UI
647647
debug_options.m_show_light_positions = false;
648648
debug_options.m_show_mesh_normals = false;
649649
m_window.set_VSync(true);
650-
m_window.set_framerate_cap(0);
650+
m_window.m_framerate_cap = 0;
651651
m_openGL_renderer.reset_debug_options();
652652
}
653653
}

0 commit comments

Comments
 (0)