Avoid copying around a CircleShape with each Chromosome

This commit is contained in:
Joscha 2017-05-05 08:08:25 +00:00
parent ac8354c686
commit 92f1876aa2
2 changed files with 12 additions and 8 deletions

View file

@ -23,6 +23,8 @@ float Chromosome::stddev_radius = .1;
float Chromosome::stddev_color = 20; float Chromosome::stddev_color = 20;
std::minstd_rand* Chromosome::re; std::minstd_rand* Chromosome::re;
sf::CircleShape Chromosome::circle;
Chromosome::Chromosome() Chromosome::Chromosome()
{ {
@ -115,12 +117,13 @@ void Chromosome::draw(sf::RenderTarget& target, sf::RenderStates states) const
this->circle.setOrigin(Chromosome::size); this->circle.setOrigin(Chromosome::size);
this->circle.setFillColor(sf::Color::White); this->circle.setFillColor(sf::Color::White);
target.draw(this->circle, states); target.draw(this->circle, states);
for (auto gene : this->genes) { for (auto gene : this->genes) {
this->circle.setPosition(gene.position); Chromosome::circle.setPosition(gene.position);
this->circle.setRadius(gene.radius); Chromosome::circle.setRadius(gene.radius);
this->circle.setOrigin(sf::Vector2f(gene.radius, gene.radius)); Chromosome::circle.setOrigin(sf::Vector2f(gene.radius, gene.radius));
this->circle.setFillColor(gene.color); Chromosome::circle.setFillColor(gene.color);
target.draw(this->circle, states); target.draw(Chromosome::circle, states);
} }
} }

View file

@ -27,6 +27,8 @@ public:
size_t length(); size_t length();
protected: protected:
static sf::CircleShape circle;
struct Gene struct Gene
{ {
sf::Vector2f position; sf::Vector2f position;
@ -34,6 +36,8 @@ protected:
sf::Color color; sf::Color color;
}; };
std::vector<Gene> genes;
float maxRadius(); float maxRadius();
Gene randomGene(); Gene randomGene();
@ -43,7 +47,4 @@ protected:
selectSegment(std::vector<Gene>& genes); selectSegment(std::vector<Gene>& genes);
std::vector<Gene>::iterator selectGene(std::vector<Gene>& genes); std::vector<Gene>::iterator selectGene(std::vector<Gene>& genes);
mutable sf::CircleShape circle; // drawing the chromosome, one draw call at a time
std::vector<Gene> genes;
}; };