add framerate control (WIP)

This commit is contained in:
MihailRis
2024-07-25 22:33:21 +03:00
parent 664faf35e9
commit a856b4f140
6 changed files with 29 additions and 8 deletions
+20 -6
View File
@@ -9,6 +9,8 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <thread>
#include <chrono>
static debug::Logger logger("window");
@@ -20,6 +22,8 @@ uint Window::width = 0;
uint Window::height = 0;
int Window::posX = 0;
int Window::posY = 0;
int Window::framerate = -1;
double Window::prevSwap = 0.0;
bool Window::fullscreen = false;
static util::ObjectsKeeper observers_keeper;
@@ -58,8 +62,7 @@ bool Window::isIconified() {
return glfwGetWindowAttrib(window, GLFW_ICONIFIED);
}
bool Window::isFocused()
{
bool Window::isFocused() {
return glfwGetWindowAttrib(window, GLFW_FOCUSED);
}
@@ -182,7 +185,8 @@ int Window::initialize(DisplaySettings* settings){
}
}, true));
glfwSwapInterval(settings->vsync.get());
glfwSwapInterval(1);
setFramerate(settings->framerate.get());
const GLubyte* vendor = glGetString(GL_VENDOR);
const GLubyte* renderer = glGetString(GL_RENDERER);
logger.info() << "GL Vendor: " << (char*)vendor;
@@ -281,8 +285,11 @@ void Window::setShouldClose(bool flag){
glfwSetWindowShouldClose(window, flag);
}
void Window::swapInterval(int interval){
glfwSwapInterval(interval);
void Window::setFramerate(int framerate) {
if ((framerate != -1) != (Window::framerate != -1)) {
glfwSwapInterval(framerate == -1);
}
Window::framerate = framerate;
}
void Window::toggleFullscreen(){
@@ -315,9 +322,16 @@ bool Window::isFullscreen() {
return fullscreen;
}
void Window::swapBuffers(){
void Window::swapBuffers() {
double currentTime = time();
if (framerate > 0 && currentTime - prevSwap < (1.0 / framerate)) {
std::cout << static_cast<int>((1.0/framerate - (currentTime-prevSwap))*1000) << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(
static_cast<int>((1.0/framerate - (currentTime-prevSwap))*1000)));
}
glfwSwapBuffers(window);
Window::resetScissor();
prevSwap = time();
}
double Window::time() {