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"
using namespace tp;
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);
}
void 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++;
}
// 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;
}
virtual void processFrame(EventHandler* eventHandler) {
}
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() {
tp::ModuleManifest* deps[] = { &tp::gModuleGraphics, nullptr };
tp::ModuleManifest testModule("Example", nullptr, nullptr, deps);
if (!testModule.initialize()) {
return 1;
}
int frames = 0;
int fps = 0;
tp::Timer timer(1000);
auto window = tp::Window::createWindow();
{
tp::Graphics graphics(window);
if (window) {
while (!window->shouldClose()) {
window->processEvents();
graphics.proc();
ImGui::Text("fps: %i", fps);
graphics.draw();
window->draw();
if (timer.isTimeout()) {
fps = frames;
frames = 0;
timer.reset();
}
frames++;
}
}
}
tp::Window::destroyWindow(window);
testModule.deinitialize();
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();
nvgBeginFrame(mContext->vg, size.x, size.y, 1.0);
}
void Canvas::draw() {
void Canvas::drawEnd() {
nvgEndFrame(mContext->vg);
}

View file

@ -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());
}

View file

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

View file

@ -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;

View file

@ -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: