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

Author Topic: Tiled map draw algorithm  (Read 1609 times)

0 Members and 1 Guest are viewing this topic.

Krechet

  • Newbie
  • *
  • Posts: 4
    • View Profile
Tiled map draw algorithm
« on: August 18, 2020, 05:13:59 pm »
Hey guys!
I'm new with SFML, can anyone give me a little advice?
I want to draw big tiled map. I'm going to use something like that:
sf::Texture texture;
sf::Sprite sprite;
texture.loadFromFile("tileAtlas.png"); //Big texture with all tiles
...
for (i = 0; i < mapWidth; i++) {
  for (j = 0; j < mapHeight; j++) {
    sprite.setTextureRect(..); //Change sprite to appropriate tile
    sprite.move(...); //Move sprite to position of tile (i,j)
    window.draw(sprite);
Is it good practice? Or better to create big array of sf::Sprite?

And another question: do I need to manually check if sprite is on screen (before draw) when using View? Or I can call "draw" to all my big amount of sprites?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Tiled map draw algorithm
« Reply #1 on: August 18, 2020, 05:53:28 pm »
You can try it, but you may get poor performances if your map is big.

Using a single vertex array for the whole map would be a better solution (less draw calls means better performances). See https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php#example-tile-map

You can draw things outside the window, but they will go through some OpenGL transformations before being discarded by the graphics pipeline. So if most of your tiles are outside the window, and performances are not good enough, you can optimize by not drawing them. But remember to never optimize prematurely ;)
Laurent Gomila - SFML developer

Krechet

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Tiled map draw algorithm
« Reply #2 on: August 18, 2020, 07:16:25 pm »
Quote
Using a single vertex array for the whole map would be a better solution
Thanks a lot!