Tileson - A Tiled json parser for Modern C++

I’ll check it out and get back to you :slight_smile:

@RacheProu: Your issue was actually due to a bug in Tileson. I have now fixed the bug and added a new release, as the bug was pretty severe, since it would happen in any case where you would have a tile that had nothing attached to it (no properties, collision data etc.). The reason why it has not been discovered until now, is that the demo-tileset actually has collision data on all tiles, which would make them all have a property. I have added unit tests for these cases to make sure they never happen again.

Thank you for reporting this issue! It was a severe error that was important to make a fix for!

If you download Release 1.0.1 of Tileson, your issue should be fixed :slight_smile:

1 Like

@SSBMTonberry
Hello,
I just found your excellent JSON parser, though I’m hopelessly stuck in using it. I’m using the demo code that you provide on your GitHub page, and my problem is I can get the tileset to load, but I’m unsure how to get the map itself to load. Here’s a pastebin of the code I’m using https://pastebin.com/ha60YUBs. Which produces this result:

. This of course makes perfect sense to me as to why it’s doing this, but I want to render the map itself and not the tileset I’m using SFML 2.5.1

@Shadow001: I see that this might be a common question, so I’ll start working on a demo showing how to draw the map using SFML :slight_smile:

A little hint, though, is that you want to use the offsetX and offsetY that is in the example (and in your pastebin) to determine what part of the tileset you need to draw based on the tile. You can get the actual size of a tile from the map itself: map.getTileSize().
This way, you could load parts of the texure you want to display via the sprite:
mSprite.setTextureRect(sf::IntRect(offsetX, offsetY, map.getTileSize().x, map.getTileSize().y)

Also:
In your code, you should not call the mTexture.loadFromFile(pathStr) inside the for-loop, but rather load it once after you have found your tileset. Then you want to use the mSprite.setTexture() once as well, but use setTextureRect() to draw parts of the texture (tiles). You will need to render the sprite for each time you use setTextureRect, though, or else you will end up with only drawing the last one. You will need to make several changes to your code to make this happen.

As I said: I will start working on an example on how to draw the demo-map using SFML, so you can get the idea from there, if you struggle figuring it out :slight_smile:

@SSBMTonberry: You’re welcome. I’m always happy to be useful.
I hope it was not too hard to fix. :slightly_smiling_face:

@RacheProu: Using your code with your map as example, it was pretty easy to spot that something was wrong. After a few debugs and a small analysis on your map, it was not too hard to spot the origin of the error :slight_smile:

Anything that makes the library more stable is greatly appreciated! :slight_smile:

@Shadow001: I now have a basic example commited, using SFML. It is a work in progress , and I have only verified that it compiles on Linux, but you can check out the code here: https://github.com/SSBMTonberry/tileson/tree/master/examples/sfml . If it were to compile in Windows, make sure you put the openal32.dll file in the executable directory. You can find that file under https://github.com/SSBMTonberry/tileson/tree/master/external_libs/libs/win/release/msvc/sfml

It currently only draws images of tile layers, but I will add the rest later. I might be able to expand the example tonight, if I have the time. Also keep in mind that it only shows how to draw what Tileson have parsed on the go. If used in a game you would use the Tileson data to generate game objects once, then have the objects themselves (or a component) do the drawing for you.

@RacheProu: You may find the example useful as well. Check it out :slight_smile:

I also did some improvements on the previous example I’ve posted, as it shows that it was a little flawed :slight_smile:

Forgot to mention: To compile the examples, the CMake flag BUILD_EXAMPLES must be activated. At this point I cannot guarantee that it compiles, as I’ve only tested the compilation on my working environment (Linux) for now.

Checked! Thanks for your example (it’s funny because your drawTileLayer class function is exactly the first modification I did on my previous code).

Once again: Thanks :slightly_smiling_face:

@SSBMTonberry
Thank you so much for posting that demo. I’m still looking at it. I’m on Windows, so I’ll see if I can get it going. Lol I hope you find the time to finish out this demo

Ok I fixed my nullptr exception, now I’m hung up on getting the demo-tileset to load. It keeps giving me an invalid path error

So I lied I’m getting the nullptr exception again here are the screenshots:

@Shadow001: You need to update to the newest version of Tileson: Release 1.0.1. It fixes an issue which probably is causing your crash :slight_smile:

@SSBMTonberry
Lol I thought I had the most recent version :sweat_smile:

I got your demo to work! Lol now I need to study it and understand what is going on inside the program. Thank you so, so much for helping me out with this. I really really hope you can find the time to finish this demo

@SSBMTonberry
Hey attached is one of my Tiled maps, well the JSON file of it. Could you look at it and tell me why it’s so different looking then the one in the demo?untitled2.json (105.8 KB)

@Shadow001: I can look at it after work tomorrow. Meanwhile you can see if you get any help from looking at the newest SFML example :slight_smile: I added drawing of image layers, and drawing of the object types of the type “object”. Not far away from having an example for drawing the whole map now, but sadly I have to call it a day. I’ll get back to you tomorrow night :slight_smile:

@Shadow001: I took a quick look at your map, and noticed that you data is base64 encoded. At this point this is not supported by Tileson, so the parser will not know what to do. You should be able to load the map if you go to map properties and change the Tile Layer Format to CSV. Also the Tile Render Order might affect how the tiles are drawn. The demo map uses “Left Down”, while you are using “Right Down”.

I now have a full example that draws all the parts of the demo map using SFML in my latest commit. Now got it building on Linux (GCC) and Windows (using MSVC). Should build on OS X as well, using the homebrew version of llvm, but I have yet to get that verified. I got an old mac lying around, but I won’t have the time to verify that it works today.

@SSBMTonberry
Thank you so much!!! Also thank you so much for all your help and patience!!!

@Shadow001 You’re welcome :slight_smile:

Hi again,

I have a question a little bit off topic:
As soon as I made my render function in a standard game loop, my framerate become too slow to be pleasent to see (8<framerate<20 FPS)

My question: is the idea of rendering all my map (not only the displayed part) is absurd (even for quite small maps (ie. 50x50))? or should I consider a new loop pattern?

50x50 isn’t big at all and should not cause problems. Are you sure you’re rendering the map in a performant way? For example, are you perhaps rendering each tile with its own draw call, instead of batching them into fewer draw calls? Are you perhaps generating the drawable object(s) from the data on every frame, instead of having your drawable(s) created once and reusing it?

(This is very off-topic for this forum though, so I’d recommend taking it to DMs or another forum, as this one doesn’t have an off-topic section.)