jamchelsea
jamchelsea

Reputation: 1

trying to call class function character.Draw(buffer)

im doing a project at the moment and trying to draw_sprite(example.bmp,NULL) to the screen via a class function of Character.Draw(Buffer). It keeps crashing and would like to know why it doesn't work. It works when im not using the class function.

        //Character.cpp
    #include "Character.h"
const int scrx = 640;
const int scry = 480;

    Character::Character(){

x = 10;
y = 10;
velx = 0;
movespeed = 5;
    gun = load_bitmap("gun.bmp",NULL);

     }

    void Character::Draw(BITMAP * buff){
draw_sprite(buff, gun, 10,10);
 }


     //Character.h

     #ifndef CHARACTER_H
     #define CHARACTER_H


     #include <allegro.h>

     class Character{

     public:
        Character();

   ~Character();


         void Draw(BITMAP *buff);
     private:
     BITMAP * gun;


     };

    //main.cpp
         #include <allegro.h>
         #include "Character.h"
          using namespace std;

          Character character;

          int main()
           {

allegro_init();
install_keyboard();


set_gfx_mode(GFX_AUTODETECT_WINDOWED, scrx, scry, 0, 0);



    while(! key[KEY_ESC])
    {

        switch (select)
        {
              case 0:
                ...

        case 2:

            clear_bitmap(buffer);
            character.Draw(buffer);

This is where it fails and if I don't use the class function to Draw. and just input Draw_Sprite(example) it works.

Upvotes: 0

Views: 60

Answers (1)

Felipe Sodre Silva
Felipe Sodre Silva

Reputation: 221

Your Character object is a global one and it's initialized before you start the main function. Its constructor will try to call the load_bitmap function, but you don't call allegro_init until the main function is executed and therefore the image loading is probably failing. You can probably fix this by declaring your Character object after you call allegro_init. Also make sure to add verifications to the resources you try to load to make sure they were correctly loaded.

Upvotes: 1

Related Questions