Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Example : Tile Map widh AGK and Tiled Map Editor

Author
Message
Crystal Noir
AGK Bronze Backer
12
Years of Service
User Offline
Joined: 7th Sep 2011
Location: France
Posted: 8th Nov 2012 15:57
Hi everyone

I would like to share a code I made to load a tiled map, made with Tiled Map Editor. This is a software you can use to make map for your game.

Before we start, I would like to thank Red Eye for his Xml parser function he shared. To use this code, you will need them.

You can download the xml_parser file (agk file with all functions we need) in this topic : http://forum.thegamecreators.com/?m=forum_view&t=193049&b=41

I attached my sample, in this topic, so you can download it, and launch it with AppGameKit if you want to test.

Also, this is a simple code to show how to load a file made with Tiled Map Editor. But it's very simple and functions like layer of the software are not cover in this example. But it's a good start for someone who wonder how he could do to load a map

Let's get started.

1. In Tiled Map Editor

For this example, we don't use layer, so there is only one unique layer for a simple map. Also, I didn't use the 64 base encoder, so in the Preferences of Tile Map Editor, be sure to desactivate this option.

You can save your map (file / save as) in tmx format. In this example I used a map already done (a sample of Tiled Map Editor). When you save your map, be sure to copy the tmx file AND the image file (that contains the tiles), in the "media" folder of your project. (See example attached to this topic).

Also, we have to make a few modification in the tmx file (witch is an xml file), we have to remplace all " characters by '

It's very easy to do that with an editor like Notepad+ with the "search/replace" function. That's because the xml parser need ' instead of " (and it works better in AGK).

2. In AGK

If we open the tmx file with and editor we have something like this :


The aim of the game, is to retreive this informations. In the "tile" section, "gid" is in fact the tile number of the image file. In my example, we have an image file with 48 tiles, so if I look at the xml file, "gid = '30' /> is in fact the image number 30 in the file.

Here's the code :



And now some explainations :

First, we include the xml_parser file. We use here SetVirtualResolution to make the code more easier to understand.

We setup the xml file with the functions of the xml parser file and we load our tile image.

tileMapWidth and tileMapHeight are variable that retrieve the size of the map, here, this is a 40 tiles X 40 tiles map.

tileWidth and tileHeight are the size of a tile. In this example we have 33 X 33.

33 ? why not 32 ? that's because in our image we have some marges (1 pixel each side). In the LoadImage instruction we added 1 to the bBlackToAlpha parameter to make them transparent

tileCount is the total number of tile in the image file.

tilexCount, tileYCount, x, y are some variables for calculation purposes.

In the For/Next loop, we read the "gid" part of the xml file, to retrieve the number of the tile to display. We create a new sprite, and we use SetSpriteAnimation to cut the image file into tile. The SetSpriteFrame command select the good tile to display, and SetSpritePosition to place the sprite.

We repeat all of these for each tile, line by line (we increment x and y and test tileXCount to make the program drop to the next line, once one is completed).

And...that's all

I hope this small example could help someone and you enjoy it. Feel free to make it better if you want, this is just a start to show how we can use the very good xml parser of Red Eye and how to load a Tiled Map Editor map in AGK.

See you soon ! Thank you

PS : the sample file is attached to this topic

Attachments

Login to view attachments
LeGugusse
17
Years of Service
User Offline
Joined: 15th Feb 2007
Location: Paris - France
Posted: 20th Dec 2012 15:07
That's great!
I'll use it in my side-scrolling game!
Thanks!

Cry "Havoc!" and let slip the dogs of war
Lost Dragon
13
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 7th Jan 2013 00:45
Unless I'm missing something obvious (quite possible) you should change this line in xmlparser.agc:

pos_b = str_find(xml_LinesHolder[iterator],"'",pos_a)

to

pos_b = str_find(xml_LinesHolder[iterator],chr(34),pos_a)

This lets you save tmx files in mapedit and then load them in your game using your code without having to change " to ' in notepad first.

It saves you a step.

Also in your example map the name of the png file you used for the tileset differs from the tileset you supplied. That is easy to fix in notepad - just open the map file and change the whatever_name_it_had.png to desert.png and save it. I guess you changed the name and forgot to update the map.

Good code. Thanks!
Mmarzex
11
Years of Service
User Offline
Joined: 26th Feb 2013
Location:
Posted: 11th Mar 2013 20:12
Is there a way to make this work with tier 2?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 11th Mar 2013 20:42
Yes. There are many different ways to parse strings.

But you can use do all of the same AppGameKit commands in Tier 2 just by adding 'agk::' before the command, make sure the command case is correct and add ';' after the command. Well, that's the basics anyway. Some things are better done in native C++ functions and libraries.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Mmarzex
11
Years of Service
User Offline
Joined: 26th Feb 2013
Location:
Posted: 11th Mar 2013 21:47
I meant more the xml parsing part of it. Is there any AppGameKit specific way of doing that?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 11th Mar 2013 22:45
They don't have any specific xml parsing commands.

That is why someone created the xmlparser.agc file.

It should be possible to convert that to a C++ .cpp/.h pair of files, since any AppGameKit commands used in it have a Tier 2 equivalent.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 1st May 2013 11:32
If you're going to do this in C++, you'd be much better off parsing the file using either TinyXML or simply getting one of the existing TMX libraries.

I'll have a Tier 1 importer myself. Just made one for DBP and thought I'd search the forum for TMX, didn't know someone made one already for AGK. Although, it appears several of the variables that should be read from the file are being hard-coded.

"You're all wrong. You're all idiots." ~Fluffy Rabbit
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 1st May 2013 17:34
I think people have had some issues with TinyXML when they get to platforms other than Windows. I could be wrong, but this is what I seem to remember.

However, if it is only used for the level editor, which is probably only run on Windows like mine, then it isn't an issue.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
3d point in space
14
Years of Service
User Offline
Joined: 30th Jun 2009
Location: Idaho
Posted: 1st May 2013 20:13 Edited at: 2nd May 2013 01:24
this part of my editor in my sprite and tile engine. I have it it teir 2, but the export of the gui can be in teir 1 or 2.
the export will look array like.
and the export will also include the paths of the pictures.

Developer of Space Chips, pianobasic, zipzapzoom, and vet pinball apps. Developed the tiled map engine seen on the showcase. Veteran for the military.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 1st May 2013 21:58
I updated my DB XML parser last night and got it to load a TMX fairly well. Today I'm going to work on porting my parser to AppGameKit, which might be a challenge without dynamic arrays. Definitely not as efficient as an object-based parser, but if you're only using it to load maps it'll do quickly enough.

"You're all wrong. You're all idiots." ~Fluffy Rabbit

Login to post a reply

Server time is: 2024-04-20 04:35:16
Your offset time is: 2024-04-20 04:35:16