graphics frame rate fixed

This commit is contained in:
Ilusha 2024-03-19 16:39:18 +03:00 committed by Ilya Shurupov
parent 16af8dc809
commit fb374e71e4
7 changed files with 203 additions and 121 deletions

View file

@ -0,0 +1,89 @@
#include "GraphicApplication.hpp"
using namespace tp;
Application::Application() :
mApplicationModule("Application", nullptr, nullptr, mModuleDeps) {
mInitialized = mApplicationModule.initialize();
if (!mInitialized) return;
mWindow = Window::createWindow();
mDrawTimer.setDuration(1000.f / mDrawPerSecond);
mProcTimer.setDuration(1000.f / mProcPerSecond);
mPerSecondTimer.setDuration(1000.f);
}
void Application::run() {
Graphics graphics(mWindow);
auto eventHandler = new EventHandler();
mWindow->setEventHandler(eventHandler);
mDrawTimer.reset();
mProcTimer.reset();
mPerSecondTimer.reset();
bool redrawNeeded = false;
while (!mWindow->shouldClose()) {
if (mProcTimer.isTimeout()) {
mWindow->processEvents();
while (eventHandler->isEvents()) {
eventHandler->processEvent();
processFrame(eventHandler);
redrawNeeded = true;
mFramesProcessed++;
}
mProcTimer.wait();
mProcTimer.reset();
}
if (mDrawTimer.isTimeout()) {
mDrawTimer.reset();
if (redrawNeeded) {
graphics.drawBegin();
drawFrame(graphics.getCanvas());
graphics.drawEnd();
mWindow->draw();
redrawNeeded = false;
mFramesDrawn++;
}
}
if (mPerSecondTimer.isTimeout()) {
mPerSecondTimer.reset();
mFramesProcessedPerSecond = mFramesProcessed;
mFramesDrawnPerSecond = mFramesDrawn;
mFramesDrawn = 0;
mFramesProcessed = 0;
}
}
delete eventHandler;
}
void Application::processFrame(EventHandler* eventHandler) {}
void Application::drawFrame(Canvas* canvas) {
// ImGui::Text("Frames processed per second: %f", mFramesProcessedPerSecond);
// ImGui::Text("Frames drawn per second: %f", mFramesDrawnPerSecond);
}
Application::~Application() {
if (!mInitialized) return;
Window::destroyWindow(mWindow);
mApplicationModule.deinitialize();
}