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 / TMX Importer v1.2

Author
Message
Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Feb 2014 22:29 Edited at: 15th Mar 2014 03:15
I didn't want to hijack Clonkex's thread, and my previous thread is too old to post anymore.

I've added a list of new commands, mostly just basic access commands that Clonkex had. If you need Tiled's property object, you'll need to keep using his version, for now. The major changes in mine are support for all encoding types; xml, csv, base64(uncompressed). Compression is still a little ways off, and even I did manage to write it, I'm not sure how fast it would be in Tier 1.

Object layers and objects are now supported.

Features:
Supports encoding: xml, csv, base64(uncompressed)
Supported map types: orthogonal, isometric, staggered isometric
Supports tileset images which use margin/spacing
Supports image layers
Layer alpha and visibility
Set the map viewport area (Define area of screen where map can only be visible)
Supports tile flipping
Support for TSX tilesets (external tileset xml)
Compression (gzip, zlib)
Objects
Properties
Terrain



Updated March 14, 2014


monotonic
16
Years of Service
User Offline
Joined: 24th Mar 2006
Location: Nottinghamshire, England
Posted: 5th Mar 2014 20:36 Edited at: 5th Mar 2014 20:37
Hi Phaelax,

Firstly thank you for posting your loader, it's appreciated.

Where would I find the base64.agc include file?

Thanks

01100001 01101110 01101111 01110100 01101000 01100101 01110010
01110000 01101111 01101001 01101110 01110100 01101100 01100101 01110011
01110011 01110011 01101001 01100111
Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Mar 2014 03:59
Oops, forgot I had another file in there. Also, I just realized the map is still limited to only 5 layers. This likely includes image and object layers. You have any errors because your map has more than that, for right now you can simply change the array definition in the loadMap function from 5 to something else. I'll work on this issue next.


Here's the base64 file.




And here's a basic template that will load the map and let you move it around with the mouse.


The Daddy
13
Years of Service
User Offline
Joined: 13th Jan 2009
Location: Essex
Posted: 6th Mar 2014 08:55
Good work! Thx for sharing!

www.bitmanip.com
All the juicy you could ever dream of!
Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 6th Mar 2014 20:40
Oh cool. I'm going to check this out this weekend!

You say:

"If you need Tiled's objects and properties, you'll need to keep using his version, for now."

Later you say:

"Object layers and objects are now supported"

Did you just forget to edit the first sentence?

I see some functions in there I think I'm going to enjoy.
Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th Mar 2014 03:27 Edited at: 7th Mar 2014 03:30
There, edited the text for the nit-pickers!

I haven't tested tile flipping, because frankly I can't figure out how to do it in Tiled.

Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 9th Mar 2014 19:49
Hehe. Sorry. I just didn't want forum readers to be confused later.

I was able to load a 3 layer map without problems.

I'm handling tile properties by storing them in a data file and referencing them by sprite ID.

I'll check out some of the other functions in a bit. So far, so good...
Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 9th Mar 2014 20:51 Edited at: 9th Mar 2014 20:53
I should be able to do something like this, right?

tmx_GetTileSpriteId(1, 3, 3)

That should report the sprite ID for layer one at position 3,3.

I get a "Array accessed with too few subscripts at line 318 in tmx_engine.agc"

Line 318 is t = tmx_GetTileIndex(layer, x, y)

What am I doing wrong?

Example attached. I commented out the code that didn't work.

This is just your example updated with the new 1.2 loader and keyboard controls.

Attachments

Login to view attachments
Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 10th Mar 2014 21:18
I forgot to call tmx_Init() which probably didn't help, so I added it just below the include in my main.agc.

That didn't solve the error either though.
Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Mar 2014 03:58
I'll take a look at it tonight or in the morning. I've had a very busy drill weekend, today was sort of my recovery don't do anything day.

Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 11th Mar 2014 04:07
It's cool.

I went through it some today, but didn't have luck. I'll have a look again tomorrow.

I also tried tmx_GetAbsoluteTileSpriteId(x, y) but it always reported 0 so I must have been feeding it the wrong values.
Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 13th Mar 2014 03:49
I've corrected a bug in tmx_GetTileIndex() and tmx_GetTileSpriteId(). They should work now. They will return 0 if no sprite exists for that tile or if X,Y is outside the map boundaries. For image layers (like background images and whatnot), it will only return the sprite ID if the coordinates are within the boundaries of that sprite. Updated code is in the first post.

