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

Author Topic: When should I update my tilemap  (Read 3096 times)

0 Members and 1 Guest are viewing this topic.

Lkcynric

  • Newbie
  • *
  • Posts: 17
    • View Profile
When should I update my tilemap
« on: January 30, 2018, 02:59:20 pm »
Short version:

I have a map of 30 x 20 tiles (32x32px).
the view shows 15x10 tiles at a time (will show partial tile if the view moves only a few px).
I load only 16x11 map, so its "view + 1".

When should I update my 16x11 map? (call the "load" from the tilemap class in the tutorial)
 - 1 - every time before I draw the map to screen (every frame).
 - 2 - After I move the view.
 - 3 - When the view passes a certain point (like ½ the next tile), so it loads only 15 times when going from side to side.

I'm using 2, but think 3 should be better. but may have problems I don't know about.

Detailed Version:

I'm learning how to use tilemaps (used the tutorial in "learn" section), I'm trying to do things on my own so I learn and when it all works, look into the proper way of doing things. But this question keeps poping in my mind, so let me ask it. I did a search, but didn't find an answer.

I have a map of 30 x 20 tiles (32x32px).
the view shows 15x10 tiles at a time (will show partial tile if the view moves only a few px).
I load only 16x11 map, so its "view + 1".

When should I update my 16x11 map? (call the "load" from the tilemap class in the tutorial)
 - 1 - every time before I draw the map to screen (every frame).
 - 2 - After I move the view.
 - 3 - When the view passes a certain point (like ½ the next tile).

Right now I'm using - 2 -. But since the camera can move only a few px at a time, when going from left to right or right to left the full map length it updates around 115 times... even though there are only 15 "new tiles" (The view cant go outside the 30x20 map) .
It updates 79 times from bot to top and 33 from top to bot (the movement is different, thats why the values are different)

Should I try to implement way - 3 - so It only updates 15 times going from side to side and 10 times when going from bot to top? Is that the proper way of doing it?

Thank you for your time.

Paul

  • Jr. Member
  • **
  • Posts: 78
    • View Profile
Re: When should I update my tilemap
« Reply #1 on: January 30, 2018, 05:25:54 pm »
Few things:
- must be map in chunks? You can load whole map and use formula to get the tiles which are on the screen

- update map/view every frame is rather unnecessary. Update it only if you're scrolling is not good idea, what if object on the map is destroyed?

- you can have fixed update procedure in game loop, let's say 20x, 30x or 60x per second.

- it depends on what you want to achieve, your world will be not only terrain I suppose. If you have static or moving objects like trees, characters, you can store their ID in tilemap/grid. If they are animated/moving 30 updates per seconds can be too low

- also scrolling can looks bad with low update rate
« Last Edit: January 30, 2018, 05:28:23 pm by Paul »

Lkcynric

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: When should I update my tilemap
« Reply #2 on: January 30, 2018, 07:11:41 pm »
Things don't have to be any way. I'm trying to learn and figure things out on my own, since I don't fully understand everything I make simple mistakes (That I usually correct later).

I load the map in chunks because I understood it wrong.
For whatever reason, I was thinking if I wanted to show the tilemap on screen with the windows.draw() I would have to load the map I want every time, or else it would always show the full map. but that doesn't make much sense I realize now.

What I should do is, load the full map ONCE and then, show ONLY the part i need. Is that the correct way?
Of course, I will update the map when it needs to, like when I destroy an object.

EDIT: Lately I have been trying to solve problems the wrong way. The problem was I was drawing the full map, instead of changing how I draw it, I changed it to load a smaller map.
« Last Edit: January 30, 2018, 07:13:28 pm by Lkcynric »

Paul

  • Jr. Member
  • **
  • Posts: 78
    • View Profile
Re: When should I update my tilemap
« Reply #3 on: January 30, 2018, 09:39:13 pm »
Yes, it's correct way. Draw only tiles which are visible on the screen.

Sometimes it can be harder for imagination, just use pencil and paper :)

Lkcynric

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: When should I update my tilemap
« Reply #4 on: January 30, 2018, 09:58:43 pm »
Yes, I had to use pen and paper to help me with some things since I fall to the habit of not thinking how something work and just testing it, and then take forever to fix something because of it.

