Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jLeslie

Passing pointers to functions

This topic is 6932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m looking for someone with a good knowledge of pointers to glance over some source code I have and tell me what I''m doing wrong.

Share this post


Link to post
Share on other sites
Advertisement
Post it, baby!

And make sure to put the code in special blocks (before code, type "code" in brackets, and after code type "/code" in brackets).

Share this post


Link to post
Share on other sites
Here it goes

If this doesn''t look right sorry, its my first time using this board

MoveModels is called from my main game loop, and I pass it a pointer to my ZoneObject struct, which holds a list of every model and its related data in the game.

The problem is with my FindFrame function. pFrame is going out of scope, (I believe that''s what its called ), when the function returns. Or rather, pFrame is still NULL when the function returns even though it does assign it to pMeshObject during the function call, I''ve checked with a debugger.

{code}
HRESULT MoveModels(ZoneObject* pZoneObject)
{

AnimationInfo* pAnimInfo;
MeshObject* pMeshObject;
pAnimInfo = new AnimationInfo;
pMeshObject = new MeshObject;

for (int i = 0;iObjectCount;i++)
{
if (pZoneObject->ZoneModel->pAnimInfo->CurrentAnimation != NULL)
{
pAnimInfo = pZoneObject->ZoneModel[i]->pAnimInfo;
pMeshObject = pZoneObject->ZoneModel[i]->pMeshObject;

if(pAnimInfo->IsBeingAnimated == TRUE)
{
///The frame calculations aren''t right yet, but I''ll fix that later.
pAnimInfo->FrameNum = DWORD(((GetTickCount() * 0.001) - pAnimInfo->StartTime));
PlayAnimation(pAnimInfo, pMeshObject);
}
else
{
pAnimInfo->IsBeingAnimated = TRUE;
///The frame calculations aren''t right yet, but I''ll fix that later.
pAnimInfo->StartTime = DWORD((GetTickCount() * 0.001));///Fill in timer function
pAnimInfo->FrameNum = 0;
PlayAnimation(pAnimInfo, pMeshObject);
}

}

}

return S_OK;
}

void PlayAnimation(AnimationInfo* pAnimInfo, MeshObject* pMeshObject)
{
AnimationSetObject* pAnimationSet;
Animation* pAnimation;

pAnimationSet = new AnimationSetObject;
pAnimation = new Animation;


if (pAnimInfo->CurrentAnimation != "ReturnToNorm")
{
LoadAnimation(pAnimInfo->CurrentAnimation, pAnimationSet);

if (pAnimationSet->Animation != NULL)
{
pAnimation = pAnimationSet->Animation;
ParseThroughAnimations(pAnimation, pMeshObject, pAnimInfo);

}
}

}

void ParseThroughAnimations(Animation* pAnimation, MeshObject* pMeshObject, AnimationInfo* pAnimInfo)
{
MeshObject* pFrame;
AnimationKey* pCurrentAnimKey;
AnimationKey* pLastAnimKey;
D3DMATRIX* pTempMatrix;

pTempMatrix = new D3DMATRIX;
pCurrentAnimKey = new AnimationKey;
pLastAnimKey = new AnimationKey;
pFrame = new MeshObject;

pFrame = NULL;
pCurrentAnimKey = NULL;
pLastAnimKey = NULL;

FindFrame(pAnimation, pMeshObject, pFrame);

}


void FindFrame(Animation* pAnimation, MeshObject* pMeshObject, MeshObject* pFrame)
{

if (strcmp(pMeshObject->ObjectName, pAnimation->Name) == 0)
{
pFrame = pMeshObject;
}
else
{
if (pMeshObject->Child == NULL && pMeshObject->Sibling == NULL)
pFrame = NULL;
if (pMeshObject->Child != NULL)
FindFrame(pAnimation, pMeshObject->Child, pFrame);
if (pMeshObject->Sibling != NULL)
FindFrame(pAnimation, pMeshObject->Sibling, pFrame);
}

}
{/code}

Share this post


Link to post
Share on other sites
The code tags need to be in square brackets

You're passing the pointer by value, that's your problem. Just like
Function(int a, int b)
will take a copy of a and b to use internally,
Function(struct* ptr)
will take a copy of ptr and use/change that internally. With pointers, you will get access to the target data (whatever ptr points at) but the actual original pointer itself is safe from your meddling

Someone posted a similar question recently. The answer is to pass the pointer by reference (or its address, in C).

So make the function declaration:

void FindFrame(Animation* pAnimation, MeshObject* pMeshObject, MeshObject*& pFrame)

This isn't tested, and I rarely have to do this, but the idea is that you are now passing a reference to a pointer to a MeshObject, so the pFrame inside your function is exactly the same pointer as you passed to it, so any changes will persist beyond the scope of the function.

If you don't understand this, just try it and see if it works. Then come back and try to work it out later.

Btw, I am assuming all your other values are being passed ok: I haven't checked your program logic.

Edited by - Kylotan on 5/3/00 5:44:38 AM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!