Tile engine code

Hey,

I’m trying to write a tile engine… but i’m having lots of trouble (it works
fine when I do sequential reads of my map, but random-access reads (which are
the thing that matter it seems like?) don’t seem to work at all… If someone
could point me towards some source code i could look at… that would help a
lot. (I would have posted my code to see if someone could help me, but it
doesn’t have any SDL yet (it’s all ASCII so far), and i was trying to keep
this semi-on-topic.)

Thank you very much
Cameron Matheson_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

I’m going to be working on a tile engine soon (2D) and am just currently
working on the parser. Got that working well last night. Next step is to get
it tiling (Using SDL of course).

Here are my plans with tons of holes in it, because I have never done this
before, and don’t have anything set in stone yet.

I just plan on creating a class called TILE, and while I’m reading the file,
I will just call a constructor which defines the tile. Then I will add it to
my linked list.

First line in my file format is the name of the tileset file I’m using.

tileset.bmp

And the rest of the file is where I load tiles, events, etc…
So inside my file I define a tile like so:
tile:
0
0
32
32

Format is not set in stone, but the 1st and 2nd numbers are the x,y
locations on the screen 3rd and 4th are the x,y locations in the tileset.bmp
file.

So I read through the file, find “tile:” and then call a constructor.
Something like

tile = new TILE(Uint16 x, Uint16 y, Uint16 t_x, Uint16 t_y); (Hopefully you
know what to do with this information.)

or incase of above:

tile = new TILE(0, 0, 32, 32);

This should all be taking place inside the WORLD class which has functions
like LoadMap();
And I will have a linked list containing all the tiles so I can do various
stuff to it, like remove it, check values, or something of the such.

Then I can have a draw function inside the tile class, things are endless
after that. Of course I have never done this before, but this is my idea as
of late. And I hope to have it working sometime this week (Yeah right, to
much school) or during next weekend.

By the way, feel free to criticize my ideas, build upon them, etc, so I can
improve upon my project as well :slight_smile:

-Bryan Arant> ----- Original Message -----

From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of
Cameron Matheson
Sent: Monday, January 28, 2002 4:43 PM
To: sdl at libsdl.org
Subject: [SDL] Tile engine code

Hey,

I’m trying to write a tile engine… but i’m having lots of trouble (it
works
fine when I do sequential reads of my map, but random-access reads (which
are
the thing that matter it seems like?) don’t seem to work at all… If
someone
could point me towards some source code i could look at… that would help a
lot. (I would have posted my code to see if someone could help me, but it
doesn’t have any SDL yet (it’s all ASCII so far), and i was trying to keep
this semi-on-topic.)

Thank you very much
Cameron Matheson


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


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I’m going to be working on a tile engine soon (2D) and am just
currently working on the parser. Got that working well last night. Next
step is to get it tiling (Using SDL of course).

Here are my plans with tons of holes in it, because I have never done
this before, and don’t have anything set in stone yet.

I just plan on creating a class called TILE,

Problem 1: Be careful not to put frequently used virtual members it that
class. Too much of that on the once-per-tile level can quickly impact
total performance.

(BTW, you should probably name it tile_t, CTile or something as "TILE"
looks like a #define rather than a type… Either way, be concistent
throughout your code! It helps. :slight_smile:

and while I’m reading the
file, I will just call a constructor which defines the tile. Then I
will add it to my linked list.

Problem 2: A linked list is probably fine for a lot of tasks, but for
real time rendering, you’ll probably need some secondary structure to
look tiles up. (Unless you’re using a small map and/or huge tiles, you’ll
have to extract and render only the tiles currently visible - just
plowing throught the whole map won’t work.)

For simple, single layer maps, a 2D array is usually the easiest and best
way to go. For maps with multiple layers, mixed size tiles, detail
sprites etc, you can either extend the basic 2D array approach into a
zone based map (hook a linked list of tiles and sprites to each "tile"
location on the map), or you can switch to some multidimensional tree
structure.

First line in my file format is the name of the tileset file I’m using.

tileset.bmp

And the rest of the file is where I load tiles, events, etc…

So it’s actually not just a plain map file, but a complete “level
definition”…?

So inside my file I define a tile like so:
tile:
0
0
32
32

Format is not set in stone, but the 1st and 2nd numbers are the x,y
locations on the screen 3rd and 4th are the x,y locations in the
tileset.bmp file.

IMHO - as there doesn’t seem to be a way of specifying tile size - it’s
much more convenient to address tiles using tile indices rather than
explicit tile coordinates. (I usually hack up some code that
automatically chops up an input image into a linear array of tiles. See
Kobo Deluxe for an example.)

As to the “screen coordinates” (I’d rather think about them as “map
coordinates”), are they in pixels or tiles? (If they’re in pixels, I can
see the point in your format. If not, I think a simple, traditional array
of tile indices would be sufficient.)

[…]

This should all be taking place inside the WORLD class which has
functions like LoadMap();
And I will have a linked list containing all the tiles so I can do
various stuff to it, like remove it, check values, or something of the
such.

Then I can have a draw function inside the tile class, things are
endless after that. Of course I have never done this before, but this
is my idea as of late.

Sounds like a reasonable design idea. The only parts I’m in doubt about
are the internal structure of the loaded map data, and the map file
format. Your design would work, but you’ll have to add some info to
avoid performance issues with large maps. I don’t quite get why you want
"tile palette" coordinates in the map data - but it would work all
right. (In fact, XKobo/Kobo Deluxe used to address tiles and sprites like
that, until I integrated it with the tile/sprite loader from Project
Spitfire.)

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Tuesday 29 January 2002 02:42, Bryan Arant wrote:

David Olofson

Problem 1: Be careful not to put frequently used virtual members it that
class. Too much of that on the once-per-tile level can quickly impact
total performance.

I won’t have to use any virtual members in my tile class.

(BTW, you should probably name it tile_t, CTile or something as "TILE"
looks like a #define rather than a type… Either way, be concistent
throughout your code! It helps. :slight_smile:

Noted!

Problem 2: A linked list is probably fine for a lot of tasks, but for
real time rendering, you’ll probably need some secondary structure to
look tiles up. (Unless you’re using a small map and/or huge tiles, you’ll
have to extract and render only the tiles currently visible - just
plowing throught the whole map won’t work.)

Hm, I was going to go through the list and check to see which ones are
currently
visible on the screen, and only draw those. That does sound a little
inefficient
though. What would be the best way to do this?

For simple, single layer maps, a 2D array is usually the easiest and best
way to go. For maps with multiple layers, mixed size tiles, detail
sprites etc, you can either extend the basic 2D array approach into a
zone based map (hook a linked list of tiles and sprites to each "tile"
location on the map), or you can switch to some multidimensional tree
structure.

Would a Multimap be efficient?

So it’s actually not just a plain map file, but a complete “level
definition”…?

Why yes… yes it is. I want to use my tiling engine for more then one thing
:slight_smile:

IMHO - as there doesn’t seem to be a way of specifying tile size - it’s
much more convenient to address tiles using tile indices rather than
explicit tile coordinates. (I usually hack up some code that
automatically chops up an input image into a linear array of tiles. See
Kobo Deluxe for an example.)

The tile size would be defined in one of the first few lines at the top
of the file. I thought of this shortly after writing this letter.

As to the “screen coordinates” (I’d rather think about them as “map
coordinates”), are they in pixels or tiles? (If they’re in pixels, I can
see the point in your format. If not, I think a simple, traditional array
of tile indices would be sufficient.)

“Screen coordinates” was just a small typo. I did mean "map coordinates"
Sorry about that.

Sounds like a reasonable design idea. The only parts I’m in doubt about
are the internal structure of the loaded map data, and the map file
format. Your design would work, but you’ll have to add some info to
avoid performance issues with large maps. I don’t quite get why you want
"tile palette" coordinates in the map data - but it would work all
right. (In fact, XKobo/Kobo Deluxe used to address tiles and sprites like
that, until I integrated it with the tile/sprite loader from Project
Spitfire.)

Thank you. After I finish the whole tile thing, I would be more then happy
to
announce it to this group.

Thanks a BUNCH David for reading through all my jarble, and responding.

//David Olofson — Programmer, Reologica Instruments AB

-Bryan Arant


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

----- Original Message -----
From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of
Sent: Wednesday, January 30, 2002 1:21 PM
To: sdl at libsdl.org
Subject: Re: [SDL] Tile engine code

[…]

Problem 2: A linked list is probably fine for a lot of tasks, but for
real time rendering, you’ll probably need some secondary structure to
look tiles up. (Unless you’re using a small map and/or huge tiles,
you’ll have to extract and render only the tiles currently visible -
just plowing throught the whole map won’t work.)

Hm, I was going to go through the list and check to see which ones are
currently
visible on the screen, and only draw those. That does sound a little
inefficient
though. What would be the best way to do this?

Simple: The way that gives the correct result with the smallest amount of
work. :wink:

It depends on your game, but in general, 2D array maps with fixed tiles,
“snapped” to the grip is the fastest and easiest way to go, by far. Just
loop over the rectangular area of the map where the screen is, and you’re
done. No active searching, filtering or anything.

You can have a look at the Scrolling Examples for a basic idea of how to
do it:

http://olofson.net/mixed.html

For simple, single layer maps, a 2D array is usually the easiest and
best way to go. For maps with multiple layers, mixed size tiles,
detail sprites etc, you can either extend the basic 2D array approach
into a zone based map (hook a linked list of tiles and sprites to
each “tile” location on the map), or you can switch to some
multidimensional tree structure.

Would a Multimap be efficient?

Multimap? (I'm tired - long night of DOS->Linux porting...)

So it’s actually not just a plain map file, but a complete “level
definition”…?

Why yes… yes it is. I want to use my tiling engine for more then one
thing

:slight_smile:

Well, just keep in mind that flexibility often comes at the expense of
performance. However, proper design is ofter enough to get a good
trade-off, especially with the control/data ratio we have these days.
(Tons of graphics and audio being pumped around, while even quite complex
higher level code ends up consuming a neglibigle share of the CPU time.)

IMHO - as there doesn’t seem to be a way of specifying tile size -
it’s much more convenient to address tiles using tile indices rather
than explicit tile coordinates. (I usually hack up some code that
automatically chops up an input image into a linear array of tiles.
See Kobo Deluxe for an example.)

The tile size would be defined in one of the first few lines at the top
of the file. I thought of this shortly after writing this letter.

Are you going to support multiple tile sizes in a single map?

[…]

Sounds like a reasonable design idea. The only parts I’m in doubt
about are the internal structure of the loaded map data, and the map
file format. Your design would work, but you’ll have to add some
info to avoid performance issues with large maps. I don’t quite get
why you want “tile palette” coordinates in the map data - but it
would work all right. (In fact, XKobo/Kobo Deluxe used to address
tiles and sprites like that, until I integrated it with the
tile/sprite loader from Project Spitfire.)

Thank you. After I finish the whole tile thing, I would be more then
happy to
announce it to this group.

Thanks a BUNCH David for reading through all my jarble, and responding.

No problem! (Can’t stay away from this kind of stuff. That’s how the
Scrolling Examples got written… :slight_smile:

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 31 January 2002 05:59, Bryan Arant wrote:

I won’t have to use any virtual members in my tile class.

That’s good, but be careful if the tile class gets little ones - non-virtual
destructors may behave badly if they’re doing something important, in some
cases.

[…]

Hm, I was going to go through the list and check to see which ones are
currently
visible on the screen, and only draw those. That does sound a little
inefficient
though. What would be the best way to do this?

Depends =) See D.O.'s answer =)

[…]

So it’s actually not just a plain map file, but a complete “level
definition”…?

Why yes… yes it is. I want to use my tiling engine for more then one
thing

:slight_smile:

I know the feeling - when i first started doing that little interface library
of mine, SIF, i wanted it to be able to do everything. After a couple of
rewrites and some good advice from (and rambling to =) David Olofson, i
realized that designing something to fit everything is generally a bad idea,
unless you’re very, very careful. It’s like designing an engine to fit cars,
boats, airplanes, space shuttles, time machines, and multidimensional
improbability/bistro-drive spaceships equally well =)

You’ll probably get the best results for designing your tile engine for what
you need it for right_now (grass mower), and leave room for enhancements
(interdimensional space-time controllers). After i started thinking this way,
i’ve done much more progress on SIF (it’s still far from done, of course, but
i’m feeling that i’m getting somewhere). Whenever i get an idea that could be
implemented into SIF (which i do all the time), i don’t actually implement it
right there and then. In stead, i put it into the back of my head, and keep
it in mind while writing the API.

For example, in SIF, i need to calculate a “hitmap” of sorts of a surface.
The hitmap is used for detecting collisions, etc. Right now, i’m planning on
using it with SDL surfaces (whoa, we’re getting on topic!), but later on i
might want to use it with other things, like fx. OpenGL textures. That will
be no problem at all, because the only time i need to access the SDL surface
is when actually calculating the hitmap - the hitmap calculator isn’t a true
part of SIF, it’s just a tool to calculate the hitmap, which is a true part
of SIF. The hitmap can be calculated in other ways, SIF doesn’t care, as long
as the hitmap is there. I’m not that good explaining, but hopefully you get
the idea =)

[…]

The tile size would be defined in one of the first few lines at the top
of the file. I thought of this shortly after writing this letter.

Different sized tiles in the same tilemap ? Be sure that you actually need
that before you go on implementing it - there’s more than one way to do
whatever it is you want to do, and the most advanced way is almost never the
best. Really =)–
Trick


Linux User #229006 * http://counter.li.org

“There is no magic” - Nakor, magic user

Simple: The way that gives the correct result with the smallest amount of
work. :wink:

:slight_smile:

It depends on your game, but in general, 2D array maps with fixed tiles,
“snapped” to the grip is the fastest and easiest way to go, by far. Just
loop over the rectangular area of the map where the screen is, and you’re
done. No active searching, filtering or anything.

You can have a look at the Scrolling Examples for a basic idea of how to
do it:

http://olofson.net/mixed.html

Neato! >:)

Multimap? (I’m tired - long night of DOS->Linux porting…)

A multimap is a STL thingamajig. (Warning: Techy Term)

Are you going to support multiple tile sizes in a single map?

Nope. I thought of this already and thought, “No need”.

No problem! (Can’t stay away from this kind of stuff. That’s how the
Scrolling Examples got written… :slight_smile:

Hah.

-Bryan Arant

[I don’t have anything to brag about :confused: ]-----------------------------------------
[Wait. “I have a kitty”]
Mreow.

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -’


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

[…]

Multimap? (I’m tired - long night of DOS->Linux porting…)

A multimap is a STL thingamajig. (Warning: Techy Term)

Oh, sorry! :slight_smile:

Well, I think you’ll need something with at least 2D “indexing” to be
efficient.

For example, if we look at the implementation on a binary tree, it’s
quite obvious how you’d go about looking up a specific node: Just start
at the root node and chose the higher or lower branch depending on your
value vs the node value, looping until you find your key. (Or the closest
match, if applicable.)

Now, try looking for a node with two values in a binary tree… In some
cases you can map a 2D space to 1D, but I don’t think it’ll work out
well in the case of looking up tiles in a rectangular world with an even
distribution of tiles.

However, it’s easy to combine the two node/target value comparisons so
that you get 5 alternatives, including the “both equal” stop condition.
Build a quadtree, and pick one of the four branches, or stop, depending
on the outcome of the two tests.

Anyway, map[y][x] is pretty much as fast as it gets! :wink:

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Friday 01 February 2002 02:50, Bryan Arant wrote:

“David Olofson” <david.olofson at reologica.se> wrote in message
news:mailman.1012532170.10837.sdl at libsdl.org

Anyway, map[y][x] is pretty much as fast as it gets! :wink:

Assuming that map is a plain array and not an object with an
overloaded ‘operator[]’ (such as ‘std::map’).–
Rainer Deyke | root at rainerdeyke.com | http://rainerdeyke.com

Yes, of course - I thought that was obvious, considering the context. :slight_smile:
(Operator overloading is just a “syntax trick”, and I was talking about
algorithms.)

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Friday 01 February 2002 04:52, Rainer Deyke wrote:

“David Olofson” <david.olofson at reologica.se> wrote in message
news:mailman.1012532170.10837.sdl at libsdl.org

Anyway, map[y][x] is pretty much as fast as it gets! :wink:

Assuming that map is a plain array and not an object with an
overloaded ‘operator[]’ (such as ‘std::map’).

Anyway, map[y][x] is pretty much as fast as it gets! :wink:

You just have no idea what kind of knowledge you just sparked inside me
head.
Hah. I mean it to. I finally get why people chose an array like:

map[400][400]

over

map[800]

Duh :slight_smile:

-Bryan Arant

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -’_______________________________________________
SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl