Graphics refactor finished

This commit is contained in:
IlyaShurupov 2024-03-20 11:31:50 +03:00 committed by Ilya Shurupov
parent bfbdd0e33b
commit 8451c56e93
12 changed files with 114 additions and 110 deletions

View file

@ -138,8 +138,11 @@ void Canvas::deleteImageHandle(ImageHandle image) {
void Canvas::drawBegin() { 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);
glViewport(0, 0, size.x, size.y);
} }
void Canvas::drawEnd() { void Canvas::drawEnd() {
const auto size = mContext->window->getSize();
glViewport(0, 0, size.x, size.y);
nvgEndFrame(mContext->vg); nvgEndFrame(mContext->vg);
} }

View file

@ -1,6 +1,8 @@
#include "EventHandler.hpp" #include "EventHandler.hpp"
#include <stdio.h>
using namespace tp; using namespace tp;
EventHandler::EventHandler() = default; EventHandler::EventHandler() = default;
@ -23,13 +25,13 @@ InputState::State transitions[4][4] = {
{ InputState::State::NONE, InputState::State::PRESSED, InputState::State::PRESSED, InputState::State::NONE }, { InputState::State::NONE, InputState::State::PRESSED, InputState::State::PRESSED, InputState::State::NONE },
{ InputState::State::PRESSED, InputState::State::PRESSED, InputState::State::HOLD, InputState::State::PRESSED }, { InputState::State::PRESSED, InputState::State::PRESSED, InputState::State::HOLD, InputState::State::PRESSED },
{ InputState::State::HOLD, InputState::State::RELEASED, InputState::State::RELEASED, InputState::State::HOLD }, { InputState::State::HOLD, InputState::State::RELEASED, InputState::State::RELEASED, InputState::State::HOLD },
{ InputState::State::RELEASED, InputState::State::NONE, InputState::State::RELEASED, InputState::State::RELEASED }, { InputState::State::RELEASED, InputState::State::NONE, InputState::State::NONE, InputState::State::RELEASED },
}; };
bool transitionsReduce[4][4] = { bool transitionsReduce[4][4] = {
{ true, true, false, true }, { true, true, false, true },
{ true, true, false, true }, { true, true, false, true },
{ true, false, true, true }, { true, false, false, true },
{ true, false, true, true }, { true, false, true, true },
}; };
@ -69,6 +71,8 @@ void EventHandler::processEvent() {
} }
} }
mPointerPressure = mInputStates[(int) InputID::MOUSE1].mCurrentState != InputState::State::NONE;
mMutex.unlock(); mMutex.unlock();
} }
@ -82,6 +86,10 @@ bool EventHandler::isReleased(InputID id) const {
return mInputStates[(int) id].mCurrentState == InputState::State::RELEASED; return mInputStates[(int) id].mCurrentState == InputState::State::RELEASED;
} }
halnf EventHandler::getPointerPressure() const {
return mPointerPressure;
}
bool EventHandler::isDown(InputID id) const { bool EventHandler::isDown(InputID id) const {
return mInputStates[(int) id].mCurrentState == InputState::State::PRESSED || return mInputStates[(int) id].mCurrentState == InputState::State::PRESSED ||
mInputStates[(int) id].mCurrentState == InputState::State::HOLD; mInputStates[(int) id].mCurrentState == InputState::State::HOLD;

View file

@ -5,6 +5,7 @@ using namespace tp;
Application::Application() { Application::Application() {
mWindow = Window::createWindow(); mWindow = Window::createWindow();
mGraphics = new Graphics(mWindow);
mDrawTimer.setDuration(1000.f / mDrawPerSecond); mDrawTimer.setDuration(1000.f / mDrawPerSecond);
mProcTimer.setDuration(1000.f / mProcPerSecond); mProcTimer.setDuration(1000.f / mProcPerSecond);
@ -12,7 +13,6 @@ Application::Application() {
} }
void Application::run() { void Application::run() {
Graphics graphics(mWindow);
auto eventHandler = new EventHandler(); auto eventHandler = new EventHandler();
mWindow->setEventHandler(eventHandler); mWindow->setEventHandler(eventHandler);
@ -44,11 +44,11 @@ void Application::run() {
mDrawTimer.reset(); mDrawTimer.reset();
if (redrawNeeded) { if (redrawNeeded) {
graphics.drawBegin(); mGraphics->drawBegin();
drawFrame(graphics.getCanvas()); drawFrame(mGraphics->getCanvas());
graphics.drawEnd(); mGraphics->drawEnd();
mWindow->draw(); mWindow->draw();
@ -79,5 +79,6 @@ void Application::drawFrame(Canvas* canvas) {
} }
Application::~Application() { Application::~Application() {
delete mGraphics;
Window::destroyWindow(mWindow); Window::destroyWindow(mWindow);
} }

View file

@ -96,8 +96,6 @@ bool Window::shouldClose() const { return glfwWindowShouldClose(mContext->window
void Window::processEvents() { void Window::processEvents() {
glfwWaitEvents(); glfwWaitEvents();
checkAxisUpdates(); checkAxisUpdates();
glViewport(0, 0, mSize.x, mSize.y);
} }
void Window::draw() { void Window::draw() {

View file

@ -69,11 +69,13 @@ namespace tp {
bool isEvents(); bool isEvents();
void processEvent(); void processEvent();
const Vec2F& getPointer() const; [[nodiscard]] const Vec2F& getPointer() const;
bool isPressed(InputID id) const; [[nodiscard]] bool isPressed(InputID id) const;
bool isReleased(InputID id) const; [[nodiscard]] bool isReleased(InputID id) const;
bool isDown(InputID id) const; [[nodiscard]] bool isDown(InputID id) const;
halnf getScrollY() const; [[nodiscard]] halnf getScrollY() const;
[[nodiscard]] halnf getPointerPressure() const;
private: private:
std::mutex mMutex = {}; std::mutex mMutex = {};
@ -83,7 +85,7 @@ namespace tp {
// input states // input states
Vec2F mPointer; Vec2F mPointer;
halnf pressure = 0; halnf mPointerPressure = 0;
InputState mInputStates[(int) InputID::LAST_KEY_CODE]{}; InputState mInputStates[(int) InputID::LAST_KEY_CODE]{};
}; };

View file

@ -18,8 +18,6 @@ namespace tp {
virtual ~Application(); virtual ~Application();
protected: protected:
Window* mWindow = nullptr;
bool mInitialized = false; bool mInitialized = false;
ualni mDrawPerSecond = 60; ualni mDrawPerSecond = 60;
@ -34,5 +32,8 @@ namespace tp {
halnf mFramesProcessed = 0; halnf mFramesProcessed = 0;
halnf mFramesDrawn = 0; halnf mFramesDrawn = 0;
Graphics* mGraphics = nullptr;
Window* mWindow = nullptr;
}; };
} }

View file

@ -1,6 +1,7 @@
#include "GUI.hpp" #include "GUI.hpp"
#include "Player.hpp" #include "Player.hpp"
#include "GraphicApplication.hpp"
// 1) artworks // 1) artworks
// 2) how to easily add more songs? // 2) how to easily add more songs?
@ -13,36 +14,38 @@
// 4) queue & repeat & shuffle... // 4) queue & repeat & shuffle...
// 5) new database with history // 5) new database with history
void runApp() { using namespace tp;
class LibraryViewer : public Application {
public:
LibraryViewer() : gui(&library, &player) {
library.loadJson(getHome() + "Library.json");
library.checkExisting();
gui.updateTracks();
}
void processFrame(EventHandler* eventHandler) override {
auto rec = RectF{ { 0, 0 }, mWindow->getSize() };
gui.proc(*eventHandler, rec, rec);
}
void drawFrame(Canvas* canvas) override {
gui.draw(*canvas);
}
private:
Player player; Player player;
Library library; Library library;
library.loadJson(getHome() + "Library.json"); LibraryWidget<EventHandler, Canvas> gui;
library.checkExisting(); };
void runApp() {
tp::GlobalGUIConfig config; tp::GlobalGUIConfig config;
tp::gGlobalGUIConfig = &config; tp::gGlobalGUIConfig = &config;
tp::LibraryWidget<tp::Window::Events, tp::Graphics::Canvas> gui(&library, &player); LibraryViewer lib;
lib.run();
auto window = tp::Window::createWindow(800, 600, "Window 1");
if (window) {
while (!window->shouldClose()) {
window->processEvents();
auto area = window->getCanvas().getAvaliableArea();
gui.proc(window->getEvents(), { area.x, area.y, area.z, area.w }, { area.x, area.y, area.z, area.w });
gui.draw(window->getCanvas());
// tp::sleep(100);
window->draw();
}
}
tp::Window::destroyWindow(window);
} }
int main() { int main() {
@ -54,8 +57,6 @@ int main() {
return 1; return 1;
} }
tp::HeapAllocGlobal::disableCallstack();
runApp(); runApp();
binModule.deinitialize(); binModule.deinitialize();

View file

@ -26,9 +26,9 @@ namespace tp {
if (!mTrack) return; if (!mTrack) return;
if (!areaParent.isOverlap(area)) return; if (!areaParent.isOverlap(area)) return;
if (area.isInside(events.getPos())) { if (area.isInside(events.getPointer())) {
col.set({ 0.15f, 0.15f, 0.15f, 1.f }); col.set({ 0.15f, 0.15f, 0.15f, 1.f });
mSelected = events.isReleased(); mSelected = events.isReleased(InputID::MOUSE1);
} else { } else {
col.set({ 0.15f, 0.15f, 0.15f, 0.f }); col.set({ 0.15f, 0.15f, 0.15f, 0.f });
} }
@ -94,6 +94,7 @@ namespace tp {
if (!this->mVisible) return; if (!this->mVisible) return;
if (!mTrack) return; if (!mTrack) return;
// canvas.rect(this->mArea, { 0.13f, 0.13f, 0.13f, 1.f }, 4.f); // canvas.rect(this->mArea, { 0.13f, 0.13f, 0.13f, 1.f }, 4.f);
renderUI();
} }
void renderUI() { void renderUI() {
@ -207,11 +208,16 @@ namespace tp {
mLibrary = (lib); mLibrary = (lib);
mPlayer = (player); mPlayer = (player);
updateTracks();
mCurrentTrackInfo.mPlayer = mPlayer;
}
void updateTracks() {
mTracks.clear();
for (auto track : mLibrary->mTraks) { for (auto track : mLibrary->mTraks) {
mTracks.append(TrackWidget<Events, Canvas>(&track.data())); mTracks.append(TrackWidget<Events, Canvas>(&track.data()));
} }
mCurrentTrackInfo.mPlayer = mPlayer;
} }
void proc(const Events& events, const RectF& areaParent, const RectF& aArea) override { void proc(const Events& events, const RectF& areaParent, const RectF& aArea) override {
@ -219,11 +225,6 @@ namespace tp {
this->mVisible = this->mArea.isOverlap(areaParent); this->mVisible = this->mArea.isOverlap(areaParent);
if (!this->mVisible) return; if (!this->mVisible) return;
mCurrentTrackInfo.renderUI();
mNeedRedraw = events.isEvent() || mCurrentTrackInfo.songFilter.IsActive();
if (!mNeedRedraw) return;
filter(); filter();
mSplitView.proc(events, this->mArea, this->mArea); mSplitView.proc(events, this->mArea, this->mArea);
@ -249,7 +250,6 @@ namespace tp {
mSplitView.draw(canvas); mSplitView.draw(canvas);
mSongList.draw(canvas); mSongList.draw(canvas);
mCurrentTrackInfo.draw(canvas); mCurrentTrackInfo.draw(canvas);
// mCurrentTrack.proc(canvas);
} }
void filter() { void filter() {
@ -292,7 +292,5 @@ namespace tp {
ScrollableWindow<Events, Canvas> mSongList; ScrollableWindow<Events, Canvas> mSongList;
TrackInfoWidget<Events, Canvas> mCurrentTrackInfo; TrackInfoWidget<Events, Canvas> mCurrentTrackInfo;
TrackWidget<Events, Canvas> mCurrentTrack; TrackWidget<Events, Canvas> mCurrentTrack;
bool mNeedRedraw = false;
}; };
} }

View file

@ -1,37 +1,35 @@
#include "GraphicApplication.hpp"
#include "Sketch3D.hpp" #include "Sketch3D.hpp"
#include "Graphics.hpp"
#include "Window.hpp"
#include "Sketch3DWidget.hpp" #include "Sketch3DWidget.hpp"
void runApp() { using namespace tp;
class Sketch3DApplication : public Application {
public:
Sketch3DApplication() : mGui(*mGraphics->getCanvas(), {1920, 1080}) {}
void processFrame(EventHandler* eventHandler) override {
auto rec = RectF( { 0, 0 }, mWindow->getSize() );
mGui.proc(*eventHandler, rec, rec);
}
void drawFrame(Canvas* canvas) override {
mGui.draw(*canvas);
}
private:
Sketch3DGUI<EventHandler, Canvas> mGui;
};
void runApp() {
tp::GlobalGUIConfig config; tp::GlobalGUIConfig config;
tp::gGlobalGUIConfig = &config; tp::gGlobalGUIConfig = &config;
auto window = tp::Window::createWindow(800, 600, "Window 1"); Sketch3DApplication app;
app.run();
{
tp::Sketch3DGUI<tp::Window::Events, tp::Graphics::Canvas> gui(window->getCanvas(), {1920, 1080});
if (window) {
while (!window->shouldClose()) {
window->processEvents();
auto area = window->getCanvas().getAvaliableArea();
gui.proc(window->getEvents(), { area.x, area.y, area.z, area.w }, { area.x, area.y, area.z, area.w });
gui.draw(window->getCanvas());
tp::sleep(10);
window->draw();
}
}
}
tp::Window::destroyWindow(window);
} }
int main() { int main() {
@ -43,11 +41,7 @@ int main() {
return 1; return 1;
} }
tp::HeapAllocGlobal::disableCallstack();
runApp(); runApp();
// FIXME : leaks in stacktrace itself
binModule.deinitialize(); binModule.deinitialize();
} }

View file

@ -26,21 +26,21 @@ namespace tp {
this->mVisible = area.isOverlap(areaParent); this->mVisible = area.isOverlap(areaParent);
if (!this->mVisible) return; if (!this->mVisible) return;
if (!this->mArea.isInside(events.getPos())) { if (!this->mArea.isInside(events.getPointer())) {
return; return;
} }
auto crs = (events.getPos() - this->mArea.pos); auto crs = (events.getPointer() - this->mArea.pos);
crs.x /= this->mArea.z; crs.x /= this->mArea.z;
crs.y /= this->mArea.w; crs.y /= this->mArea.w;
crs = (crs - 0.5) * 2; crs = (crs - 0.5) * 2;
// TODO : make better api for events // TODO : make better api for events
Vec2F absolutePos = events.getPos() - this->mArea.pos; Vec2F absolutePos = events.getPointer() - this->mArea.pos;
if (events.isPressed()) { if (events.isPressed(InputID::MOUSE1)) {
mAction = true; mAction = true;
} else if (events.isReleased()) { } else if (events.isReleased(InputID::MOUSE1)) {
mAction = false; mAction = false;
} }
@ -48,6 +48,8 @@ namespace tp {
Vec2F relativePosPrev = ((mActionPosAbsolutePrev / this->mArea.size) - 0.5f) * 2.f; Vec2F relativePosPrev = ((mActionPosAbsolutePrev / this->mArea.size) - 0.5f) * 2.f;
Vec2F relativeDelta = relativePos - relativePosPrev; Vec2F relativeDelta = relativePos - relativePosPrev;
mProject.mCamera.setRatio(this->mArea.w / this->mArea.z);
switch (mMode) { switch (mMode) {
case Mode::MOVE: { case Mode::MOVE: {
if (mAction) mProject.mCamera.move(relativePos, relativePosPrev); if (mAction) mProject.mCamera.move(relativePos, relativePosPrev);
@ -63,7 +65,7 @@ namespace tp {
break; break;
} }
case Mode::DRAW: { case Mode::DRAW: {
mProject.sample(events.pressure, this->mArea.w / this->mArea.z, crs); mProject.sample(events.getPointerPressure(), this->mArea.w / this->mArea.z, crs);
break; break;
} }
default: break; default: break;

View file

@ -181,7 +181,7 @@ namespace tp {
} }
public: public:
Buffer<MessageWidget<Events, Canvas>*> mMessages; Buffer<MessageWidget<Events, Canvas>> mMessages;
ScrollableWindow<Events, Canvas> mHistoryView; ScrollableWindow<Events, Canvas> mHistoryView;
TextInputWidget<Events, Canvas> mMessage; TextInputWidget<Events, Canvas> mMessage;
ButtonWidget<Events, Canvas> mSend; ButtonWidget<Events, Canvas> mSend;
@ -196,28 +196,28 @@ namespace tp {
this->addValue("Padding", "Padding"); this->addValue("Padding", "Padding");
// todo : fetch code // todo : fetch code
mUsers.append(new UserWidget<Events, Canvas>()); mUsers.append(UserWidget<Events, Canvas>());
mUsers.append(new UserWidget<Events, Canvas>()); mUsers.append(UserWidget<Events, Canvas>());
mUsers.append(new UserWidget<Events, Canvas>()); mUsers.append(UserWidget<Events, Canvas>());
mUsers[0]->mArea = { 0, 0, 100, 100 }; mUsers[0].mArea = { 0, 0, 100, 100 };
mUsers[1]->mArea = { 0, 0, 100, 100 }; mUsers[1].mArea = { 0, 0, 100, 100 };
mUsers[2]->mArea = { 0, 0, 100, 100 }; mUsers[2].mArea = { 0, 0, 100, 100 };
for (auto message : mUsers) { for (auto message : mUsers) {
mSideView.mContents.append(message.data()); mSideView.mContents.append(&message.data());
} }
mActive.mMessages.append(new MessageWidget<Events, Canvas>()); mActive.mMessages.append(MessageWidget<Events, Canvas>());
mActive.mMessages.append(new MessageWidget<Events, Canvas>()); mActive.mMessages.append(MessageWidget<Events, Canvas>());
mActive.mMessages.append(new MessageWidget<Events, Canvas>()); mActive.mMessages.append(MessageWidget<Events, Canvas>());
mActive.mMessages[0]->mArea = { 0, 0, 100, 100 }; mActive.mMessages[0].mArea = { 0, 0, 100, 100 };
mActive.mMessages[1]->mArea = { 0, 0, 100, 100 }; mActive.mMessages[1].mArea = { 0, 0, 100, 100 };
mActive.mMessages[2]->mArea = { 0, 0, 100, 100 }; mActive.mMessages[2].mArea = { 0, 0, 100, 100 };
for (auto message : mActive.mMessages) { for (auto message : mActive.mMessages) {
mActive.mHistoryView.mContents.append(message.data()); mActive.mHistoryView.mContents.append(&message.data());
} }
} }
@ -240,7 +240,7 @@ namespace tp {
} }
public: public:
Buffer<UserWidget<Events, Canvas>*> mUsers; Buffer<UserWidget<Events, Canvas>> mUsers;
ScrollableWindow<Events, Canvas> mSideView; ScrollableWindow<Events, Canvas> mSideView;
ActiveChatWidget<Events, Canvas> mActive; ActiveChatWidget<Events, Canvas> mActive;
SplitView<Events, Canvas> mSplitView; SplitView<Events, Canvas> mSplitView;

View file

@ -136,11 +136,7 @@ namespace tp {
this->addValue("Padding", "Padding"); this->addValue("Padding", "Padding");
} }
~ScrollableWindow() { ~ScrollableWindow() = default;
for (auto content : mContents) {
delete content.data();
}
}
// takes whole area // takes whole area
void proc(const Events& events, const tp::RectF& areaParent, const tp::RectF& aArea) override { void proc(const Events& events, const tp::RectF& areaParent, const tp::RectF& aArea) override {