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

Author Topic: Trouble adding spacing and margin to Tiled map loader  (Read 6576 times)

0 Members and 1 Guest are viewing this topic.

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Trouble adding spacing and margin to Tiled map loader
« on: September 09, 2012, 04:13:08 pm »
I took "Tiled Map Loader" from Walker which is 1.6 and converted it to sfml 2. Works perfectly but now i want to implement spacing margin of the tileset.

int columns = tilesetTexture.getSize().x / tileWidth;
    int rows = tilesetTexture.getSize().y / tileHeight;

tilesetTexture is obviously the texture i loaded my tileset image into.
Now i should somehow add spacing and margin into consideration.

I managed to load spacing and margins from xml file into two variables: spacing, margin. But how to implement them into the calculations ? Somekind of for loop ? Plz show me how.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Trouble adding spacing and margin to Tiled map loader
« Reply #1 on: September 09, 2012, 04:18:45 pm »
I'm not sure if anyone else is familiar with that loader and since you didn't even link to the old nor the new code, it's hard to follow you... ;)

What do the spacing and margin refere to? Does every tile get some margin? Does the texture subrect need to be moved, because the tiles have some spacing? etc...

Btw if the loader works well, you should consider to publish it and/or maybe add it to SFML's wiki. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Re: Trouble adding spacing and margin to Tiled map loader
« Reply #2 on: September 09, 2012, 04:22:24 pm »
Here it is, converted for sfml 2, everyone who wants can use it.

Margins exist on top, and on the left.
Spacing exists between every tile(not picture) which is defined with the width and height.

Both are integers.

level.h
http://pastebin.com/gwasT3hR

level.cpp
http://pastebin.com/U1NG8eKD

This is written by Walker for sfml 1.6:
http://en.sfml-dev.org/forums/index.php?topic=3023.0

I know VertexArrays are better for situations like this, but im inexperienced and will use this for now until i get some knownledge.
« Last Edit: September 09, 2012, 04:28:03 pm by OutlawLee »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Trouble adding spacing and margin to Tiled map loader
« Reply #3 on: September 09, 2012, 05:19:25 pm »
Margins exist on top, and on the left.
Spacing exists between every tile(not picture) which is defined with the width and height.
I'm still not sure where you're problem exactly is and I don't want to read and try to understand the source code at the moment, but thanks for sharing it! :)

Does margin relate to the image you're loading? If so you can load the image into a sf::Image and only copy the part you want to a sf::Texture (without the margin).

As for the spacing there a numberous ways and they all depend on how the tiles get created/stored. How are tiles handled in your loader?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Re: Trouble adding spacing and margin to Tiled map loader
« Reply #4 on: September 09, 2012, 05:28:23 pm »
Margins-Spacing vary for each tileset. Since this loader can load only one tileset margins-spacing are always the same(in a certain map). Margin-spacing are the ones from the image.

What i (walker) do here is load the tileset in to image apply a mask and then load it into a texture that loads in a sprite which is then being cut with this:

//Columns and rows (of tileset image)
    int columns = tilesetTexture.getSize().x / tileWidth;
    int rows = tilesetTexture.getSize().y / tileHeight;

    std::vector <sf::Rect<int> > subRects;//container of subrects (to divide the tilesheet image up)

    //tiles/subrects are counted from 0, left to right, top to bottom
    for (int y = 0; y < rows; y++)
    {
        for (int x = 0; x < columns; x++)
        {
            sf::Rect <int> rect;
            rect.top = y * tileHeight;
            rect.height = y * tileHeight + tileHeight;
            rect.left = x * tileWidth;
            rect.width = x * tileWidth + tileWidth;
            subRects.push_back(rect);
        }
    }
 

Here you can see that it only cuts by the number of columns and rows, it doesnt take margins and spacing in account.

I hope i helped.
« Last Edit: September 09, 2012, 06:49:00 pm by OutlawLee »

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Re: Trouble adding spacing and margin to Tiled map loader
« Reply #5 on: September 09, 2012, 07:40:57 pm »
I think your width and height calculations are wrong, but try this:

//Columns and rows (of tileset image)
    int columns = (tilesetTexture.getSize().x - margin) / tileWidth;
    int rows = (tilesetTexture.getSize().y - margin) / tileHeight;

    std::vector <sf::Rect<int> > subRects;//container of subrects (to divide the tilesheet image up)

    //tiles/subrects are counted from 0, left to right, top to bottom
    for (int y = 0; y < rows; y++)
    {
        for (int x = 0; x < columns; x++)
        {
            sf::Rect <int> rect;
            rect.top = margin + y * tileHeight + space;
            rect.height = tileHeight - 2 * space;
            rect.left = margin + x * tileWidth + space;
            rect.width = tileWidth - 2 * space;
            subRects.push_back(rect);
        }
    }
 

I've assumed you had margin and space variables.

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Re: Trouble adding spacing and margin to Tiled map loader
« Reply #6 on: September 09, 2012, 09:36:00 pm »
@Tex Killer

That made my map look just like mine, just that every tile picture is messed up but the general look is alright.
It seems it cuts the sprite wrong, strange since when i tried without margins and spacing it worked.

Heres the picture:

http://i48.tinypic.com/mc4zy9.jpg

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: Trouble adding spacing and margin to Tiled map loader
« Reply #7 on: September 17, 2012, 10:36:46 am »
Hey. I've been working on this class for one of my own projects to add fuller support for the tmx format. Currently I have most of the tags supported (transparency, visibilty, spacing etc), base64 decoding and zlib compression - when I have support for polygons completed I shall share what I have. To solve the margin problem I did this:

Code: [Select]
//check tileset spacing if it exists
m_spacing = (tilesetElement->Attribute("spacing")) ? atoi(tilesetElement->Attribute("spacing")) : 0;
m_margin = (tilesetElement->Attribute("margin")) ? atoi(tilesetElement->Attribute("margin")) : 0;

    //tiles/subrects are counted from 0, left to right, top to bottom
    for (int y = 0; y < rows; y++)
    {
        for (int x = 0; x < columns; x++)
        {
            sf::Rect <int> rect; //must account for any spacing or margin on the tileset
            rect.top = y * (m_tileHeight + m_spacing);
    rect.top += m_margin;
    rect.height = m_tileHeight;
            rect.left = x * (m_tileWidth + m_spacing);
    rect.left += m_spacing;
            rect.width = m_tileWidth;
            subRects.push_back(rect);
        }
    }

HTH

 

anything