SOLVED-Allegro: probs with vector of BITMAPs

Started by
31 comments, last by TDragon 18 years, 8 months ago
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]
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.

[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]

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?
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?)

[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]

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.
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.
crudbreeder is perfectly right.

You should NEVER allocate BITMAPS * using new. create_bitmap does all the allocation you need.
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.
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.

[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]

My code just crashes, so I reckon whatever crudbreeder's code does will be an improvement!

This topic is closed to new replies.

Advertisement