I haven't got tmx_GetAbsoluteTileSpriteId() to work properly yet. I'm working on that one.

Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 13th Mar 2014 22:57


I tried mouse position, world position, local map position, and in the example above a hard-coded value but none of them report the correct sprite ID.

I'm not giving it the correct x,y. What am I doing incorrectly?

PS: Do you have a donation or tip jar setup somewhere?
Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 14th Mar 2014 02:21 Edited at: 14th Mar 2014 03:57


I have also tried using that to gather the x,y coordinates of the tile.

And then this to try and get the tile sprite ID at those coordinates.



It reports a number over 10,000 which never changes and if I move the map with the mouse it reports 0.



If I use that then I get the expected result. I assume the GID is always the sprite number, right?

vx and vy seem to report the correct map coordinates.

Note: I'm only using Orthogonal. I'm not testing the other styles anymore.
Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 14th Mar 2014 07:02
Performance report:

The load time for a 512x512 tile map with 1 layer using base 64 encoding was 610002 on my gaming rig. After loading it was too slow to scroll.

At 192x192 xml the loading was bearable and scrolling worked, but was choppy. Maybe the mechanism for scrolling does not cull tiles outside the viewable area. I thought that was automatic in AGK.

In my testing loading a 128x128 xml tile map was okay on a PC.

If you want big maps you will probably have to write your own binary converter from tmx to your own personal format. I think someone on the forum already wrote one in C, but I don't know C so I didn't look at it.
Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 14th Mar 2014 17:58 Edited at: 14th Mar 2014 19:09
The X,Y coords for that function is screen coordinates, so you could use the mouse.

The GID is not the sprite number, this would refer to the animation frame used for that tile.

