Sign in to follow this  

Dynamic array of TBitmap

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
std::vector<Graphics::TBitmap *> bitmaps;
bitmaps.push_back( new Graphics::TBitmap() );
bitmaps.push_back( new Graphics::TBitmap() );

// or

Graphics::TBitmap bitmaps[2];
bitmaps[0] = new Graphics::TBitmap();
bitmaps[1] = new Graphics::TBitmap();

// or

Graphics::TBitmap *bitmaps= new Bitmap[2];
bitmaps[0] = new Graphics::TBitmap();
bitmaps[1] = new Graphics::TBitmap();
...
delete[] bitmaps;

Share this post


Link to post
Share on other sites
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[i] = 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.

Share this post


Link to post
Share on other sites
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.

// create vector
std::vector<Graphics::TBitmap> bitmaps;

// -OPTIONAL- you can also reserve space in the vector to improve performance by avoiding extra memory allocation
bitmaps.reserve(2);

// create bitmaps
Graphics::TBitmap b1,b2;
// add to vector
bitmaps.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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this