Drawing a bitmap in a class
Navarrano

Hello,
I'm completly new to Allegro library so please be understanding.

I want to write a class which constructor will draw a bitmap on the display.

#include "main.h"
#include <allegro5\allegro.h>

using namespace std;

class Bouncer
{
public:
  ALLEGRO_BITMAP *bouncer;

  Bouncer::Bouncer( int a, int x, int y, int r, int g, int b );
  Bouncer::~Bouncer(void);
};

#include "Bouncer.h"

Bouncer::Bouncer( int a, int x, int y, int r, int g, int b )
{
  
  bouncer = al_create_bitmap(a,a);
  al_set_target_bitmap(bouncer);
  al_clear_to_color(al_map_rgb(r,g,b));

  al_draw_bitmap(bouncer, x, y, 0 );
}

Of course it is not working because my al_draw_bitmap(bouncer, x, y, 0) should work on my display backbuffer.
However, I don't know how to connect my class with a display created in my "main.cpp" file

ALLEGRO_DISPLAY *Window = NULL;
if( !(Window = al_create_display(640, 480) ) )
  {
    cerr << "Failed to create dipslay!" << endl;
    return -1;
  }

I tried solving it by putting external declaration in "main.h"

#include <allegro5\allegro.h>

 extern ALLEGRO_DISPLAY *Window;

but then I get unresolved external symbol error.

Thanks for Your help in advance.

Matthew Leverton

You could do this:

ALLEGRO_STATE state;
al_store_state(&state, ALLEGRO_STATE_TARGET_BITMAP);

// change target bitmap, etc 

al_restore_state(&state);
al_draw_bitmap(bouncer, x, y, 0);

Or you could use al_set_target_backbuffer(al_get_current_display()).

Edgar Reynaldo

You probably don't want to be doing all that in your constructor, and you don't need to make your ALLEGRO_DISPLAY* an extern one (by the way it is an unresolved symbol because you didn't define it in any source file - using extern only declares it).

Try separating out the functions involved :

#SelectExpand
1class Bitmap { 2private : 3 ALLEGRO_BITMAP* bmp; 4public : 5 Bitmap(ALLEGRO_BITMAP* bitmap) : bmp(bitmap) {} 6 7 void ColorBitmap(int r, int g , int b , int a) { 8 al_set_target_bitmap(bmp); 9 al_clear_to_color(al_map_rgba(r,g,b,a)); 10 } 11 void DrawBitmap() { 12 al_draw_bitmap(bmp , 0 , 0 , 0); 13 } 14}; 15 16// In main : 17ALLEGRO_BITMAP* bouncer = al_create_bitmap(BOUNCER_W , BOUNCER_H); 18Bitmap b(bouncer); 19 20b.ColorBitmap(0,0,64,255); 21al_set_target_bitmap(al_get_backbuffer(display)); 22b.DrawBitmap();

Navarrano

Isn't

ALLEGRO_DISPLAY *Window = NULL;
if( !(Window = al_create_display(640, 480) ) )
  {
    cerr << "Failed to create dipslay!" << endl;
    return -1;
  }

a definition of Window?

Thanks a lot for the answers. They helped me a lot.
I guess I should stick to the idea of not creating everything in the constructor cuz this simple bitmap is intended to be a character in my game for the half-term examination so I will need lots of other methods =)

Edgar Reynaldo
Navarrano said:

Isn't

ALLEGRO_DISPLAY *Window = NULL;

a definition of Window?

Yes it is. Did you include main.h in main.cpp? And in Bouncer.cpp?

Navarrano

I have already deleted that source code but as far as I remember, I didn't include "main.h" in the "main.cpp" file but I did for "bouncer.h".

My programs were never complicated enough to use "extern" specificator so I'm not used to it.

For the future, I need to include such library with exter variables also in "main.cpp", right? :)

Edgar Reynaldo

Well, you should declare the variable as extern once in a header, and define it once in a source file that includes the header, and include that header whenever you need to access it.

Don't know why it didn't work for you, because you had Window declared as extern in your Bouncer.cpp through includes and you had it defined in main.cpp.

Thread #608926. Printed from Allegro.cc