Jump to content
  • Advertisement
Sign in to follow this  
darenking

SOLVED-Allegro: probs with vector of BITMAPs

This topic is 4796 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

Howdy. I'm storing my sprite BITMAPs in a vector of pointers like this:
BITMAP* newSprite = new BITMAP;
m_Sprites.push_back( newSprite );
newSprite = create_bitmap(width, height);


I've stored other objects in vectors and had no problems accessing their methods. But I'm having trouble getting these sprite BITMAPs to draw to the screen. I've tried drawing the first sprite like this: draw_sprite(buffer, m_Sprites[0], x, y); But the program just crashes horridly. Something to do with dereferencing mebbe? [Edited by - darenking on July 21, 2005 3:42:35 PM]

Share this post


Link to post
Share on other sites
Advertisement
Nope, nothing to do with dereferencing. You cannot create a BITMAP* by allocating memory with new, which just allocates memory leaving all data in undefined state (for example what is the width of the BITMAP?). You must use either create_bitmap(int width, int height) or load_bitmap(const char *filename, PALETTE *pal), and destroy_bitmap(BITMAP *bmp) when you no longer need it. More info in the manual.

Share this post


Link to post
Share on other sites
Sorry, I just left out some of the details in the example. I'm certainly creating the BITMAPs correctly. The full code is more like this:


BITMAP* newSprite = new BITMAP;
m_Sprites.push_back( newSprite );
newSprite = create_bitmap(width, height);
blit( from, newSprite, startX, startY, 0, 0, width, height );



The problem is that I don't know how to reference the contents of the vector in such a way that I can draw the BITMAP to the screen, something like:

draw_sprite(buffer, m_Sprites[0], x, y);

Here I'm attempting to draw the first BITMAP in the vector, presumably referred to as 0.

Any ideas?

Share this post


Link to post
Share on other sites
BITMAP objects should only be created with create_bitmap(). In your code, both the 'new' and the 'create_bitmap()' statements allocate memory, which means you are leaking memory (delete the 'new' one, it's not needed). This should not crash your app however.

Really, I've never encountered any problems using BITMAP stored in a vector. Try putting in the very top of your file '#define DEBUGMODE' and place TRACE() statements between every function call in this block to see where exactly it crashes. Keep in mind that the create_bitmap() statement may fail if there is not enough memory, and blitting to and from an invalid object will crash (is your buffer valid?)

Share this post


Link to post
Share on other sites
I know nothing about allegro, bur your codes looks rather strange to me.

// First you allocate empty space
BITMAP* newSprite = new BITMAP;

// The you store that pointer in your vector
m_Sprites.push_back( newSprite );

// Then you create bitmap overwriting your old pointer,
// but not storing it in the vector.
newSprite = create_bitmap(width, height);

What you get is a vector with a pointer to allocated memory (Which is an empty, uninitialized bitmap), and another pointer to a bitmap which is never used.

When you are attempting to draw with:
draw_sprite(buffer, m_Sprites[0], x, y);
you are accessing the uninitialized bitmap, which contains junk values, which SHOULD crash you program horribly. So this behaviour is expected.

I believe the proper code should look like this:
BITMAP *newSprite = create_bitmap(width, height);
m_Sprites.push_back( newSprite );

then you can draw it with
draw_sprite(buffer, m_Sprites[0], x, y);

And when you are done with the bitmap you deallocate it with
destroy_bitmap(BITMAP *bmp);
as Fiddler said.

Share this post


Link to post
Share on other sites
I don't really understand what you mean. You say "BITMAP objects should only be created with create_bitmap()", but that is precisely what I do.

Confused.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
crudbreeder is perfectly right.

You should NEVER allocate BITMAPS * using new. create_bitmap does all the allocation you need.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
And BITMAP is a C struct not a C++ class, so it doesn't have any constructor.
While a "Class * pClass = new Class" would allocate memory and call the default constructor of "Class", "new Struct" will only allocate a block of sizeof(Struct) bytes. This is why your BITMAP * points to a zone of garbage and causes your program to crash.

Share this post


Link to post
Share on other sites
Please, take a look at the code you've posted. You first create a BITMAP with new and then you create it _again_ with create_bitmap(). You must _only_ use create_bitmap().

And, please take the time to find out where exactly it crashes. Doing error checking would be useful, ie assert that your bitmaps are not NULL before using them.

EDIT: What the previous AP said is the reason why you should not use 'new' or malloc() with a BITMAP*. Always use the create_bitmap() and load_bitmap() functions.

EDIT2: Copy and paste crudbreeder's code and see if it works (it should). Try to check what his code does against what your code does.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!