Object Visual Composition and Animation Structure


Overview

This document explains the visual elements and animation structure of objects through CSpriteComponent and CVFXComponent. It covers how object’s visual elements operate consistently and flexibly by managing resources, ensuring animation independence via shallow copies, and controlling visual effects.


Resource Relationship of Objects and Visual Components

When multiple objects share the same animation, they are synchronized and executed simultaneously. To allow individual control, animations are shallow-copied, and textures are shared across components using std::shared_ptr.


Class Descriptions

1. CAnimationManager & CTextureManager Classes

  • Role:
    • CAnimationManager and CTextureManager are resource managers. Each handles animation and texture resources by loading and storing them by key, provides them upon request, and manages their lifetimes.

2. CAnimation Class | View Repository

class CAnimation
{
    friend class CAnimationManager;
    friend class CSpriteComponent;
    friend class CVFXComponent;
    
public:
    CAnimation();
    ~CAnimation();
    
protected:
    std::unordered_map<EAnimationState, std::shared_ptr<FAnimationData>> mAnimationStates;
    EAnimationState mCurrentState;
    
    // for EAnimationType::MOVE
    CTransform* mTransform;
    FVector2D mPrevPos;
    
    // for EAnimationType::MOVE & EAnimationType::TIME
    float mFrameInterval;
    int   mCurrIdx;
    bool  mLooped;
    
private:
    void Update(float deltaTime);
    void Release();
    
    CAnimation* Clone() const;
    
public:
    const SDL_Rect& GetFrame();
    
    bool IsPlayedOnce() const;
	void ResetPlayedOnce();
    
    EAnimationState GetState() const;
    void SetState(EAnimationState state);
    void AddState(EAnimationState state, std::shared_ptr<FAnimationData> data);
};

  • Role:
    • Manages animation states and frame data. EAnimationType::TIME switches frames based on elapsed time, while EAnimationType::MOVE switches frames based on movement distance.
    • Creates individual instances through shallow copying, allowing each object to play animations independently.
  • Key Variable:
  • Key Methods:

3. CSpriteComponent Class | View Repository

class CSpriteComponent : public CComponent
{
public:
	CSpriteComponent();
	virtual ~CSpriteComponent();
	
private:
	std::shared_ptr<CTexture> mTexture;
	CAnimation* mAnimation;
	SDL_Rect mFrame;
	
	SDL_RendererFlip mFlip;
	
private:
	virtual void Update(float deltaTime)        final;
	virtual void Render(SDL_Renderer* renderer) final;
	virtual void Release()                      final;
	
public:
	std::shared_ptr<CTexture> GetTexture() const { return mTexture; }
	CAnimation* GetAnimation() const { return mAnimation; }
	
	void SetTexture(const std::string& key);
	void SetAnimation(const std::string& key);
	void SetFrame(const std::string& key);
	
	void SetFlip(SDL_RendererFlip flip) { mFlip = flip; }
 
private:
	const SDL_Rect& GetFrame() const;
	SDL_Rect GetDest() const;
	
	bool IsVisibleToCamera() const;
	SDL_Rect GetCameraSpaceRect() const;
};


4. CVFXComponent Class | View Repository

class CVFXComponent : public CComponent
{
public:
	CVFXComponent();
	virtual ~CVFXComponent();
	
private:
	std::shared_ptr<CTexture> mTexture;
	CAnimation* mAnimation;
	
	bool mPlayVFX;
	
private:
	virtual void Update(float deltaTime)        final;
	virtual void Render(SDL_Renderer* renderer) final;
	virtual void Release()                      final;
	
public:
	std::shared_ptr<CTexture> GetTexture() const { return mTexture; }
	CAnimation* GetAnimation() const { return mAnimation; }
	
	void SetTexture(const std::string& key);
	void SetAnimation(const std::string& key);
	
	void PlayVFX(const FVector2D& pos);
	
private:
	SDL_Rect GetDest() const;
	
	bool IsVisibleToCamera() const;
	SDL_Rect GetCameraSpaceRect() const;
};

  • Role:
    • Component that plays visual effect animations for the object.
    • Performs camera culling to render only when visible on the screen.
    • Visual effects render only while playing; the next effect does not start until the current one completes.
  • Key Variables:
    • mTexture:
      • A texture resource managed by std::shared_ptr.
    • mAnimation:
      • An animation instance cloned for each object.
    • mPlayVFX:
      • Flag indicating whether the VFX is currently playing.
  • Key Methods:

Conclusion

By using the CSpriteComponent and CVFXComponent, the visual elements of objects can be managed efficiently and flexibly. The texture structure managed by std::shared_ptr optimizes resources, and animations cloned with shallow copies allow independent control per object.