patterncppMinor
Sprite and AnimatedSprite
Viewed 0 times
andspriteanimatedsprite
Problem
I have a
Sprite.h
```
#ifndef CSPRITE_H
#define CSPRITE_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "CBitmapCache.h"
class Sprite {
public:
static Sprite CreateSprite(BITMAP file, int centerX, int centerY);
static Sprite* CreateSprite(std::string file, int width, int height);
static Sprite* CreateSprite(std::string file, int width, int height, int centerX, int centerY);
static Sprite* CreateSprite(const Sprite& sprite);
virtual ~Sprite();
std::string* GetFilename() const;
virtual BITMAP* GetImage() const;
double GetX() const;
double GetY() const;
double GetZ() const;
Point GetPosition() const;
virtual int GetWidth() const;
virtual int GetHeight() const;
int GetFrameWidth() const;
int GetFrameHeight() const;
int GetCenterX() const;
int GetCenterY() const;
double GetRotation() const;
double GetScaleX() const;
double GetScaleY() const;
fixed GetScaleAsFixed() const;
int GetTint() const;
unsigned char GetTintIntensity() const;
unsigned char GetAlpha() const;
int GetRotationRadius() const;
void SetX(double x);
void SetY(double y);
void SetZ(double z);
void SetPosition(double x, double y);
void SetPosition(double x, double y, double z);
void SetPosition(const Point& position);
void SetCenterX(int x);
void SetCenterY(int y);
void SetScaleX(double x);
void SetScaleY(double y);
void SetRotation(double angle);
void SetTint(unsigned int tint);
void SetTintIntensity(unsigned char intensity);
void SetAlpha(unsigned char alpha);
void SetRadius(int radius);
Sprite& Clone(cons
Sprite class and an AnimatedSprite subclass, and I'd like to decouple these them and maintain substitutability, according to the Liskov Substitution Principle. I find that when dealing with pointers to Sprites I always use GetFrameWidth and GetFrameHeight instead of GetWidth and GetHeightjust in case the pointer is actually pointing to an AnimatedSprite that requires just the frame width or height instead of the entire sprite sheet.Sprite.h
```
#ifndef CSPRITE_H
#define CSPRITE_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "CBitmapCache.h"
class Sprite {
public:
static Sprite CreateSprite(BITMAP file, int centerX, int centerY);
static Sprite* CreateSprite(std::string file, int width, int height);
static Sprite* CreateSprite(std::string file, int width, int height, int centerX, int centerY);
static Sprite* CreateSprite(const Sprite& sprite);
virtual ~Sprite();
std::string* GetFilename() const;
virtual BITMAP* GetImage() const;
double GetX() const;
double GetY() const;
double GetZ() const;
Point GetPosition() const;
virtual int GetWidth() const;
virtual int GetHeight() const;
int GetFrameWidth() const;
int GetFrameHeight() const;
int GetCenterX() const;
int GetCenterY() const;
double GetRotation() const;
double GetScaleX() const;
double GetScaleY() const;
fixed GetScaleAsFixed() const;
int GetTint() const;
unsigned char GetTintIntensity() const;
unsigned char GetAlpha() const;
int GetRotationRadius() const;
void SetX(double x);
void SetY(double y);
void SetZ(double z);
void SetPosition(double x, double y);
void SetPosition(double x, double y, double z);
void SetPosition(const Point& position);
void SetCenterX(int x);
void SetCenterY(int y);
void SetScaleX(double x);
void SetScaleY(double y);
void SetRotation(double angle);
void SetTint(unsigned int tint);
void SetTintIntensity(unsigned char intensity);
void SetAlpha(unsigned char alpha);
void SetRadius(int radius);
Sprite& Clone(cons
Solution
Looking over your code and comments, might I suggest a different strategy?
AnimatedSprite could be thought of as a collection of individual sprites. Rather than deriving it from Sprite, use composition to resuse Sprite objects inside of an AnimatedSprite.
If there is a genuine need to use both interchangeably (without knowing specifically whether you have a Sprite or AnimatedSprite) I would create an abstract class (interface) that collects the common methods and implement it in each of Sprite and AnimatedSprite.
AnimatedSprite could be thought of as a collection of individual sprites. Rather than deriving it from Sprite, use composition to resuse Sprite objects inside of an AnimatedSprite.
If there is a genuine need to use both interchangeably (without knowing specifically whether you have a Sprite or AnimatedSprite) I would create an abstract class (interface) that collects the common methods and implement it in each of Sprite and AnimatedSprite.
Context
StackExchange Code Review Q#4134, answer score: 4
Revisions (0)
No revisions yet.