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

Author Topic: repeating part of a texture in a vertex array  (Read 762 times)

0 Members and 1 Guest are viewing this topic.

Pristine_Grade4783

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
repeating part of a texture in a vertex array
« on: June 24, 2022, 03:27:57 am »
So im pretty new to SFML. i've been trying to get a section of a tileset to repeat in a vertex array but it just stretches it, any one know a solution if so that would be a great help.

sf::Texture tileset;
        tileset.loadFromFile("assets\\platform\\tiles and background_foreground (new)\\tileset.png");
        tileset.setRepeated(true);
       
        sf::VertexArray grass(sf::Quads, 4);
        grass[0].position = sf::Vector2f(0, 500);
        grass[1].position = sf::Vector2f(1024, 500);
        grass[2].position = sf::Vector2f(1024, 720);
        grass[3].position = sf::Vector2f(0, 720);

        grass[0].texCoords = sf::Vector2f(16, 0);
        grass[1].texCoords = sf::Vector2f(32, 0);
        grass[2].texCoords = sf::Vector2f(32, 16);
        grass[3].texCoords = sf::Vector2f(16, 16);
« Last Edit: June 24, 2022, 10:02:47 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: repeating part of a texture in a vertex array
« Reply #1 on: June 24, 2022, 04:18:08 pm »
When you set a texture to repeating, think of it as if it turned into an infinitely repeating 2D plane.
Instead of picking a 16x16 piece out of that plane, you define what ever size of repeating tiles you want.
For example, if you load a texture that's 16x16 and you set the texCoords in a way that you get a 160x160 rectangle, you'll get that 16x16 texture 10x repeated in each direction.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Pristine_Grade4783

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: repeating part of a texture in a vertex array
« Reply #2 on: June 24, 2022, 04:48:52 pm »
thanks for the reply, how would i do this if im using a tileset

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: repeating part of a texture in a vertex array
« Reply #3 on: June 24, 2022, 05:03:14 pm »
For tilesets you can't really just repeat the whole texture, so you'll have to create the necessary vertices and set the texCoords to match the wanted tile on the texture.

See the example in the VertexArray tutorial: https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php#example-tile-map
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything