stuff-from-scratch/test/graphics/TestD2dOffScreenRendering.cpp
2023-12-21 09:18:44 +00:00

140 lines
No EOL
3.2 KiB
C++

#include "TestFramework.h"
#include "TestUtils.h"
#include "TestRenderUtils.h"
#include "RectangleNode.h"
#include "CircleNode.h"
#include "LineNode.h"
#include "PathNode.h"
#include "PolygonNode.h"
void addRect(const Point2& loc, Vector<Ptr<MaterialNode> >& nodes, double radius = 0.0)
{
auto node = std::make_unique<RectangleNode>(Transform(loc), 150.0, 100.0);
node->setRadius(radius);
nodes.push_back(std::move(node));
}
void addCircle(const Point2& loc, Vector<Ptr<MaterialNode> >& nodes, double minorRadius = 0.0)
{
const auto radius = 50.0;
auto centre_loc = loc;
centre_loc.moveBy(0.0, radius);
auto node = std::make_unique<CircleNode>(Transform(centre_loc), radius);
if (minorRadius != 0.0)
{
node->setMinorRadius(minorRadius);
}
nodes.push_back(std::move(node));
}
void addLine(const Point2& loc, Vector<Ptr<MaterialNode> >& nodes)
{
Vector<Point2> points = { Point2(150.0, 100.0) };
auto node = std::make_unique<LineNode>(Transform(loc), points);
nodes.push_back(std::move(node));
}
void addPath(const Point2& loc, const String& path, Vector<Ptr<MaterialNode> >& nodes)
{
auto node = std::make_unique<PathNode>(Transform(loc), path);
nodes.push_back(std::move(node));
}
void addPolygon(const Point2& loc, Vector<Ptr<MaterialNode> >& nodes)
{
auto p0 = Point2{ 0.0, 0.0 };
auto p1 = Point2{ 150.0, 0.0 };
auto p2 = Point2{ 75.0, 75.0 };
auto node = std::make_unique<PolygonNode>(Transform(loc));
node->setPoints({ p0, p1, p2 });
nodes.push_back(std::move(node));
}
void addShapes(const Point2& start_loc, Vector<Ptr<MaterialNode> >& nodes, bool use_fill = false)
{
auto loc = start_loc;
auto fill_color = Color(200, 0, 200);
addRect(loc, nodes);
loc.moveBy(250, 0);
addCircle(loc, nodes);
loc.moveBy(100, 0);
addLine(loc, nodes);
loc.moveBy(200, 0);
addPolygon(loc, nodes);
loc = Point2(10, 150);
addRect(loc, nodes, 10.0);
loc.moveBy(250, 0);
addCircle(loc, nodes, 75.0);
loc.moveBy(100, 0);
addPath(loc, "M0 0 h150 v100 h-150Z", nodes);
loc = Point2(10, 300);
addPath(loc, "M0 0 h150 q50 50 0 100 h-150Z", nodes);
loc.moveBy(250, 0);
addPath(loc, "M0 0 h150 c25 25 25 75 0 100 h-150Z", nodes);
loc.moveBy(250, 0);
addPath(loc, "M0 0 h150 a50 50 0 0 1 0 100 h-150Z", nodes);
if (use_fill)
{
for (auto& node : nodes)
{
node->setFillColor(fill_color);
}
}
}
TEST_CASE(TestD2dOffScreenRendering_Outlines, "graphics")
{
TestRenderer renderer(800, 800);
Vector<Ptr<MaterialNode> > nodes;
auto loc = Point2(10, 10);
addShapes(loc, nodes, false);
auto scene = renderer.getScene();
for (const auto& node : nodes)
{
scene->addNode(node.get());
}
renderer.writeSvg(TestUtils::getTestOutputDir(__FILE__) / "outlines.svg");
renderer.write(TestUtils::getTestOutputDir(__FILE__) / "outlines.png");
};
TEST_CASE(TestD2dOffScreenRendering_Fill, "graphics")
{
TestRenderer renderer(800, 800);
Vector<Ptr<MaterialNode> > nodes;
auto loc = Point2(10, 10);
addShapes(loc, nodes, true);
auto scene = renderer.getScene();
for (const auto& node : nodes)
{
scene->addNode(node.get());
}
renderer.writeSvg(TestUtils::getTestOutputDir(__FILE__) / "fill.svg");
renderer.write(TestUtils::getTestOutputDir(__FILE__) / "fill.png");
};