Alright, then. I don't know why it's lagging, so I just post my similar implementation I did long ago.
Stars.hpp
#ifndef STARS_HPP
#define STARS_HPP
#include <SFML/Graphics.hpp>
#include <vector>
class Star
{
public:
Star (float x, float y);
~Star ();
void Draw (sf::RenderTarget& target) const;
private:
float mPosX;
float mPosY;
sf::Shape mCircle;
};
class StarManager
{
public:
StarManager (unsigned int width, unsigned int height);
~StarManager ();
void GenerateStars ();
void DrawStars (sf::RenderTarget& target);
private:
unsigned int mWidth;
unsigned int mHeight;
unsigned int mMaxStars;
std::vector<Star> Stars;
};
#endif
Stars.cpp
#include "Stars.hpp"
Star::Star (float x, float y):
mPosX(x),
mPosY(y)
{
mCircle = sf::Shape::Circle(mPosX, mPosY, 1, sf::Color::Yellow);
}
Star::~Star ()
{
}
void Star::Draw (sf::RenderTarget& target) const
{
target.Draw(mCircle);
}
StarManager::StarManager (unsigned int width, unsigned int height):
mWidth(width),
mHeight(height),
mMaxStars(200)
{
}
StarManager::~StarManager ()
{
Stars.clear();
}
void StarManager::GenerateStars ()
{
unsigned int lx;
unsigned int ly;
for (unsigned int i = 0; i != mMaxStars; ++i)
{
lx = sf::Randomizer::Random(0, mWidth);
ly = sf::Randomizer::Random(0, mHeight);
Star aStar(lx, ly);
Stars.push_back(aStar);
}
}
void StarManager::DrawStars (sf::RenderTarget& target)
{
std::vector<Star>::iterator iter;
for (iter = Stars.begin(); iter != Stars.end(); ++iter)
{
iter->Draw(target);
}
}
And here is how you use it:
StarManager smgr(400, 400); // (400, 400) is the width and height of the area, where the stars will be
smgr.GenerateStars();
// in your render code
smgr.DrawStars(mywindow); // mywindow is your sf::RenderWindow
It's old code, but should work without problems. Hope it helps.