This time I just mixed loading the whole map with displaying the whole map. Its the 2nd one I don't want to do.
Thank you for the help!

Lkcynric

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: When should I update my tilemap
« Reply #5 on: January 31, 2018, 01:26:29 am »
I'm kinda embarrassed about it but I need help.
Since my last post I have been reading a ton on how to draw only the tiles that are visible, but I only got confused about if thats even possible.

This is my "tile manager", is the one from the tutorial with 2 changes.
(click to show/hide)

Changed from receiving "const int" to "std::vector<std::vector<int>>"
I added the m_vertices.clear() so when I change maps the old don't stay on screen.
and added
if (tileNumber == 0) // so 0 = no texture.
        continue; //skips giving texture to this quad vertice.
else
        tileNumber--;
 
So when the tile is 0 it doesn't show anything. (I'm not sure if I should be doing this, but thats a question for another day)

This is how I load the level:
map.load("Assets/tileSet2.png", sf::Vector2u(32, 32), _currentLevel, 30, 20);
This is how I draw the tilemap:
window.draw(map);

As you can see my map is 30x20.
My view is 15x10.
I want to draw only 16x11.
is that possible the way I have things set up?

What I was doing was, instead of loading the full map (30x20), i was loading only the 16x11, and update it it when i need the next tile. Since that is wrong, I don't know how to do it.
I read when you draw a vertex array it always fully draws, there is no way to draw only part of it. then read SFML doesn't render whats outside the view anyway. But I also saw in so many topics that rendering only the visible tiles its the correct way, so I'm just confused now.

I'm probably mixing concepts again. If anyone can help, i would appreciate it.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: When should I update my tilemap
« Reply #6 on: January 31, 2018, 08:38:53 am »
You always need to understand the reasoning behind something, only then can you understand how something needs to be done.

Only drawing the things that are visible is important, because sending all the vertex data to the GPU can become a performance issue, once you reach a high enough number. For the extreme imagine a huge level with 100000 tiles, you don't want to send all that data to the GPU every single frame when in fact you're only displaying like 50 tiles at once.

With that being said, the issue is not about having the tile set loaded or the tile map data loaded in memory, but it's about the vertex data transfer from the CPU/RAM to the GPU/VRAM.

What you need to do is determine the sub-rectangle on your tile map, that needs to be render to the screen and then build the vertex array with just that data.
Alternatively, if your tile map is not really big, it might not be worth caring about this, as there's no real performance impact. Which is generally a good rule, if you don't have performance issues (aka bottleneck) then there's no point in trying to optimize something.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lkcynric

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: When should I update my tilemap
« Reply #7 on: January 31, 2018, 07:51:07 pm »
Thank you, I understand how it works much better now. You are correct, was a bad approach on my part.
I decided to do and finish things even if in a bad way to see if I could do it and learn along the way, only in the end read more about it and check how to properly do it. Now I was trying to solve a problem that doesn't even exist from something I don't fully understand... not a good idea.

It didn't even occur to me that loading a bigger tileset might even have a better performance than a badly implemented "solution".

Let me ask you one last question to make sure I fully understood what you meant.

If there's a performance hit and I need to fix it, a good way is to instead of having a vertex array with, lets say  10 000 tiles, devide that in smaller chunks. like the tiles that fit on the screen +a buffer.
The buffer might be just 1, but should probably be a little higher so I don't have to update the vertex array so often (but again, the "problem" is the transfer not remaking the vertex array, so +1 buffer would work)

Is that what you meant?



eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: When should I update my tilemap
« Reply #8 on: January 31, 2018, 09:09:18 pm »
Yes. :)

And it's not uncommon that people's "optimization" turns out to perform worse than without it, especially if you're not actually optimizing the bottleneck and your implementation isn't performant enough. So don't try to prematurely optimize things, unless you really know what you're doing. This of course shouldn't prevent you from making smart decisions as you go, but it should merely prevent you from wasting time on things that are irrelevant to begin with. ;)
« Last Edit: January 31, 2018, 09:12:30 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lkcynric

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: When should I update my tilemap
« Reply #9 on: January 31, 2018, 09:49:33 pm »
I see, that makes a lot of sense.
Thank you so much! :)

 

anything