Going to write a tile-engine

Hey,

I’m going to write a tile-engine for an RPG i hope to make (w/ the SDL of
course), but I don’t know a whole lot about programming w/ files. I was
wondering what the best way to store the data to a file would be (this is
what i was thinking):

–MAPFILE-- //unique identifier-type-thing
x //# of rows and cols

map data goes here

I’m using a struct w/ three members for the tiles (x, y, and terrain type (an
enumerator)). Would I want to store these in binary format, or just write
them as ASCII? (and what would be the best form for this)?

Thanks,
Cameron Matheson_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

Hey,

I’m going to write a tile-engine for an RPG i hope to make (w/ the SDL of
course), but I don’t know a whole lot about programming w/ files. I was
wondering what the best way to store the data to a file would be (this is
what i was thinking):

–MAPFILE-- //unique identifier-type-thing
x //# of rows and cols

map data goes here

I’m using a struct w/ three members for the tiles (x, y, and terrain type (an
enumerator)). Would I want to store these in binary format, or just write
them as ASCII? (and what would be the best form for this)?

If you code it as ASCII, it makes it easy (easier?) to edit the maps by hand.
I suggest using an RFC822-like format. Here’s an example for you to chew on:

– BEGIN EXAMPLE MAP FILE –

Foobar Game Map File
Format-Version: 1
Symbol-Map: # wall_normal,
% wall_diggable,
@ player_start,
$ player_deathmatch_start,
S monster_soldier,
M monster_medic,
h item_health_small,
H item_health_large,
* item_invulnerabilityOn Fri, 11 May 2001 16:37:20 -0600 Cameron Matheson wrote:

#####################################

#

@ # S S

# $

%

M S #

hhh $ H

#####%###############################

H*H

#######

– END EXAMPLE MAP FILE –

Using this map format, it is easy to understand the map using a plain old text
editor, and not hard to parse programmatically. It also eliminates the need for
specifying dimensions of the map in the headers. Note a blank line between the
end of the headers and the beginning of the map proper. Note also the use of
RFC822-style “folding” of the header lines, allowing them to be organized like
they are, in a relatively neat manner. Note finally the first line, which replaces
—MAPFILE— with something more, uhm, distinctive. :wink:

Don’t worry too much about the fact that it seems wasteful of disk space;
that’s what gzip and friends are good for. I suggest adding gzipped map support
to your game for this reason.

Best of luck!

Regards,

Alex.

Hey,

I’m going to write a tile-engine for an RPG i hope to make (w/ the SDL of
course), but I don’t know a whole lot about programming w/ files. I was
wondering what the best way to store the data to a file would be (this is
what i was thinking):

–MAPFILE-- //unique identifier-type-thing
x //# of rows and cols

map data goes here

I’m using a struct w/ three members for the tiles (x, y, and terrain type
(an enumerator)).

What’s x and y? Keep in mind that it’s hard to get the graphics to look good
if you’re playing with sub tile offsets… (Unless you’re using multilayer
maps with “sprite” style tiles for the top layers, of course - that’s when
you should use offsets to make things a lot more flexible.)

Would I want to store these in binary format, or just
write them as ASCII?

Depends on what kind of resolution (ie number of tiles/textures/whatever you
need to address), how big your maps are, what your average target systems is
and so on…

In general; ASCII formats are significantly bigger, but can help debugging a
little. For more complex things, (bordering to scripting, like in most newer
3D games), ASCII formats have the big advantage of not needing various custom
editors. They’re also more flexible and easier to keep compatible accross
versions, at least if you think some about the syntax.

However, large tiled maps aren’t much fun to edit in a text editor, so you
pretty much have to find or hack an editor anyway. As a result,
ASCII/binary doesn’t matter much. Further, as tiled maps are usually just
huge arrays of very simple structs, binary formats are easier to deal with
form the code perspective (no parsing; just load the raw data and dig in),
faster (again, no parsing).

(and what would be the best form for this)?

I’d probably go with something along the lines of a fixed size header with an
ID string and a version code (don’t forget that one!), map size etc, followed
an array of structs. The latter should preferably have some nice alignment,
and sensible field sizes. (Don’t try to squeeze things into little bit fields
and stuff right from the start. Count on changing things a few times…) If
you’re not terribly worried about file size, you can reserve space for
additional fields, so you don’t have to physically convert all map data every
time you need to add something to the format.

If you’re planning to build a massive set of complex tools around the file
format, one way to still keep things simple could be using two formats; one
simple “source” format (big, fixed size structs, lots of “reserved for future
use” fields etc) and one “compiled” format that the engine uses. That way,
you can limit the file format dependencies to the engine and the “compiler”,
and you can also have the “compiler” reorganize things in just about any way,
just to please the engine.

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Saturday 12 May 2001 00:37, Cameron Matheson wrote:

However, large tiled maps aren’t much fun to edit in a text editor, so you
pretty much have to find or hack an editor anyway. As a result,
ASCII/binary doesn’t matter much. Further, as tiled maps are usually just
huge arrays of very simple structs, binary formats are easier to deal with
form the code perspective (no parsing; just load the raw data and dig in),
faster (again, no parsing).

And not portable between machines, or even compilers in some cases. Bad idea.
Dumping structs from memory to disk is okay with saved games (which Quake II
does), but saving supposedly portable map files in such a format is a Big Fat
Mistake ™.

Regards,

Alex.On Sat, 12 May 2001 02:00:46 +0200 David Olofson <david.olofson at reologica.se> wrote:

On Saturday 12 May 2001 00:37, Cameron Matheson wrote:

I’m going to write a tile-engine for an RPG i hope to make (w/ the SDL of
course), but I don’t know a whole lot about programming w/ files. I was
wondering what the best way to store the data to a file would be (this is
what i was thinking):

Don’t worry too much about the fact that it seems wasteful of disk
space;
that’s what gzip and friends are good for. I suggest adding gzipped >map
support to your game for this reason.

You could also use a bitmapped map system if you wanted to spend a little
more time. Similar to what TuxRacer uses, is just a normal image (you can’t
use lossy formats though.)

This also lets you do some neat tricks, you can use the paint programs
flood-fill option to quickly “fill” a map.

And other silly things like using 24bit if you aren’t going to use 16
million tiles, if you were only going to use 65535 tiles tops, you could use
the third channel for “information”.

Of course, this is probably much more overcomplicated then it has to be.
You’d also have to assume all the tiles were fixed sizes.

Though the general basis is that a map is just a macro image of a bunch of
smaller images. (Like using using display lists in display lists in openGL,
you have a single tile as the smallest element, a macrotile, which is a
combination of a bunch of tiles or the map itself, then you can just
translate the entire thing when you move the map.)

More complexity = more things you can do = more time doing it.


Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

----- Original Message -----

Of course, as we’re talking about a file format here, it’s assumed that you
take control of you read and write! Anything else will fail sooner or
later, possibly even when using different compiler versions on some patforms.

However, anything that’s properly defined in terms of field size, endian and
number formats is portable for all practical matters. (Examples: zip, mp3,
gif, jpg, png, wav, mod, xm, mid etc etc)

Still, I have to agree that this is a problem with binary files, especially
when you have to deal with a wide range of compilers. (Packed structs aren’t
a native feature of C.) It’s a lot easier and more reliable to port code that
deals with ASCII.

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Saturday 12 May 2001 03:04, Alexander Hvostov wrote:

On Sat, 12 May 2001 02:00:46 +0200 David Olofson <david.olofson at reologica.se> wrote:

On Saturday 12 May 2001 00:37, Cameron Matheson wrote:
However, large tiled maps aren’t much fun to edit in a text editor, so
you pretty much have to find or hack an editor anyway. As a result,
ASCII/binary doesn’t matter much. Further, as tiled maps are usually just
huge arrays of very simple structs, binary formats are easier to deal
with form the code perspective (no parsing; just load the raw data and
dig in), faster (again, no parsing).

And not portable between machines, or even compilers in some cases. Bad
idea. Dumping structs from memory to disk is okay with saved games (which
Quake II does), but saving supposedly portable map files in such a format
is a Big Fat Mistake ™.

“David Olofson” <david.olofson at reologica.se> wrote in message
news:01051419460301.19195 at cutangle.admeo.se

On Sat, 12 May 2001 02:00:46 +0200
And not portable between machines, or even compilers in some cases. Bad
idea. Dumping structs from memory to disk is okay with saved games
(which

Quake II does), but saving supposedly portable map files in such a
format

is a Big Fat Mistake ™.

Of course, as we’re talking about a file format here, it’s assumed that
you
take control of you read and write! Anything else will fail sooner or
later, possibly even when using different compiler versions on some
patforms.

However, anything that’s properly defined in terms of field size, endian
and
number formats is portable for all practical matters. (Examples: zip,
mp3,
gif, jpg, png, wav, mod, xm, mid etc etc)

Still, I have to agree that this is a problem with binary files,
especially
when you have to deal with a wide range of compilers. (Packed structs
aren’t
a native feature of C.) It’s a lot easier and more reliable to port code
that
deals with ASCII.

It’s not a problem with binary file formats in general, but with “dumping
structs from memory to disk”.> On Saturday 12 May 2001 03:04, Alexander Hvostov wrote:


Rainer Deyke (root at rainerdeyke.com)
Shareware computer games - http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor

Use text. It’s portable and easy to edit etc. You can run standard tools
(grep, XML parsers, etc.) on it.

