Jump to content
  • Advertisement
Sign in to follow this  
suliman

Using a condition as parameter (C++)

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

Hi I have a function like this to draw my diablo-style inventory: drawInventory(posx,posy,allowPlayerToAlter); I would like to add a parameter to control what kind of object should be visible in the inventory, like: draw only group 2 and 4 (pistols and drugs) draw only quality min 2 (good or better items) Problem is i don't wanna make a specific parameter for each condition... I know this is a pretty wide question but is there any way to do it in c++? To write the condition in the function call sort of... Thanks Erik

Share this post


Link to post
Share on other sites
Advertisement

// C++
class HudFlags {
bool drawItems;
bool drawHealth;
...
public:
HudFlags () : drawItems (false), drawHealth (false), ... {}
void enableItems (bool enable) { drawItems = enable; }
void enablehealth (bool enable) { drawHealth = enable; }
...
};

HudFlags flags;
flags.enableItems (true);
draw (flags);




// C
struct HudFlags {
bool drawItems;
bool drawHealth;
};

HudFlags flags;
memset (&flags, 0, sizeof (flags));
flags.enableItems = true;
draw (flags);





// C
#define DRAWITEMS (1<<0)
#define DRAWHEALTH (1<<1)

draw (DRAWITEMS | DRAWHEALTH); // bitwise or





// C/C++
enum Flags {
draw_item = (1<<0),
draw_health = (1<<1)
};

draw (draw_item | draw_health); // bitwise or




Just to name some.

Share this post


Link to post
Share on other sites
If there are not too many categories, binary comparison would work. Pseudo code:

Group
{
Pistols = 1,
Drugs = 2,
Bunnies = 4,
Cake = 8
}

function drawInventory(posx, posy, groups)
{
if (groups & Group.Pistols)
drawPistols()

// etc.
}

drawInventory(0, 0, Group.Pistols | Group.Drugs)

Share this post


Link to post
Share on other sites
You could always pass a functor to the function. Example


struct MinQuality
{
int min_quality;

MinQuality(int qual) : min_quality(qual) {}

bool operator()(Item item)
{
return item.Quality() >= min_quality;
}
}

template<class T>
void drawInventory( /* stuff */, T condition)
{
for(size_t i = 0; i < items_vec.size(); ++i
if (condition(items_vec))
items_vec.render();
}




Usage:


// only render "good or better" items
drawInventory(/*whatever goes here*/, MinQuality(2));




EDIT: This approach is best if you only have a few, but complicated, combinations of things you'd want to draw.

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!