Fixes and improvements to the graphics module

This commit is contained in:
IlyaShurupov 2024-07-17 09:40:49 +03:00 committed by Ilya Shurupov
parent 860474654a
commit 5e3f5594b8
19 changed files with 161 additions and 55 deletions

View file

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

View file

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

View file

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

View file

@ -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) {