Skip to content

Commit

Permalink
reduce FPS when out of focus
Browse files Browse the repository at this point in the history
CPU usage is significant with a few instances running
  • Loading branch information
wootguy committed Apr 26, 2024
1 parent fe0d84f commit 741f14b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/editor/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
g_scroll += round(yoffset);
}

void window_focus_callback(GLFWwindow* window, int focused)
{
g_app->isFocused = focused;
}

void cursor_enter_callback(GLFWwindow* window, int entered)
{
g_app->isHovered = entered;
}

void window_iconify_callback(GLFWwindow* window, int iconified)
{
g_app->isIconified = iconified;
}


Renderer::Renderer() {
g_settings.loadDefault();
g_settings.load();
Expand Down Expand Up @@ -108,6 +124,9 @@ Renderer::Renderer() {
glfwSetWindowPosCallback(window, window_pos_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetWindowMaximizeCallback(window, window_maximize_callback);
glfwSetWindowFocusCallback(window, window_focus_callback);
glfwSetCursorEnterCallback(window, cursor_enter_callback);
glfwSetWindowIconifyCallback(window, window_iconify_callback);

glewInit();

Expand Down Expand Up @@ -488,6 +507,10 @@ void Renderer::renderLoop() {
if (glerror != GL_NO_ERROR) {
logf("Got OpenGL Error: %d\n", glerror);
}

if (!isFocused && !isHovered) {
sleepms(50);
}
}

glfwTerminate();
Expand Down
3 changes: 3 additions & 0 deletions src/editor/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class Renderer {
MergeResult mergeResult;

bool hideGui = false;
bool isFocused = false;
bool isHovered = false;
bool isIconified = false;

Renderer();
~Renderer();
Expand Down
10 changes: 10 additions & 0 deletions src/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,3 +916,13 @@ string getConfigDir()
return string("") + getenv("HOME") + "/.config/bspguy/";
}
#endif


void sleepms(uint32_t ms)
{
#ifdef _WIN32
Sleep(ms);
#else
usleep(ms * 1000);
#endif
}
4 changes: 3 additions & 1 deletion src/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,6 @@ enum class FIXUPPATH_SLASH
};

void fixupPath(std::string& path, FIXUPPATH_SLASH needstartslash, FIXUPPATH_SLASH needendslash);
void replaceAll(std::string& str, const std::string& from, const std::string& to);
void replaceAll(std::string& str, const std::string& from, const std::string& to);

void sleepms(uint32_t ms);

0 comments on commit 741f14b

Please sign in to comment.