how to represent a 2d world.

0abbcc8bc5719dac608a68733a03c40e
0
hidayat 101 Oct 06, 2010 at 11:26

Im creating my first 2d game. To represent the world/level i create a 2d array of ints where for example 0 is nothing, 1 is rock, and 2 is water etc.

If the level is large the 2d array will become to big to be in the memory at the same time. So I guess I have to stream the array from the disk somehow.

Is this the normal way to work in a 2d game, save the 2d array on disk and stream it to the game at run time? I have search Google for examples or tutorials but I could not find any good information on this.

2 Replies

Please log in or register to post a reply.

8676d29610e6c98d6dd2d9c38528cd9c
0
alphadog 101 Oct 06, 2010 at 12:32

I’m going to assume that your array is a map to image tiles, IOW that you are doing a tiled 2D map.

First, an array of ints has to be pretty big to start challenging the memory space of the average computer. A large array will really only be an issue once it goes beyond the bounds of available memory, since the OS will start paging to disk sections of it. Random access over such a map will likely be undesirable.

However, there are techniques to manage tiled 2D maps. For example, a 9x9 area of grass doesn’t have to use 9 ints, but rather some encoding to say “place 9 grass tiles here”. More complicated is the use of quadtrees. Or, predictive streaming from storage.

Anyways, you are skirting the edge of “premature optimization” here. I’d say just code a big array for your tile engine and, if needed, introduce techniques to make any performance issue go away.

PS: Don’t use ints for storing values with limited range (ex: using an int for six types of “terrain”). Use a data type that requires less storage, such as a short or byte.

PPS: Many games use multiple tile maps overlayed on each other. Sometimes it’s multiple arrays, sometimes it’s a complex datastructure at each point. The end effect is the same, memory-wise: more data. So, heads up, staying simple now may not work later.

0abbcc8bc5719dac608a68733a03c40e
0
hidayat 101 Oct 07, 2010 at 12:26

@alphadog

I’m going to assume that your array is a map to image tiles, IOW that you are doing a tiled 2D map.

First, an array of ints has to be pretty big to start challenging the memory space of the average computer. A large array will really only be an issue once it goes beyond the bounds of available memory, since the OS will start paging to disk sections of it. Random access over such a map will likely be undesirable.

However, there are techniques to manage tiled 2D maps. For example, a 9x9 area of grass doesn’t have to use 9 ints, but rather some encoding to say “place 9 grass tiles here”. More complicated is the use of quadtrees. Or, predictive streaming from storage.

Anyways, you are skirting the edge of “premature optimization” here. I’d say just code a big array for your tile engine and, if needed, introduce techniques to make any performance issue go away.

PS: Don’t use ints for storing values with limited range (ex: using an int for six types of “terrain”). Use a data type that requires less storage, such as a short or byte.

PPS: Many games use multiple tile maps overlayed on each other. Sometimes it’s multiple arrays, sometimes it’s a complex datastructure at each point. The end effect is the same, memory-wise: more data. So, heads up, staying simple now may not work later.

thanks