|
|
| Drawing a bitmap in a class |
|
Navarrano
Member #13,776
November 2011
|
Hello, 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. 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
Supreme Loser
January 1999
|
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
Major Reynaldo
May 2007
|
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 : 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();
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
Navarrano
Member #13,776
November 2011
|
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. |
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Navarrano said: Yes it is. Did you include main.h in main.cpp? And in Bouncer.cpp? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
Navarrano
Member #13,776
November 2011
|
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
Major Reynaldo
May 2007
|
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. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
|