This commit is contained in:
Ilusha 2024-03-19 17:35:02 +03:00
parent 59b1d0b89a
commit e41714c2bb
6 changed files with 140 additions and 42 deletions

View file

@ -6,46 +6,124 @@
#include "imgui.h" #include "imgui.h"
int main() { using namespace tp;
tp::ModuleManifest* deps[] = { &tp::gModuleGraphics, nullptr };
tp::ModuleManifest testModule("Example", nullptr, nullptr, deps);
if (!testModule.initialize()) { class Application {
return 1; 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; void run() {
int fps = 0; Graphics graphics(mWindow);
tp::Timer timer(1000); auto eventHandler = new EventHandler();
auto window = tp::Window::createWindow(); mWindow->setEventHandler(eventHandler);
{ mDrawTimer.reset();
tp::Graphics graphics(window); mProcTimer.reset();
mPerSecondTimer.reset();
if (window) { bool redrawNeeded = false;
while (!window->shouldClose()) {
window->processEvents();
graphics.proc();
ImGui::Text("fps: %i", fps); while (!mWindow->shouldClose()) {
if (mProcTimer.isTimeout()) {
graphics.draw(); mWindow->processEvents();
window->draw();
if (timer.isTimeout()) { while (eventHandler->isEvents()) {
fps = frames; eventHandler->processEvent();
frames = 0; processFrame(eventHandler);
timer.reset();
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();
} }

View file

@ -135,11 +135,11 @@ void Canvas::deleteImageHandle(ImageHandle image) {
} }
} }
void Canvas::proc() { void Canvas::drawBegin() {
const auto size = mContext->window->getSize(); const auto size = mContext->window->getSize();
nvgBeginFrame(mContext->vg, size.x, size.y, 1.0); nvgBeginFrame(mContext->vg, size.x, size.y, 1.0);
} }
void Canvas::draw() { void Canvas::drawEnd() {
nvgEndFrame(mContext->vg); nvgEndFrame(mContext->vg);
} }

View file

@ -47,7 +47,7 @@ DebugGUI::~DebugGUI() {
delete mContext; delete mContext;
} }
void DebugGUI::proc() { void DebugGUI::drawBegin() {
tp::HeapAllocGlobal::startIgnore(); tp::HeapAllocGlobal::startIgnore();
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
@ -57,7 +57,7 @@ void DebugGUI::proc() {
tp::HeapAllocGlobal::stopIgnore(); tp::HeapAllocGlobal::stopIgnore();
} }
void DebugGUI::draw() { void DebugGUI::drawEnd() {
ImGui::Render(); ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
} }

View file

@ -2,7 +2,7 @@
#include "Window.hpp" #include "Window.hpp"
class GLFWwindow; struct GLFWwindow;
namespace tp { namespace tp {

View file

@ -63,8 +63,8 @@ namespace tp {
void postEvent(InputID inputID, InputEvent inputEvent); // Record event void postEvent(InputID inputID, InputEvent inputEvent); // Record event
public: // User interface public: // User interface
bool isEvents(); bool isEvents() { return false; }
void processEvent(); void processEvent() {}
const Vec2F& getPointer() const; const Vec2F& getPointer() const;

View file

@ -17,8 +17,11 @@ namespace tp {
explicit DebugGUI(Window* window); explicit DebugGUI(Window* window);
~DebugGUI(); ~DebugGUI();
void proc(); void procBegin() {}
void draw(); void procEnd() {}
void drawBegin();
void drawEnd();
}; };
class Canvas { class Canvas {
@ -29,8 +32,11 @@ namespace tp {
explicit Canvas(Window* window); explicit Canvas(Window* window);
~Canvas(); ~Canvas();
void proc(); void procBegin() {}
void draw(); void procEnd() {}
void drawBegin();
void drawEnd();
public: public:
enum Align : int2 { enum Align : int2 {
@ -67,14 +73,28 @@ namespace tp {
public: public:
explicit Graphics(Window* window) : mGui(window), mCanvas(window) {} explicit Graphics(Window* window) : mGui(window), mCanvas(window) {}
void proc() { void procBegin() {
mCanvas.proc(); mCanvas.procBegin();
mGui.proc(); mGui.procBegin();
} }
void draw() { void procEnd() {
mCanvas.draw(); mCanvas.procEnd();
mGui.draw(); mGui.procEnd();
}
void drawBegin() {
mCanvas.drawBegin();
mGui.drawBegin();
}
void drawEnd() {
mCanvas.drawEnd();
mGui.drawEnd();
}
Canvas* getCanvas() {
return &mCanvas;
} }
private: private: