Thread: A small problem with bitmaps and classes in Alllegro 5

  1. #1
    Registered User Eielef's Avatar
    Join Date
    Apr 2012
    Posts
    9

    Question A small problem with bitmaps and classes in Alllegro 5

    Hello. I'm programming a 2D game in Allegro, and I'm stuck in the very beggining. I have a main program and a class for the player. In that class, I have a function who draws a circle:
    Code:
    void PlayerBen::draw() {
             al_draw_filled_circle(x,y,30,al_map_rgb(0,0,0));
    //x and y are two variables that belong to the class
         }
    In my main program I have initialized some allegro stuff, and I have a circle who moves throught the screen. Everything works fine.
    The problem starts when I want to create a bitmap variable to change the primitive circle for something else. I created, inside of the class, a variable ALLEGRO_BITMAP *bitmap; and I changed the code in the function draw for that:
    Code:
    void PlayerBen::draw() {
             al_draw_bitmap(bitmap,x,y,0);
          }
    Well, the problem could be the way I inicialize the *bitmap variable. This is the constructor of the class:
    Code:
    PlayerBen::PlayerBen(int Vidas, int Speed, int X, int Y,ALLEGRO_BITMAP *Bitmap) {
             vidas= Vidas;
             speed= Speed;
             x= X;
             y= Y;
             bitmap= Bitmap;
         }
    The parameter *Bitmap is correctly inicialized in the main program. I also draw that bitmap in the main program with the same order that I use inside my class,just for testing, and it works. But when I call the function draw, the program crash. What's happening here?

    I tried to be as specific as I could, to make things easier if anyone wants to help, or ask something. Thank you in advance for the help.

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    You should show us the code where you create the bitmap. Are you by any chance creating the bitmap as a local variable and then passing a pointer to it, to your constructor?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Registered User Eielef's Avatar
    Join Date
    Apr 2012
    Posts
    9
    Yes, I create a pointer to an ALLEGRO_BITMAP, and that pointer is what I pass to my constructor. The code is here_

    In the main program, I use that part of the code to create and initialize a bitmap variable:
    Code:
    ALLEGRO_BITMAP *bitmap_Ben;
    al_init_image_addon();
    bitmap_Ben=al_create_bitmap(BITMAP_BEN_WIDTH,BITMAP_BEN_HEIGHT);
    bitmap_Ben=al_load_bitmap("GuyBen.png");
    And then I use the constructor to create my player:
    Code:
    PlayerBen player(PLAYER_INIT_LIVES, PLAYER_INIT_SPEED, PLAYER_INIT_X_POS, PLAYER_INIT_Y_POS, bitmap_Ben);
    Here is also the full header file of the PlayerBen class:
    Code:
    #ifndef PLAYER_BEN
    #define PLAYER_BEN
    
    #include <allegro5\allegro5.h>
    #include <allegro5\allegro_primitives.h>
    #include <allegro5\allegro_image.h>
    #include <allegro5\allegro_native_dialog.h>
    
    namespace myPlayerBen {
        class PlayerBen {
        public:
            PlayerBen();
            PlayerBen(int Vidas, int Speed, int X, int Y,ALLEGRO_BITMAP *Bitmap);
    
            void moveToLeft();
            void moveToRight();
    
            int getLives();
            void setLives(int lives);
    
            void draw(ALLEGRO_DISPLAY *Display);
    
        private:
            ALLEGRO_BITMAP *bitmap;
            int vidas;
            int speed;
            int x;
            int y;
        };
    }
    #endif
    I can put the full main program and the PlayerBen.cpp file if you want it.

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    You don't seem to be doing any error checking, both al_create_bitmap() and al_load_bitmap() returns NULL on error, try to check their return values.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    Registered User Eielef's Avatar
    Join Date
    Apr 2012
    Posts
    9
    I checked it. In the main function it's ok, but in the class variable the bitmap is NULL. I don't get it...

  6. #6
    Registered User Eielef's Avatar
    Join Date
    Apr 2012
    Posts
    9
    Oh, I found the mistake....I'm so silly... I had the initialation code separated in a function called init(), and in the main function I was calling init() AFTER the class constructor... ¬¬ (Oh, C'mon, I'm better than this) Thank you, anyway. Now it works completely fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 8Bit Bitmaps Problem
    By cboard_member in forum Game Programming
    Replies: 3
    Last Post: 08-19-2005, 05:55 PM
  2. Problem Loading Bitmaps in Allegro
    By LiNeAr in forum Game Programming
    Replies: 1
    Last Post: 08-15-2005, 04:12 PM
  3. Problem displaying bitmaps
    By batman123 in forum Game Programming
    Replies: 2
    Last Post: 01-09-2005, 02:01 AM
  4. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM