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

Author Topic: How to render a tile based game efficiently?  (Read 2605 times)

0 Members and 1 Guest are viewing this topic.

Nex

  • Newbie
  • *
  • Posts: 1
    • View Profile
How to render a tile based game efficiently?
« on: June 17, 2014, 09:27:35 pm »
Hi!

I'm currently trying to create a small, tile-based adventure game implemented in C++ and SFML and need a way to render my tiles efficiently. Right now I use a vector of sprites (std::vector<Sprite>) in which I fill in the Sprites created in the map generation algorithm (I created 3 Sprites whose textures are set to be soil, grass and water, then, while generating the map, I change their position and put the moved Sprite into the vector before changing the position again). This vector is passed to the main class in which I render every Sprite with a for-loop that calls the window.draw() function for every individual Sprite.

Drawing all of those Sprites every time in the main loop and moving them around via the sf::View class is incredibly exhausting for my computer - so there has to be a more efficient way. I probably need to reduce the draw calls. I thought about only re-drawing my tiles everytime there's a change to the map and using the View class to hover over a mostly static image - I also read that I should send my draw calls in a batch or draw a big image via the window.draw() call instead of calling this function for every tile, but I'm still not sure about what the most efficient way to do this is.

I hope someone can give me advice on how to render my map resource-friendly - sadly I'm not that experienced with this library.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: How to render a tile based game efficiently?
« Reply #1 on: June 17, 2014, 09:35:28 pm »
Clearing the screen, drawing a few hundred sprites and then displaying the result, shouldn't be a big problem.
You may get better results with a VertexArray, but if you are just drawing a few tens or hundreds of sprites I wouldn't expect miracles.
Also consider only drawing what is actually visible. SFML won't cull draws that are outside the visible screen area for you, so if you have a huge map then it may make sense for you to cull those yourself.

 

anything