SFML community forums

Help => Graphics => Topic started by: jarbarman on February 21, 2024, 07:15:54 pm

Title: Trying to create custom texture for level using block sprites [Solved]
Post by: jarbarman on February 21, 2024, 07:15:54 pm
I have a 64 x 64 sprite of a block and I want to combine them inside the draw(sf::RenderTarget& target, sf::RenderStates states) method to create a texture of a wall of blocks depending on size.

At first I tried just using target.draw to draw each of the sprites individually, but the sprite.move method doesn't effect the target.

[
void Level::draw(sf::RenderTarget& target, sf::RenderStates states){
    sf::Texture Tblock;
    if (!Tblock.loadFromFile("block.png"))
        exit(1);
    sf::Sprite Sblock(Tblock);
    sf::Texture level;

    if (!level.create(getWidth()*64, getHeight()*64)) {
        exit(1);
    }  

    for(int y = 0; y < getHeight(); y++){
        for (int x = 0; x < getWidth(); x++){
            //draw sprite onto texture
            //I don&#39;t know what to do here
        }
    }

    sf::Sprite Level(level);
    target.draw(Level);

}
]

Any help or hints would be appreciated. Thank you.

 
Title: Re: Trying to create custom texture for level using block sprites [Solved]
Post by: jarbarman on February 21, 2024, 10:55:05 pm
I just found out the states.transform exists which works great, and idk why but I couldn't change the texture of a sprite multiple times or it became really buggy whenever I tried it.
Title: Re: Trying to create custom texture for level using block sprites [Solved]
Post by: eXpl0it3r on February 22, 2024, 07:54:47 am
Your texture needs to be kept alive for the whole duration of the rendering, including the display call, so you can't/shouldn't create it locally inside a function.

Try to load the texture as member of the Level class.

Additionally, if you plan to inherit from sf::Drawable keep in mind that draw() is actually defined as const, so you can't modify e.g. the Level class itself.