SFML community forums

Help => Graphics => Topic started by: albert_lazaro on May 02, 2021, 09:15:21 pm

Title: TileMap: array of sprites vs vertexArray
Post by: albert_lazaro on May 02, 2021, 09:15:21 pm
Hi,
I'm doing a videogame (or I'm trying to do  :P) that renders a top-down map based on tiles. I had followed the guides and I saw that there is an example that used a vertex array for displaying a map and I had implemented it, but now, before I proceed further, I want to make the correct decisions for the design of the game.

I have been reading some posts related to this topic and I have extracted the next points:


Are the points correct? I'm missing something?

For the moment I'm using VertexArray for displaying a 64x128 tiled map, where tiles are 16x16 pixels and the view is 256*224 pixels (SNES resolution).
Title: Re: TileMap: array of sprites vs vertexArray
Post by: Stauricus on May 02, 2021, 10:23:59 pm
you're kind of right. just some points:

if you need any directions on how to do these, just ask, i'm also a lover of tiles :)
Title: Re: TileMap: array of sprites vs vertexArray
Post by: kojack on May 03, 2021, 12:21:56 am
Another way to go is using a tile shader.

Using just a single quad and 1 draw call, you can have a huge area with no off screen drawing. Have an atlas texture for all your tiles and an index texture to specify which tiles to use.
The colour of each pixel in the index texture is the index of a tile in the atlas.
By changing the pixels of the index texture, you can animate tiles.
Title: Re: TileMap: array of sprites vs vertexArray
Post by: albert_lazaro on May 03, 2021, 07:37:49 pm
Thanks a lot, Stauricus!

For the second point, I can use one texture that is the atlas and set the corresponding sprite to the tiles by an index in the int array that represents the map.

I will try to make or find an algorithm to draw only what is viewed in the viewport. I will try to do it with the VertexArray instead of with tiles.


kojack, I don't want to use a shader for this. I think there are other options before that.
Title: Re: TileMap: array of sprites vs vertexArray
Post by: albert_lazaro on May 04, 2021, 09:08:13 pm
I want to render only what is viewed in the view, the first thing I need is to obtain the left top pixel of the current visualization, in order to get first tile that I want to draw. How can I obtain this pixel? I have been looking the View wiki and didn't found anything.

----- EDIT

I'm looking the tutorials and I see that you can define the Viewport, setting a FloatRect with left, top, witdh and height parameters.

So I have to get the viewport from the current view and get the x and y attributes. This would work if viewport is defined with pixels, not with relative position to the window.

I'm lost at this point.
Title: Re: TileMap: array of sprites vs vertexArray
Post by: Stauricus on May 05, 2021, 02:20:03 am
shaders are also great. too bad I'm not good at them  :(


I want to render only what is viewed in the view, the first thing I need is to obtain the left top pixel of the current visualization, in order to get first tile that I want to draw. How can I obtain this pixel? I have been looking the View wiki and didn't found anything.

you can do something like this (i'm not getting the first pixel, but the first tile to be drawn). chunk_size is in tiles, not in pixels.
sf::Vector2i central_tile(std::floor(view.getCenter().x/tile_size.x), std::floor(view.getCenter().y/tile_size.y)); //central tile in the view
sf::Vector2i first_tile(central_tile.x-chunk_size.x/2, central_tile.y-chunk_size.y/2); //first tile, upper left
sf::Vector2i last_tile(first_tile + chunk_size); //last tile, bottom right

//[...]

for(size_t y=first_tile.y; y<last_tile.y; y++){
    for(size_t x=first_tile.x; y<last_tile.x; x++){
        //draw tile[x][y];
    }
}