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

Author Topic: 'Tiled' Tile-map Loader  (Read 201359 times)

0 Members and 4 Guests are viewing this topic.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: 'Tiled' Tile-map Loader
« Reply #195 on: December 03, 2014, 09:51:35 pm »
Hi everybody,

I recently interested in using Tiled with SFML and I am looking for a small sample of code before using it, who shows me how to display a tilemap from a Tiled file and how to handle collision with a little rectangle and the tilemap.

Can you help me please?

Thank you in advance.

There are a set of examples included in the repository  for a range of the map loaders features, as well as some blog posts about collision linked via the wiki.

OualidH38

  • Newbie
  • *
  • Posts: 46
    • View Profile
    • Email
Re: 'Tiled' Tile-map Loader
« Reply #196 on: December 04, 2014, 03:04:18 pm »
Thank you all  ;)

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: 'Tiled' Tile-map Loader
« Reply #197 on: December 04, 2014, 11:33:10 pm »
An alternative to using fallahn's excellent tmx loader could be to do what I've done for a few projects; save as json in Tiled and then use PicoJSON to parse the files...

Theshooter7

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: 'Tiled' Tile-map Loader
« Reply #198 on: February 27, 2015, 03:51:21 am »
I've implemented the Tiled loader into my game successfully and all, and have been using it quite heavily. One problem I continue to have, however, is a strange visual artifact:
(click to show/hide)
If you can't quite spot it, here is an image cropped out to highlight the problem (notice the internal edges of the tiles looking really strange?)
(click to show/hide)
The kind of placeholders I am using aside, for some reason the tiles become quite distorted. Characters and such do not face this problem as I use sf::Texture::setSmooth() on them, but even adding that into the tiled loader source does not produce acceptable results (and messes with the use of LTBL's light texture).

Now I don't believe it is specifically a fault of the tiled loader, but I'm wondering if something can be done. And yes, my sf::View's are clamped to whole integers. My tiles are 32x32, and my sf::View defaults to 800x600 at a 4:3 resolution (though it adjusts for widescreen resolutions as well), if that helps.

[EDIT] Also, sorry for bumping this from page 4, if that's a concern.

xerca

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: 'Tiled' Tile-map Loader
« Reply #199 on: March 21, 2015, 04:28:48 pm »
Hello. I have encountered another problem while using the map loader and solved it, so I am posting this here in case anyone else gets the same problem or fallahn decides to update it.

The loader works with no problem when the tilesets have 1 px spacing and 1 px margin. But I had a map made with a tileset that had no margins and despite Tiled showing it without a problem, in the game, the tiles were all messed up. I looked through the code and noticed these lines in MapLoaderPrivate.cpp:
//slice into tiles
int columns = (sourceImage.getSize().x - margin) / (tileWidth + spacing);
int rows = (sourceImage.getSize().y - margin) / (tileHeight + spacing);
 
Here, sourceImage.getSize().x and .y  are the dimensions of the tileset image. The problem with this is, there are 2 margins in both dimensions (one left + one right and one up + one down) and "tiles in a row/column" - 1 spacings (since there is no spacing before the first and after the last tile).

In my case, this resulted in 56 instead of 57 tiles in a row: (968 - 0)/(16 + 1) = 56.94 which rounds down to 56.

I think the correct calculation should be like this:
//slice into tiles
int columns = (sourceImage.getSize().x - 2*margin + spacing) / (tileWidth + spacing);
int rows = (sourceImage.getSize().y - 2*margin + spacing) / (tileHeight + spacing);
 
So I changed it and now it works.

madnotdead

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: 'Tiled' Tile-map Loader
« Reply #200 on: April 05, 2015, 07:18:39 am »
Hi guys! I've been using sfml for a while and just discovered the tmx loader. I'm using it in a college project but i'm having a particular issue. When using a layer to perform collisions my player collides well for a while but then pass througth the solid objects. This is my collision code:


for (auto layer = layers.begin(); layer != layers.end(); ++layer)
                {
                        if (layer->name == "Coll")
                        {
                                for (auto object = layer->objects.begin(); object != layer->objects.end(); ++object)
                                {
                                        if (object->Visible())
                                        {
                                                if (object->GetAABB().intersects(getGlobalBounds(), area))
                                                {
                                                        if (area.width > area.height)
                                                        {
                                                                if (area.contains({ area.left, getPosition().y }))
                                                                {
                                                                        // Up side crash
                                                                        setPosition({ getPosition().x, getPosition().y + area.height });
                                                                        std::cout << "Up side crash" << std::endl;
                                                                }
                                                                else
                                                                {
                                                                        // Down side crash
                                                                        onGround = true;
                                                                        inAir = 0.f;
                                                                        setPosition({ getPosition().x, getPosition().y - area.height});
                                                                        std::cout << "Down side crash" << std::endl;
                                                                }
                                                        }
                                                        else if (area.width < area.height)
                                                        {
                                                                if (area.contains({ getPosition().x + getGlobalBounds().width - 1.f, area.top + 1.f }))
                                                                {
                                                                        //Right side crash
                                                                        setPosition({ getPosition().x - area.width, getPosition().y });
                                                                        std::cout << "Right side crash" << std::endl;
                                                                }
                                                                else
                                                                {
                                                                        //Left side crash
                                                                        setPosition({ getPosition().x + area.width, getPosition().y });
                                                                        std::cout << "Left side crash" << std::endl;
                                                                }
                                                        }
                                                }
                                        }

                                }
                        }
 

The code works ok if i just collide againts a simple sprite but when i use it with the .tmx map it fails. Any help?
« Last Edit: April 05, 2015, 07:41:38 am by madnotdead »

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Re: 'Tiled' Tile-map Loader
« Reply #201 on: April 16, 2015, 08:27:25 am »
Matt, Please check the latest comment on github from me about FPS and maps...

https://github.com/fallahn/sfml-tmxloader/issues/28


Anyone else here have an ideas for code they have updated the project with to help speed up render FPS?
Thanks!

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: 'Tiled' Tile-map Loader
« Reply #202 on: April 23, 2015, 03:59:13 pm »
I think the correct calculation should be like this:
//slice into tiles
int columns = (sourceImage.getSize().x - 2*margin + spacing) / (tileWidth + spacing);
int rows = (sourceImage.getSize().y - 2*margin + spacing) / (tileHeight + spacing);
 
So I changed it and now it works.

Thanks for finding this! I have included it in the repo now. I've also been working on killing a few other bugs, and improving the rendering of large maps. The performance should now be many times better than it previously was.

agmcleod

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: 'Tiled' Tile-map Loader
« Reply #203 on: May 12, 2015, 04:02:10 am »
Sorry for newb question, just having trouble building the xcode project. Wondering if anyone has successfully. I think the issue is the with the compiler set.

I get errors like:

   
// /Users/aaronmcleod/Documents/programming/c/sfml-tmxloader/include/tmx/MapLayer.h:75:45: A space is required between consecutive right angle brackets (use '> >')
    mutable std::vector<std::vector<sf::Vertex>> m_patches;

All of these get "No matching function for call to make_shared:

m_children.push_back(std::make_shared<QuadTreeNode>(m_level + 1, sf::FloatRect(x + halfWidth, y, halfWidth, halfHeight)));
        m_children.push_back(std::make_shared<QuadTreeNode>(m_level + 1, sf::FloatRect(x, y, halfWidth, halfHeight)));
        m_children.push_back(std::make_shared<QuadTreeNode>(m_level + 1, sf::FloatRect(x, y + halfHeight, halfWidth, halfHeight)));
        m_children.push_back(std::make_shared<QuadTreeNode>(m_level + 1, sf::FloatRect(x+ halfWidth, y + halfHeight, halfWidth, halfHeight)));
 

I went into build settings, and ensured the Language C++ is set to use C++11, and libc++ (LLVM C++ standard library with C++11 support). But the compile errors still come back.

SeriousITGuy

  • Full Member
  • ***
  • Posts: 123
  • Still learning...
    • View Profile
Re: 'Tiled' Tile-map Loader
« Reply #204 on: May 12, 2015, 09:35:20 am »
std::make_shared is a C++14 Feature. Make sure your compiler supports this feature, VC12 and GCC 4.8 and higher do support it I know from experience, but don't know about LLVM.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: 'Tiled' Tile-map Loader
« Reply #205 on: May 12, 2015, 09:47:11 am »
std::make_shared is a C++14 Feature. Make sure your compiler supports this feature, VC12 and GCC 4.8 and higher do support it I know from experience, but don't know about LLVM.
std::make_shared is a C++11 feature, std::make_unique is a C++14 feature. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

SeriousITGuy

  • Full Member
  • ***
  • Posts: 123
  • Still learning...
    • View Profile
Re: 'Tiled' Tile-map Loader
« Reply #206 on: May 13, 2015, 02:05:45 pm »
Hm, I actually thought it's a C++14 feature because std::make_unique is also.
But you are right, it's C++11, good catch  ;D

Nevertheless, the compiler must support it, and it looks like agmcleod's compiler doesn't support it (which is also more likely due to his first error with two right angle brackets ).

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: 'Tiled' Tile-map Loader
« Reply #207 on: May 13, 2015, 02:43:43 pm »
All of these get "No matching function for call to make_shared:
This doesn't mean make_shared is not found, otherwise you'd get something along the lines of "undeclared identifier".

The problem might be that the code is relying on implicit conversions. Make sure that the arguments provided to std::make_share have exactly the same types as the parameters in the constructor, and that there are no ambiguous constructor overloads. (Although I would expect a slightly different error message for that, too...)

Or maybe you have a crippled std::make_shared() implementation (before variadic templates), which supports only one argument?
« Last Edit: May 13, 2015, 02:46:27 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

agmcleod

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: 'Tiled' Tile-map Loader
« Reply #208 on: May 13, 2015, 07:32:56 pm »
Quote
The problem might be that the code is relying on implicit conversions. Make sure that the arguments provided to std::make_share have exactly the same types as the parameters in the constructor, and that there are no ambiguous constructor overloads. (Although I would expect a slightly different error message for that, too...)

Or maybe you have a crippled std::make_shared() implementation (before variadic templates), which supports only one argument?

I was thinking that too, I started looking at the tmxloader code base a bit last night to see if it was mix matched somehow.

Keep in mind, this is me just clone the repo, generating the Xcode template via cmake, and trying to compile it. I have done no code changes, left it completely as is.

tyrbo123

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: 'Tiled' Tile-map Loader
« Reply #209 on: July 20, 2015, 04:42:00 am »
1> Test.obj: error LNK2019: link to the unresolved external symbol "public: __cdecl tmx :: MapLoader :: MapLoader (class std :: basic_string <char, struct std :: char_traits <char>, class std :: allocator <char >> const &, unsigned char) "(?? 0MapLoader @ tmx@@QEAA @ AEBV? $ basic_string @ DU? $ char_traits @ D @ std@@V? $ allocator @ D @ 2@@std@@E @ Z ) in main
1> Test.obj: error LNK2019: link to the unresolved external symbol "public: bool __cdecl tmx :: MapLoader :: Load (class std :: basic_string <char, struct std :: char_traits <char>, class std :: allocator <char>> const &) "(? Load @ MapLoader @ tmx@@QEAA_NAEBV? $ basic_string @ DU? $ char_traits @ D @ std@@V? $ allocator @ D @ 2@@std@Z) in main
1> F: \ 1SFMLPROJECTS \ TestProject (v12) \ x64 \ Debug \ TestProject.exe: fatal error LNK1120: unresolved external elements: 2

My libs
sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;sfml-audio-d.lib;sfml-graphics-s.lib;sfml-window-s.lib;sfml-system-s.lib;sfml-audio-s.lib;sfml-graphics-s-d.lib;sfml-window-s-d.lib;sfml-system-s-d.lib;sfml-audio-s-d.lib;Box2D.lib;tmx-loader-d.lib;tmx-loader.lib;pugixmls.lib;pugixmld.lib;zlibd.lib;freetype.lib;jpeg.lib;glew32.lib;opengl32.lib;zlibstatic.lib;

I just try load and draw map.

 

anything