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

Author Topic: Should I be able to switch between drawing two different textures?  (Read 1068 times)

0 Members and 1 Guest are viewing this topic.

pbortler

  • Newbie
  • *
  • Posts: 8
    • View Profile
I'm trying to implement a 2d tilemap by using some existing libraries to parse map files so I can figure out what to render from the sprite sheets. I was only using one texture at first, but soon discovered the map files support multiple textures. I had been using one vertex array with one texture, but now I'm trying to use one vertex array with two textures. To draw the tilemap, I iterate over each layer (preserving Z order), then each tileset (minimizing the total number of texture switches), then each tile (configuring quads for screen draw rect and texture crop rect).

The problem I'm having is that the first call to draw(vertexArray, states) which references the first texture is being drawn as though it used the second texture from the second call to draw(vertexArray, states). It seems the changes to vertexArray went through, so the tiles are in the right locations, but what's being drawn is messed up because it's not respecting the changes to RenderStates::texture pointer between subsequent draw() calls.

Is there something I can do to make it draw the pixels from the first texture, then when I switch the texture it draws the pixels from the second texture? It seems like there must be, otherwise you'd never be able to draw two different textures to the screen at once, and I am assuming that's not the case.

edit: I found the solution with the help of someone on reddit. I needed one VertexArray per Texture per call to draw(). Cheers.
« Last Edit: August 24, 2020, 03:28:32 pm by pbortler »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Should I be able to switch between drawing two different textures?
« Reply #1 on: September 11, 2020, 08:55:49 pm »
Alternative solutions would include to create a texture atlas from the multiple textures, that way you don't need to switch texture or have multiple vertex arrays.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything