Skip to content

Commit

Permalink
Added a toString() method for all objects
Browse files Browse the repository at this point in the history
Signed-off-by: Bhavye Mathur <[email protected]>
  • Loading branch information
BhavyeMathur committed Jan 9, 2024
1 parent 8958f71 commit 2601ce4
Show file tree
Hide file tree
Showing 21 changed files with 47 additions and 46 deletions.
Binary file modified binaries/lib-macos/libgoopylib.dylib
Binary file not shown.
5 changes: 2 additions & 3 deletions src/goopylib/objects/Circle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ namespace gp {
return p.x * p.x + p.y * p.y < m_RadiusSquared;
}

std::ostream &operator<<(std::ostream &out, const Circle &obj) {
out << strformat("Circle((%g, %g), radius=%g)", obj.m_Position.x, obj.m_Position.y, obj.m_Radius1);
return out;
std::string Circle::toString() const {
return strformat("Circle((%g, %g), radius=%g)", m_Position.x, m_Position.y, m_Radius1);
}
}
2 changes: 1 addition & 1 deletion src/goopylib/objects/Circle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace gp {

GPAPI ~Circle() override = default;

GPAPI friend std::ostream &operator<<(std::ostream &out, const Circle &obj);
GPAPI std::string toString() const override;

private:
float m_RadiusSquared;
Expand Down
6 changes: 2 additions & 4 deletions src/goopylib/objects/Ellipse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ namespace gp {
return RenderableSubclass::Ellipse;
}

std::ostream &operator<<(std::ostream &out, const Ellipse &obj) {
out << strformat("Ellipse((%g, %g), radii=(%g, %g))",
obj.m_Position.x, obj.m_Position.y, obj.m_Radius1, obj.m_Radius2);
return out;
std::string Ellipse::toString() const {
return strformat("Ellipse((%g, %g), radii=(%g, %g))", m_Position.x, m_Position.y, m_Radius1, m_Radius2);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/goopylib/objects/Ellipse.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace gp {

GPAPI virtual ~Ellipse() = default;

GPAPI friend std::ostream &operator<<(std::ostream &out, const Ellipse &obj);
GPAPI std::string toString() const override;

/**
* Sets the fill color of the object
Expand Down
5 changes: 2 additions & 3 deletions src/goopylib/objects/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ namespace gp {
GP_CORE_INFO("gp::Image::Image('{0}', ({1}, {2}), ({3}, {4}))", path, p1.x, p1.y, p2.x, p2.y);
}

std::ostream &operator<<(std::ostream &out, const Image &obj) {
out << strformat("Image(%s, (%g, %g))", obj.m_Path.c_str(), obj.m_Position.x, obj.m_Position.y);
return out;
std::string Image::toString() const {
return strformat("Image(%s, (%g, %g))", m_Path.c_str(), m_Position.x, m_Position.y);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/goopylib/objects/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace gp {
*/
GPAPI Image(std::string path, Point p1, Point p2);

GPAPI friend std::ostream &operator<<(std::ostream &out, const Image &obj);
GPAPI std::string toString() const override;

/**
*
Expand Down
6 changes: 2 additions & 4 deletions src/goopylib/objects/Line.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ namespace gp {
#endif
}

std::ostream &operator<<(std::ostream &out, const Line &obj) {
out << strformat("Line((%g, %g), (%g, %g))",
obj.getP1().x, obj.getP1().y, obj.getP2().x, obj.getP2().y);
return out;
std::string Line::toString() const {
return strformat("Line((%g, %g), (%g, %g))", getP1().x, getP1().y, getP2().x, getP2().y);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/goopylib/objects/Line.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace gp {

GPAPI ~Line() override = default;

GPAPI friend std::ostream &operator<<(std::ostream &out, const Line &obj);
GPAPI std::string toString() const override;

/**
* Sets the outline color of the line
Expand Down
8 changes: 3 additions & 5 deletions src/goopylib/objects/Quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ namespace gp {
return RenderableSubclass::Quad;
}

std::ostream &operator<<(std::ostream &out, const Quad &obj) {
out << strformat("Quad((%g, %g), (%g, %g), (%g, %g), (%g, %g))",
obj.getP1().x, obj.getP1().y, obj.getP2().x, obj.getP2().y,
obj.getP3().x, obj.getP3().y, obj.getP4().x, obj.getP4().y);
return out;
std::string Quad::toString() const {
return strformat("Quad((%g, %g), (%g, %g), (%g, %g), (%g, %g))",
getP1().x, getP1().y, getP2().x, getP2().y, getP3().x, getP3().y, getP4().x, getP4().y);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/goopylib/objects/Quad.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace gp {

GPAPI virtual ~Quad() = default;

GPAPI friend std::ostream &operator<<(std::ostream &out, const Quad &obj);
GPAPI std::string toString() const override;

/**
* Sets the fill color of the object
Expand Down
8 changes: 3 additions & 5 deletions src/goopylib/objects/Rectangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ namespace gp {
{position.x + width / 2.0f, position.y + height / 2.0f},
{position.x - width / 2.0f, position.y + height / 2.0f}) {
};

Rectangle::Rectangle(Point p1, Point p2)
: Quad(p1, {p2.x, p1.y}, p2, {p1.x, p2.y}) {
};

std::ostream &operator<<(std::ostream &out, const Rectangle &obj) {
out << strformat("Rectangle((%g, %g), (%g, %g))",
obj.getP1().x, obj.getP1().y, obj.getP3().x, obj.getP3().y);
return out;
std::string Rectangle::toString() const {
return strformat("Rectangle((%g, %g), (%g, %g))", getP1().x, getP1().y, getP3().x, getP3().y);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/goopylib/objects/Rectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace gp {

GPAPI ~Rectangle() override = default;

GPAPI friend std::ostream &operator<<(std::ostream &out, const Rectangle &obj);
GPAPI std::string toString() const override;

GPAPI void setP1(Point point) override;

Expand Down
9 changes: 9 additions & 0 deletions src/goopylib/objects/Renderable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ namespace gp {
m_Height = m_MaxY - m_MinY;
}

std::ostream &operator<<(std::ostream &out, const Renderable &obj) {
out << obj.toString();
return out;
}

std::string Renderable::toString() const {
return "Renderable()";
}

void Renderable::draw(Window &window) {
GP_CORE_DEBUG("gp::Renderable::_drawRenderable({0})", window.getTitle());

Expand Down
6 changes: 6 additions & 0 deletions src/goopylib/objects/Renderable.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ namespace gp {
public:
GPAPI friend std::ostream &operator<<(std::ostream &out, const Renderable &obj);

/**
*
* @return a string representation of the object
*/
GPAPI virtual std::string toString() const;

/**
* Draws the object to a window
*
Expand Down
8 changes: 3 additions & 5 deletions src/goopylib/objects/TexturedQuad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ namespace gp {
return RenderableSubclass::TexturedQuad;
}

std::ostream &operator<<(std::ostream &out, const TexturedQuad &obj) {
out << strformat("TexturedQuad(%s, (%g, %g), (%g, %g), (%g, %g), (%g, %g))", obj.m_Texture.c_str(),
obj.getP1().x, obj.getP1().y, obj.getP2().x, obj.getP2().y,
obj.getP3().x, obj.getP3().y, obj.getP4().x, obj.getP4().y);
return out;
std::string TexturedQuad::toString() const {
return strformat("TexturedQuad(%s, (%g, %g), (%g, %g), (%g, %g), (%g, %g))", m_Texture.c_str(),
getP1().x, getP1().y, getP2().x, getP2().y, getP3().x, getP3().y, getP4().x, getP4().y);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/goopylib/objects/TexturedQuad.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace gp {
*/
GPAPI TexturedQuad(std::string texture, const Ref<Bitmap> &bitmap, Point p1, Point p2, Point p3, Point p4);

GPAPI friend std::ostream &operator<<(std::ostream &out, const TexturedQuad &obj);
GPAPI std::string toString() const override;

[[nodiscard]] GPAPI std::string getTextureName() const;

Expand Down
7 changes: 3 additions & 4 deletions src/goopylib/objects/TexturedRectangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ namespace gp {
Rectangle() {
};

std::ostream &operator<<(std::ostream &out, const TexturedRectangle &obj) {
out << strformat("TexturedQuad(%s, (%g, %g), (%g, %g))", obj.m_Texture.c_str(),
obj.getP1().x, obj.getP1().y, obj.getP3().x, obj.getP3().y);
return out;
std::string TexturedRectangle::toString() const {
return strformat("TexturedQuad(%s, (%g, %g), (%g, %g))", m_Texture.c_str(),
getP1().x, getP1().y, getP3().x, getP3().y);
}
}
2 changes: 1 addition & 1 deletion src/goopylib/objects/TexturedRectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace gp {
*/
GPAPI TexturedRectangle(std::string texture, const Ref<Bitmap> &bitmap, Point p1, Point p2);

GPAPI friend std::ostream &operator<<(std::ostream &out, const TexturedRectangle &obj);
GPAPI std::string toString() const override;

protected:
/**
Expand Down
7 changes: 3 additions & 4 deletions src/goopylib/objects/Triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ namespace gp {
return RenderableSubclass::Triangle;
}

std::ostream &operator<<(std::ostream &out, const Triangle &obj) {
out << strformat("Triangle((%g, %g), (%g, %g), (%g, %g))",
obj.getP1().x, obj.getP1().y, obj.getP2().x, obj.getP2().y, obj.getP3().x, obj.getP3().y);
return out;
std::string Triangle::toString() const {
return strformat("Triangle((%g, %g), (%g, %g), (%g, %g))",
getP1().x, getP1().y, getP2().x, getP2().y, getP3().x, getP3().y);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/goopylib/objects/Triangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace gp {

GPAPI virtual ~Triangle() = default;

GPAPI friend std::ostream &operator<<(std::ostream &out, const Triangle &obj);
GPAPI std::string toString() const override;

/**
* Sets the fill color of the triangle
Expand Down

0 comments on commit 2601ce4

Please sign in to comment.