#### Archived

This topic is now archived and is closed to further replies.

This topic is 5456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok I think I got this right, but i'm not very experienced allocating memory in this fasion... basically i have the map int** map[16]; This is just for testing purposes, its not my real map tile data structure. The 16 represents the floor of the map. To make it easier, Each floor is only 8x8, and each 8x8 tile can hold 4 objects on it. My question is, is allocation for this correct how i've done it? I realise if these are static values it's easier like this, but these are not static values in the actual situation to put it simply... have I allocated the memory correctly?
	int** map[16];
int k, level;

for(k=0; k<16; k++){
// allocating pointers to the 8x8 squares on the 16 floors
map[k] = new int*[8*8];
}

for(level=0; level<16; level++){
for(k=0; k<8*8; k++){
// allocating room for 4 objects on each 8x8 tile on all 16 floors
*(map[level]+k) = new int[4];
}
}

[edited by - drekkar on December 13, 2003 11:32:00 PM]

##### Share on other sites
If you want an 8 by 8 map, you should have 64 elements, instead of 16.

##### Share on other sites
map[k] = new int*[8*8];

16 represents the floor (height) of the map (the map size is not static, that is why it is allocated, its loaded off file). The int*[8*8] is 8x8 map, and they are pointers so I can allocate room for the 4 objects on the tile (tile layers, or something). The object number is not static either, that is why the tiles on each floor are pointers.

[edited by - drekkar on December 13, 2003 11:35:17 PM]

##### Share on other sites
quote:
Original post by drekkar
map[k] = new int*[8*8];

16 represents the floor (height) of the map (the map size is not static, that is why it is allocated, its loaded off file). The int*[8*8] is 8x8 map, and they are pointers so I can allocate room for the 4 objects on the tile (tile layers, or something). The object number is not static either, that is why the tiles on each floor are pointers.

[edited by - drekkar on December 13, 2003 11:35:17 PM]

Well, what you are doing seems fine, but I am not sure that this:

*(map[level]+k) = new int[4];

will work, considering an int is 4 byts and k is going up 1 at a time (1 byte). But since map is an int pointer, it should work, because the compiler takes care of all of that.

[edited by - PlayGGY on December 13, 2003 11:54:30 PM]

##### Share on other sites
Why not use structures to make it easier to read and write?

struct Tile_S
{
int *Object_IDs; //List of objects
};

struct Floor_S
{
Tiles_S *Tiles; //Stores an array of tiles...
};

struct Map_S
{
Floor_S *Floors; //Stores an array of floors
};

So, now when you allocate it, it''s MUCH simpler:

Map_S MainMap;MainMap.Floors = new Floor_S[16]; //16 floors total..int k, level;for (k=0;k<16;++k){ MainMap.Floors[k].Tiles = new Tile_S[8*8]; //8x8 squares for (level=0;level<8*8;++level) {  MainMap.Floors[k].Tiles[level].Object_IDs = new int[4]; //allocate 4 ints for each object_id. }}

Now, you can store things inside these classes as well, like say, you want to store the # of objects so you only have to go through the number that are there and not all 4 each time, etc.

##### Share on other sites
thanks for the confirmation
i threw a quick printf("%x\n", map[0]+i) into the loop and they were 4 bytes apart time to implement this into the game now x_x

i'll take that renaming into consideration also when adding it to the game thnx guys

edit:
just started on it, I must say it is so much easier with renamed stuff, thanks ^^

[edited by - drekkar on December 13, 2003 12:15:12 AM]

##### Share on other sites
quote:
Original post by drekkar
edit:
just started on it, I must say it is so much easier with renamed stuff, thanks ^^

Assuming that was directed towards me... no problem .

##### Share on other sites
heh definately... I just finished, took me nearly an hour with that easy way (up until now, it only supported 1 floor (128x128) with 1 object, and 1 tile on it). Plus it''s so much easier to handle how stuff works now. So now since my server supports multi level maps, I have to design and test some, so that''s my handful of work tommorow. Thank god I was saved from 3 hours of messy stuff I love this site and the community

1. 1
Rutin
64
2. 2
3. 3
4. 4
5. 5

• 16
• 10
• 29
• 20
• 9
• ### Forum Statistics

• Total Topics
633414
• Total Posts
3011764
• ### Who's Online (See full list)

There are no registered users currently online

×