Advertisement

I need HELP with my tilemap!

Started by May 16, 2001 04:12 AM
3 comments, last by Soehnlein_Brillant 23 years, 8 months ago
I'm working on a tilemap in C++ and my classes are like this:
  
class Tile
{
private:
	int w,h; // size of tile

	BITMAP *bitmap; // bitmap source

...
public:
	Tile(int _w, int _h);
...
};

class Layer
{
private:
	int w,h;// size of layer

	int cam_x,cam_y; // camera position 

	int scroll_x, scroll_y;
	Tile *tile_data;  // tiledata


public:
// constructor: _w,_h = size of layer; _gw,_gh = size of tiles

	Layer(int _w, int _h, int _gw, int _gh); 
...
};

//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<w*h;i++)
     tile_data[i]= new Tile(_gw, _gh); // that doesn't work

}    

//can anybody tell me the right way to do it?

  
Edited by - Soehnlein_Brillant on May 17, 2001 7:33:27 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>
Advertisement
please forgive me my mistakes!
I''m not used with this forum
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
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
;-)...


This topic is closed to new replies.

Advertisement