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

Author Topic: Dynamic Tile Map  (Read 3558 times)

0 Members and 1 Guest are viewing this topic.

zakkor

  • Newbie
  • *
  • Posts: 27
    • View Profile
Dynamic Tile Map
« on: June 30, 2014, 09:31:14 am »
The tile map in the tutorial is static, right? I was wondering, for a dynamic map, what changes would you need to make to the load() function to further optimize it? So far i've put the function that loads the tileset into a constructor, so it doesn't get loaded every cycle, but the performance is still pretty poor (around 40-50 fps on a 10x10 map, but also 40-50fps on a 100x100 map).

Is there anything else you'd recommend?

Also, is this a bad approach? What's the best way to render a map of dynamic NPCs?
is there a way to only redraw the tiles that have been modified in the vertex array? and leave everything else static?

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: Dynamic Tile Map
« Reply #1 on: June 30, 2014, 10:40:09 am »
Provide us with minimal code example
Which tutorial are your referring to? give us a link.

I would suggest to read the "Read before posting" http://en.sfml-dev.org/forums/index.php?topic=5559.0
It will help you, to help us, to help you.

Best way you gonna performance if you drawing with sfml is to use sf::VertexArray
Yes there is a way, but its gonna be supper inefficient, mostly it would be more efficient just to redraw the whole thing.
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

zakkor

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Dynamic Tile Map
« Reply #2 on: June 30, 2014, 10:52:58 am »
Here's the tutorial in question from the SFML wiki : http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#example-tile-map

Here's the load() function that I modified (this gets called every game loop):

http://pastebin.com/GHrVmttD

As you can see I've moved the tileset loading and vertex array resizing to outside this for better performance.

And here's the main render function:

http://pastebin.com/1VTx3vkS

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: Dynamic Tile Map
« Reply #3 on: June 30, 2014, 11:50:58 am »
First of all std::cout kills performance, remove it when testing for performance.

And why are you restructuring each sf::VertexArray before draw call?, i mean there are reasons why would you do that, but the platform surely has memory to hold 1mb of data, just make more sf::VertexArray and don't load then each loop, just draw them.

Is there a reason why you reload/reconstruct sf::VertexArray each loop? whats the point to that?


Also for god sake whats this supposed to do?
Code: [Select]
if (!mapObj.tmap.load(mapObj.tmap.getTileSet(),
                          sf::Vector2u(64, 64),
                          mapObj.getTerrainType(),
                          mapObj.getMapWidth(),
                          mapObj.getMapHeight()));
Why did you put ;(semicolon) at the end? there is no code to execute, why is it in if statement then?
What you want to do here is something like
Code: [Select]
if (!mapObj.tmap.load(mapObj.tmap.getTileSet(),
                          sf::Vector2u(64, 64),
                          mapObj.getTerrainType(),
                          mapObj.getMapWidth(),
                          mapObj.getMapHeight()))
window.draw(mapObj.tmap);
But, that is not what you want to do each loop.
« Last Edit: June 30, 2014, 11:57:53 am by BaneTrapper »
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

zakkor

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Dynamic Tile Map
« Reply #4 on: June 30, 2014, 11:57:09 am »
Maybe I'm misunderstanding this VertexArray, but I reconstruct it each loop because it changes in the meantime...?
If any of the tiles change (or the player moves), the vertex array has to be reconstructed, right?

Oh, that's a leftover from when the if did have code to execute... it doesn't make any difference so I just left it like that for now

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: Dynamic Tile Map
« Reply #5 on: June 30, 2014, 12:06:27 pm »
One great way to improve your performance is to edit the vertexArray directly instead of loading the whole thing again, because only one quad changed.
For example, your unit has quad id 0-3 and your vertex array size is 400. You are doing allot needless operations.

Just do what you do at load
Code: [Select]
  quad[0].position = sf::Vector2f(unitPos.x, unitPos.y);
            quad[1].position = sf::Vector2f(unitPos.x + unitSize.x, unitPos.y);
            quad[2].position =  sf::Vector2f(unitPos.x + unitSize.x, unitPos.y + unitSize.y);
            quad[3].position =  sf::Vector2f(unitPos.x, unitPos.y + unitSize,y);
Depending in which way you structure quads(going clockwise or counterclockwise, and what is your first tile, in this example i start with left up and go clockwise
That is what you want to do when you need something to move in vertex array.
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Dynamic Tile Map
« Reply #6 on: June 30, 2014, 12:16:20 pm »
Why are you using a vertex array for your NPCs? It would be much simpler to use sprites. sf::VertexArray is great only in certain conditions, it's not the magical solution to everything.
Laurent Gomila - SFML developer

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Dynamic Tile Map
« Reply #7 on: June 30, 2014, 12:57:36 pm »
it's not the magical solution to everything.
It would be great if there was such a solution.

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: Dynamic Tile Map
« Reply #8 on: June 30, 2014, 03:20:45 pm »
it's not the magical solution to everything.
It would be great if there was such a solution.
Solution can be achieved in different ways, reinventing the wheels? there is no point to that.
They got enough stuff to do anyway.
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0