The function is getting the correct tile index, but it's not returning the proper sprite ID every time. I've made a few attempts to specifically set the value to 0 if no GID is present for that tile (it's empty), to avoid any initialization issues. But it's still not doing something right. I'm trying to track it down.


I finally tracked down the probably, and of course it was a stupid mistake on my part. The reason I was getting such strange random numbers is because it was accessing the array WAY outside of bounds. This has caused me so many problems in AppGameKit that I never get any kind of errors with this. Even I get an app crash or it just returns weird numbers rather than saying, "hey idiot, thats not in range!" like DBP always did.

Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 15th Mar 2014 03:14
Using screen coordinates. I've made some corrections to the functions and renamed a few as well. No donations requires, you're helping out quite bit by testing this for me.

Returns an absolute tile ID. An absolute tile ID a single number which contains information about grid coordinates of a tile as well as which layer it's on. The value ranges from 1 to mapWidth*mapHeight*layerCount.

tmx_PickScreenTileAbsoluteId(layer, x, y)


To convert that number into a layer id and a local tile id (local as in it refers to a specific layer):

layerID = tmx_GetTileLayerId(tileId)
tileID = tmx_GetTileLocalId(tileId)

Local tile IDs range from 1 to mapWidth*mapHeight. To get the sprite associated with a tile, there are two ways:

s = tmx_GetTileSpriteId(tileId)

or

s = tmx_PickScreenTileSpriteId(layer, x, y)

With that last function, you can specify 0 for the layer and it will return the sprite ID of the top most visible sprite at that screen location. This includes image layers.

Here's a new demo snippet (updated library is in 1st post).
Use the number keys to turn layers off an on. I'll attach the same example file.


Attachments

Login to view attachments
Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 15th Mar 2014 23:53
Thanks. I'll try this out this weekend and get back to you.

I don't mind testing it. I find it quite useful. Other people will too once people start realizing what they can do with it. If you get around to tile properties later I will test those too.

It sounds like it's a very good idea to do a lot of error trapping on your own. That's good to know.
Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 17th Mar 2014 18:46 Edited at: 17th Mar 2014 18:51
I copied the latest tmx library, example map, and your demo above into a fresh AppGameKit project and compiled it.

The sprite either reports as 0 or some number over 10000 regardless of what layer is on/off.

The mouse cursor also leaves a trail of red tinted blocks. I don't know if that was intended or not.
Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Mar 2014 19:27
Quote: "The sprite either reports as 0 or some number over 10000 regardless of what layer is on/off."

That's normal. 0 means there is no sprite associated with that tile. To conserve memory, it only creates sprites where a tile was placed in Tiled. Sprites numbers, for whatever reason, start numbering from 10000.

Quote: "The mouse cursor also leaves a trail of red tinted blocks. I don't know if that was intended or not."


If you look at the example code, you should see why.




This was to give visible feedback in the example that the proper sprite was indeed being returned.

Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 17th Mar 2014 21:20 Edited at: 17th Mar 2014 21:23
I ate lunch and I see why the red is red now. Thanks.

Maybe I should eat more lunch, but...

I created a 1 layer map. The map has grass and water only. There are only 2 things that tmx_GetTileSpriteId(tileId) should be able to report (grass sprite or water sprite), right?

I used your code with this alteration (1 instead of 3 as I have only 1 layer):



Variable "s" ( reported by tmx_GetTileSpriteId(tileId) ) is not the sprite ID. It is a number very close to the variable "tile" (3 digits off).

I expected it to report 1 (or 10001) and 2 (or 10002) not 10120, 10121, 10123, 10124, etc.
Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Mar 2014 22:02
I do not assign the sprite IDs, AppGameKit handles it automatically so I have no real idea what the numbers will be.

What happens if you manipulate the sprite number returned by tmx_GetTileSpriteId()? I can't guarantee what the sprites numbers will be.

Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 18th Mar 2014 00:00
tmx_GetTileSpriteId() .... Is that one renamed too? Even if I unfold your code in the top comment and search with the browser it is gone.

Maybe we're talking about two different things. If the sprite number of grass is 300 and the sprite number of water is 301, I should not see values of 302, 303, 4000, or anything else, right?

I should only see 300 or 301.

Since tile properties aren't in yet I use the sprite number to tell what tile I'm standing on what object the player wants to manipulate.
Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 18th Mar 2014 02:17
I got the results I wanted using:



I will probably only use 3 layers on a map - terrain, object (eg: a sword), characters.

If I can know what a tile has on each layer I can do anything from allow player interaction to slow movement, etc. I build very old school style tile RPGs if that helps.

I load up tile properties from a text file and then I can go "if tile player is standing on = swamp_tile (gid 3) then poison player".
Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 18th Mar 2014 03:49
I made this function. It seems to work okay.

It returns the gid in the map file for whatever layer you tell it to.

You can probably go outside the map array or request layers that don't exist so error checking is recommended.

Example:

Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Mar 2014 05:19
Quote: "If the sprite number of grass is 300 and the sprite number of water is 301, I should not see values of 302, 303, 4000, or anything else, right?"


Yes you will. Every single tile is a different sprite number. They might all use the same image, but the sprite IDs are all unique. Technically, they are all animated sprites, and each tile has its animation frame set to reflect the proper image tile in the tilemap. So what it sounds like you really want is the sprite's frame number.

You can use this (again use screen coords for x,y) to get the frame number of each tile. This will return the same number for every grass tile.
tmx_PickScreenTileImageFrame(layer, x, y)

The frame numbers are set based on the GID values, so yes that would work as well.

Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 18th Mar 2014 06:35
Oh, ok. I see. Thanks for the explanation and patience.

When I said I wanted the "sprite ID" I really wasn't using the right language to describe what I was going for.

I will check out tmx_PickScreenTileImageFrame tomorrow.
Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 18th Mar 2014 17:21
It occurred to me this morning that if large maps (eg: 512x512) were loaded in chunks of 64x64 tiles at a time the loading times would not be so long.

That style of loading would allow map files could be arbitrarily large, but the game would only ever have to deal with a small section of it.

I don't know how you would avoid bumping into edges all the time though. If you loaded 64x64 you'd see a map edge too often. I will investigate this thought over the weekend.
Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Mar 2014 18:58
The problem isn't so much creating the tiles, they're all just clones of a single base tile, but from reading the file. There's no random access, so reading only portions of the file at a time isn't really possible. I thought of reading the file into a memblock, but you'd still be reading the entire file first anyway, so it seems pointless.

Lost Dragon
11
Years of Service
User Offline
Joined: 22nd Aug 2010
Location:
Posted: 19th Mar 2014 04:48
Ah. I'll just use smaller maps and design around the limitation.

I made minor changes to tmx_PickScreenTileImageFrame() so it reports 0 if variable s is 0 instead of crashing.

Phaelax
DBPro Master
19
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 19th Mar 2014 06:31
Oops, thought I had that check in there.

Login to post a reply

Server time is: 2022-08-07 17:26:03
Your offset time is: 2022-08-07 17:26:03