I have a problem. I make a class that use two BITMAPS objects in private ambit. But also I want to use the same objects in the public ambit. To your understanding, the code:
1 | #ifndef _FGB_BCK |
2 | #define _FGB_BCK |
3 | |
4 | #include <allegro.h> |
5 | |
6 | class fbackground { |
7 | private: |
8 | BITMAP* background, *maskedbackground; |
9 | SAMPLE* explode; |
10 | |
11 | public: |
12 | BITMAP *fbackg, *fmaskedbackg; |
13 | |
14 | int loadbackground(char* filename); |
15 | int loadmaskedbackground(char* filename); |
16 | void explodeterrain(int x, int y, int radius); |
17 | fbackground(); |
18 | void dumptoscreen(void); |
19 | ~fbackground(); |
20 | }; |
21 | |
22 | #endif //_FGB_BCK |
Basicaly I want the pointers fbackg y fmaskedbackg, pointing to background and maskedbackground respectively. I need your help.
Thanks for your time. (I include the declaration and the implementation of the class as an attachments.)
http://www.allegro.cc/files/attachment/590609 -> fbackground.h
http://www.allegro.cc/files/attachment/590610 -> fbackground.cpp
There are a variety of solutions. The simplest would be to make a public function you could call that would assign the pointers. Another, more advanced method, would be to overload the = operator so that when you use it when loading a bitmap, the bitmap pointer is assigned to both variables.
Though quite frankly, why do you need to keep track of both a private and public copy of the same pointer? It would be much simpler just to have the public bitmap pointers instead of both.
Remember that when you create a bitmap, you're still assigning a pointer to it, not the data itself. For example, this is legal:
BITMAP *bitmap_one, *bitmap_two; bitmap_one = create_bitmap(64,64); bitmap_two = bitmap_one; bitmap_one = NULL; destroy_bitmap(bitmap_two);
--- Kris Asick (Gemini)
--- http://www.pixelships.com
void fbackground::explodeterrain(int x, int y, int radius) { //... //Suena un .wav (requiere que la opción de sonido esté activa) explode = load_sample("explode.wav"); // this line play_sample(explode, 255, 128, 1000, 0); }
you shouldn't use load_sample each time you want to play a sample.
I suggest you load it the same way you load a BITMAP :
1 | int fbackground::loadExplosionSample(char* filename) |
2 | { |
3 | explode = load_sample("explode.wav"); |
4 | if (!explode ) { |
5 | return -1; //error |
6 | } |
7 | else |
8 | { |
9 | return 0; //éxito |
10 | } |
11 | } |
12 | |
13 | fbackground::~fbackground() |
14 | { |
15 | //liberamos memoria de los 2 bitmaps |
16 | destroy_bitmap(background); |
17 | destroy_bitmap(maskedbackground); |
18 | if(explode!=NULL){ |
19 | destroy_sample(explode); |
20 | explode=NULL; |
21 | } |
22 | } |
Basicaly I want the pointers fbackg y fmaskedbackg, pointing to background and maskedbackground respectively.
I can't see the reason why you want to do this, but you have to do it after
loading the BITMAP :
int fbackground::loadbackground(char* filename) { background=load_bitmap(filename,NULL); if (!background) { return -1; //error } else { fbackg=background; return 0; //éxito } }
Thanks for your sugestions. Really I going to change to a public variable instead of pointing twice. Your code works!