SOLVED-Allegro: probs with vector of BITMAPs

Started by
31 comments, last by TDragon 18 years, 8 months ago
BITMAP* newSprite = new BITMAP;m_Sprites.push_back( newSprite );newSprite = create_bitmap(width, height);


Should be

m_Sprites.push_back(create_bitmap(width, height));


There should also be a check to make sure it actually got created.

When it comes time to destroy all the sprites, don't even think about using delete. Use something like this instead:

#include <algorithm>//...std::for_each(m_Sprites.begin(), m_Sprites.end(), destroy_bitmap);m_Sprites.clear();
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
You have to have a pointer to an Allegro BITMAP structure because they're all different sizes, and have extra data at the end of the BITMAP structure that varies depending on the types (video, mode-x, memory, sub-bitmap, etc.), kind of like C++ virtual inheritance.

So again, they're not normal C++ objects, you have to use the Allegro BITMAP functions to create, manipulate, and destroy them.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Quote:Original post by smart_idiot
m_Sprites.push_back(create_bitmap(width, height));


Thank you, smart_idiot. That works right away, and my code is now two lines shorter, which is always a good sign.

Do I really have to delete them? I mean, you don't have to delete integers, for example. Why would you have to delete BITMAPs?

Also, if I want to access that new BITMAP immediatlely, what's the best way to access it? It's the newest BITMAP added to the vector, so maybe just with the vector .size() method? Or is there a better way? (I ask because I'm setting up a clever sprite creation system, that may for example create ten people sprites wearing t-shirts, then draw different things on their t-shirts, for example, effectively creating ten different sprites from just a few bitmaps. So I may want to create the sprite then blit various different images onto it as soon as I've created it.)

The last created bitmap would be m_Sprites.back().

And yes, you have to destroy them, using destroy_bitmap, NOT using the delete keyword. As I said above, they don't have a destructor. Allegro allocates a bunch of memory for them, and doesn't release it until you tell it you're done with it.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Quote:Original post by smart_idiot
std::for_each(m_Sprites.begin(), m_Sprites.end(), destroy_bitmap);
m_Sprites.clear();


Am I right in thinking that the first line deletes the actual bitmaps, then the second line clears the vector?

I've put these two lines in and now when I exit the program it crashes (ie instead of ending cleanly a window appears with an ugly message saying the program "has encountered a problem and needs to close"). Without the above two lines, the program ends cleanly, as expected (though perhaps with some memory wasted as I am not destroying the bitmaps).

It's certainly not the algothingy that's making it crash. I've just tried deleting the bitmaps like this and it still crashes:

for ( int sprite=0 ; sprite < m_Sprites.size() ; sprite++ )
destroy_bitmap( m_Sprites[sprite] );

In case it is relevant, the vector is created like this:
std::vector<BITMAP*> m_Sprites;

This presumably means that the vector holds pointers to BITMAPs rather than BITMAPs. Does this change the way I have to destroy the BITMAPs?

[Edited by - darenking on July 9, 2005 5:01:34 AM]
It's supposed to be pointers, it looks like you're doing it right. Post some more code, maybe.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Quote:Original post by darenking
It's certainly not the algothingy that's making it crash. I've just tried deleting the bitmaps like this and it still crashes:

for ( int sprite=0 ; sprite < m_Sprites.size() ; sprite++ )
destroy_bitmap( m_Sprites[sprite] );

Nothing wrong there. Somewhere in the rest of your code, perhaps one or more of those pointers becomes invalid (say, you inadvertantly destroy the bitmap some other way) ...

Jesus saves ... the rest of you take 2d4 fire damage.

Where should I destroy the BITMAPs? In the destructor for the object that creates them? That's where I've put it, in the destructor for my World object. Here's the destructor. Notice it logs messages to my message object, saying how many sprites it is trying to destroy etc. At least, it tries to, but the program crashes before the message object has a chance to save the log file.

World::~World(){	g_Message.Log("~[World.~World] World object destroyed");//delete my Character objects, this works just fine...	for ( int character = 0 ; character < m_Characters.size() ; character++ )		delete m_Characters[character];//delete my Actor objects, this works too...	for ( int actor = 0 ; actor < m_Actors.size() ; actor++ )		delete m_Actors[actor];//this is where the pie hits the fan. If I remove this stuff, the program runs fine. With it in, the program crashes at this point...	g_Message.Log("~[World.~World] ****** TOTAL SPRITES before destroy", m_Sprites.size());	for ( int sprite = 0 ; sprite < m_Sprites.size() ; sprite++ )	{		g_Message.Log("~[World.~World] ****** Destroying sprite number", sprite);		destroy_bitmap( m_Sprites[sprite] );	}	g_Message.Log("~[World.~World] ****** TOTAL SPRITES after destroyed", m_Sprites.size());}
Quote:Original post by darenking
Where should I destroy the BITMAPs? In the destructor for the object that creates them? That's where I've put it, in the destructor for my World object.

One question: is the World object global? Because if so, I've found your problem ...

Jesus saves ... the rest of you take 2d4 fire damage.

Nice try! The only global object though is the message logging object, Message.

In general, should BITMAPs be destroyed in the desctructor for the object that creates them?

This topic is closed to new replies.

Advertisement