Dynamic array of TBitmap
I am attempting to create a dynamic array of TBitmap in C++ Builder 4. I now how to create a single bitmap:
Graphics::TBitmap *Bitmap = new Graphics::TBitmap();
and an array of bitmaps:
Graphics::TBitmap *Bitmap[2];
Bitmap[0] = new Graphics::TBitmap();
Bitmap[1] = new Graphics::TBitmap();
But I can not for the life of me figure out how to create a dynamic array of bitmaps at run time. If anyone has the answer to this, I would greatly appreciate it.
std::vector<Graphics::TBitmap *> bitmaps;bitmaps.push_back( new Graphics::TBitmap() );bitmaps.push_back( new Graphics::TBitmap() );// orGraphics::TBitmap bitmaps[2];bitmaps[0] = new Graphics::TBitmap();bitmaps[1] = new Graphics::TBitmap();// orGraphics::TBitmap *bitmaps= new Bitmap[2];bitmaps[0] = new Graphics::TBitmap();bitmaps[1] = new Graphics::TBitmap();...delete[] bitmaps;
I thank you for your assistance, but none of those three ideas help. I will include some of my code and then maybe my question will be better understood:
class Bitmaps
{
protected:
int MaxBitmaps;
public:
Bitmaps() {TotalBitmaps = 0; MaxBitmaps = 999; }
~Bitmaps() { if (Bitmap) delete [] Bitmap; }
int TotalBitmaps;
AnsiString BitmapDir, BaseFileName;
// a static array of 999 Bitmap pointers: needs to be dynamic
Graphics::TBitmap *Bitmap[999];
int Init(int totalBitmaps)
{
TotalBitmaps = totalBitmaps;
if (TotalBitmaps > MaxBitmaps)
{
TotalBitmaps = MaxBitmaps;
int error = 1; return error;
}
for (int i = 0; i < TotalBitmaps; i++)
Bitmap = new Graphics::TBitmap();
return 0;
}
void Destroy() {if (Bitmap) delete [] Bitmap; }
int GetMaxBitmaps() {return MaxBitmaps; }
int LoadTextures(AnsiString bitmapDir, AnsiString baseFileName);
int RotateAllBitmaps180();
int RotateBitmap180(int bitmapIndex);
};
The above code creates a Bitmaps class with a static array of 999 bitmap pointers. Only the Bitmap pointers up to the TotalBitmaps get initialized when the Init(totalBitmaps) function is called. This works great, unless a larger number than 999 Bitmaps want to be loaded. I am trying to modify this so that the static array of 999 pointers becomes dynamic. When the Init(totalBitmaps) is called, the total number of Bitmap pointers gets set to the TotalBitmaps. I hope this makes sense. Any help is appreciated.
class Bitmaps
{
protected:
int MaxBitmaps;
public:
Bitmaps() {TotalBitmaps = 0; MaxBitmaps = 999; }
~Bitmaps() { if (Bitmap) delete [] Bitmap; }
int TotalBitmaps;
AnsiString BitmapDir, BaseFileName;
// a static array of 999 Bitmap pointers: needs to be dynamic
Graphics::TBitmap *Bitmap[999];
int Init(int totalBitmaps)
{
TotalBitmaps = totalBitmaps;
if (TotalBitmaps > MaxBitmaps)
{
TotalBitmaps = MaxBitmaps;
int error = 1; return error;
}
for (int i = 0; i < TotalBitmaps; i++)
Bitmap = new Graphics::TBitmap();
return 0;
}
void Destroy() {if (Bitmap) delete [] Bitmap; }
int GetMaxBitmaps() {return MaxBitmaps; }
int LoadTextures(AnsiString bitmapDir, AnsiString baseFileName);
int RotateAllBitmaps180();
int RotateBitmap180(int bitmapIndex);
};
The above code creates a Bitmaps class with a static array of 999 bitmap pointers. Only the Bitmap pointers up to the TotalBitmaps get initialized when the Init(totalBitmaps) function is called. This works great, unless a larger number than 999 Bitmaps want to be loaded. I am trying to modify this so that the static array of 999 pointers becomes dynamic. When the Init(totalBitmaps) is called, the total number of Bitmap pointers gets set to the TotalBitmaps. I hope this makes sense. Any help is appreciated.
I suggest you lose the pointers unless you need them. I'd also suggest boost smart pointers but those may be a bit advanced for you.
Go here, I highly recommend the first link. It will save you a lot of time if you learn that first, before you do more coding.
// create vectorstd::vector<Graphics::TBitmap> bitmaps;// -OPTIONAL- you can also reserve space in the vector to improve performance by avoiding extra memory allocationbitmaps.reserve(2);// create bitmapsGraphics::TBitmap b1,b2;// add to vectorbitmaps.push_back(b1);bitmaps.push_back(b2);
Go here, I highly recommend the first link. It will save you a lot of time if you learn that first, before you do more coding.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement