Fixes and improvements to the graphics module
This commit is contained in:
parent
860474654a
commit
5e3f5594b8
19 changed files with 161 additions and 55 deletions
|
|
@ -11,7 +11,7 @@ class ExampleApplication : public Application {
|
|||
public:
|
||||
ExampleApplication() = default;
|
||||
|
||||
void processFrame(EventHandler* eventHandler) override {
|
||||
void processFrame(EventHandler* eventHandler, halnf) override {
|
||||
// example
|
||||
}
|
||||
|
||||
|
|
@ -20,10 +20,11 @@ public:
|
|||
|
||||
ImGui::ShowDemoWindow();
|
||||
|
||||
ImGui::Text("Frames processed per second: %f", this->mFramesProcessedPerSecond);
|
||||
ImGui::Text("Frames drawn per second: %f", this->mFramesDrawnPerSecond);
|
||||
drawDebug();
|
||||
}
|
||||
|
||||
bool forceNewFrame() override { return false; }
|
||||
|
||||
virtual ~ExampleApplication() = default;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,9 @@ Canvas::~Canvas() {
|
|||
delete mContext;
|
||||
}
|
||||
|
||||
void Canvas::rect(const RectF& rec, const RGBA& col, halnf round) {
|
||||
void Canvas::rect(RectF rec, const RGBA& col, halnf round) {
|
||||
rec.pos += mOrigin;
|
||||
|
||||
nvgBeginPath(mContext->vg);
|
||||
|
||||
if (round == 0) {
|
||||
|
|
@ -50,7 +52,9 @@ void Canvas::rect(const RectF& rec, const RGBA& col, halnf round) {
|
|||
nvgFill(mContext->vg);
|
||||
}
|
||||
|
||||
void Canvas::circle(const Vec2F& pos, halnf size, const RGBA& col) {
|
||||
void Canvas::circle(Vec2F pos, halnf size, const RGBA& col) {
|
||||
pos += mOrigin;
|
||||
|
||||
nvgBeginPath(mContext->vg);
|
||||
nvgCircle(mContext->vg, pos.x, pos.y, size);
|
||||
|
||||
|
|
@ -58,6 +62,10 @@ void Canvas::circle(const Vec2F& pos, halnf size, const RGBA& col) {
|
|||
nvgFill(mContext->vg);
|
||||
}
|
||||
|
||||
void Canvas::setOrigin(const Vec2F& origin) {
|
||||
mOrigin = origin;
|
||||
}
|
||||
|
||||
void Canvas::pushClamp(const RectF& rec) {
|
||||
RectF intersection = rec;
|
||||
if (mScissors.size()) {
|
||||
|
|
@ -82,7 +90,9 @@ void Canvas::popClamp() {
|
|||
void Canvas::text(
|
||||
const char* string, const RectF& aRec, halnf size, Align align, halnf marging, const RGBA& col
|
||||
) {
|
||||
|
||||
RectF rec = { aRec.x + marging, aRec.y + marging, aRec.z - marging * 2, aRec.w - marging * 2 };
|
||||
rec.pos += mOrigin;
|
||||
|
||||
pushClamp(rec);
|
||||
|
||||
|
|
@ -121,7 +131,9 @@ void Canvas::text(
|
|||
popClamp();
|
||||
}
|
||||
|
||||
void Canvas::drawImage(const RectF& rec, ImageHandle* image, halnf angle, halnf alpha, halnf rounding) {
|
||||
void Canvas::drawImage(RectF rec, ImageHandle* image, halnf angle, halnf alpha, halnf rounding) {
|
||||
rec.pos += mOrigin;
|
||||
|
||||
auto imgPaint = nvgImagePattern(mContext->vg, rec.x, rec.y, rec.z, rec.w, angle, image->id, alpha);
|
||||
nvgBeginPath(mContext->vg);
|
||||
nvgRoundedRect(mContext->vg, rec.x, rec.y, rec.z, rec.w, rounding);
|
||||
|
|
|
|||
|
|
@ -42,6 +42,14 @@ bool transitionsReduce[4][4] = {
|
|||
{ true, true, true, true },
|
||||
};
|
||||
|
||||
void EventHandler::processAllEvent() {
|
||||
mMutex.lock();
|
||||
while (mEventQueue.size()) {
|
||||
processEventUnguarded();
|
||||
}
|
||||
mMutex.unlock();
|
||||
}
|
||||
|
||||
void EventHandler::processEvent() {
|
||||
mMutex.lock();
|
||||
|
||||
|
|
@ -50,10 +58,17 @@ void EventHandler::processEvent() {
|
|||
return;
|
||||
}
|
||||
|
||||
auto lastEvent = &mEventQueue.last();
|
||||
processEventUnguarded();
|
||||
|
||||
const auto& eventData = lastEvent->second;
|
||||
const auto& inputId = lastEvent->first;
|
||||
mMutex.unlock();
|
||||
}
|
||||
|
||||
void EventHandler::processEventUnguarded() {
|
||||
|
||||
auto firstEvent = &mEventQueue.first();
|
||||
|
||||
const auto& eventData = firstEvent->second;
|
||||
const auto& inputId = firstEvent->first;
|
||||
|
||||
switch (eventData.type) {
|
||||
case InputEvent::Type::MOUSE_POS:
|
||||
|
|
@ -71,8 +86,6 @@ void EventHandler::processEvent() {
|
|||
|
||||
mInputStates[(int) inputId].mCurrentState = transitions[currentState][reportedEvent];
|
||||
|
||||
// printf("%i - %i \n", reportedEvent, mInputStates[(int) InputID::MOUSE1].mCurrentState);
|
||||
|
||||
if (transitionsReduce[currentState][reportedEvent]) {
|
||||
mEventQueue.popFront();
|
||||
}
|
||||
|
|
@ -98,13 +111,15 @@ void EventHandler::processEvent() {
|
|||
}
|
||||
|
||||
mPointerPressure = mInputStates[(int) InputID::MOUSE1].mCurrentState != InputState::State::NONE;
|
||||
|
||||
mMutex.unlock();
|
||||
}
|
||||
|
||||
const Vec2F& EventHandler::getPointer() const { return mPointer; }
|
||||
void EventHandler::setCursorOrigin(const Vec2F& origin) {
|
||||
mPointerOrigin = origin;
|
||||
}
|
||||
|
||||
const Vec2F& EventHandler::getPointerPrev() const { return mPointerPrev; }
|
||||
Vec2F EventHandler::getPointer() const { return mPointer - mPointerOrigin; }
|
||||
|
||||
Vec2F EventHandler::getPointerPrev() const { return mPointerPrev - mPointerOrigin; }
|
||||
|
||||
bool EventHandler::isPressed(InputID id) const {
|
||||
return mInputStates[(int) id].mCurrentState == InputState::State::PRESSED;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
|
||||
#include "GraphicApplication.hpp"
|
||||
|
||||
#include "imgui.h"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
Application::Application() {
|
||||
|
|
@ -12,7 +14,7 @@ Application::Application() {
|
|||
mPerSecondTimer.setDuration(1000.f);
|
||||
}
|
||||
|
||||
void Application::run() {
|
||||
void Application::runDefaultLoop() {
|
||||
auto eventHandler = new EventHandler();
|
||||
|
||||
mWindow->setEventHandler(eventHandler);
|
||||
|
|
@ -25,8 +27,8 @@ void Application::run() {
|
|||
|
||||
// proc first frame by default
|
||||
{
|
||||
mWindow->processEvents();
|
||||
processFrame(eventHandler);
|
||||
mWindow->processEvents(false);
|
||||
processFrame(eventHandler, 0);
|
||||
|
||||
mGraphics->drawBegin();
|
||||
drawFrame(mGraphics->getCanvas());
|
||||
|
|
@ -34,17 +36,33 @@ void Application::run() {
|
|||
mWindow->draw();
|
||||
}
|
||||
|
||||
time_ms prevProcTime = get_time();
|
||||
|
||||
bool isForcedNewFrame = true;
|
||||
|
||||
while (!mWindow->shouldClose()) {
|
||||
mWindow->processEvents();
|
||||
mWindow->processEvents(!isForcedNewFrame);
|
||||
updateGlobalTime();
|
||||
|
||||
if (mProcTimer.isTimeout() || eventHandler->isEvents()) {
|
||||
if (mProcTimer.isTimeout() || eventHandler->isEvents() || isForcedNewFrame) {
|
||||
|
||||
while ((eventHandler->isEvents() || isForcedNewFrame)) {
|
||||
|
||||
while (eventHandler->isEvents()) {
|
||||
eventHandler->processEvent();
|
||||
processFrame(eventHandler);
|
||||
|
||||
time_ms currentTime = get_time();
|
||||
processFrame(eventHandler, halnf(currentTime - prevProcTime));
|
||||
prevProcTime = currentTime;
|
||||
|
||||
redrawNeeded = true;
|
||||
mFramesProcessed++;
|
||||
|
||||
isForcedNewFrame = forceNewFrame();
|
||||
|
||||
if (isForcedNewFrame) {
|
||||
// mWindow->processEvents();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mProcTimer.wait();
|
||||
|
|
@ -82,13 +100,46 @@ void Application::run() {
|
|||
delete eventHandler;
|
||||
}
|
||||
|
||||
void Application::processFrame(EventHandler* eventHandler) {}
|
||||
void Application::runDebugLoop() {
|
||||
auto eventHandler = new EventHandler();
|
||||
mWindow->setEventHandler(eventHandler);
|
||||
|
||||
time_ms prevProcTime = get_time();
|
||||
|
||||
while (!mWindow->shouldClose()) {
|
||||
mWindow->processEvents();
|
||||
eventHandler->processAllEvent();
|
||||
time_ms currentTime = get_time();
|
||||
processFrame(eventHandler, halnf(currentTime - prevProcTime));
|
||||
prevProcTime = currentTime;
|
||||
|
||||
|
||||
mGraphics->drawBegin();
|
||||
drawFrame(mGraphics->getCanvas());
|
||||
mGraphics->drawEnd();
|
||||
mWindow->draw();
|
||||
}
|
||||
|
||||
delete eventHandler;
|
||||
}
|
||||
|
||||
void Application::run() {
|
||||
runDefaultLoop();
|
||||
// runDebugLoop();
|
||||
}
|
||||
|
||||
void Application::processFrame(EventHandler* eventHandler, halnf deltaTime) {}
|
||||
|
||||
void Application::drawFrame(Canvas* canvas) {
|
||||
// ImGui::Text("Frames processed per second: %f", mFramesProcessedPerSecond);
|
||||
// ImGui::Text("Frames drawn per second: %f", mFramesDrawnPerSecond);
|
||||
}
|
||||
|
||||
void Application::drawDebug() {
|
||||
ImGui::Text("Frames processed per second: %f", this->mFramesProcessedPerSecond);
|
||||
ImGui::Text("Frames drawn per second: %f", this->mFramesDrawnPerSecond);
|
||||
}
|
||||
|
||||
Application::~Application() {
|
||||
delete mGraphics;
|
||||
Window::destroyWindow(mWindow);
|
||||
|
|
|
|||
|
|
@ -90,9 +90,9 @@ void Window::destroyWindow(Window* window) {
|
|||
|
||||
bool Window::shouldClose() const { return glfwWindowShouldClose(mContext->window); }
|
||||
|
||||
void Window::processEvents() {
|
||||
// glfwPollEvents();
|
||||
glfwWaitEvents();
|
||||
void Window::processEvents(bool wait) {
|
||||
if (wait) glfwWaitEvents();
|
||||
else glfwPollEvents();
|
||||
checkAxisUpdates();
|
||||
}
|
||||
|
||||
|
|
@ -166,12 +166,12 @@ static void mouseButtonCallback(GLFWwindow* window, int button, int action, int
|
|||
auto id = (InputID) ((int) InputID::MOUSE1 + button);
|
||||
|
||||
if (action == GLFW_PRESS) {
|
||||
eventHandler->postEvent(id, { InputEvent::Type::BUTTON_ACTION, InputEvent::ButtonAction::PRESS, {} }
|
||||
);
|
||||
// printf("mouse\n");
|
||||
eventHandler->postEvent(id, { InputEvent::Type::BUTTON_ACTION, InputEvent::ButtonAction::PRESS, {} });
|
||||
} else if (action == GLFW_RELEASE) {
|
||||
eventHandler->postEvent(id, { InputEvent::Type::BUTTON_ACTION, InputEvent::ButtonAction::RELEASE, {} });
|
||||
// eventHandler->postEvent(id, { InputEvent::Type::BUTTON_ACTION, InputEvent::ButtonAction::NONE, {} });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void scrollCallback(GLFWwindow* window, double xOffset, double yOffset) {
|
||||
|
|
|
|||
|
|
@ -71,9 +71,13 @@ namespace tp {
|
|||
public: // User interface
|
||||
bool isEvents();
|
||||
void processEvent();
|
||||
void processAllEvent();
|
||||
|
||||
[[nodiscard]] const Vec2F& getPointer() const;
|
||||
[[nodiscard]] const Vec2F& getPointerPrev() const;
|
||||
|
||||
void setCursorOrigin(const Vec2F& origin);
|
||||
|
||||
[[nodiscard]] Vec2F getPointer() const;
|
||||
[[nodiscard]] Vec2F getPointerPrev() const;
|
||||
[[nodiscard]] Vec2F getPointerDelta() const;
|
||||
|
||||
[[nodiscard]] bool isPressed(InputID id) const;
|
||||
|
|
@ -83,6 +87,9 @@ namespace tp {
|
|||
|
||||
[[nodiscard]] halnf getPointerPressure() const;
|
||||
|
||||
private:
|
||||
void processEventUnguarded();
|
||||
|
||||
private:
|
||||
std::mutex mMutex = {};
|
||||
|
||||
|
|
@ -90,6 +97,8 @@ namespace tp {
|
|||
List<std::pair<InputID, InputEvent>> mEventQueue;
|
||||
|
||||
// input states
|
||||
Vec2F mPointerOrigin = { 0, 0 };
|
||||
|
||||
Vec2F mPointer;
|
||||
Vec2F mPointerPrev;
|
||||
Vec2F mScrollDelta;
|
||||
|
|
|
|||
|
|
@ -12,16 +12,24 @@ namespace tp {
|
|||
|
||||
void run();
|
||||
|
||||
virtual void processFrame(EventHandler* eventHandler);
|
||||
virtual bool forceNewFrame() { return false; }
|
||||
|
||||
virtual void processFrame(EventHandler* eventHandler, halnf deltaTime);
|
||||
virtual void drawFrame(Canvas* canvas);
|
||||
|
||||
virtual ~Application();
|
||||
|
||||
void drawDebug();
|
||||
|
||||
private:
|
||||
void runDefaultLoop();
|
||||
void runDebugLoop();
|
||||
|
||||
protected:
|
||||
bool mInitialized = false;
|
||||
|
||||
ualni mDrawPerSecond = 60;
|
||||
ualni mProcPerSecond = 160;
|
||||
ualni mProcPerSecond = 100;
|
||||
|
||||
Timer mDrawTimer;
|
||||
Timer mProcTimer;
|
||||
|
|
|
|||
|
|
@ -55,21 +55,25 @@ namespace tp {
|
|||
ualni id = 0;
|
||||
};
|
||||
|
||||
void setOrigin(const Vec2F& origin);
|
||||
|
||||
void pushClamp(const RectF& rec);
|
||||
void popClamp();
|
||||
void rect(const RectF& rec, const RGBA& col, halnf round = 0);
|
||||
void circle(const Vec2F& pos, halnf size, const RGBA& col);
|
||||
|
||||
void rect(RectF rec, const RGBA& col, halnf round = 0);
|
||||
void circle(Vec2F pos, halnf size, const RGBA& col);
|
||||
void text(const char*, const RectF&, halnf size, Align, halnf padding, const RGBA&);
|
||||
|
||||
ImageHandle createImageFromTextId(ualni id, Vec2F size);
|
||||
void updateTextureID(ImageHandle handle, ualni id);
|
||||
|
||||
void deleteImageHandle(ImageHandle image);
|
||||
void drawImage(const RectF& rec, ImageHandle* image, halnf angle = 0, halnf alpha = 1.f, halnf rounding = 0.f);
|
||||
void drawImage(RectF rec, ImageHandle* image, halnf angle = 0, halnf alpha = 1.f, halnf rounding = 0.f);
|
||||
|
||||
private:
|
||||
Buffer<RectF> mScissors;
|
||||
bool mIsClamping = false;
|
||||
Vec2F mOrigin = { 0, 0 };
|
||||
};
|
||||
|
||||
class Graphics {
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ namespace tp {
|
|||
|
||||
public:
|
||||
void draw();
|
||||
void processEvents();
|
||||
void processEvents(bool wait = true);
|
||||
|
||||
void setEventHandler(EventHandler* eventHandler);
|
||||
[[nodiscard]] EventHandler* getEventHandler();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue