Skip to main content
gamedev.net
🔒 Locked

Multiple Layers

Started by whiten Feb 13, 2003 at 9:45 PM 5 replies 450+ views
Original Post
whiten
whiten
I am in the process of creating a small game in Java (tile based), but at the moment I am only using a single layer, which is for the tile map. The tile map is simply a 2d array that holds information about each tile, i.e walkable / non - walkable etc. All other sprites are simply draw on top of the map Anyway, how do a code for multiple layers in a game so that I can have a background and foreground. If possible could someone point me in the direction of tutorial or a basic code snippet to get me started. It does not matter if the tutorials include examples in C++. Cheers in advance
wizard341
wizard341
Well i dont know of any tutorials but the theory is really simple. All you need is to define an array for each layer that you want. Draw them all back to front (background gets drawn first, then the next layer, all the way up to the top). Also you need to have an array for the "actual" board, one that isnt concerned with layers but rather it is concered with the screen itself. Its sole purpose would be to tell which array tile is "active" at the current state.

For example, you want 3 layers, a background a forground and maybe a roof layer (for houses and such). So define 3 arrays for each layer, draw them, and use an array to keep track of which tile is on top of the other tiles, so when you move into another tile you check the array and it points you to which layer to look in for information.

I hope this answers the question that your asking. Also keep in mind that im assuming that this is coded in 2D.

come visit me at www.worldofwc.org!
whiten
whiten
Thanks,

Will give this a whirl.
Taco2002
Taco2002
Yeah thats the way I did it too, except I used a multi-dimensional array...eg

int map_array [128][128][3]

that will create basically and array with three layers each 128*128. Use can use greater or lesser values its up to yourself, one for your base layer, one for your object layer, and one for a fringe layer.
ShadowChaser
ShadowChaser
For the love of *god* don''t use 3d arrays!

Arrays are a REALLY bad idea for "height" (Z) in an iso/tile game because for the most part, anything above 0 is going to be empty space. If you want a really high sky, that means you''re wasting tons and tons of memory for nothing!

Since your game will always have ground, it''s safe to use a 2D array for your ground terrain.

For any objects that are above the ground (entities) - players, walls, items, clouds, etc, use a stl vector (a list, basically) of items.

Give each item a 3D coordinate (X, Y, and Z) that defines its position. That way you''re only using memory for the items that exist in the game, not empty space which your game will never process (in fact, it will technically slow it down since you need to loop through all the empty space)

To render your items, its very simple - just make sure the list is always sorted! As long as the list is sorted in the same order you will render, then all you need to do is:

for (int i = 0; i < entitylist.size(); i++)
entitylist.at(i).render();

voila! :-)

For more information, look up STL vectors (stl::vector I believe...). Make every entity a class something like this:

class Entity
{
public:
int X;
int Y;
int Z;

void Render();

// Anything else you want, like hit points, etc
}

if you want to get fancy you can inherit the Entity class for more specialized types

like..

class Entity_Monster: public Entity
{
// Monster variables and AI code
}

class Entity_Item: public Entity
{
// Item code, like quality, price, etc
}
Cornutopia
Cornutopia
I disagree. Most game maps are small. Most computers are big on memory. ''Wasting'' 100k of memory is better than making your code complicated and slower. If you want speed over memory, a multi dimensional array (using a 1D array and 3D macro is best) is the best option I think.

Mark
Xiol
Xiol
A 3d array is only the best option if you are using most of a layer. Otherwise it''s will probably be much slower to work with. I agree that memory is cheap, but when you draw, checking 100 * 100 extra tiles (to see if you draw them or not) will be more time consuming than going through a list of 20-30 entities.

Also it will make your file size much larger. (Ok, even the simplest compression will remove that problem).

The list will also make you maps much more flexible, allowing you to place entities anywhere within a layer, not limited to just tile positions. A normal tile map is only advantagous when you use most of the tiles.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.