Allegro.cc - Online Community

Allegro.cc 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
3 
4#include <allegro.h>
5 
6class 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

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;
destroy_bitmap(bitmap_two);

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Simon Parzer
Member #3,330
March 2003
avatar

Why not make two "getter" functions?

public:
  inline BITMAP* getBackground()
  {
    return this->background;
  }
  inline BITMAP* getMaskedBackground()
  {
    return this->maskedbackground;
  }

jamal
Member #3,326
March 2003
avatar

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)
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 
13fbackground::~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}

Quote:

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
    }
}

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: