graphics frame rate fixed
This commit is contained in:
parent
16af8dc809
commit
fb374e71e4
7 changed files with 203 additions and 121 deletions
89
Graphics/private/GraphicApplication.cpp
Normal file
89
Graphics/private/GraphicApplication.cpp
Normal 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();
|
||||
}
|
||||
|
|
@ -3,6 +3,8 @@
|
|||
#include "WindowContext.hpp"
|
||||
#include "Allocators.hpp"
|
||||
|
||||
#include "Rect.hpp"
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
#ifdef ENV_OS_WINDOWS
|
||||
|
|
@ -92,11 +94,8 @@ void Window::destroyWindow(Window* window) {
|
|||
bool Window::shouldClose() const { return glfwWindowShouldClose(mContext->window); }
|
||||
|
||||
void Window::processEvents() {
|
||||
glfwPollEvents();
|
||||
|
||||
int w, h;
|
||||
glfwGetWindowSize(mContext->window, &w, &h);
|
||||
mSize = { (halnf) w, (halnf) h };
|
||||
glfwWaitEvents();
|
||||
checkAxisUpdates();
|
||||
}
|
||||
|
||||
void Window::draw() {
|
||||
|
|
@ -111,6 +110,35 @@ void Window::setEventHandler(EventHandler* eventHandler) { mEventHandler = event
|
|||
EventHandler* Window::getEventHandler() { return mEventHandler; }
|
||||
|
||||
|
||||
void Window::checkAxisUpdates() {
|
||||
if (!mEventHandler) return;
|
||||
|
||||
int w, h;
|
||||
glfwGetWindowSize(mContext->window, &w, &h);
|
||||
|
||||
if (mSize != Vec2F((halnf) w, (halnf) h)) {
|
||||
mSize = { (halnf) w, (halnf) h };
|
||||
mEventHandler->postEvent(InputID::WINDOW_RESIZE, {});
|
||||
}
|
||||
|
||||
double x, y;
|
||||
glfwGetCursorPos(mContext->window, &x, &y);
|
||||
|
||||
int posX, posY;
|
||||
glfwGetWindowPos(mContext->window, &posX, &posY);
|
||||
|
||||
if (mPointerPos != Vec2F{ (halnf) x, (halnf) y }) {
|
||||
mPointerPos = { (halnf) x, (halnf) y };
|
||||
|
||||
auto pos = Vec2F{ (halnf) posX, (halnf) posY };
|
||||
RectF windowRec = { { 0, 0 }, mSize };
|
||||
|
||||
if (windowRec.isInside(mPointerPos)) {
|
||||
mEventHandler->postEvent(InputID::MOUSE1, { {}, {}, mPointerPos });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
||||
|
||||
auto* self = static_cast<Window*>(glfwGetWindowUserPointer(window));
|
||||
|
|
@ -119,6 +147,8 @@ static void keyCallback(GLFWwindow* window, int key, int scancode, int action, i
|
|||
EventHandler* eventHandler = self->getEventHandler();
|
||||
if (!eventHandler) return;
|
||||
|
||||
self->checkAxisUpdates();
|
||||
|
||||
// post key event
|
||||
/*
|
||||
if (action == GLFW_PRESS) {
|
||||
|
|
@ -137,6 +167,8 @@ static void mouseButtonCallback(GLFWwindow* window, int button, int action, int
|
|||
EventHandler* eventHandler = self->getEventHandler();
|
||||
if (!eventHandler) return;
|
||||
|
||||
self->checkAxisUpdates();
|
||||
|
||||
// post mouse button event
|
||||
// inputManager->mouseButtonStates[button] = action;
|
||||
// inputManager->isEvent = true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue