improved fps counter

This commit is contained in:
Djairo Hougee 2025-01-11 15:40:50 +01:00
parent 884b53655b
commit 6fdfee571a
3 changed files with 14 additions and 8 deletions

View File

@ -96,12 +96,8 @@ void Window::tick() {
float diff = (float) std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_frame).count(); float diff = (float) std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_frame).count();
this->last_frame = now; this->last_frame = now;
// TODO: remove debug line at some point
std::cout << 1000.0/diff << " fps\n";
// input // input
this->widget->tick(); this->widget->tick(1000.0/diff);
// tick render // tick render
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);

View File

@ -1,6 +1,8 @@
#include "Widget.h" #include "Widget.h"
#include "linalg/linalg.h" #include "linalg/linalg.h"
#include "consts.h" #include "consts.h"
#include <cstdio>
#include <stdlib.h>
Widget::Widget(GLFWwindow* window) { Widget::Widget(GLFWwindow* window) {
@ -15,11 +17,13 @@ Widget::Widget(GLFWwindow* window) {
this->cameraPos = Point3::init(-0.7, -1.0, -2.0); this->cameraPos = Point3::init(-0.7, -1.0, -2.0);
this->cameraUp = Vec3::init(0.0, 1.0, 0.0).normalize(); this->cameraUp = Vec3::init(0.0, 1.0, 0.0).normalize();
this->lightPos = Point3::init(1.5, 2.0, -1.0); this->lightPos = Point3::init(1.5, 2.0, -1.0);
this->fps = (char*)malloc(512*sizeof(char));
this->paused = true; this->paused = true;
this->renderOnce = false; this->renderOnce = false;
}; };
void Widget::tick() { void Widget::tick(double fps) {
if (this->renderOnce) { if (this->renderOnce) {
this->renderOnce = false; this->renderOnce = false;
this->paused = true; this->paused = true;
@ -38,12 +42,15 @@ void Widget::tick() {
ImGui::DragScalar("Z coordinate", ImGuiDataType_Double, &this->lightPos.z, 0.005f, &min, &max, "%.3f"); ImGui::DragScalar("Z coordinate", ImGuiDataType_Double, &this->lightPos.z, 0.005f, &min, &max, "%.3f");
ImGui::End(); ImGui::End();
ImGui::Begin("Pause"); ImGui::Begin("Miscellaneous");
if (ImGui::Button(this->paused ? "Unpause" : "Pause")) this->paused = !this->paused; if (ImGui::Button(this->paused ? "Unpause" : "Pause")) this->paused = !this->paused;
ImGui::SameLine();
if (ImGui::Button("render Once")) { if (ImGui::Button("render Once")) {
this->paused = !this->paused; this->paused = !this->paused;
this->renderOnce = true; this->renderOnce = true;
} }
sprintf(this->fps, "%.3f fps\n", fps);
ImGui::Text(this->fps);
ImGui::End(); ImGui::End();
ImGui::Begin("Camera Controls"); ImGui::Begin("Camera Controls");
@ -74,4 +81,5 @@ Widget::~Widget() {
ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown(); ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext(); ImGui::DestroyContext();
free(this->fps);
} }

View File

@ -13,12 +13,14 @@ public:
Vec3 cameraPos; Vec3 cameraPos;
Vec3 cameraUp; Vec3 cameraUp;
Point3 lightPos; Point3 lightPos;
bool paused; bool paused;
bool renderOnce; bool renderOnce;
char* fps;
ImGuiIO io; ImGuiIO io;
void tick(); void tick(double fps);
void render(); void render();
void copyToDevice(); void copyToDevice();