From 16af8dc809e84181b5b544c37f2ff57607a39d29 Mon Sep 17 00:00:00 2001 From: Ilusha Date: Tue, 19 Mar 2024 17:35:02 +0300 Subject: [PATCH] tmp --- Graphics/examples/Example.cpp | 128 +++++++++++++++++++++++------ Graphics/private/Canvas.cpp | 4 +- Graphics/private/DebugGUI.cpp | 4 +- Graphics/private/WindowContext.hpp | 2 +- Graphics/public/EventHandler.hpp | 4 +- Graphics/public/Graphics.hpp | 40 ++++++--- 6 files changed, 140 insertions(+), 42 deletions(-) diff --git a/Graphics/examples/Example.cpp b/Graphics/examples/Example.cpp index deca845..489a08b 100644 --- a/Graphics/examples/Example.cpp +++ b/Graphics/examples/Example.cpp @@ -6,46 +6,124 @@ #include "imgui.h" -int main() { - tp::ModuleManifest* deps[] = { &tp::gModuleGraphics, nullptr }; - tp::ModuleManifest testModule("Example", nullptr, nullptr, deps); +using namespace tp; - if (!testModule.initialize()) { - return 1; +class Application { +public: + Application() : + mApplicaitonModule("Application", nullptr, nullptr, mModuleDeps) { + mInitialized = mApplicaitonModule.initialize(); + if (!mInitialized) return; + + mWindow = Window::createWindow(); + + mDrawTimer.setDuration(1000.f / mDrawPerSecond); + mProcTimer.setDuration(1000.f / mProcPerSecond); + mPerSecondTimer.setDuration(1000.f); } - int frames = 0; - int fps = 0; - tp::Timer timer(1000); + void run() { + Graphics graphics(mWindow); + auto eventHandler = new EventHandler(); - auto window = tp::Window::createWindow(); + mWindow->setEventHandler(eventHandler); - { - tp::Graphics graphics(window); + mDrawTimer.reset(); + mProcTimer.reset(); + mPerSecondTimer.reset(); - if (window) { - while (!window->shouldClose()) { - window->processEvents(); - graphics.proc(); + bool redrawNeeded = false; - ImGui::Text("fps: %i", fps); + while (!mWindow->shouldClose()) { + if (mProcTimer.isTimeout()) { - graphics.draw(); - window->draw(); + mWindow->processEvents(); - if (timer.isTimeout()) { - fps = frames; - frames = 0; - timer.reset(); + while (eventHandler->isEvents()) { + eventHandler->processEvent(); + processFrame(eventHandler); + + redrawNeeded = true; + mFramesProcessed++; } - frames++; + // graphics.procBegin(); + // graphics.procEnd(); + 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; } - tp::Window::destroyWindow(window); + virtual void processFrame(EventHandler* eventHandler) { + } - testModule.deinitialize(); + virtual void drawFrame(Canvas* canvas) { + ImGui::Text("Frames processed per second: %f", mFramesProcessedPerSecond); + ImGui::Text("Frames drawn per second: %f", mFramesDrawnPerSecond); + } + + ~Application() { + if (!mInitialized) return; + Window::destroyWindow(mWindow); + mApplicaitonModule.deinitialize(); + } + +private: + ModuleManifest* mModuleDeps[2] = { &gModuleGraphics, nullptr }; + ModuleManifest mApplicaitonModule; + + Window* mWindow = nullptr; + + bool mInitialized = false; + + ualni mDrawPerSecond = 60; + ualni mProcPerSecond = 1000; + + Timer mDrawTimer; + Timer mProcTimer; + Timer mPerSecondTimer; + + halnf mFramesProcessedPerSecond = 0; + halnf mFramesDrawnPerSecond = 0; + + halnf mFramesProcessed = 0; + halnf mFramesDrawn = 0; +}; + +int main() { + Application app; + app.run(); } diff --git a/Graphics/private/Canvas.cpp b/Graphics/private/Canvas.cpp index 418454c..94c5df8 100644 --- a/Graphics/private/Canvas.cpp +++ b/Graphics/private/Canvas.cpp @@ -135,11 +135,11 @@ void Canvas::deleteImageHandle(ImageHandle image) { } } -void Canvas::proc() { +void Canvas::drawBegin() { const auto size = mContext->window->getSize(); nvgBeginFrame(mContext->vg, size.x, size.y, 1.0); } -void Canvas::draw() { +void Canvas::drawEnd() { nvgEndFrame(mContext->vg); } diff --git a/Graphics/private/DebugGUI.cpp b/Graphics/private/DebugGUI.cpp index ca608e5..5066fa3 100644 --- a/Graphics/private/DebugGUI.cpp +++ b/Graphics/private/DebugGUI.cpp @@ -47,7 +47,7 @@ DebugGUI::~DebugGUI() { delete mContext; } -void DebugGUI::proc() { +void DebugGUI::drawBegin() { tp::HeapAllocGlobal::startIgnore(); ImGui_ImplOpenGL3_NewFrame(); @@ -57,7 +57,7 @@ void DebugGUI::proc() { tp::HeapAllocGlobal::stopIgnore(); } -void DebugGUI::draw() { +void DebugGUI::drawEnd() { ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); } \ No newline at end of file diff --git a/Graphics/private/WindowContext.hpp b/Graphics/private/WindowContext.hpp index 02fdd40..c237297 100644 --- a/Graphics/private/WindowContext.hpp +++ b/Graphics/private/WindowContext.hpp @@ -2,7 +2,7 @@ #include "Window.hpp" -class GLFWwindow; +struct GLFWwindow; namespace tp { diff --git a/Graphics/public/EventHandler.hpp b/Graphics/public/EventHandler.hpp index 38d5364..78f8f5f 100644 --- a/Graphics/public/EventHandler.hpp +++ b/Graphics/public/EventHandler.hpp @@ -63,8 +63,8 @@ namespace tp { void postEvent(InputID inputID, InputEvent inputEvent); // Record event public: // User interface - bool isEvents(); - void processEvent(); + bool isEvents() { return false; } + void processEvent() {} const Vec2F& getPointer() const; diff --git a/Graphics/public/Graphics.hpp b/Graphics/public/Graphics.hpp index 7f26869..6eb5aa6 100644 --- a/Graphics/public/Graphics.hpp +++ b/Graphics/public/Graphics.hpp @@ -17,8 +17,11 @@ namespace tp { explicit DebugGUI(Window* window); ~DebugGUI(); - void proc(); - void draw(); + void procBegin() {} + void procEnd() {} + + void drawBegin(); + void drawEnd(); }; class Canvas { @@ -29,8 +32,11 @@ namespace tp { explicit Canvas(Window* window); ~Canvas(); - void proc(); - void draw(); + void procBegin() {} + void procEnd() {} + + void drawBegin(); + void drawEnd(); public: enum Align : int2 { @@ -67,14 +73,28 @@ namespace tp { public: explicit Graphics(Window* window) : mGui(window), mCanvas(window) {} - void proc() { - mCanvas.proc(); - mGui.proc(); + void procBegin() { + mCanvas.procBegin(); + mGui.procBegin(); } - void draw() { - mCanvas.draw(); - mGui.draw(); + void procEnd() { + mCanvas.procEnd(); + mGui.procEnd(); + } + + void drawBegin() { + mCanvas.drawBegin(); + mGui.drawBegin(); + } + + void drawEnd() { + mCanvas.drawEnd(); + mGui.drawEnd(); + } + + Canvas* getCanvas() { + return &mCanvas; } private: