diff --git a/include/pyro/graphics.h b/include/pyro/graphics.h index 5341812..2a26f5f 100644 --- a/include/pyro/graphics.h +++ b/include/pyro/graphics.h @@ -26,7 +26,8 @@ namespace Pyro enum class GraphicsMode { - CAIRO + CAIRO, + PYRO3D }; class Graphics : public Image diff --git a/include/pyro/graphics_pyro3d.h b/include/pyro/graphics_pyro3d.h new file mode 100644 index 0000000..d797fae --- /dev/null +++ b/include/pyro/graphics_pyro3d.h @@ -0,0 +1,19 @@ +#pragma once + +#include "graphics.h" + +namespace Pyro +{ + class GraphicsPyro3D : public Graphics + { + public: + GraphicsPyro3D(const GraphicsPyro3D &in); + GraphicsPyro3D(unsigned int width, unsigned int height, unsigned int channels, unsigned int dpi); + ~GraphicsPyro3D(); + + GraphicsPyro3D &operator=(const GraphicsPyro3D &in); + + void point(float x0, float y0); + void line(float x0, float y0, float x1, float y1, Unit unit = Unit::current); + }; +} \ No newline at end of file diff --git a/screenshot-tests/test-graphics-pyro3d.cpp b/screenshot-tests/test-graphics-pyro3d.cpp new file mode 100644 index 0000000..e69de29 diff --git a/screenshot-tests/test-settings.h b/screenshot-tests/test-settings.h index aeecb97..35b7b52 100644 --- a/screenshot-tests/test-settings.h +++ b/screenshot-tests/test-settings.h @@ -6,7 +6,7 @@ static inline std::string const actual_folder = "./screenshots/expected/"; static inline std::string const current_folder = "./screenshots/current/"; -constexpr static inline Pyro::GraphicsMode testmode = Pyro::GraphicsMode::CAIRO; +constexpr static inline Pyro::GraphicsMode testmode = Pyro::GraphicsMode::PYRO3D; class ImageMatch : public Catch::MatcherBase { diff --git a/src/graphics.cpp b/src/graphics.cpp index 591cf48..2ad3358 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -3,6 +3,7 @@ #include "pyro/graphics.h" #include "pyro/graphics_cairo.h" +#include "pyro/graphics_pyro3d.h" #include "pyro/utils.h" #include @@ -32,6 +33,8 @@ namespace Pyro height = unit2pixels(height, unit, dpi); switch (mode) { + case GraphicsMode::PYRO3D: + return new GraphicsPyro3D(width, height, ARGB, dpi); case GraphicsMode::CAIRO: default: return new GraphicsCairo(width, height, ARGB, dpi); diff --git a/src/graphics_pyro3d.cpp b/src/graphics_pyro3d.cpp new file mode 100644 index 0000000..0744867 --- /dev/null +++ b/src/graphics_pyro3d.cpp @@ -0,0 +1,59 @@ +#include "pyro/graphics_pyro3d.h" + +namespace Pyro +{ + + float interpolate(float i0, float d0, float i1, float d1) + { + if (i0 == i1) + { + return {d0}; + } + } + + GraphicsPyro3D::GraphicsPyro3D(unsigned int width, unsigned int height, unsigned int format, unsigned int dpi) : Graphics(width, height, format, dpi) + { + } + + GraphicsPyro3D::~GraphicsPyro3D() + { + } + + void GraphicsPyro3D::point(float x, float y) + { + this->set(int(x), int(y), this->stroke_color.to_uint()); + } + + void GraphicsPyro3D::line(float x0, float y0, float x1, float y1, Unit unit) + { + if (this->stroke_enable) + { + if (abs(x1 - x0) > (y1 - y0)) + { + // Horizontal-ish + } + else + { + // Vertical-ish + } + if (x0 > x1) + { + float t = x1; + x1 = x0; + x0 = t; + t = y1; + y1 = y0; + y0 = t; + } + + float a = (y1 - y0) / (x1 - x0); + float y = y0; + for (unsigned int x = int(x0); x <= x1; x++) + { + this->set(x, int(y), this->stroke_color.to_uint()); + y += a; + } + } + } + +} \ No newline at end of file diff --git a/src/meson.build b/src/meson.build index 5d27dfa..081fe5f 100644 --- a/src/meson.build +++ b/src/meson.build @@ -6,6 +6,7 @@ pyro_sources = ['pyro.cpp', 'font_impl.cpp', 'graphics.cpp', 'graphics_cairo.cpp', + 'graphics_pyro3d.cpp', 'image.cpp', 'math.cpp', 'shape.cpp', diff --git a/tests/meson.build b/tests/meson.build index 7f498b8..d92cec1 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -8,7 +8,8 @@ test_sources = ['test-main.cpp', 'test-pyro.cpp', 'test-vector.cpp', 'test-string.cpp', - 'test-transformer2d.cpp'] + 'test-transformer2d.cpp', + 'test-engine.cpp'] testexe = executable('unit-tests', test_sources, include_directories: inc_dir, diff --git a/tests/test-engine.cpp b/tests/test-engine.cpp new file mode 100644 index 0000000..7738b09 --- /dev/null +++ b/tests/test-engine.cpp @@ -0,0 +1,33 @@ +#include + +#include "pyro/graphics.h" + +TEST_CASE("Graphics objects can be initialized with different engines", "[graphics]") +{ + SECTION("Create a graphics object with default engine") + { + Pyro::Graphics *graphics = Pyro::Graphics::create(400, 300); + + REQUIRE(graphics != nullptr); + REQUIRE(graphics->width() == 400); + REQUIRE(graphics->height() == 300); + } + + SECTION("Create a graphics object with cairo engine") + { + Pyro::Graphics *graphics = Pyro::Graphics::create(400, 300, Pyro::GraphicsMode::CAIRO); + + REQUIRE(graphics != nullptr); + REQUIRE(graphics->width() == 400); + REQUIRE(graphics->height() == 300); + } + + SECTION("Create a graphics object with 3d engine") + { + Pyro::Graphics *graphics = Pyro::Graphics::create(400, 300, Pyro::GraphicsMode::PYRO3D); + + REQUIRE(graphics != nullptr); + REQUIRE(graphics->width() == 400); + REQUIRE(graphics->height() == 300); + } +} \ No newline at end of file