Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Trying to create custom texture for level using block sprites [Solved]  (Read 1418 times)

0 Members and 1 Guest are viewing this topic.

jarbarman

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
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.

 
« Last Edit: February 21, 2024, 10:53:29 pm by jarbarman »

jarbarman

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Trying to create custom texture for level using block sprites [Solved]
« Reply #1 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Trying to create custom texture for level using block sprites [Solved]
« Reply #2 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/