Jump to content
  • Advertisement
Sign in to follow this  
caleb_yau

va_arg help

This topic is 3887 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

void animation::blit(SDL_Surface * screen, SDL_Rect & position) // possibly make this inline { SDL_BlitSurface (source, & rectangles[current_frame], screen, & position); } void draw_scene (SDL_Surface * screen, SDL_Rect & position, int num_sprites, ...) { int i; va_list arguments; va_start (arguments, num_sprites); for (i = 0; i < num_sprites; i++) (va_arg(arguments, animation *))->blit(screen, position); va_end(arguments); SDL_Flip (screen); } Although the code above works, i would really like to pass refrences to my animation objects rather than pointers. I've been reading that va_arg just doesn't support that at this time because of a non-POD issue. I think i've found an alternative to using va_arg but I don't really understand it because i'm new to c++. It invariably involves the ':' which i have never seen before nor can find what it is. " public: LUAInvoker(const std::string& funcName, LUAInterpreter* context, Entity* e) : funcName(funcName), context(context), e(e) {} " Can anyone show me a safe way to rewrite my code and explain to me what is actually happening? Thanks a lot in advance.

Share this post


Link to post
Share on other sites
Advertisement
It would appear that all your arguments are all of the same type. Thus the simplest solution is to pass a container of some sort. I think std::vector would suffice:

typedef std::vector<animation*> AnimationList;

void draw_scene (SDL_Surface * screen, SDL_Rect & position, const AnimationList &animations)
{
for( AnimationList::iterator it = animations.begin() ; it != animations.end() ; ++it )
{
(*it)->blit(screen,position);
}
SDL_Flip(screen);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by caleb_yau

" public:
LUAInvoker(const std::string& funcName, LUAInterpreter* context, Entity* e) :
funcName(funcName), context(context), e(e) {}
"

Can anyone show me a safe way to rewrite my code and explain to me what is actually happening? Thanks a lot in advance.


This looks like a constructor with an initialization list. The container approach is definitely better than the unsafe c style approach to variadic functions. Another one might be operator overloading:


class animation
{
public:
// class constructor with initializer list
animation(SDL_Rect & position) : position(position)
{
}
void blit(SDL_Surface * screen)
{
SDL_BlitSurface (source, & rectangles[current_frame], screen, & position);
}
SDL_Surface * source;
SDL_Rect position;
SDL_Rect * rectangles;
unsigned int current_frame;
};

class scene_drawer
{
public:
// class constructor with initializer list
scene_drawer(SDL_Surface * screen) : screen(screen)
{
}
scene_drawer & operator << (animation & a)
{
a.blit(screen);
return *this;
}
SDL_Surface * screen;
};

// sample usage:
SDL_Surface * screen;
scene_drawer drawer(screen);
SDL_Rect pos;
animation a1(pos), a2(pos), a3(pos);
drawer << a1 << a2 << a3;



Of course you'd need to initialize all of your variables too, but that's generally how it would work.

Share this post


Link to post
Share on other sites
I'm really new to c++ and i'm liking your suggestions a lot. I have never used a dynamic container except for the string class and it will definetly help my code out in so many other ways than just this one method. I'm going to try the operator overloading route in a moment but i'm still having trouble. When I try to compile rip off's code i get this error and I don't know what to do to fix it. I've tried to randomly dereference pointers I suspected to be responsible for it, but still to no avail.

typedef std::vector<animation*> AnimationList;

void draw_scene (SDL_Surface * screen, SDL_Rect & position, const AnimationList &animations)
{

//error here on this for loop
for( AnimationList::iterator it = animations.begin() ; it != animations.end() ; ++it )
{
(*it)->blit(screen,position);
}
SDL_Flip(screen);
}

The error is : " conversion from ‘animation* const’ to non-scalar type ‘__gnu_cxx::__normal_iterator<animation**, std::vector<animation*, std::allocator<animation*> > >’ requested"

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!