I'm trying to create a loop to draw 10 blocks on the screen, but nothing is showing. I got no error, so I think that the vector is not storing the sprites. I'm new to SFML, so I don't really know what I'm doing wrong.
sf::Texture bTexture;
sf::Texture bloqueTexture;
sf::Sprite bloqueSprite;
//create vector of blocks
std::vector<sf::Sprite> bricks(10, sf::Sprite(bloqueTexture));
fondo.setTexture(img_mgr.getImage("fondo.jpg"));
personaje.setTexture(img_mgr.getImage("jumper.png"));
personaje.setPosition(100,POSICION_TERRENO_Y);
bloqueSprite.setTexture(img_mgr.getImage("bloque.png"));
bloqueTexture.loadFromFile("Recursos/imagenes/bloque.png");
//Fill the vector with the texture
for (int i = 0; i < bricks.size(); i++)
{
bricks[i].setTexture(bloqueTexture);
bricks[i].setPosition(100 + (i * 45) , 320);
window.draw(bricks[i]);
}
2nd edit with final answer : if you want to display png
files with SFML, save them 8bit.
Edit: I had some bad copy/paste in the second code, I fixed it
As SFML is made for multi media applications (mostly games), you need to refresh and draw to screen many times by second (that's frames
). That being said, the basic approach is to have a main loop doing 3 things : handling inputs, updating your game logic and then drawing.
See the classic example from SFML's website :
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
Your texture loading and filling the vector have to be done before the main loop, and then between window.clear()
and window.display
you need to draw everything you want to display (your blocks).
You may end up with something like this :
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::Texture bTexture;
sf::Texture bloqueTexture;
sf::Sprite bloqueSprite;
//create vector of blocks
std::vector<sf::Sprite> bricks(10, sf::Sprite(bloqueTexture));
fondo.setTexture(img_mgr.getImage("fondo.jpg"));
personaje.setTexture(img_mgr.getImage("jumper.png"));
personaje.setPosition(100,POSICION_TERRENO_Y);
bloqueSprite.setTexture(img_mgr.getImage("bloque.png"));
bloqueTexture.loadFromFile("Recursos/imagenes/bloque.png");
for (int i = 0; i < bricks.size(); i++)
{
bricks[i].setTexture(bloqueTexture);
bricks[i].setPosition(100 + (i * 45) , 320);
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
for (int i = 0; i < bricks.size(); i++)
{
window.draw(bricks[i];
}
// Consider doing this :
// for(const auto& brick : bricks)
// window.draw(brick);
window.display();
}
return 0;
}
I think problem is with loading textures, try to check is loadFromFile function returning true.