Sign in to follow this  
kzar

Reading platformer map file

Recommended Posts

hey i was hoping to make the platformer map file look somthing like this..
Quote:
map { width = 100 height = 100 tileset = fred.bmp } tile { x = 10 y = 2 picture_x = 5 picture_y = 3 }
Problem is im a bit confused how to read somthing like that. Im not sure if i would want to loop through the characters or the lines of the text file. I searched around a bit, but i could only find information for c++ not c. Could someone point me in the right direction? Thanks, kzar

Share this post


Link to post
Share on other sites
Probably your best bet is to make the format simpler. It sounds like with that format you'd need a tokeniser and all sorts of messing around to match up the curly brackets etc.

I'd go for the simplest thing possible. As a platformer is probably tile-mapped, then one character per tile and a text file which looks like


G =
*******=******
=
=
************
***
****

S
***********************





Or something - say for example, if a * is a platform and = a ladder or something, you could make S the player start point and G the goal.

Even if you have angled tiles, creative use could be made of the ascii-art favourites / and
If you need variable-sized maps, either just place the size at the top on two separate lines (which can be read with fgets() and atoi or sscanf or something like that), or store them somewhere else.

If your maps are too big to easily edit this way in a text editor, consider using a bitmap editor instead. Presumably your program has some facility to load bitmaps in some format - you can just read the colour indices out (if it's an indexed format) and use them for tile IDs.

Mark

Share this post


Link to post
Share on other sites
Your format looks nice for XML processing if you just change it a little. Anyway, it will be better if you use a binary format instead of a text format.

Luck!
Guimo

Share this post


Link to post
Share on other sites
Binary formats suck unless you have a convenient editor for them.

There are two obvious formats for tilemaps - text files (Editable in your friendly text editor, complete with cut / paste and undo)

Or bitmap graphics - Editable in your friendly bitmap editor, completely with cut / paste and undo.

If you use some weird custom binary format, you'll need to write either an editor or converter, which would be extra code.

And seeing as good programmers are inherently lazy :) ...

Mark

Share this post


Link to post
Share on other sites
thanks guys those are some really clever sugestions!

edit: how does the format being xml help by the way? surely it would be just as hard to read?

Share this post


Link to post
Share on other sites
Use a XML parser... like TinyXML. Just open your file and start reading the data. Easy.

But binary format may be better (hey... a bitmap is binary too remember). You need an editor but as someone else said, if you have your game working, then you can tweak it a little and create a level editor.

Luck!
Guimo



Share this post


Link to post
Share on other sites
hmm well the thing is im starting the game with the level loading, i havnt programmed it at all yet! I think il have a try at the xml way, it sounds good.

Share this post


Link to post
Share on other sites
oh I dunno, heres some random puesdo-code

bool inTag, inVar
char *var, *tag, value

while(char character != end of file)
character = read_next_character

if( character = ' ' || '\n' )
continue
if( character = '{' )
inTag = true
else if( character = '}' )
inTag = false

if( character = '=' )
inVar = true

if( !inTag )
tag += character
if( inTag )
if( inVar )
var += character
else if( character = ';' )
inVar = false
tag.var = value
else if (!inVar)
value += character
end while


Dont take that seriously, but you could write a string parser which checks for your own syntex. The above one ignores spaces, end of line characters, expects ; at end of assignments etc... in theory at least :)

Share this post


Link to post
Share on other sites
hey that doesnt look tooo complicated actualy, i think il give it a shot. I know what to be doing roughly now ;).

Share this post


Link to post
Share on other sites
You know, your map file looks a lot like Lua or GameMonkey.

My current GameMonkey map files look something like this:-


global GameMap = table(

Width = 10,
Height = 14,

Layers = table(),
Tiles = table()

);

GameMap.Layers.GroundLayer = table(
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1
);

GameMap.Tiles[0] = table()
{
id = 1,
texture = "ground.tga",
width = 64,
height = 32
};

WorldBuilder.Reset();
WorldBuilder.UseMap(GameMap);





The beauty of this is that the script loads itself ;)

Alternatively, Xml is an option - there's this article which might help you ;)

Share this post


Link to post
Share on other sites
You should not start your game by making a level editor or loader.

Start with hard-coded levels until you have some physics you're comfortable with. Then you will have a better idea what the requirements for the level data are.

Mark

Share this post


Link to post
Share on other sites
ok i have it reading a map like this and drawing it on the screen :D .

Quote:

gggggg
mmmmmm


Its not the best way of doing it i dont think but its easier and i needed to get it working before i could do other things.

cheers for the help everyone!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this