|
I need HELP with my tilemap!
I'm working on a tilemap in C++
and my classes are like this:
Edited by - Soehnlein_Brillant on May 17, 2001 7:33:27 AM
May 16, 2001 04:29 AM
sorry there is a mistake:
...
And now my problem:
In the constructor of Layer i want to initialize the Tile data
Layer::Layer(int _w, int _h, int _gw, int _gh):
w(_w),
h(_h),
...
{
for(int i=0;i // it is tile_data!!
tile_data= new Tile(_gw, _gh); // that doesn''t work<br>} <br><br><br> </i>
...
And now my problem:
In the constructor of Layer i want to initialize the Tile data
Layer::Layer(int _w, int _h, int _gw, int _gh):
w(_w),
h(_h),
...
{
for(int i=0;i
tile_data= new Tile(_gw, _gh); // that doesn''t work<br>} <br><br><br> </i>
May 16, 2001 01:17 PM
Since you don''t have a preallocated table of pointers to which you''d assign each tile too, you''ll first have to create a pointer array for those tiles. The tile_data should be a pointer to a pointer of a tile_array which you dynamically allocate;
Tile** tile_data;
Since
// condtuctor: _w,_h = size of layer; _gw,_gh = size of tiles
Layer(int _w, int _h, int _gw, int _gh)
{
tile_data = NULL;
//be sure to check for invalid _gw and _gh etc..
int num_tiles_width = _w/_gw;
int num_tiles_height = _h/_gh;
int num_tiles_total = num_tiles_width*num_tiles_height;
tile_data = new Tile*[total];
if (tile_data == NULL)
{
//failure condition, could not allocate array of that size etc..
//abort constructor
}
//you will want to store away the bounds of the array the
//num_tiles_width and num_tile_height to make sure you dont
//go out of bounds
//Now you can create the tiles to fill the pointer array
for (int x= 0; x< num_tiles_width; x++)
{
for (int y= 0; y< num_tiles_height; y++)
{
tile_data[x][y] = new Tile(x,y);
}
}
//that should do it. To destroy this object you will have to
//deallocate all the tiles then the array by :
//delete tile; // for each tile
//delete [] tile_date; // for the tile pointer array
}
Hope this is correct 8^) Good Luck
-ddn
Tile** tile_data;
Since
// condtuctor: _w,_h = size of layer; _gw,_gh = size of tiles
Layer(int _w, int _h, int _gw, int _gh)
{
tile_data = NULL;
//be sure to check for invalid _gw and _gh etc..
int num_tiles_width = _w/_gw;
int num_tiles_height = _h/_gh;
int num_tiles_total = num_tiles_width*num_tiles_height;
tile_data = new Tile*[total];
if (tile_data == NULL)
{
//failure condition, could not allocate array of that size etc..
//abort constructor
}
//you will want to store away the bounds of the array the
//num_tiles_width and num_tile_height to make sure you dont
//go out of bounds
//Now you can create the tiles to fill the pointer array
for (int x= 0; x< num_tiles_width; x++)
{
for (int y= 0; y< num_tiles_height; y++)
{
tile_data[x][y] = new Tile(x,y);
}
}
//that should do it. To destroy this object you will have to
//deallocate all the tiles then the array by :
//delete tile; // for each tile
//delete [] tile_date; // for the tile pointer array
}
Hope this is correct 8^) Good Luck
-ddn
Yes, finally I got it working!!
Thank you very much for your help
Bud i had to use 3 pointers for tile_data:
Tile ***tile_data;
//later
...
tile_data[x][y]=new Tile(w,h);
// tile_data has to be a pointer
// but I don''t know why!
Anyway, you gave me tha right idea how to do it.
so thanks a lot, now I can concentrate on the really important things...whatever they will be...
I think, no I know i will soon post a new topic about tilemaps
;-)...
Thank you very much for your help
Bud i had to use 3 pointers for tile_data:
Tile ***tile_data;
//later
...
tile_data[x][y]=new Tile(w,h);
// tile_data has to be a pointer
// but I don''t know why!
Anyway, you gave me tha right idea how to do it.
so thanks a lot, now I can concentrate on the really important things...whatever they will be...
I think, no I know i will soon post a new topic about tilemaps
;-)...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement