Advertisement

using Struct in struct in struct . and with pointers, oh my :)

Started by September 28, 2000 01:52 PM
4 comments, last by Mader 24 years, 4 months ago
Hi all! I''m having trouble with my Structures. I have 3 structures that are like this: BOB needs the struct ANIMATIONINFO and ANIMATIONINFO needs the struct in ANIMATIONFRAMEINFO BOB |-> ANIMATIONINFO ----|->ANIMATIONFRAMEINFO The problem is when i access any other property than in the struct BOB,, the game crashes! I know this for sure, the bug is here: //a loop in my create bob function for (index=0; index { mybob->animations[index]->max_anim_frame = 0; MessageBox(main_window_handle,"anim intitialize done", "tit", MB_OK); } I never receve the msgbox,, so it crashes here here are the struct: typedef struct ANIMATIONFRAMEINFO_TYP { int anim_frame; //current frame RECT colisionbox1; // For colision detection RECT colisionbox2; // RECT attackbox1; // int time; //number of miliseconds the frame(or image) will be displayed int damage; int defence; int priority; }ANIMATIONFRAMEINFO_OBJ, * ANIMATIONFRAMEINFO_OBJ_PTR; typedef struct ANIMATIONINFO_TYP { ANIMATIONFRAMEINFO_OBJ *animation[1] ; //The number of the frames are here. First the index animation, second the frame index int max_anim_frame; //Total frame in this animation }ANIMATIONINFO_OBJ, * ANIMATIONINFO_OBJ_PTR; typedef struct BOB_TYP { int attributes; // Special attributes int state; //the state of the bitmap object, alive, dead, ko, Dizzy int action1; //Bob''s displacment, walk, swim, fly,falling int action2; //Bob''s move, defend up, attack7, parry down int x,y; //absolute posision of the bob,, used in the display int vx,vy; //Velocity of the bob int width, height; //dimension of the bob int counter; //General counter int num_frames; //Total number of frame int num_anim; //total number of animations int curr_frame; //Index of current animation frame int curr_anim; //Index of current animation LPDIRECTDRAWSURFACE7 image[MAX_BOB_FRAMES]; //Offscreen surface where the bitmap is in ANIMATIONINFO_OBJ *animations[MAX_BOB_ANIMATIONS];//Animations }BOB_OBJ, *BOB_OBJ_PTR; Each time i do mybob->animations[index]->max_anim_frame = 0; the app crashes. how comme? This is the best method i could find to make a good frame heavy 2d fighting game, I used some code from window game prog for dum(by andrée) some people sais this was an memory alocation problem but i tried, with no success thanks
http://www.fighterX.com
Yep, definitely a memory allocation problem.

Your BOB class defines an array of POINTERS TO ANIMATIONINFO_OBJ structures. The ANIMATIONINFO_OBJ structures are never allocated so when you try to access them - kaboom!

It looks like MAX_BOB_ANIMATIONS is a constant at compile-time, so I''d suggest changing your structure to use an array of ANIMATIONINFO_OBJ structures, rathe than an array of pointers to the same.

If MAX_BOB_ANIMATIONS is dynamic you''ll have to write an init function for your structures that allocates the memory before using it, and another structure to free the memory. In that case, you''ll be much better off using a class where the memory can be allocated and freed in your constructor and destructor respectively.

So what you want is this:

typedef struct ANIMATIONINFO_TYP
{
ANIMATIONFRAMEINFO_OBJ animation[1];
.
.
.
}ANIMATIONINFO_OBJ, * ANIMATIONINFO_OBJ_PTR;


typedef struct BOB_TYP
{
.
.
.
ANIMATIONINFO_OBJ animations[MAX_BOB_ANIMATIONS];//Animations
}BOB_OBJ, *BOB_OBJ_PTR;

Then access the internal structures this way:

mybob.animations[index].max_anim_frame = 0;

Hope this helps.


Advertisement
If that is your actual code, you need to examine the for loop much closer. There is a *small* syntax error, which leads me to believe that it is not your actually code. I suspect what you posted wouldn''t compile. Other than that, are you sure that your pointers are not NULL? Using a null pointer is a no-no. Supposedly it will protect you from screwing up another app, but it has still been known to screw up your app. You should do some error checking (not necessarily in the loop, because that would be slow. Just check to see none of the pointers are invalid every time you modify them, or once per frame right before you access them, whichever is called less. Aside from that, no big errors jump out at me. I have that book, and the WGPDUMB engine will not compile for me, so I tore the source to shreds and built my own engine, which isn''t quite as complete (aka less functions for stuff) and involves a little more DD than WGPDUMB, but it at least works without a problem. I can email it to you if you want. Send me an email asking for it, and I will get it to you ASAP (which may be a while because I generally check my email at school and the source is at home).

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming


You are unique. Just like everybody else.

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Mader, why are you posting your questions more than once? I answered your question and told you that you haven''t allocated memory for the animations structures...

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..
Thanks fifty1 . It worked!!

but the thing is, i hardcoded the MAX_BOB_ANIMATION,
I was gona try and use pointer some day. maybe even use a class.

I''m gona try it right now !
http://www.fighterX.com
well Gladiator, you said your answer in a question. I though you were unsure.

so i posed here since that other guy said i was in the wrong forum.
http://www.fighterX.com

This topic is closed to new replies.

Advertisement