Skip to content

Commit 40ef8e1

Browse files
refactor: Organize Shape class methods
Reorganize the methods in the Shape class (Shape.hpp and Shape.cpp) by categorizing them with comments and grouping related functionalities. This improves code readability and maintainability.
1 parent 57da6f2 commit 40ef8e1

2 files changed

Lines changed: 146 additions & 120 deletions

File tree

main/include/Shapes/Shape.hpp

Lines changed: 77 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -66,115 +66,127 @@ class Shape {
6666

6767
Collider *collider;
6868

69+
// Protected helper methods
70+
std::pair<int, int> getTransformedPosition(int x, int y);
71+
void bresenhamLine(Pixels &points, int x0, int y0, int x1, int y1);
72+
void wuLine(Pixels &points, int x0, int y0, int x1, int y1);
73+
void addPixel(Pixels &points, int x, int y, float alpha);
74+
void getInsidePoints(Pixels &points,
75+
const std::vector<std::pair<int, int>> &vertices);
76+
6977
public:
7078
Shape(const ShapeParams &params);
7179
virtual ~Shape();
7280

73-
void addCollider(Collider *collider = nullptr);
74-
Collider *getCollider() const { return collider; }
81+
// Pure virtual methods
82+
virtual void drawAntiAliased(Pixels &pixels) = 0;
83+
virtual void drawAliased(Pixels &pixels) = 0;
7584
virtual Collider *defaultCollider() = 0;
76-
void removeCollider();
77-
bool intersects(Shape *other);
85+
86+
// Drawing
87+
void draw(Pixels &pixels, const DrawOptions &options);
88+
89+
// Position and transformation methods
90+
void setPosition(int x, int y);
7891
void translate(int dx, int dy);
7992
void translate(float dx, float dy);
93+
void rotate(float angle);
94+
void setPivot(int x, int y);
95+
96+
void setScale(float scaleX, float scaleY, float originX = -1,
97+
float originY = -1);
98+
void scaleX(float scaleX, float originX = -1);
99+
void scaleY(float scaleY, float originY = -1);
100+
void setScaleOrigin(int x, int y);
80101

102+
// Collider methods
103+
void addCollider(Collider *collider = nullptr);
104+
void removeCollider();
105+
bool intersects(Shape *other);
106+
107+
// Texture methods
81108
void setTexture(Texture *texture);
82-
Texture *getTexture() const { return texture; }
83109
void setTextureScale(float scaleX, float scaleY);
84110
void setTextureOffset(float offsetX, float offsetY);
85-
void setFixTexture(bool fixed);
86-
bool getFixTexture() const { return fixTexture; }
87111
void setTextureRotation(float rotation);
112+
void setFixTexture(bool fixed);
88113
Color sampleTexture(int x, int y);
89114

90-
float getUVScaleX() const { return uvTransform.scaleX; }
91-
float getUVScaleY() const { return uvTransform.scaleY; }
92-
float getUVOffsetX() const { return uvTransform.offsetX; }
93-
float getUVOffsetY() const { return uvTransform.offsetY; }
94-
float getUVRotation() const { return uvTransform.rotation; }
95-
96-
void setUVScaleX(float scaleX) {
97-
this->uvTransform.scaleX = scaleX;
98-
this->uvTransform.invScaleX = 1.0f / scaleX;
99-
}
100-
void setUVScaleY(float scaleY) {
101-
this->uvTransform.scaleY = scaleY;
102-
this->uvTransform.invScaleY = 1.0f / scaleY;
103-
}
104-
void setUVOffsetX(float offsetX) { this->uvTransform.offsetX = offsetX; }
105-
void setUVOffsetY(float offsetY) { this->uvTransform.offsetY = offsetY; }
106-
void setUVRotation(float rotation) {
107-
this->uvTransform.rotation = rotation;
108-
invalidateTexTrigCache();
109-
}
110-
115+
// Parent-child relationship
111116
void setParent(Shape *parent);
112-
Shape *getParent() const { return parent; }
113117

118+
// Cache invalidation
114119
void invalidateTrigCache() { trigCacheValid = false; }
115120
void invalidateTexTrigCache() { texTrigCacheValid = false; }
116121

122+
// Getters
117123
float getX() const { return x; }
118124
float getY() const { return y; }
119125
int getZ() const { return z; }
120126
const Color &getColor() const { return color; }
121127

128+
float getRotationAngle() const { return rotation.angle; }
129+
int getRotationX() const { return rotation.x; }
130+
int getRotationY() const { return rotation.y; }
131+
132+
float getScaleX() const { return scale.x; }
133+
float getScaleY() const { return scale.y; }
134+
int getScaleOriginX() const { return scale.originX; }
135+
int getScaleOriginY() const { return scale.originY; }
136+
137+
Texture *getTexture() const { return texture; }
138+
bool getFixTexture() const { return fixTexture; }
139+
140+
float getUVScaleX() const { return uvTransform.scaleX; }
141+
float getUVScaleY() const { return uvTransform.scaleY; }
142+
float getUVOffsetX() const { return uvTransform.offsetX; }
143+
float getUVOffsetY() const { return uvTransform.offsetY; }
144+
float getUVRotation() const { return uvTransform.rotation; }
145+
146+
Collider *getCollider() const { return collider; }
147+
Shape *getParent() const { return parent; }
148+
149+
// Setters
122150
void setX(int x) {
123151
this->x = x;
124152
if (collider) {
125153
collider->setX(x);
126154
}
127155
}
156+
128157
void setY(int y) {
129158
this->y = y;
130159
if (collider) {
131160
collider->setY(y);
132161
}
133162
}
134-
void setColor(const Color &color) { this->color = color; }
135-
136-
protected:
137-
std::pair<int, int> getTransformedPosition(int x, int y);
138163

139-
void bresenhamLine(Pixels &points, int x0, int y0, int x1, int y1);
140-
void wuLine(Pixels &points, int x0, int y0, int x1, int y1);
141-
void addPixel(Pixels &points, int x, int y, float alpha);
142-
143-
public:
144-
void setScale(float scaleX, float scaleY, float originX = -1,
145-
float originY = -1);
146-
void scaleX(float scaleX, float originX = -1);
147-
void scaleY(float scaleY, float originY = -1);
148-
void setScaleOrigin(int x, int y);
164+
void setColor(const Color &color) { this->color = color; }
165+
void setZ(int z);
149166

150-
float getScaleX() const { return scale.x; }
151-
float getScaleY() const { return scale.y; }
152-
int getScaleOriginX() const { return scale.originX; }
153-
int getScaleOriginY() const { return scale.originY; }
167+
void setRotationAngle(float angle) { this->rotation.angle = angle; }
168+
void setRotationX(int x) { this->rotation.x = x; }
169+
void setRotationY(int y) { this->rotation.y = y; }
154170

155171
void setScaleX(float scaleX) { this->scale.x = scaleX; }
156172
void setScaleY(float scaleY) { this->scale.y = scaleY; }
157173
void setScaleOriginX(int x) { this->scale.originX = x; }
158174
void setScaleOriginY(int y) { this->scale.originY = y; }
159-
void changeColor(const Color &color);
160-
void setZ(int z);
161-
void setPosition(int x, int y);
162-
void rotate(float angle);
163-
void setPivot(int x, int y);
164-
165-
float getRotationAngle() const { return rotation.angle; }
166-
int getRotationX() const { return rotation.x; }
167-
int getRotationY() const { return rotation.y; }
168175

169-
void setRotationAngle(float angle) { this->rotation.angle = angle; }
170-
void setRotationX(int x) { this->rotation.x = x; }
171-
void setRotationY(int y) { this->rotation.y = y; }
176+
void setUVScaleX(float scaleX) {
177+
this->uvTransform.scaleX = scaleX;
178+
this->uvTransform.invScaleX = 1.0f / scaleX;
179+
}
172180

173-
void draw(Pixels &pixels, const DrawOptions &options);
174-
virtual void drawAntiAliased(Pixels &pixels) = 0;
175-
virtual void drawAliased(Pixels &pixels) = 0;
181+
void setUVScaleY(float scaleY) {
182+
this->uvTransform.scaleY = scaleY;
183+
this->uvTransform.invScaleY = 1.0f / scaleY;
184+
}
176185

177-
protected:
178-
void getInsidePoints(Pixels &points,
179-
const std::vector<std::pair<int, int>> &vertices);
186+
void setUVOffsetX(float offsetX) { this->uvTransform.offsetX = offsetX; }
187+
void setUVOffsetY(float offsetY) { this->uvTransform.offsetY = offsetY; }
188+
void setUVRotation(float rotation) {
189+
this->uvTransform.rotation = rotation;
190+
invalidateTexTrigCache();
191+
}
180192
};

0 commit comments

Comments
 (0)