Sign in to follow this  

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

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
I would do something like this:

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

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,


Share this post

Link to post
Share on other sites

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