Okay so after a couple days, I got my plugin reworking using OpenGL and it saves quite a bit of CPU as predicted. If anyone can I would appreciate if someone could look at this snippet of code I have, built after reading openGL Juce Demo, to see if Im doing this correctly or if theres a way to improve my methodology.
So first I have a struct which basically just acts as drawing instructions. I have multiple of these, one for each animating component I have.
//=================================================================
struct Title : public Component
{
Title() //
{
createImage();
}
void createImage() //Draws all SVGs into an image, called again when slider moves to adjust hue
{
colourPath = titleColours->getOutlineAsPath();
colourPath.applyTransform(AffineTransform::translation(13.f,6.f));
Graphics ID (titleImage);
ID.addTransform(AffineTransform::scale(2.0f,2.0f));
titleBack -> drawWithin(ID, Rectangle<float> {177.f,57.f,static_cast<float>(getWidth()),static_cast<float>(getHeight())}, RectanglePlacement(juce::RectanglePlacement::doNotResize),1.0f);
ColourGradient titleGradient (editColour(Colour(0xfffc955b)),0.f,0.0f,editColour(Colour(0xfffb2b6b)),0.f,100.0f,false);
ID.setGradientFill(titleGradient);
ID.fillPath(colourPath);
}
void drawTitle(OpenGLContext* context, float desktopScale) // Draws the image and applies animation
{
std::unique_ptr<juce::LowLevelGraphicsContext> glRenderer (createOpenGLGraphicsContext (*context,desktopScale*getWidth(),desktopScale*getHeight()));
if (glRenderer.get() != nullptr)
{
Graphics g (*glRenderer);
g.addTransform(AffineTransform::scale(desktopScale/2,desktopScale/2));
if (isJiggle)
{
float randX = randomNumber.nextInt(Range<int>(-3,3));
float randY = randomNumber.nextInt(Range<int>(-3,3));
g.drawImageAt(titleImage, 300 + randX, 0 + randY, false);
}
else
{
g.drawImageAt(titleImage,300,0,false);
}
}
}
void setHue(float newHue)
{ hue = newHue/7; }
void setJiggleFlag(bool jiggle)
{
isJiggle = jiggle;
}
Colour editColour(Colour initColour)
{
initColour = initColour.withRotatedHue(hue);
initColour = initColour.withSaturationHSL(1.1f);
return initColour;
}
private:
float hue = 0;
bool isJiggle = false;
Random randomNumber;
std::unique_ptr<Drawable> titleColours = (Drawable::createFromImageData(BinaryData::titleColour_svg,BinaryData::titleColour_svgSize));
std::unique_ptr<Drawable> titleBack = (Drawable::createFromImageData(BinaryData::titleBack_svg,BinaryData::titleBack_svgSize));
Image titleImage {Image::ARGB,345*2, 115*2, true};
Path colourPath;
};
Then in my main editor, Ive created an open GL context, and call title.drawTitle in my renderOpenGL method.
//=================================================================
DistortionPluginAudioProcessorEditor::DistortionPluginAudioProcessorEditor (DistortionPluginAudioProcessor& p)
: AudioProcessorEditor (&p), processor (p)
{
//the openGlContext was created in the header
setOpaque(true);
openGLContext.attachTo (*this);
openGLContext.setRenderer(this);
openGLContext.setContinuousRepainting(true);
setSize (660, 440);
}
DistortionPluginAudioProcessorEditor::~DistortionPluginAudioProcessorEditor()
{
openGLContext.detach();
}
void DistortionPluginAudioProcessorEditor::renderOpenGL()
{
auto desktopScale = (float) openGLContext.getRenderingScale();
if (openGLContext.getCurrentContext() != nullptr)
{
background.drawBackGround(openGLContext.getCurrentContext(), desktopScale);
actionLines.drawActionLines(openGLContext.getCurrentContext(), desktopScale);
title.drawTitle(openGLContext.getCurrentContext(), desktopScale);
}
}
If anyone has any critiques about my code in general Id also appreciate feedback
Extra Question: My VST has no problem opening and closing, but sometimes when I drag my VST window onto another monitor, it causes this error
******* GL_INVALID_FRAMEBUFFER_OPERATION at /Users/Wiznat/Desktop/JUCE/modules/juce_opengl/opengl/juce_OpenGLGraphicsContext.cpp : 1282**
JUCE Assertion failure in juce_opengl.cpp:216
As I hardly understand openGL, I really have no clue what causes this or how I would go about fixing it. I did not have this issue in a test project I used to practice the openGL module.