updating only the level meters in my Mixer will cause everything between all the level meters also to be repainted
So everything in that big region will be repainted, not only the LevelMeters.
zamrate wrote:I've modified my LevelMeters code to only use 2 calls: fillRect() and drawImageRect(). Still I get a completely insane CPU usage of 40% for 20 level meters. That's just not normal.
// MainComponent.h
#include "JuceHeader.h"
class LevelMeter;
class MainComponent:public Component, public Timer, public Button::Listener
{
public:
MainComponent();
~MainComponent();
private:
Array<LevelMeter*> meters;
void timerCallback();
void buttonClicked(Button *btn);
Button *btnVertical, *btnHorizontal;
};// MainComponent.cpp
#include "MainComponent.h"
static float mainPhase=0.0f;
// *********************************************************************************
class LevelMeter:public Component
{
public:
LevelMeter()
{
phase=mainPhase;
mainPhase+=0.04f;
setOpaque(true);
}
void paint(Graphics &g)
{
int y=int(phase*float(getHeight()));
g.setColour(Colours::white);
g.fillRect(0,0,getWidth(),y);
g.setColour(Colours::red);
g.fillRect(0,y,getWidth(),getHeight()-y);
}
void tick()
{
phase+=0.01f;
phase-=int(phase);
repaint();
}
private:
float phase;
};
// *********************************************************************************
MainComponent::MainComponent()
{
// add 30 horizontal level meters
for (int i=0; i<30; i++)
{
LevelMeter* meter=new LevelMeter();
addAndMakeVisible(meter);
meter->setBounds(50+i*40, 350, 10, 300);
meters.add(meter);
}
// add 30 vertical level meters
for (int i=0; i<30; i++)
{
LevelMeter* meter=new LevelMeter();
addAndMakeVisible(meter);
meter->setBounds(10, 10+i*14, 30, 10);
meters.add(meter);
}
// add button to turn on/off vertical level meters
addAndMakeVisible(btnVertical=new ToggleButton("vertical level meters"));
btnVertical->addListener(this);
btnVertical->setBounds(200,10,300,30);
btnVertical->setToggleState(true, true);
// add button to turn on/off horizontal level meters
addAndMakeVisible(btnHorizontal=new ToggleButton("horizontal level meters"));
btnHorizontal->addListener(this);
btnHorizontal->setBounds(200,40,300,30);
btnHorizontal->setToggleState(true, true);
// start common timer
startTimer(50);
}
// ---------------------------------------------------------------------------------
MainComponent::~MainComponent()
{
deleteAllChildren();
}
// ---------------------------------------------------------------------------------
void MainComponent::timerCallback()
{
for (int i=0; i<meters.size(); i++)
meters[i]->tick();
}
// ---------------------------------------------------------------------------------
void MainComponent::buttonClicked(Button *btn)
{
bool b=btn->getToggleState();
// change visibility of the vertical level meters
if (btn==btnVertical)
for (int i=30; i<60; i++)
meters[i]->setVisible(b);
// change visibility of the horizontal level meters
else
for (int i=0; i<30; i++)
meters[i]->setVisible(b);
}
// *********************************************************************************class LevelMeter:public Component
{
public:
LevelMeter()
: lastHeight (0), currentHeight (0)
{
phase=mainPhase;
mainPhase+=0.04f;
setOpaque(true);
}
void paint(Graphics &g)
{
// int y=int(phase*float(getHeight()));
g.setColour(Colours::white);
g.fillRect(0, 0, getWidth(), currentHeight);
g.setColour(Colours::red);
g.fillRect(0, currentHeight, getWidth(), getHeight() - currentHeight);
}
void tick()
{
phase+=0.01f;
phase-=int(phase);
currentHeight = int (phase * float (getHeight()));
if (currentHeight != lastHeight)
{
lastHeight = currentHeight;
repaint();
}
}
private:
float phase;
int lastHeight, currentHeight;
};
class LevelMeter:public Component
{
public:
LevelMeter()
: lastHeight (0), currentHeight (0)
{
phase=mainPhase;
mainPhase+=0.04f;
setOpaque(true);
}
void resized()
{
onImage = Image (Image::RGB, getWidth(), getHeight(), false);
offImage = Image (Image::RGB, getWidth(), getHeight(), false);
{
Graphics g (offImage);
g.fillAll (Colours::white);
}
{
Graphics g (onImage);
g.fillAll (Colours::red);
}
}
void paint(Graphics &g)
{
// int y=int(phase*float(getHeight()));
// g.setColour(Colours::white);
// g.fillRect(0, 0, getWidth(), currentHeight);
//
// g.setColour(Colours::red);
// g.fillRect(0, currentHeight, getWidth(), getHeight() - currentHeight);
const int meterHeight = getHeight() - currentHeight;
g.drawImageAt (offImage, 0, 0);
g.drawImage (onImage,
0, currentHeight, getWidth(), meterHeight,
0, currentHeight, getWidth(), meterHeight);
}
void tick()
{
phase+=0.01f;
phase-=int(phase);
currentHeight = int (phase * float (getHeight()));
if (currentHeight != lastHeight)
{
lastHeight = currentHeight;
repaint();
}
}
private:
float phase;
int lastHeight, currentHeight;
Image onImage, offImage;
};Return to General JUCE discussion
Users browsing this forum: andi, Google [Bot], Google Feedfetcher and 3 guests