Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create graphics #44

Merged
merged 4 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@
"utility": "cpp",
"valarray": "cpp",
"variant": "cpp",
"vector": "cpp"
"vector": "cpp",
"nonlinearoptimization": "cpp",
"ios": "cpp"
},
"mesonbuild.buildFolder": "build",
"terminal.integrated.env.linux": {
Expand Down
22 changes: 15 additions & 7 deletions include/pyro/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ namespace Pyro
Graphics(unsigned int width, unsigned int height, unsigned int channels, unsigned int dpi, Unit unit = Unit::PX);
virtual ~Graphics() override;

static Graphics *create(unsigned int width, unsigned int height, GraphicsMode mode = GraphicsMode::CAIRO, unsigned int dpi = 72, Unit unit = Unit::PX);
virtual void init() override;

void imagemode(int mode)
{
this->_image_mode = mode;
};
void image(Image *img, float x, float y);
virtual void image_impl(Image * /*img*/, float /*x*/, float /*y*/){};
virtual void image_impl(Image * /*img*/, float /*x*/, float /*y*/) {};
Image *loadimage(std::string const &filename) { return Image::load(filename); };

// Color functions
void nostroke();
void nofill();

virtual void blendmode(int /*mode*/){};
virtual void blendmode(int /*mode*/) {};
virtual void colormode(int mode)
{
this->stroke_color.colormode(mode);
Expand Down Expand Up @@ -113,7 +113,7 @@ namespace Pyro
};
virtual void background(float r, float g, float b, float a = 1.0);

virtual void shape(Shape /*s*/, float /*x*/, float /*y*/, Unit /*unit*/ = Unit::CURRENT){};
virtual void shape(Shape /*s*/, float /*x*/, float /*y*/, Unit /*unit*/ = Unit::CURRENT) {};

void beginshape(int kind = DEFAULT)
{
Expand Down Expand Up @@ -151,7 +151,7 @@ namespace Pyro

// Primitive shapes
void point(float x, float y, Unit unit = Unit::CURRENT);
virtual void line(float /*x0*/, float /*y0*/, float /*x1*/, float /*y1*/, Unit /*unit*/ = Unit::CURRENT){};
virtual void line(float /*x0*/, float /*y0*/, float /*x1*/, float /*y1*/, Unit /*unit*/ = Unit::CURRENT) {};
void curve(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3, Unit unit = Unit::CURRENT);
void bezier(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3, Unit unit = Unit::CURRENT);

Expand Down Expand Up @@ -185,9 +185,17 @@ namespace Pyro
void text(std::string const &text, float x, float y, Unit unit = Unit::CURRENT);
void textfont(Font *font);

virtual void textfont_impl(Font * /*font*/){};
virtual void text_impl(std::string /*text*/, float /*x*/, float /*y*/, Unit /*unit*/ = Unit::CURRENT){};
virtual void textfont_impl(Font * /*font*/) {};
virtual void text_impl(std::string /*text*/, float /*x*/, float /*y*/, Unit /*unit*/ = Unit::CURRENT) {};
};
/**
* Create a graphics object
*
* @param width The width of the canvas
* @param height The height of the canvas
*/
Graphics *creategraphics(unsigned int width, unsigned int height, GraphicsMode mode = GraphicsMode::CAIRO);

};

#endif
1 change: 1 addition & 0 deletions include/pyro/graphics_cairo.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Pyro
public:
GraphicsCairo(unsigned int width, unsigned int height, unsigned int channels, unsigned int dpi, Unit unit);
~GraphicsCairo() override;
void init() override;

void blendmode(int mode) override;

Expand Down
12 changes: 8 additions & 4 deletions include/pyro/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace Pyro
BILINEAR
};


class Image
{
private:
Expand All @@ -38,12 +37,15 @@ namespace Pyro
unsigned int mode);

protected:
unsigned int _width{0};
unsigned int _height{0};
unsigned int _pixelwidth{0};
unsigned int _pixelheight{0};
unsigned int density{1};

bool modified{false};
int mx1{0}, my1{0}, mx2{0}, my2{0};
bool initialized{false};

public:
unsigned int format{ARGB};
Expand All @@ -55,12 +57,11 @@ namespace Pyro
Image(const Image &in);
Image();
Image(unsigned int width, unsigned int height, unsigned int format = RGB, unsigned int factor = 1, unsigned int dpi = 72, Unit unit = Unit::PX);
void init(unsigned int width, unsigned int height, unsigned int format = RGB, unsigned int factor = 1, unsigned int dpi = 72, Unit unit = Unit::PX);
virtual void init();

Image &operator=(const Image &in);
virtual ~Image();

static Image *create(unsigned int width, unsigned int height);
static Image *load(const std::string &filename);
static Image *loadPNG(const std::string &filename);
static Image *loadJPEG(const std::string &filename);
Expand All @@ -72,6 +73,9 @@ namespace Pyro
void savePNG(const std::string &filename, unsigned int dpi);

unsigned int get_dpi() { return this->dpi; }
void set_dpi(int dpi);

void set_unit(Unit unit);

// Pixel access
void *get_data() const { return this->data; };
Expand Down Expand Up @@ -104,6 +108,6 @@ namespace Pyro
Image *rotate_bilinear(double angle);
};

Image *createimage(unsigned int width, unsigned int height, int format = RGB, unsigned int dpi = 72, Unit unit = Unit::PX);
Image *createimage(unsigned int width, unsigned int height, int format = RGB);
}
#endif
21 changes: 0 additions & 21 deletions include/pyro/pyro.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,6 @@ namespace Pyro
extern unsigned int height;
extern uint32_t *pixels;

/**
* Create a graphics object
*
* @param width The width of the canvas
* @param height The height of the canvas
*/
inline Graphics *create(unsigned int width, unsigned int height)
{
return Graphics::create(width, height);
}

inline Graphics *create(unsigned int width, unsigned int height, GraphicsMode mode)
{
return Graphics::create(width, height, mode);
}

inline Graphics *create(unsigned int width, unsigned int height, Unit unit = Unit::PX, unsigned int dpi = 72)
{
return Graphics::create(width, height, GraphicsMode::CAIRO, dpi, unit);
}

// Initialize the library with this.
void size(unsigned int width, unsigned int height, Unit unit = Unit::PX, unsigned int dpi = 72);

Expand Down
4 changes: 2 additions & 2 deletions screenshot-tests/test-colors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SCENARIO("A color can be specified with various types")
{
SECTION("Fill can be set with ints")
{
Pyro::Graphics *p = Pyro::Graphics::create(1024, 512, testmode);
Pyro::Graphics *p = Pyro::creategraphics(1024, 512, testmode);
std::string filename = "fill_with_ints.png";
p->nostroke();
for (int i = 0; i < 256; i++)
Expand All @@ -29,7 +29,7 @@ SCENARIO("A color can be specified with various types")
}
SECTION("Fill can be set with floas")
{
Pyro::Graphics *p = Pyro::Graphics::create(1024, 512, testmode);
Pyro::Graphics *p = Pyro::creategraphics(1024, 512, testmode);
std::string filename = "fill_with_floats.png";
p->nostroke();
for (int i = 0; i < 256; i++)
Expand Down
2 changes: 1 addition & 1 deletion screenshot-tests/test-image-composition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TEST_CASE("images can be composited")
{
std::string filename = "image_a_over_b_with_alpha.png";
Pyro::Image *a = Pyro::Image::load(actual_folder + "test_image.png");
Pyro::Image *b = Pyro::Image::create(a->width(), a->height());
Pyro::Image *b = Pyro::createimage(a->width(), a->height(), Pyro::ARGB);

unsigned char *data = b->load_bytes();
for (uint32_t y = 0; y < b->height(); y++)
Expand Down
10 changes: 5 additions & 5 deletions screenshot-tests/test-image-placements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TEST_CASE("Place image full screen")
return;
}

Pyro::Graphics *p = Pyro::Graphics::create(img->width(), img->height(), testmode);
Pyro::Graphics *p = Pyro::creategraphics(img->width(), img->height(), testmode);
p->image(img, 0, 0);

p->save(current_folder + filename);
Expand All @@ -35,7 +35,7 @@ TEST_CASE("Place image 50% at center rotated 45 degrees")
return;
}

Pyro::Graphics *p = Pyro::Graphics::create(img->width(), img->height(), testmode);
Pyro::Graphics *p = Pyro::creategraphics(img->width(), img->height(), testmode);
p->imagemode(Pyro::CENTER);
p->translate(p->width() / 2.0f, p->height() / 2.0f);
p->rotate(Pyro::QUARTER_PI);
Expand All @@ -59,7 +59,7 @@ TEST_CASE("Place images with different channel count")
Pyro::Image *img = Pyro::Image::load(actual_folder + "test_image.png");
REQUIRE(img->channels() == 4);

Pyro::Graphics *p = Pyro::create(img->width(), img->height(), testmode);
Pyro::Graphics *p = Pyro::creategraphics(img->width(), img->height(), testmode);
p->imagemode(Pyro::CORNER);
p->image(img, 0, 0);
p->save(current_folder + filename);
Expand All @@ -75,7 +75,7 @@ TEST_CASE("Place images with different channel count")
Pyro::Image *img = Pyro::Image::load(actual_folder + "test_image.png")->convert(Pyro::RGB);
REQUIRE(img->channels() == 3);

Pyro::Graphics *p = Pyro::create(img->width(), img->height(), testmode);
Pyro::Graphics *p = Pyro::creategraphics(img->width(), img->height(), testmode);
p->imagemode(Pyro::CORNER);
p->image(img, 0, 0);
p->save(current_folder + filename);
Expand All @@ -91,7 +91,7 @@ TEST_CASE("Place images with different channel count")
Pyro::Image *img = Pyro::Image::load(actual_folder + "test_image.png")->convert(Pyro::GRAY);
REQUIRE(img->channels() == 1);

Pyro::Graphics *p = Pyro::create(img->width(), img->height(), testmode);
Pyro::Graphics *p = Pyro::creategraphics(img->width(), img->height(), testmode);
p->imagemode(Pyro::CORNER);
p->image(img, 0, 0);
p->save(current_folder + filename);
Expand Down
12 changes: 6 additions & 6 deletions screenshot-tests/test-lines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

TEST_CASE("Draw line", "[strokes]")
{
Pyro::Graphics *p = Pyro::Graphics::create(100, 100, testmode);
Pyro::Graphics *p = Pyro::creategraphics(100, 100, testmode);

SECTION("Stroke weight")
{
Expand Down Expand Up @@ -43,7 +43,7 @@ TEST_CASE("Draw line", "[strokes]")

SECTION("Stroke join")
{
Pyro::Graphics *p = Pyro::Graphics::create(100, 300, testmode);
Pyro::Graphics *p = Pyro::creategraphics(100, 300, testmode);

std::string filename = "stroke_join.png";
p->background(192);
Expand Down Expand Up @@ -90,7 +90,7 @@ TEST_CASE("Test curve", "[shapes]")
{
SECTION("Draw with curvevertex()")
{
Pyro::Graphics *p = Pyro::Graphics::create(100, 100, testmode);
Pyro::Graphics *p = Pyro::creategraphics(100, 100, testmode);
std::string filename = "shape_curve_vertex.png";
p->background(192);
p->nofill();
Expand All @@ -109,7 +109,7 @@ TEST_CASE("Test curve", "[shapes]")

SECTION("Draw with curve()")
{
Pyro::Graphics *p = Pyro::Graphics::create(100, 100, testmode);
Pyro::Graphics *p = Pyro::creategraphics(100, 100, testmode);
std::string filename = "shape_curve.png";
p->background(192);
p->nofill();
Expand All @@ -131,7 +131,7 @@ TEST_CASE("Bezier curve", "[shapes]")
std::string filename = "";
SECTION("Draw with beziervertex()")
{
Pyro::Graphics *p = Pyro::Graphics::create(100, 100, testmode);
Pyro::Graphics *p = Pyro::creategraphics(100, 100, testmode);
std::string filename = "shape_curve_bezier_vertex.png";
p->background(192);
p->nofill();
Expand All @@ -146,7 +146,7 @@ TEST_CASE("Bezier curve", "[shapes]")

SECTION("Draw with bezier()")
{
Pyro::Graphics *p = Pyro::Graphics::create(100, 200, testmode);
Pyro::Graphics *p = Pyro::creategraphics(100, 200, testmode);
std::string filename = "shape_curve_bezier.png";
p->background(192);
p->nofill();
Expand Down
2 changes: 1 addition & 1 deletion screenshot-tests/test-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
TEST_CASE("Create test image")
{
std::string filename = "test_image.png";
Pyro::Graphics *p = Pyro::create(128, 128, testmode);
Pyro::Graphics *p = Pyro::creategraphics(128, 128, testmode);

p->nostroke();

Expand Down
4 changes: 2 additions & 2 deletions screenshot-tests/test-noise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
TEST_CASE("Test noise")
{
std::string filename = "noise_2d.png";
Pyro::Image *img = Pyro::Image::create(128, 128);
Pyro::Image *img = Pyro::createimage(128, 128, Pyro::ARGB);
unsigned int *pixels = img->load_pixels();

for (uint32_t y = 0; y < 128; y++)
Expand All @@ -16,7 +16,7 @@ TEST_CASE("Test noise")
Pyro::noisedetail(1, 1.0);
double v = Pyro::noise(x * 0.1, y * 0.1);
REQUIRE(v >= 0.0);
pixels[y * 128 + x] = 0xff000000 | (uint8_t)((v)*255);
pixels[y * 128 + x] = 0xff000000 | (uint8_t)((v) * 255);
}
}

Expand Down
10 changes: 8 additions & 2 deletions screenshot-tests/test-resolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ TEST_CASE("Test varied DPI")
{
SECTION("Check result matching")
{
Pyro::Graphics *pa = Pyro::Graphics::create(508, 254, testmode, 100, Pyro::Unit::MM);
Pyro::Graphics *pa = Pyro::creategraphics(508, 254, testmode);
pa->set_dpi(100);
pa->set_unit(Pyro::Unit::MM);
pa->init();

REQUIRE(pa->width() == 2000);
REQUIRE(pa->height() == 1000);

Pyro::Graphics *pb = Pyro::Graphics::create(508, 254, testmode, 200, Pyro::Unit::MM);
Pyro::Graphics *pb = Pyro::creategraphics(508, 254, testmode);
pb->set_dpi(200);
pb->set_unit(Pyro::Unit::MM);
pb->init();

REQUIRE(pb->width() == 4000);
REQUIRE(pb->height() == 2000);
Expand Down
10 changes: 5 additions & 5 deletions screenshot-tests/test-shapes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ TEST_CASE("Test Rect", "[shapes]")

SECTION("Corner drawing")
{
Pyro::Graphics *p = Pyro::Graphics::create(250, 250, testmode);
Pyro::Graphics *p = Pyro::creategraphics(250, 250, testmode);
filename = "shape_rect_corner.png";
p->nofill();
p->stroke(0.0f, 1.0f);
Expand All @@ -33,7 +33,7 @@ TEST_CASE("Test Rect", "[shapes]")

SECTION("Center drawing")
{
Pyro::Graphics *p = Pyro::Graphics::create(250, 250, testmode);
Pyro::Graphics *p = Pyro::creategraphics(250, 250, testmode);
std::string filename = "shape_rect_center.png";
p->rectmode(Pyro::CENTER);
p->nofill();
Expand All @@ -60,7 +60,7 @@ TEST_CASE("Test Rect", "[shapes]")

TEST_CASE("Draw shapes", "[shapes]")
{
Pyro::Graphics *p = Pyro::Graphics::create(128, 128, testmode);
Pyro::Graphics *p = Pyro::creategraphics(128, 128, testmode);
SECTION("Draw triangle")
{
std::string filename = "shape_triangle.png";
Expand Down Expand Up @@ -99,7 +99,7 @@ TEST_CASE("Draw shapes", "[shapes]")

TEST_CASE("Variable side count ellipses", "[shapes]")
{
Pyro::Graphics *p = Pyro::Graphics::create(512, 512, testmode);
Pyro::Graphics *p = Pyro::creategraphics(512, 512, testmode);
SECTION("Draw ellipses with n siders")
{
std::string filename = "shape_n_ellipses.png";
Expand All @@ -126,7 +126,7 @@ TEST_CASE("Variable side count ellipses", "[shapes]")

TEST_CASE("Complex shapes", "[shapes]")
{
Pyro::Graphics *p = Pyro::Graphics::create(100, 100, testmode);
Pyro::Graphics *p = Pyro::creategraphics(100, 100, testmode);
SECTION("Draw shape with hole")
{
std::string filename = "shape_multi_contour.png";
Expand Down
2 changes: 1 addition & 1 deletion screenshot-tests/test-typography.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TEST_CASE("Write text")
std::string filename = "";
SECTION("Simple strings")
{
Pyro::Graphics *p = Pyro::Graphics::create(100, 100, testmode);
Pyro::Graphics *p = Pyro::creategraphics(100, 100, testmode);
Pyro::Font *fnt = Pyro::create_font("./fonts/Orkney Bold.otf", 12);

filename = "typography_simple_string.png";
Expand Down
Loading
Loading