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

Author Topic: [SOLVED] tmxLoader by fallahn not drawing correctly when view is rotated  (Read 2542 times)

0 Members and 1 Guest are viewing this topic.

Guido_Ion

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Flow Soccer development
I'm using tmxLoader by fallahn and my view rotates with the movement of the player. The issue is that some parts of the map are not drawn when rotating. It seems that the drawing only takes the size of the view without the rotation.

As you can see in attachment 1, when the player and the view are not rotated (or slightly) the maps draws correctly, and in attachment 2 you can see that when they are rotated part of the map is not drawn.

Before drawing this is being called (private method):
Code: [Select]
void MapLoader::SetDrawingBounds(const sf::View& view)
{
if(view.getCenter() != m_lastViewPos)
{
sf::FloatRect bounds;
bounds.left = view.getCenter().x - (view.getSize().x / 2.f);
bounds.top = view.getCenter().y - (view.getSize().y / 2.f);
bounds.width = view.getSize().x;
bounds.height = view.getSize().y;

//add a tile border to prevent gaps appearing
bounds.left -= static_cast<float>(m_tileWidth);
bounds.top -= static_cast<float>(m_tileHeight);
bounds.width += static_cast<float>(m_tileWidth * 2);
bounds.height += static_cast<float>(m_tileHeight * 2);
m_bounds = bounds;

for(auto& layer : m_layers)
layer.Cull(m_bounds);
}
m_lastViewPos = view.getCenter();
}

Do I need to change tmxLoader myself or is there a way to set it up manually outside MapLoader?
And how do I need to change it?   ???

Note: I didn't want to post in the tmxLoader specific Topic since is been inactive for 4 years now.
« Last Edit: April 29, 2020, 05:36:36 pm by Guido Bisocoli »

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: tmxLoader by fallahn not drawing correctly when view is rotated
« Reply #1 on: April 29, 2020, 11:05:39 am »
It's been a LONG time since I worked on this (as I pretty much abandoned it in favour of tmxlite), so my memory is somewhat foggy. I think the quick and dirty solution would be to just increase the border until it covers the viewable area:

    const std::size_t borderSize = 3; //adjust this

    bounds.left -= static_cast<float>(m_tileWidth * borderSize);
    bounds.top -= static_cast<float>(m_tileHeight * borderSize);
    bounds.width += static_cast<float>(m_tileWidth * (2 * borderSize));
    bounds.height += static_cast<float>(m_tileHeight * (2 * borderSize));

 

Guido_Ion

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Flow Soccer development
Re: tmxLoader by fallahn not drawing correctly when view is rotated
« Reply #2 on: April 29, 2020, 05:35:59 pm »
Thanks fallahn! This is the final code, tested, working great:

Code: [Select]
void MapLoader::SetDrawingBounds(const sf::View& view) // MODIFIED - SEE https://en.sfml-dev.org/forums/index.php?topic=27182.0
{
// place this here so I don't modify more code outside
static float lastAngle = 0.f;

// only if center and/or rotation changed
if (view.getCenter() != m_lastViewPos || lastAngle != view.getRotation())
{
sf::FloatRect bounds;
bounds.left = view.getCenter().x - (view.getSize().x / 2.f);
bounds.top = view.getCenter().y - (view.getSize().y / 2.f);
bounds.width = view.getSize().x;
bounds.height = view.getSize().y;

///add a tile border to prevent gaps appearing
// additional space to add (half of x axis lenght)
const float borderSizeToAdd = view.getSize().x / 2.f;

bounds.left -= borderSizeToAdd;
bounds.top -= borderSizeToAdd;
bounds.width += 2 * borderSizeToAdd;
bounds.height +=2 * borderSizeToAdd;
m_bounds = bounds;

for(auto& layer : m_layers)
layer.Cull(m_bounds);
}
m_lastViewPos = view.getCenter();
lastAngle = view.getRotation();
}

IMPORTANT: the method "void MapLoader::draw(sf::RenderTarget& rt, sf::RenderStates states) const" has to be modified the same way so you can call renderWindow.draw (drawing the entire map) or MapLoader.Draw (drawing each layer separately)

So far tmxLoader is working great but I'll check tmxlite just in case.

EDIT: borderSizeToAdd was unnecesary large and some calculations weren't necessary
« Last Edit: April 30, 2020, 10:44:46 pm by Guido Bisocoli »

 

anything