If you find that file size is a problem, then later you can gzip it, or
use a binary format.

Never “just dump structs”. That’s lazy and unportable (even between
compiler versions).–
Marc A. Lepage
http://www.antimeta.com/
Minion open source game, RTS game programming, etc.

Right, but then again, the problems are closely related in real life: Dumping
structs with bit fields and whatnot in them is an all too “nice” and easy
method of writing binary files in an efficient manner… heh

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Monday 14 May 2001 23:33, Rainer Deyke wrote:

Still, I have to agree that this is a problem with binary files,
especially when you have to deal with a wide range of compilers.
(Packed structs aren’t a native feature of C.) It’s a lot easier
and more reliable to port code that deals with ASCII.

It’s not a problem with binary file formats in general, but with “dumping
structs from memory to disk”.

“David Olofson” <david.olofson at reologica.se> wrote in message
news:01051501363502.19195 at cutangle.admeo.se

Still, I have to agree that this is a problem with binary files,
especially when you have to deal with a wide range of compilers.
(Packed structs aren’t a native feature of C.) It’s a lot easier
and more reliable to port code that deals with ASCII.

It’s not a problem with binary file formats in general, but with
"dumping

structs from memory to disk".

Right, but then again, the problems are closely related in real life:
Dumping
structs with bit fields and whatnot in them is an all too “nice” and easy
method of writing binary files in an efficient manner… heh

Hm. All of my file formats are binary, and I never just dump structs to
files. I get a huge speed boost over text files, much smaller files, and
total portability.

Given that just dumping structs is unacceptable, binary is still better than
text in most cases.> On Monday 14 May 2001 23:33, Rainer Deyke wrote:


Rainer Deyke (root at rainerdeyke.com)
Shareware computer games - http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor

The easiest way I know of to convert objects to portable binary data
streams is using a set of stream classes. In C++ it is just a matter
of writing iostream friend functions. In C there is a nice little
library called XDR (man xdr) that has similar semantics and is installed
with glibc in most RedHat developer configurations.

I’ve also written a library based on XDR for building and parsing XML
files to objects without losing the loose coupling that is what really
makes XML useful. It is described at this URL:

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/~checkout~/dr1/dr1/xdrxml/article.html?rev=1.1

If you like it you like the idea, you can find the source in my project
CVS tree at http://dr1.sourceforge.net

Cheers,
-klsOn Mon, May 14, 2001 at 07:29:20PM -0400, mlepage at antimeta.com wrote:

Use text. It’s portable and easy to edit etc. You can run standard tools
(grep, XML parsers, etc.) on it.

If you find that file size is a problem, then later you can gzip it, or
use a binary format.

Never “just dump structs”. That’s lazy and unportable (even between
compiler versions).


Marc A. Lepage
http://www.antimeta.com/
Minion open source game, RTS game programming, etc.


// .–=,
…::://::::::::::::::::::::::::::::… (o O & @kevin_at_ank.com
:::::::://:::://://://:/:://::||// / V K
:::::://:::://:/:|//’/’ // ,|’ r , ‘qk
:’’’/
__ // / // |_// // || .’~. .~`,
kls _/-=_/

“David Olofson” <david.olofson at reologica.se> wrote in message
news:01051501363502.19195 at cutangle.admeo.se

Still, I have to agree that this is a problem with binary files,
especially when you have to deal with a wide range of compilers.
(Packed structs aren’t a native feature of C.) It’s a lot easier
and more reliable to port code that deals with ASCII.

It’s not a problem with binary file formats in general, but with
"dumping

structs from memory to disk".

Right, but then again, the problems are closely related in real life:
Dumping
structs with bit fields and whatnot in them is an all too “nice” and easy
method of writing binary files in an efficient manner… heh

Hm. All of my file formats are binary, and I never just dump structs to
files. I get a huge speed boost over text files, much smaller files, and
total portability.

Given that just dumping structs is unacceptable, binary is still better than
text in most cases.

Since text files are only parsed at level load time (at which point they’re
presumably read into structs or something), I don’t think the speed boost is
of any real significance. As for file size, well, that’s what gzip is for. :slight_smile:

Regards,

Alex.
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20010514/d46a5221/attachment.pgpOn Mon, 14 May 2001 19:30:35 -0700 “Rainer Deyke” wrote:

On Monday 14 May 2001 23:33, Rainer Deyke wrote:

[…]

I’ve also written a library based on XDR for building and parsing XML
files to objects without losing the loose coupling that is what really
makes XML useful. It is described at this URL:

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/~checkout~/dr1/dr1/xdrxml/ar
ticle.html?rev=1.1

Sounds interesting. (I’m going to need something like that in the near
future, I think… If I ever get out of this work-around-the-clock loophole,
that is. heh)

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Tuesday 15 May 2001 05:03, kevin at ank.com wrote: