- Online Community Forums » Programming Questions » Pointing to a Bitmap

Credits go to jamal, Kris Asick, and Simon Parzer for helping out!
This thread is locked; no one can reply to it. rss feed Print
Pointing to a Bitmap
Bruno Espinoza
Member #6,831
January 2006

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
4#include <allegro.h>
6class fbackground {
7 private:
8 BITMAP* background, *maskedbackground;
9 SAMPLE* explode;
11 public:
12 BITMAP *fbackg, *fmaskedbackg;
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();
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.) -> fbackground.h -> fbackground.cpp

A complete newbie who wants to learn something.

Kris Asick
Member #1,424
July 2001

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;

--- Kris Asick (Gemini)

--- Kris Asick (Gemini)

Simon Parzer
Member #3,330
March 2003

Why not make two "getter" functions?

  inline BITMAP* getBackground()
    return this->background;
  inline BITMAP* getMaskedBackground()
    return this->maskedbackground;

Member #3,326
March 2003

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 :

1int fbackground::loadExplosionSample(char* filename)
3 explode = load_sample("explode.wav");
4 if (!explode ) {
5 return -1; //error
6 }
7 else
8 {
9 return 0; //éxito
10 }
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 }


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)
    if (!background) {
                      return -1; //error
        return 0; //éxito

Bruno Espinoza
Member #6,831
January 2006

Thanks for your sugestions. Really I going to change to a public variable instead of pointing twice. Your code works! ;D

A complete newbie who wants to learn something.

Go to: