Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Copy bitmap to a struct.

This thread is locked; no one can reply to it. rss feed Print
Copy bitmap to a struct.
Tony Montana
Member #7,365
June 2006

The compiler says: "which is of non-class type BITMAP*"
My code:

//DEFINITION
struct sprite{
    BITMAP *sprite;
};
//The code with error
BITMAP *sprite = load_bitmap("test.bmp",0);
struct sprite.sprite = sprite;

I also wanted to write some information to a textfile:

debug<<nh<<"-"<<nv<<"-"<<spritesize<<"\n";//works
debug<<sprite->w<<"-"<<sprite->h<<"-"<<spritesize<<"\n";//doesn't work, it's placed after the line above.

Thanks.

miran
Member #2,407
June 2002

You need to create an object of type sprite. What you have right now is just a sprite struct, not an actual physical object.

Code:

// DECLARATION of type sprite
struct sprite{
    BITMAP *sprite;
};

// DEFINITION of an object of type sprite
sprite my_sprite;

// The code with no error
BITMAP *sprite = load_bitmap("test.bmp",0);
my_sprite.sprite = sprite;

--
sig used to be here

Tony Montana
Member #7,365
June 2006

Hmmm, my real code:

BITMAP *tile = load_bitmap(str.c_str(),0);

struct sprite a_sprite;

sprite = create_sub_bitmap(tile,nh,nv,spritesize,spritesize);

sprite.sprite = sprite;//Now, when writing a post I see not "a_sprite"...

Ok, ;)

My second question, I also made 2 ints that hold the width and height of the sprite, but the program crashes ("A problem occured"):

int tempx, temy;

tempx=sprite->x;
tempy=sprite->y;

debug<<sprite->w<<"-"<<sprite->h<<"-"<<spritesize<<"\n";

And aslo, when I don't use the debug line (last line) the program crashes.

miran
Member #2,407
June 2002

Why are you using the -> operator? ??? And why on sprite? ???

Working code:

int tempx, temy;

tempx=a_sprite.x;
tempy=a_sprite.y

--
sig used to be here

Tony Montana
Member #7,365
June 2006

I thought they are the same, not?
And I wanted to have the width and height of the sprite (I wrote temp_x,...), for debug purpose, I'm writing a tile loader (with create_sub_bitmap from tile).
(still with "." same problem);

miran
Member #2,407
June 2002

You seriously need to sit down with a good C/C++ book (even online tutorial will do) and learn the basics of programming in C.

Quote:

I thought they are the same, not?

No.

--
sig used to be here

Tony Montana
Member #7,365
June 2006

Next year I'll study informatics (high school).

About my second question:
I think that I can't get the width and height because getting create_sub_bitmap() didn't work well.

1Tiles::Tiles(int spritesize, const std::string &str) {
2 BITMAP *tile = load_bitmap(str.c_str(),0);
3 BITMAP *sprite = create_bitmap(32,32);//This must be destroyed at the beginning
4 
5 int h = tile->h;
6 int v = tile->w;
7 
8 int nh=0, nv=0;
9 int id = 0;
10 
11 struct sprite a_sprite;
12 
13 ofstream debug ("debug.txt");
14 while(nv<v+spritesize) {
15 
16 while(nh<h+spritesize) {
17 
18 destroy_bitmap(sprite);
19 sprite = create_sub_bitmap(tile,nh,nv,32,32);
20 
21 a_sprite.sprite = sprite;
22 a_sprite.id=id, id++;
23 
24 sprites_list.push_back(a_sprite);
25 
26 debug<<nh<<"-"<<nv<<"-"<<nh<<"\n";
27 debug<<sprites_list.size()<<"\n";
28 
29 nh+=spritesize;
30 //debug<<sprite->w<<"-"<<sprite->h<<"-"<<spritesize<<"\n";
31 }
32 
33 nv+=spritesize;
34 nh = 0;
35 }
36 debug.close();

sprite = create_sub_bitmap(tile,nh,nv,32,32);//I used this code in the main loop and it worked.
I also used bit();, I only get one bitmap drawed in the vector.

Indeterminatus
Member #737
November 2000
avatar

1) Check return values. Both load_bitmap and create_bitmap can return 0. The moment you try to dereference a null-pointer, you're doomed.

2) struct sprite a_sprite;: Since you're using C++, you won't need the struct here - but you can, of course.

3) a_sprite.id=id, id++;: Please don't do that. This is filled with side-effects and thus, error-prone. Just to check, please explain :P

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

Tony Montana
Member #7,365
June 2006

Indeterminatus<<Just to be able to search them, each struct own id...

Jonny Cook
Member #4,055
November 2003

I don't think you can have a struct name "sprite" and have an instance named "sprite" at the same time.

The face of a child can say it all, especially the mouth part of the face.

Go to: