Page 1 of 1
OO Class Question
Posted: Thu Aug 25, 2005 1:19 pm
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)?
Re:OO Class Question
Posted: Thu Aug 25, 2005 1:31 pm
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?
Re:OO Class Question
Posted: Thu Aug 25, 2005 1:35 pm
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.
Re:OO Class Question
Posted: Thu Aug 25, 2005 1:45 pm
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.
Re:OO Class Question
Posted: Thu Aug 25, 2005 1:53 pm
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.
Re:OO Class Question
Posted: Thu Aug 25, 2005 2:11 pm
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.
Re:OO Class Question
Posted: Thu Aug 25, 2005 2:39 pm
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.
Re:OO Class Question
Posted: Thu Aug 25, 2005 2:52 pm
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();
Re:OO Class Question
Posted: Thu Aug 25, 2005 2:59 pm
by Kon-Tiki
WOOHOO! That works. And I see why, too
Re:OO Class Question
Posted: Thu Aug 25, 2005 6:43 pm
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?