Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using an enum in a re-usable class

32 views
Skip to first unread message

JiiPee

unread,
Jan 25, 2019, 12:27:43 PM1/25/19
to
In this example I am animating movements to 4 directions.

First the code:

// handles one animation:

struct Animation
{
    Animation(const std::string& textureFileName) :
        m_textureFileName{ textureFileName }
    {}
    // stuff for animation
    // ...
private:
    std::string m_textureFileName;
};

// contains all animations for the player
class PlayerAnimate
{
    void addAnimation(const std::string& textureFileName) {
        m_animation.emplace_back(textureFileName);
    }
    Animation& getAnimation(int index) {
        return m_animation[index];
    }
private:
    std::vector<Animation> m_animation;
};

int program()
{
    // first add all animations:
    PlayerAnimate playerAnimation;
    playerAnimation.addAnimation("WalkingLeft.png");
    playerAnimation.addAnimation("WalkingRight.png");
    playerAnimation.addAnimation("WalkingUp.png");
    playerAnimation.addAnimation("WalkingDown.png");

    // other code...
    // ...
    // then after, maybe in another function we want to animate right-
    // movement:
    Animation& animationRight = playerAnimation.getAnimation(1);
    draw(animationRight); // do the drawing
}

But I would like to make the call using an enum something like:
    Animation & animationRight =
playerAnimation.getAnimation(Direction::Right);

The problem is that it's not really good to put that enum in
PlayerAnimate (or Animation) class/file
because this class is a re-usable class and animation Direction is not
something
which is general and re-usable (I do not want to pollute PlayerAnimate
with Direction
because its not something all future usage of it needs.. maybe in the
next program direction
is not needed...).

Because here Direction is only a name for the animation. Many in next
animation I need "Jump"-type.. and so on.

I could use template:

template<typename AnimateType>
class PlayerAnimate
{
    void addAnimation(const std::string& textureFileName, AnimateType
type) {
        m_animation.emplace_back(textureFileName, type);
    }
};

(and store that type into the Animation class, or map it somehow to
indexes in PlayerAnimate) and then call like this:

enum class Direction {Right};
PlayerAnimate<Direction> playerAnimation;
Animation& animationRight = playerAnimation.getAnimation(Direction::Right);

But that makes it quite "heavy" because then other classes who use
PlayerAnimate
(there are couple of other classes using it) they would also then be
changed to
template classes (I think?).

So how to do this so that I don't need to put that enum class Direction in
PlayerAnimate or Animation class?
Do I need to create some kind of polymorhism pointer and then inherit a
class which
contains  my enum class Direction? Was a little thinking about that.

I could have the type as a std::string, and then it would always work,
but prefer using

enums rather than strings....

Mr Flibble

unread,
Jan 25, 2019, 12:45:07 PM1/25/19
to
No need to use either.

Use a vector (mathematical not std::vector) of arity 2 (assuming 2D not
3D) to represent velocity and map the vector's direction to an animation.
The vector will also allow you to update position too.

/Flibble

--
“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who
doesn’t believe in any God the most. Oh, no..wait.. that never happens.” –
Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are
confronted by God," Bryne asked on his show The Meaning of Life. "What
will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery
that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a
world that is so full of injustice and pain. That's what I would say."

JiiPee

unread,
Jan 25, 2019, 2:26:05 PM1/25/19
to
On 25/01/2019 17:44, Mr Flibble wrote:
> No need to use either.
>
> Use a vector (mathematical not std::vector) of arity 2 (assuming 2D
> not 3D) to represent velocity and map the vector's direction to an
> animation. The vector will also allow you to update position too.


its not about the direction or moving... its about having enum in a
class. how can I use an enum in class...

Öö Tiib

unread,
Jan 28, 2019, 4:33:33 AM1/28/19
to
You put it up sort of messy way. Start from basics.
What is your program data structure and relations?
You have data types "texture", "player", "direction" and "animation"?
What other data there is? What is possible state of objects of
those types?
Is some of these types property of other type? How?
Can there be none? many? Always fixed amount or changing?
What of those relations can change during program run?
Then what are the actions with objects of those data types?
What can do what using what?
The "texture" can perhaps be "drawn"?
The "animation" can perhaps be "animated"?
The "player" can perhaps "move" to "direction"?
What other actions are there?
What action is part of what action?
Write that all down in plain prose English first.
Do not make templates before it is clearly compile-
time fixed relation, value or property.
0 new messages