Allegro Problems

Started by
2 comments, last by 23yrold3yrold 17 years, 4 months ago
Hello, Here is my code: CODE void player::draw_player(int x, int y) { BITMAP *my_pic = NULL; my_pic = load_bitmap("picture.bmp", NULL); blit(my_pic, screen, x ,y ,0,0,480,360); } void player::destroy_player(my_pic) { destroy_bitmap(my_pic); } Now, im trying to set up a class for the player, I'm currently trying to draw the sprite for the player. Theres a problem with this line: CODE void player::destroy_player(my_pic) With the parameter. I don't know what type the variable is (int/bool) im pretty sure its a pointer though. I just dont know what type that is. Can someone please fix up this code for me so it should hopefully work. Also, Do i have to add a private variable for my_pic? - Cheers, Daniel
Advertisement
The variable is a bitmap pointer:

void player::destroy_player(BITMAP* my_pic)
Quote:Also, Do i have to add a private variable for my_pic?

Well, you might want a member variable depending on how you're laying this out. Whether it's public or private is up to you too. [smile]

Jesus saves ... the rest of you take 2d4 fire damage.

Now, Heres my code again: (With the updates and a few other changes)
CODE

#include <allegro.h>

class player
{
private:

BITMAP *my_pic;

public:

player();
~player();

void draw_player(int x, int y);
void destroy_player(BITMAP *my_pic);

};

player::player()
{

}

player::~player()
{

}

void player::draw_player(int x, int y)
{

my_pic = load_bitmap("picture.bmp", NULL);

blit(my_pic, screen,x ,y ,0,0,480,360);

}

void player::destroy_player(BITMAP *my_pic)
{

destroy_bitmap(my_pic);

}

int main() {

int depth, res;

allegro_init();

depth = desktop_color_depth();

if (depth == 0) depth = 32;

set_color_depth(depth);

res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);

if (res != 0) {

allegro_message(allegro_error);

exit(-1);

}

install_timer();
install_keyboard();
install_mouse();

while (!key[KEY_ESC]) {

int x = 0;
int y = 0;

player::player();
player::draw_player(x, y);

readkey();

player::destroy_player(BITMAP *my_pic);
player::~player();

}

clear_keybuf();

return 0;
}
END_OF_MAIN()



Now, on line 77 (This one:
CODE

player::draw_player(x, y);

I get an error that says:
QUOTE

- cannot call member function 'void player::draw_player(int, int)' without object.


Now I don't understand this cause the objects created on the line of code above it and the objects destroyed 4 lines below it....

Whats wrong?

Thanks, Daniel
Quote:Now I don't understand this cause the objects created on the line of code above it and the objects destroyed 4 lines below it....

Whats wrong?

With all due respect, it's "your basic understanding of C++."

What you wrote:

player::player();player::draw_player(x, y);readkey();player::destroy_player(BITMAP *my_pic);player::~player();

What you should have wrote:

player myplayer;              // declare the objectmyplayer.draw_player(x, y);   // call the object's member functionreadkey();player.destroy_player();      // call another member function. You don't need to pass the object its own member variable.// player.~player();          // destructor automatically called as it goes out of scope

Whether there are other errors, I didn't check. I suggest you handle bitmap loading in the constructor and unloading in the destructor though. You don't want to load the bitmap from file every time you want to draw it.

Jesus saves ... the rest of you take 2d4 fire damage.

This topic is closed to new replies.

Advertisement