|
Copy bitmap to a struct. |
Tony Montana
Member #7,365
June 2006
|
The compiler says: "which is of non-class type BITMAP*" //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;
-- |
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
-- |
Tony Montana
Member #7,365
June 2006
|
I thought they are the same, not? |
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. -- |
Tony Montana
Member #7,365
June 2006
|
Next year I'll study informatics (high school). About my second question:
sprite = create_sub_bitmap(tile,nh,nv,32,32);//I used this code in the main loop and it worked. |
Indeterminatus
Member #737
November 2000
|
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 _______________________________ |
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. |
|