From af80d41aef6baeb45c41134e2187503a498c3aec Mon Sep 17 00:00:00 2001 From: falkTX Date: Sat, 6 Nov 2021 15:19:06 +0000 Subject: [PATCH] Store entire time info in context; More ImGui/Ildaeil fixups Signed-off-by: falkTX --- plugins/Cardinal/src/Ildaeil.cpp | 42 ++++++++++++++------------ plugins/Cardinal/src/ImGuiWidget.cpp | 28 +++++++++++++++-- plugins/Cardinal/src/plugincontext.hpp | 6 ++-- src/CardinalPlugin.cpp | 5 +++ src/PluginContext.hpp | 17 ++++++++--- 5 files changed, 68 insertions(+), 30 deletions(-) diff --git a/plugins/Cardinal/src/Ildaeil.cpp b/plugins/Cardinal/src/Ildaeil.cpp index 81afb68..4c1feb2 100644 --- a/plugins/Cardinal/src/Ildaeil.cpp +++ b/plugins/Cardinal/src/Ildaeil.cpp @@ -89,6 +89,8 @@ struct IldaeilModule : Module { NUM_LIGHTS }; + CardinalPluginContext* const pcontext; + const NativePluginDescriptor* fCarlaPluginDescriptor = nullptr; NativePluginHandle fCarlaPluginHandle = nullptr; @@ -107,6 +109,7 @@ struct IldaeilModule : Module { unsigned audioDataFill = 0; IldaeilModule() + : pcontext(reinterpret_cast(APP)) { config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS); @@ -172,19 +175,19 @@ struct IldaeilModule : Module { const NativeTimeInfo* hostGetTimeInfo() const noexcept { - if (CardinalPluginContext* const pcontext = reinterpret_cast(APP)) + if (pcontext != nullptr) { fCarlaTimeInfo.playing = pcontext->playing; - // fCarlaTimeInfo.frame = timePos.frame; - // fCarlaTimeInfo.bbt.valid = timePos.bbt.valid; + fCarlaTimeInfo.frame = pcontext->frame; + fCarlaTimeInfo.bbt.valid = pcontext->bbtValid; fCarlaTimeInfo.bbt.bar = pcontext->bar; fCarlaTimeInfo.bbt.beat = pcontext->beat; fCarlaTimeInfo.bbt.tick = pcontext->tick; - // fCarlaTimeInfo.bbt.barStartTick = timePos.bbt.barStartTick; + fCarlaTimeInfo.bbt.barStartTick = pcontext->barStartTick; fCarlaTimeInfo.bbt.beatsPerBar = pcontext->beatsPerBar; - // fCarlaTimeInfo.bbt.beatType = timePos.bbt.beatType; + fCarlaTimeInfo.bbt.beatType = pcontext->beatType; fCarlaTimeInfo.bbt.ticksPerBeat = pcontext->ticksPerBeat; - // fCarlaTimeInfo.bbt.beatsPerMinute = timePos.bbt.beatsPerMinute; + fCarlaTimeInfo.bbt.beatsPerMinute = pcontext->beatsPerMinute; } return &fCarlaTimeInfo; @@ -405,7 +408,7 @@ struct IldaeilWidget : ImGuiWidget, Thread { IldaeilModule* const module; - IldaeilWidget(IldaeilModule* const m, const uintptr_t nativeWindowId) + IldaeilWidget(IldaeilModule* const m) : ImGuiWidget(), module(m) { @@ -424,7 +427,7 @@ struct IldaeilWidget : ImGuiWidget, Thread { const CarlaHostHandle handle = module->fCarlaHostHandle; char winIdStr[24]; - std::snprintf(winIdStr, sizeof(winIdStr), "%lx", (ulong)nativeWindowId); + std::snprintf(winIdStr, sizeof(winIdStr), "%lx", (ulong)module->pcontext->nativeWindowId); carla_set_engine_option(handle, ENGINE_OPTION_FRONTEND_WIN_ID, 0, winIdStr); /* carla_set_engine_option(handle, ENGINE_OPTION_FRONTEND_UI_SCALE, getScaleFactor()*1000, nullptr); @@ -626,21 +629,18 @@ struct IldaeilWidget : ImGuiWidget, Thread { { ImGuiWidget::onContextCreate(e); - /* - if (module == nullptr || module->fCarlaHostHandle == nullptr) + if (module == nullptr || module->pcontext == nullptr || module->fCarlaHostHandle == nullptr) return; - */ - - const uintptr_t nativeWindowId = reinterpret_cast(APP)->nativeWindowId; char winIdStr[24]; - std::snprintf(winIdStr, sizeof(winIdStr), "%lx", (ulong)nativeWindowId); + std::snprintf(winIdStr, sizeof(winIdStr), "%lx", (ulong)module->pcontext->nativeWindowId); carla_set_engine_option(module->fCarlaHostHandle, ENGINE_OPTION_FRONTEND_WIN_ID, 0, winIdStr); } void onContextDestroy(const ContextDestroyEvent& e) override { - carla_set_engine_option(module->fCarlaHostHandle, ENGINE_OPTION_FRONTEND_WIN_ID, 0, "0"); + if (module != nullptr && module->fCarlaHostHandle != nullptr) + carla_set_engine_option(module->fCarlaHostHandle, ENGINE_OPTION_FRONTEND_WIN_ID, 0, "0"); ImGuiWidget::onContextDestroy(e); } @@ -1122,11 +1122,13 @@ struct IldaeilModuleWidget : ModuleWidget { setModule(module); setPanel(APP->window->loadSvg(asset::plugin(pluginInstance, "res/glBars.svg"))); - ildaeilWidget = new IldaeilWidget(static_cast(module), - reinterpret_cast(APP)->nativeWindowId); - ildaeilWidget->box.pos = Vec(2 * RACK_GRID_WIDTH, 0); - ildaeilWidget->box.size = Vec(box.size.x - 2 * RACK_GRID_WIDTH, box.size.y); - addChild(ildaeilWidget); + if (module != nullptr && module->pcontext != nullptr) + { + ildaeilWidget = new IldaeilWidget(module); + ildaeilWidget->box.pos = Vec(2 * RACK_GRID_WIDTH, 0); + ildaeilWidget->box.size = Vec(box.size.x - 2 * RACK_GRID_WIDTH, box.size.y); + addChild(ildaeilWidget); + } addChild(createWidget(Vec(0, 0))); addChild(createWidget(Vec(0, RACK_GRID_HEIGHT - RACK_GRID_WIDTH))); diff --git a/plugins/Cardinal/src/ImGuiWidget.cpp b/plugins/Cardinal/src/ImGuiWidget.cpp index 9d67358..b58bd76 100644 --- a/plugins/Cardinal/src/ImGuiWidget.cpp +++ b/plugins/Cardinal/src/ImGuiWidget.cpp @@ -16,6 +16,7 @@ */ #include "ImGuiWidget.hpp" +#include "DistrhoUtils.hpp" #ifndef DGL_NO_SHARED_RESOURCES # include "../../../dpf/dgl/src/Resources.hpp" @@ -24,12 +25,14 @@ #include "DearImGui/imgui_impl_opengl2.h" struct ImGuiWidget::PrivateData { - ImGuiContext* context; + ImGuiContext* context = nullptr; + bool created = false; PrivateData(const double scaleFactor = 1.0) { IMGUI_CHECKVERSION(); context = ImGui::CreateContext(); + ImGui::SetCurrentContext(context); ImGuiIO& io(ImGui::GetIO()); io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; @@ -77,6 +80,13 @@ struct ImGuiWidget::PrivateData { ~PrivateData() { + // this should not happen + if (created) + { + ImGui::SetCurrentContext(context); + ImGui_ImplOpenGL2_Shutdown(); + } + ImGui::DestroyContext(context); } }; @@ -92,15 +102,21 @@ ImGuiWidget::~ImGuiWidget() void ImGuiWidget::onContextCreate(const ContextCreateEvent& e) { OpenGlWidget::onContextCreate(e); + DISTRHO_SAFE_ASSERT_RETURN(!imData->created,); ImGui::SetCurrentContext(imData->context); ImGui_ImplOpenGL2_Init(); + imData->created = true; } void ImGuiWidget::onContextDestroy(const ContextDestroyEvent& e) { - ImGui::SetCurrentContext(imData->context); - ImGui_ImplOpenGL2_Shutdown(); + if (imData->created) + { + ImGui::SetCurrentContext(imData->context); + ImGui_ImplOpenGL2_Shutdown(); + imData->created = false; + } OpenGlWidget::onContextDestroy(e); } @@ -115,6 +131,12 @@ void ImGuiWidget::drawFramebuffer() io.DisplayFramebufferScale = ImVec2(fbSize.x / box.size.x, fbSize.y / box.size.y); io.DisplaySize = ImVec2(box.size.x, box.size.y); + if (!imData->created) + { + ImGui_ImplOpenGL2_Init(); + imData->created = true; + } + // TODO io.DeltaTime ImGui_ImplOpenGL2_NewFrame(); diff --git a/plugins/Cardinal/src/plugincontext.hpp b/plugins/Cardinal/src/plugincontext.hpp index 787ebb7..339f285 100644 --- a/plugins/Cardinal/src/plugincontext.hpp +++ b/plugins/Cardinal/src/plugincontext.hpp @@ -33,8 +33,10 @@ struct CardinalPluginContext : rack::Context { uint32_t bufferSize; double sampleRate; float parameters[kModuleParameters]; - bool playing, reset; - int32_t bar, beat, beatsPerBar; + bool playing, reset, bbtValid; + int32_t bar, beat, beatsPerBar, beatType; + uint64_t frame; + double barStartTick, beatsPerMinute; double tick, tickClock, ticksPerBeat, ticksPerClock, ticksPerFrame; uintptr_t nativeWindowId; Plugin* const plugin; diff --git a/src/CardinalPlugin.cpp b/src/CardinalPlugin.cpp index 015a15f..741c703 100644 --- a/src/CardinalPlugin.cpp +++ b/src/CardinalPlugin.cpp @@ -818,6 +818,8 @@ protected: { const TimePosition& timePos(getTimePosition()); context->playing = timePos.playing; + context->bbtValid = timePos.bbt.valid; + context->frame = timePos.frame; if (timePos.bbt.valid) { @@ -827,6 +829,9 @@ protected: context->bar = timePos.bbt.bar; context->beat = timePos.bbt.beat; context->beatsPerBar = timePos.bbt.beatsPerBar; + context->beatType = timePos.bbt.beatType; + context->barStartTick = timePos.bbt.barStartTick; + context->beatsPerMinute = timePos.bbt.beatsPerMinute; context->tick = timePos.bbt.tick; context->ticksPerBeat = timePos.bbt.ticksPerBeat; context->ticksPerClock = timePos.bbt.ticksPerBeat / timePos.bbt.beatType; diff --git a/src/PluginContext.hpp b/src/PluginContext.hpp index 171c91d..e34d684 100644 --- a/src/PluginContext.hpp +++ b/src/PluginContext.hpp @@ -40,8 +40,10 @@ struct CardinalPluginContext : rack::Context { uint32_t bufferSize; double sampleRate; float parameters[kModuleParameters]; - bool playing, reset; - int32_t bar, beat, beatsPerBar; + bool playing, reset, bbtValid; + int32_t bar, beat, beatsPerBar, beatType; + uint64_t frame; + double barStartTick, beatsPerMinute; double tick, tickClock, ticksPerBeat, ticksPerClock, ticksPerFrame; uintptr_t nativeWindowId; Plugin* const plugin; @@ -51,9 +53,14 @@ struct CardinalPluginContext : rack::Context { sampleRate(p->getSampleRate()), playing(false), reset(false), - bar(0), - beat(0), - beatsPerBar(0), + bbtValid(false), + bar(1), + beat(1), + beatsPerBar(4), + beatType(4), + frame(0), + barStartTick(0.0), + beatsPerMinute(120.0), tick(0.0), tickClock(0.0), ticksPerBeat(0.0),