OO Class Question

Programming, for all ages and all languages.
Post Reply
Kon-Tiki

OO Class Question

Post by Kon-Tiki »

I've got a bit of a problem with my current code. I'm learning Allegro, and am refreshing my very basic C++ knowledge while I'm at it. I've stumbled across a problem I can't seem to solve, though. This's my code so far ('t Is meant as a test for how to do animations):

Code: Select all

#include <allegro.h>

class Player {
      public:
        Player();
        ~Player();
        void animate();
      protected:
        int frame;
        BITMAP *player;
};
Player::Player() {
   player = load_bitmap("animatie.bmp", NULL);
   frame = 1;
}
Player::~Player() {
}
void Player::animate() {
  blit(player, buffer, (60*(4-frame)),0,100,100,(20*frame),20);
}



int main(){
 
    allegro_init();
    install_keyboard();
  
  
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);

    BITMAP *buffer = create_bitmap(640, 480);

    return 0;
}   
END_OF_MAIN();
The problem lies with buffer. The compiler error is this:
In member function 'void Player::animate();' 'buffer' undeclared (first use this function)
In other words: I'd need to declare buffer again in my Player class, but that doesn't seem quite right. 's There a better solution than that (as it feels like that'd give quite some bugs)?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:OO Class Question

Post by Solar »

I get another error before that:

Code: Select all

error: syntax error before `*' token
And then the error you quoted. Could it be that BITMAP is not defined by allegro.h?
Every good solution is obvious once you've found it.
Kon-Tiki

Re:OO Class Question

Post by Kon-Tiki »

The normal way of declaring a BITMAP (except the buffer) is like this:

Code: Select all

BITMAP *player = load_bitmap("animatie.bmp", NULL);
That didn't work either, though. It gave even more errors. Had to cut it into two, but only to reduce the number of errors, not take away all of them.
troflip

Re:OO Class Question

Post by troflip »

buffer is declare in your main() function. So no one outside of main can access it.
Make it a global variable, and it should compile.
Kon-Tiki

Re:OO Class Question

Post by Kon-Tiki »

Hmmm... I'll give that a try. Hey, what do you know? I thought that one needed to be below the Allegro initialization, which needed to be in the main loop. Thanks :)

Edit: That gives a runtime crash, I'm afraid.
AR

Re:OO Class Question

Post by AR »

What did you do with the object? You should have something like:

Code: Select all

BITMAP *buffer;
int main()
{

    allegro_init();
    install_keyboard();
 
 
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);

    buffer = create_bitmap(640, 480);

    return 0;
}
Also, does "Allegro" include garbage collection? You aren't destructing the objects which creates a memory leak.
Kon-Tiki

Re:OO Class Question

Post by Kon-Tiki »

My code looks like this at the moment:

Code: Select all

#include <allegro.h>

BITMAP *buffer = create_bitmap(640, 480);

class Player {
      public:
        Player();
        ~Player();
        void animate();
      protected:
        int frame;
        BITMAP *player;
};
Player::Player() {
   player = load_bitmap("animatie.bmp", NULL);
   frame = 1;
}
Player::~Player() {
  destroy_bitmap(player);
}
void Player::animate() {
  blit(player, buffer, (frame*20),0,100,100,20,20);
  if (frame < 3) frame++;
  else frame = 1;
}



int main(){
 
    allegro_init();
    install_keyboard();
    
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
    
    Player player;
    player.animate();
    
    blit(buffer,screen,0,0,0,0,640,480);
    release_screen();              

    return 0;
}   
END_OF_MAIN();
The memory leak's taken care of, and I'm using the buffer now, but now it crashes during runtime.
AR

Re:OO Class Question

Post by AR »

In C++, the global variables are initalised before main is called, the "create_bitmap()" function is called before main (meaning before the library has been initalised).

Code: Select all

#include <allegro.h>

BITMAP *buffer;

class Player {
      public:
        Player();
        ~Player();
        void animate();
      protected:
        int frame;
        BITMAP *player;
};
Player::Player() {
  player = load_bitmap("animatie.bmp", NULL);
  frame = 1;
}
Player::~Player() {
  destroy_bitmap(player);
}
void Player::animate() {
  blit(player, buffer, (frame*20),0,100,100,20,20);
  if (frame < 3) frame++;
  else frame = 1;
}



int main(){

    allegro_init();
    install_keyboard();
   
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
   
    buffer = create_bitmap(640, 480);

    Player player;
    player.animate();
   
    blit(buffer,screen,0,0,0,0,640,480);
    release_screen();             

    return 0;
} 
END_OF_MAIN();
Kon-Tiki

Re:OO Class Question

Post by Kon-Tiki »

WOOHOO! That works. And I see why, too :D
Kon-Tiki

Re:OO Class Question

Post by Kon-Tiki »

Progress on this tinkering so far He moonwalks to the left, but at least he walks and's animated while doing so. Am working on fixing that now (preferably without setting another set of sprites under the current one) After that: collision detection and attacking.

Edit: Btw, sprite's just for testing purposes. I know I ripped it, but needed something to test with, no?
Post Reply