# 2D tilemap from 1d array blowing my mind!

• 08-11-2011, 06:49 PM
Ruuhkis
2D tilemap from 1d array blowing my mind!
Hello folks!
I've been facing problem with tile map for a while, for some reason i'd like to store all the tile map data on one file containing all the tile info in format as given in example

Code:

```0011 1100 1010 0101```
and then store all that in 1D array only knowing the regions width at the first, in this example it would be 2 so we could get 4 regions

Code:

```00 11```
,
Code:

```11 00```
,
Code:

```10 01```
and
Code:

```10 01```
okay, i have no problem loading the 1d array into memory but when it comes to divide that 1d array in pieces of map called regions I can't figure out the best way nor any other way as long as i don't hardcode it.. :/

okay, lets see whats my ideas for this!

theres amount of tiles in each region stored into memory (this example case 2) then i have method that returns 2d int array containing all the tiles taking regionX and regionY ints as params

the program starts figuring out the starting place(x and y) from the 1d array to the 2d array like this

int startPos = regionX*regionWidth*regionY

okay, we now know the start position is 0 if regionX is 0 and regionWidth is 0 and regionY, the first problem here is that i want it work just smooth even if regionY was 0 and X 1 but when it *'s the number with 0 it becomes zero, i've tried something like
(regionX>0?regionX:1) but it will just mess all up i know! thats the first problem i want solved!
but even if the both regions were like 1 and 1 the starting position would be
1*2*1 assuming that regions width is 2, the answer would be 2
below i marked it with x

Code:

```00x1 1100 1010 0101```
okay, we know now where to start from, looking good! now we only need to know where the looping should end at.. should be easy, but cant figure this out, i thought adding a square would help me out but it would only be like this..
Code:

```00x1 1o00 1010 0101```
how can this be so hard? or is this the worst way to handle tilemap in regions human can think of? i just want efficient way that stores the tiles in regions from 1d array thats generated by map editor in same form, or do i really have to generate new file for each region? :S

please help me out with this, i can't really find anything on internet about tilemaps as regions or anything :(
• 08-11-2011, 07:55 PM
Norm
Perhaps the examples would be clearer if you used unique letters for each element in the map. Then you would be able to see where each element ended up when the original map was split into parts.

What is a tilemap?