Working example based on response from @Vortico
// we want to draw stuff procedurally only when things change
// suppose we have SomeModule with an integer "someValue" and a bool "dirty"
// whenever "someValue" changes "dirty" gets set to true
// create a container for widgets that draw things
struct SomeWidgetBuffer : FramebufferWidget{
SomeModule *module;
SomeWidgetBuffer(SomeModule *m){
module = m;
}
void step() override{
if(module->dirty){
FramebufferWidget::dirty = true;
module->dirty = false;
}
FramebufferWidget::step();
}
};
// create widget that draws something based on values of the module
struct SomeDrawingWidget : VirtualWidget{
SomeModule *module;
SomeDrawingWidget(SomeModule *m){
module = m;
box.pos = Vec(0,0);
box.size = Vec(100, 100);
}
void draw(NVGcontext *vg) override{
printf("drawing stuff only once\n");
nvgBeginPath(vg);
nvgRect(vg, 0, 0,box.size.x, box.size.y);
nvgFillColor(vg, nvgRGB(module->someValue,0,0));
nvgFill(vg);
}
};
// then somewhere inside the ModuleWidget's constructor
{
SomeWidgetBuffer *fb = new SomeWidgetBuffer(module);
SomeDrawingWidget *dw = new SomeDrawingWidget(module);
fb->addChild(dw);
addChild(fb);
}
ORIGINAL POST :
Sorry if obvious, but could anyone explain what’s the proper way to use the FramebufferWidget with procedural drawing instead of SVGs? I’m using nanovg calls inside the widgets draw function to render everything on my modules (feels more at home than fiddling with inkscape) and I’d like to optimize things that don’t change much. Any open source module doing this?
Let’s say I have a widget with some drawing function like this
struct SomeWidget : FramebufferWidget {
SomeModule *module;
SomeWidget(SomeModule *m) {
module = m;
}
void drawStuff(NVGcontext *vg){
nvgBeginPath(vg);
nvgRect(vg, 0,0,10,10);
nvgFillColor(vg, nvgRGB(module->someValue,0,0));
nvgFill(vg);
}
};
and the module has a bool that gets set to true whenever someValue is changed, then I’d like to update the framebuffer with drawStuff, where should I check for the bool and how to update the framebuffer?
A somewhat related question, is there a way to monitor the drawing performance of modules? Afaik the module consumption measurement in Rack is independent from rendering, but I’ve noticed with rcomian’s port that limiting the fps to 30 or 15 can help a lot with audio dropouts, so the profiler could report 10ms on a module while it is drawing so many things that it is lagging out Rack.