• Advertisement
Sign in to follow this  

How do I return a pointer (or reference?) to a BITMAP?

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

Hello! I have a class which stores a *BITMAP as part of its private members, well lots in fact: class Tile { public: [various things in here] private: BITMAP* m_Tile[100]; int m_TileAmount; }; I want another class to be able to draw one of these bitmaps to the screen. The other class, as you may guess, is my map class, which stores a grid of squares, like a maze. I want it to be able to count through it's grid (easy with a loop, I know how to do all that side of things), and, for each square, "ask" the Tile class for a pointer or reference (?) to the bitmap. (I'm hoping my program isn't badly organised. I think it would be too messy to put the tile bitmaps in the same class as the map/maze, as my game is very ambitious. The gameplay is going to be very sophisticated, so I think it's best to separate the gameplay classes from the graphics, don't you?) So, unless anyone can convince me I should organise things differently, can anyone tell me how to return a pointer to a private bitmap? (Of course the BITMAP isn't properly encapsulated as it's stored as a pointer to an Allegro BITMAP.)

Share this post


Link to post
Share on other sites
Advertisement
I would do something like this:


class Tile
{
public:
BITMAP& GetBitmapByIndex( unsigned int index )
{
return *( m_Tile[ index ] );
}

private:
BITMAP* m_Tile[100];
int m_TileAmount;
};



A couple of other things:

1) OK: I would use std::vector for the array of tiles.
2) Better: I would use the vector and have all the pointers stored in std::auto_ptr<> so they clenaup properly.
3) Very Good: I would do as 2) with the smart pointer but store the tiles in a std::map. This way the searching for a particular tiles is much faster. You can store each tile by name so the map declaration would look like:

std::map< std::string, std::auto_ptr< BITMAP > > m_tileCollection;

You can now call m_tileCollection.find( std::string ) to find a tile you want and have the Get function return the BITMAP by reference.

Hope this helps,

ace

Share this post


Link to post
Share on other sites
Thanks for that. I think I'm too much of a beginner to use vectors right now. I think I'll get everything working than reread your tips and see if I can change it at a later date!

Share this post


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

  • Advertisement