Skip to content

Commit

Permalink
Update shapes rendering
Browse files Browse the repository at this point in the history
TBD: Line, Text, Polyline (fill)
  • Loading branch information
ntkwan committed Dec 3, 2023
1 parent 5b2edc7 commit feb71ff
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 238 deletions.
1 change: 0 additions & 1 deletion src/Graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "graphics/Polygon.hpp"
#include "graphics/Polyline.hpp"
#include "graphics/Rect.hpp"
#include "graphics/Shape.hpp"
#include "graphics/Text.hpp"

#endif // GRAPHICS_HPP_
16 changes: 15 additions & 1 deletion src/Parser.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
#ifndef PARSER_HPP_
#define PARSER_HPP_

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#include "../external/rapidxml/rapidxml.hpp"
#include "Graphics.hpp"
#include "graphics/Circle.hpp"
#include "graphics/Color.hpp"
#include "graphics/Ellipse.hpp"
#include "graphics/Group.hpp"
#include "graphics/Line.hpp"
#include "graphics/Path.hpp"
#include "graphics/Polygon.hpp"
#include "graphics/Polyline.hpp"
#include "graphics/Rect.hpp"
#include "graphics/Text.hpp"

using namespace rapidxml;

Expand Down
108 changes: 86 additions & 22 deletions src/graphics/Renderer.cpp → src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,39 @@ Renderer* Renderer::getInstance(Gdiplus::Graphics& graphics) {
return instance;
}

