Skip to content

Commit

Permalink
Add framerate cap
Browse files Browse the repository at this point in the history
  • Loading branch information
pwalig committed Jul 3, 2024
1 parent 6e69129 commit b878936
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/graphics_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,31 @@

game::graphics_menu* game::graphics_menu::instance = nullptr;
bool game::graphics_menu::vsynch_enabled = true;
unsigned int game::graphics_menu::framerate_cap = 250;
bool game::graphics_menu::framerate_cap_enable = false;

game::graphics_menu::graphics_menu(const std::function<void()>& on_close) :
title("GRAPHICS", "../assets/fonts/bitmap/handwiriting-readable.png",
glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(800.0f, 1080.0f, -10.0f)), glm::vec3(30.0f, 50.0f, 1.0f))),
fov_text("FIELD OF VIEW : " + std::to_string(renderer::global_fov), "../assets/fonts/bitmap/handwiriting-readable.png",
glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(800.0f, 1000.0f, -10.0f)), glm::vec3(17.0f, 30.0f, 1.0f))),
vsynch_text("VSYNCH", "../assets/fonts/bitmap/handwiriting-readable.png",
framerate_text("FRAMERATE CAP : " + std::to_string(framerate_cap), "../assets/fonts/bitmap/handwiriting-readable.png",
glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(800.0f, 850.0f, -10.0f)), glm::vec3(17.0f, 30.0f, 1.0f))),
vsynch_text("VSYNCH", "../assets/fonts/bitmap/handwiriting-readable.png",
glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(800.0f, 700.0f, -10.0f)), glm::vec3(17.0f, 30.0f, 1.0f))),
minimization_text("MINIMIZATION", "../assets/fonts/bitmap/handwiriting-readable.png",
glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(800.0f, 775.0f, -10.0f)), glm::vec3(17.0f, 30.0f, 1.0f))),
magnification_text("MAGNIFICATION", "../assets/fonts/bitmap/handwiriting-readable.png",
glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(800.0f, 625.0f, -10.0f)), glm::vec3(17.0f, 30.0f, 1.0f))),
anisotropy_text("ANISOTROPY : ", "../assets/fonts/bitmap/handwiriting-readable.png",
magnification_text("MAGNIFICATION", "../assets/fonts/bitmap/handwiriting-readable.png",
glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(800.0f, 475.0f, -10.0f)), glm::vec3(17.0f, 30.0f, 1.0f))),
anisotropy_text("ANISOTROPY : ", "../assets/fonts/bitmap/handwiriting-readable.png",
glm::scale(glm::translate(glm::mat4(1.0f), glm::vec3(800.0f, 325.0f, -10.0f)), glm::vec3(17.0f, 30.0f, 1.0f))),
fov(glm::vec3(960.0f, 925.0f, -10.0f), glm::vec2(120.0f, 20.0f)),
vsynch(glm::vec3(1050.0f, 850.0f, -10.0f), glm::vec2(15.0f, 15.0f)),
minimization(glm::vec3(960.0f, 700.0f, -8.0f), glm::vec2(120.0f, 32.0f)),
magnification(glm::vec3(960.0f, 550.0f, -9.0f), glm::vec2(120.0f, 32.0f)),
anisotropy(glm::vec3(960.0f, 400.0f, -10.0f), glm::vec2(120.0f, 32.0f)),
framerate(glm::vec3(960.0f, 775.0f, -10.0f), glm::vec2(120.0f, 20.0f)),
framerate_enable(glm::vec3(1100.0f, 775.0f, -10.0f), glm::vec2(15.0f, 15.0f)),
vsynch(glm::vec3(1050.0f, 700.0f, -10.0f), glm::vec2(15.0f, 15.0f)),
minimization(glm::vec3(960.0f, 550.0f, -8.0f), glm::vec2(120.0f, 32.0f)),
magnification(glm::vec3(960.0f, 400.0f, -9.0f), glm::vec2(120.0f, 32.0f)),
anisotropy(glm::vec3(960.0f, 250.0f, -10.0f), glm::vec2(120.0f, 20.0f)),
back(glm::vec3(960.0f, 100.0f, -10.0f), glm::vec2(75.0f, 32.0f), "../assets/textures/White_Square.png", "BACK", "../assets/fonts/bitmap/handwiriting-readable.png")
{
// singleton stuff
Expand All @@ -37,6 +43,9 @@ game::graphics_menu::graphics_menu(const std::function<void()>& on_close) :
// SET VALUES
fov.value = (renderer::global_fov - 1.0f) / 168.0f;
fov.update_visual();
framerate.value = std::sqrt((framerate_cap - 30.0f) / 470.0f);
framerate.update_visual();
framerate_enable.update_check(framerate_cap_enable);
vsynch.update_check(vsynch_enabled);
float maxAnisotropy;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &maxAnisotropy);
Expand All @@ -49,6 +58,13 @@ game::graphics_menu::graphics_menu(const std::function<void()>& on_close) :
renderer::global_fov = (new_fov * 168.0f) + 1.0f;
this->fov_text.text = "FIELD OF VIEW: " + std::to_string(renderer::global_fov);
};
framerate.on_value_changed = [this](float new_framerate) {
framerate_cap = (new_framerate * new_framerate * 470.0f) + 30.0f;
this->framerate_text.text = "FRAMERATE CAP: " + std::to_string(framerate_cap);
};
framerate_enable.on_click.subscribe([this]() {
framerate_cap_enable = this->framerate_enable.get_value();
});
vsynch.on_click.subscribe([this]() {
vsynch_enabled = this->vsynch.get_value();
glfwSwapInterval(vsynch_enabled ? 1 : 0);
Expand Down
5 changes: 5 additions & 0 deletions src/graphics_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace game {

ui_system::ui_slider fov;
ui_system::ui_text fov_text;
ui_system::ui_slider framerate;
ui_system::ui_text framerate_text;
ui_system::ui_checkbox framerate_enable;
ui_system::ui_checkbox vsynch;
ui_system::ui_text vsynch_text;

Expand All @@ -28,5 +31,7 @@ namespace game {

static graphics_menu* instance;
static bool vsynch_enabled;
static unsigned int framerate_cap;
static bool framerate_cap_enable;
};
}
4 changes: 4 additions & 0 deletions src/main_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ int main(void)
input_system::mouse_delta = glm::vec2(0.0, 0.0); // reseting mouse delta
glfwPollEvents(); //Process callback procedures corresponding to the events that took place up to now
game::debugger::callbacks_time += glfwGetTime() - time_sum;

if (game::graphics_menu::framerate_cap_enable) {
while (glfwGetTime() < 1.0f / game::graphics_menu::framerate_cap) {};
}
}
freeOpenGLProgram(engine::window);

Expand Down

0 comments on commit b878936

Please sign in to comment.