Calling structures externally.

Started by
9 comments, last by pauljg 21 years, 11 months ago
Can anyone please guide me onto the right track !. I''ve setup a global stucture: struct sprite { int max_speed; int anim_timer; int hide; int frame; int max_animate; SDL_Rect position; }; In my main windows loop, I initialize it like so: sprite fly; But I''d like to write a function that uses the structure, here''s what I''ve got so far. (but it doesnt work) void animation(sprite *spr) { spr.anim_timer++; } And I''ll calling the function like so: animation(&fly); But when compiling I get this error: tut.cpp(137) : error C2228: left of ''.anim_timer'' must have class/struct/union type Help !, please !!!
Advertisement
From MSDN:

"Compiler Error C2228left of ''.identifier'' must have class/struct/union type
The operand to the left of the period (.) is not a class, structure, or union.

Example

// C2228.cpp
int i;
struct S
{
public:
int member;
} s, *ps;
int main()
{
i.member = 0; // C2228, i is not a class type
ps.member = 0; // C2228, ps is a pointer to a structure
s.member = 0; // OK, s is a structure type
ps->member = 0; // OK, ps points to a structure S
}"


Bottom line:
spr->anim_timer++;
Thank You.
Here''s another of my ''little problems''..

I''m loading 4 images into SDL_Surface banks, like so:

SDL_Surface *fly_image[5];
char filename[20];

Then in my main program loop:

fly_image[1]=SDL_LoadBMP("fly1.bmp");
fly_image[2]=SDL_LoadBMP("fly2.bmp");
etc...

It would be great if I could call the bitmap loading from a routine, I''ve written up a function:

void load_sprite(SDL_Surface *spr2,char *filename)
{ spr2=SDL_LoadBMP(filename); }

Which of course passes the address to load the bitmap and the filename - it compiles up fine without errors, although on running it - it crashes out.

I''ve worked out that its just not loading the bitmap, what am I doing wrong ?. I guess I''m just not passing the correct address over to it ??.

Many Thanks.
Ok, little hard to explain.. but you''ll have to pass a pointer to a pointer to the function..
so instead of
void load_sprite(SDL_Surface *spr2,char *filename)
{
spr2=SDL_LoadBMP(filename);
}

do
void load_sprite(SDL_Surface **spr2, char *filename)
{
*spr2=SDL_LoadBMP(filename);
}

and call it this way:

SDL_Surface *blabla;
load_sprite(&blabla, "somegeniusbitmap.bmp");


what you do here, is you pass the adress of the pointer instead of the value in the pointer itself.. that way you can then save the pointer value you get back from SDL_LoadBMP() at the address of the pointer.

What happens if you don''t do so? the pointer value you pass to the function is copied to a temporary value (on the stack), then you assign the pointer value returned by SDL_LoadBMP() to the temporary pointer the used to point to the surface. At function return the temporary pointer with the address of the loaded SDL surface is popped of the stack and lost somewhere in the OS with that you don''t know where the memory for the SDL surface is, so you''ll have troubles deallocating is -> you get a memory leak.. unless SDL keeps track of loaded surfaces.. I don''t know SDL so I don''t know..
anyhow, the variable in the calling function will still have the same value as when you passed it to the function..

okidoki,
hope that helped!
cya,
Phil



Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this questions a few times over in the states )
Visit Rarebyte! and no!, there are NO kangaroos in Austria (I got this question a few times over in the states ;) )
quote:Original post by phueppl1
Ok, little hard to explain.. but you''ll have to pass a pointer to a pointer to the function..

It would be easier if he passed a reference to a pointer:

void load_sprite(SDL_Surface *&spr2, char *filename)
{
spr2=SDL_LoadBMP(filename);
}

[C++ FAQ Lite | ACCU | Boost | Python]
Nice one Thanks Gents.
Actually, there is one more

It''s not really a part of my project, so its no hardship if an answer can be found. I''m just interested how you guys would tackle the problem.

Here''s my animation routine

void animation(sprite *spr)
{
spr->anim_timer++;

if (spr->anim_timer==spr->max_speed) {
spr->frame++;
spr->anim_timer=0;
if (spr->frame>=spr->max_animate) spr->frame=1; }
}

At the mo I call it to adjust the structure info from my main loop, before returning back to it to blit it.

like so:

animation(&fly);
SDL_BlitSurface(fly_image[fly.frame],NULL,screen,&fly.position);

If I wanted to put the Blitsurface function in the animation function how would I go about it ?

ok, I could pass an addess over it - but the function itself decides what sprite in the array is the correct one to draw. Without passing all the addresses of the sprites that it could access over to it.. how else could I do it ?

eg:

animation(&fly,fly_image[1],fly_image[2]...etc)

Which of course isnt really feasible ?.
Generally, you wouldn''t want to. Changing the frame and displaying one would usually be considered different processes.

But if you really wanted to, you''d just pass the array in. No big deal.

animation(&fly, fly_image);

Then the animation function might look like:
void animation(sprite *spr, SDL_Surface* images){    spr->anim_timer++;    if (spr->anim_timer==spr->max_speed)    {        spr->frame++;         spr->anim_timer=0;         if (spr->frame>=spr->max_animate) spr->frame=1;    }    SDL_BlitSurface(images[spr->frame],NULL,screen,&spr->position);} 

Read through it, make sure you understand.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions ]
I tried it, just to see if it would work - and it didnt.

I guess it doesnt like an array with different memory locations being passed as one address.

tut.cpp(73) : error C2664: ''animation'' : cannot convert parameter 2 from ''struct SDL_Surface *[5]'' to ''struct SDL_Surface *''

This topic is closed to new replies.

Advertisement