void Renderer::draw(Shape* shape) const {
if (shape->getClass() == "Polyline") {
void Renderer::draw(SVGElement* shape) const {
std::cout << shape->getClass() << std::endl;
if (shape->getClass() == "Group") {
Group* group = dynamic_cast< Group* >(shape);
for (auto elem : group->getElements()) {
std::cout << elem->getClass() << std::endl;
if (elem->getClass() == "Polyline") {
Plyline* polyline = dynamic_cast< Plyline* >(elem);
drawPolyline(polyline);
} else if (elem->getClass() == "Text") {
Text* text = dynamic_cast< Text* >(elem);
drawText(text);
} else if (elem->getClass() == "Rect") {
Rect* rectangle = dynamic_cast< Rect* >(elem);
drawRectangle(rectangle);
} else if (elem->getClass() == "Circle") {
Circle* circle = dynamic_cast< Circle* >(elem);
drawCircle(circle);
} else if (elem->getClass() == "Ellipse") {
Ell* ellipse = dynamic_cast< Ell* >(elem);
drawEllipse(ellipse);
} else if (elem->getClass() == "Line") {
Line* line = dynamic_cast< Line* >(elem);
drawLine(line);
} else if (elem->getClass() == "Polygon") {
Plygon* polygon = dynamic_cast< Plygon* >(elem);
drawPolygon(polygon);
} else if (elem->getClass() == "Path") {
Path* path = dynamic_cast< Path* >(elem);
drawPath(path);
}
}
} else if (shape->getClass() == "Polyline") {
Plyline* polyline = dynamic_cast< Plyline* >(shape);
drawPolyline(polyline);
} else if (shape->getClass() == "Text") {
Expand Down Expand Up @@ -50,45 +81,78 @@ void Renderer::drawLine(Line* line) const {

void Renderer::drawRectangle(Rect* rectangle) const {
mColor fill_color = rectangle->getFillColor();
Gdiplus::SolidBrush RectFill(
Gdiplus::Color(fill_color.a, fill_color.r, fill_color.g, fill_color.b));
graphics.FillRectangle(&RectFill, rectangle->getPosition().x,
rectangle->getPosition().y, rectangle->getWidth(),
rectangle->getHeight());
mColor outline_color = rectangle->getOutlineColor();

Gdiplus::Pen RectOutline(Gdiplus::Color(outline_color.a, outline_color.r,
outline_color.g, outline_color.b),
rectangle->getOutlineThickness());
Gdiplus::SolidBrush RectFill(
Gdiplus::Color(fill_color.a, fill_color.r, fill_color.g, fill_color.b));
graphics.DrawRectangle(&RectOutline, rectangle->getPosition().x,
rectangle->getPosition().y, rectangle->getWidth(),
rectangle->getHeight());
graphics.FillRectangle(&RectFill, rectangle->getPosition().x,
rectangle->getPosition().y, rectangle->getWidth(),
rectangle->getHeight());
}

void Renderer::drawCircle(Circle* circle) const {}
void Renderer::drawCircle(Circle* circle) const {
mColor fill_color = circle->getFillColor();
mColor outline_color = circle->getOutlineColor();
Gdiplus::Pen circleOutline(Gdiplus::Color(outline_color.a, outline_color.r,
outline_color.g, outline_color.b),
circle->getOutlineThickness());
Gdiplus::SolidBrush circleFill(
Gdiplus::Color(fill_color.a, fill_color.r, fill_color.g, fill_color.b));
graphics.DrawEllipse(&circleOutline,
circle->getPosition().x - circle->getRadius().x,
circle->getPosition().y - circle->getRadius().y,
circle->getRadius().x * 2, circle->getRadius().x * 2);
graphics.FillEllipse(&circleFill,
circle->getPosition().x - circle->getRadius().x,
circle->getPosition().y - circle->getRadius().y,
circle->getRadius().x * 2, circle->getRadius().y * 2);
}

void Renderer::drawEllipse(Ell* ellipse) const {}
void Renderer::drawEllipse(Ell* ellipse) const {
mColor fill_color = ellipse->getFillColor();
mColor outline_color = ellipse->getOutlineColor();
Gdiplus::Pen ellipseOutline(
Gdiplus::Color(outline_color.a, outline_color.r, outline_color.g,
outline_color.b),
ellipse->getOutlineThickness());
Gdiplus::SolidBrush ellipseFill(
Gdiplus::Color(fill_color.a, fill_color.r, fill_color.g, fill_color.b));

graphics.DrawEllipse(
&ellipseOutline, ellipse->getPosition().x - ellipse->getRadius().x,
ellipse->getPosition().y - ellipse->getRadius().y,
ellipse->getRadius().x * 2, ellipse->getRadius().y * 2);
graphics.FillEllipse(
&ellipseFill, ellipse->getPosition().x - ellipse->getRadius().x,
ellipse->getPosition().y - ellipse->getRadius().y,
ellipse->getRadius().x * 2, ellipse->getRadius().y * 2);
}

void Renderer::drawPolygon(Plygon* polygon) const {
mColor fill_color = polygon->getFillColor();
mColor outline_color = polygon->getOutlineColor();
Gdiplus::Pen polygonOutline(
Gdiplus::Color(outline_color.a, outline_color.r, outline_color.g,
outline_color.b),
polygon->getOutlineThickness());
Gdiplus::SolidBrush polygonFill(
Gdiplus::Color(fill_color.a, fill_color.r, fill_color.g, fill_color.b));

std::vector< Gdiplus::PointF > points;
Gdiplus::PointF* points = new Gdiplus::PointF[polygon->getPoints().size()];
int idx = 0;
const std::vector< Vector2Df >& vertices = polygon->getPoints();
for (const Vector2Df& vertex : vertices) {
points.push_back(Gdiplus::PointF(vertex.x, vertex.y));
for (const Vector2Df vertex : vertices) {
points[idx++] = Gdiplus::PointF(vertex.x, vertex.y);
}

graphics.FillPolygon(&polygonFill, &points[0],
static_cast< int >(points.size()));

mColor outline_color = polygon->getOutlineColor();
Gdiplus::Pen polygonOutline(
Gdiplus::Color(outline_color.a, outline_color.r, outline_color.g,
outline_color.b),
polygon->getOutlineThickness());
graphics.DrawPolygon(&polygonOutline, &points[0],
static_cast< int >(points.size()));
graphics.DrawPolygon(&polygonOutline, points, idx);
graphics.FillPolygon(&polygonFill, points, idx);
}

void Renderer::drawText(Text* text) const {
Expand Down
14 changes: 12 additions & 2 deletions src/graphics/Renderer.hpp → src/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
#include <gdiplus.h>

//clang-format on
#include "Graphics.hpp"

#include "graphics/Circle.hpp"
#include "graphics/Color.hpp"
#include "graphics/Ellipse.hpp"
#include "graphics/Group.hpp"
#include "graphics/Line.hpp"
#include "graphics/Path.hpp"
#include "graphics/Polygon.hpp"
#include "graphics/Polyline.hpp"
#include "graphics/Rect.hpp"
#include "graphics/Text.hpp"

class Renderer {
public:
Expand All @@ -16,7 +26,7 @@ class Renderer {
void operator=(const Renderer&) = delete;

Gdiplus::Graphics& graphics;
void draw(Shape* shape) const;
void draw(SVGElement* shape) const;

private:
void drawLine(Line* line) const;
Expand Down
3 changes: 0 additions & 3 deletions src/graphics/Circle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@ Circle::Circle(float radius, const Vector2Df &center, mColor fill,
: Ell(Vector2Df(radius, radius), center, fill, stroke, stroke_width) {}

std::string Circle::getClass() const { return "Circle"; }

// void Circle::render(Renderer &renderer) const { renderer.renderCircle(*this);
// }
7 changes: 0 additions & 7 deletions src/graphics/Circle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ class Circle : public Ell {
*
*/
std::string getClass() const override;

// /**
// * @brief Renders the shape using the given renderer.
// *
// * @param renderer The renderer to be used for rendering the shape.
// */
// void render(Renderer &renderer) const override;
};

#endif // CIRCLE_HPP_
10 changes: 5 additions & 5 deletions src/graphics/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ Group::~Group() {

std::string Group::getClass() const { return "Group"; }

// void Group::render(Renderer& renderer) const {
// for (auto shape : shapes) {
// shape->render(renderer);
// }
// }
void Group::render(Renderer& renderer) const {
for (auto shape : shapes) {
renderer.draw(shape);
}
}

Attributes Group::getAttributes() const { return attributes; }

Expand Down
4 changes: 3 additions & 1 deletion src/graphics/Group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

#include <vector>

#include "Renderer.hpp"
#include "SVGElement.hpp"

typedef std::vector< std::pair< std::string, std::string > > Attributes;

class Renderer;
class Group : public SVGElement {
public:
Group();
Expand All @@ -17,7 +19,7 @@ class Group : public SVGElement {

std::string getClass() const override;

// void render(Renderer& renderer) const override;
void render(Renderer& renderer) const;

Attributes getAttributes() const;

Expand Down
2 changes: 0 additions & 2 deletions src/graphics/Rect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ Rect::Rect(float width, float height, Vector2Df position, Vector2Df radius,

std::string Rect::getClass() const { return "Rect"; }

// void Rect::render(Renderer &renderer) const { renderer.renderRect(*this); }

void Rect::setWidth(float width) {
this->width = width;
points[1].x = width;
Expand Down
11 changes: 0 additions & 11 deletions src/graphics/SVGElement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "Color.hpp"
#include "Vector2D.hpp"
class Renderer;

/**
* @brief Represents an element in an SVG file.
Expand Down Expand Up @@ -33,16 +32,6 @@ class SVGElement {
*/
virtual std::string getClass() const = 0;

// /**
// * @brief Renders the shape using the given renderer.
// *
// * @param renderer The renderer to be used for rendering the shape.
// *
// * @note This function is pure virtual and must be implemented by derived
// * classes.
// */
// virtual void render(Renderer& renderer) const = 0;

/**
* @brief Sets the fill color of the shape.
*
Expand Down
44 changes: 0 additions & 44 deletions src/graphics/Shape.cpp

This file was deleted.

Loading

0 comments on commit feb71ff

Please sign in to comment.