2015-03-10 01:21:45 +01:00
|
|
|
#include <cairo/cairo.h>
|
|
|
|
|
2017-10-12 22:36:56 +02:00
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2015-03-10 01:21:45 +01:00
|
|
|
|
2017-10-12 22:36:56 +02:00
|
|
|
class Turtle final {
|
2015-03-10 01:21:45 +01:00
|
|
|
public:
|
2017-10-12 22:36:56 +02:00
|
|
|
Turtle(int sizeX, int sizeY) {
|
|
|
|
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, sizeX, sizeY);
|
|
|
|
context = cairo_create(surface);
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
~Turtle() {
|
|
|
|
cairo_destroy(context);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
setColor(1, 1, 1);
|
|
|
|
paint();
|
|
|
|
setLineWidth(3);
|
|
|
|
setColor(1, 0, 0);
|
|
|
|
moveTo(0, 0);
|
|
|
|
}
|
|
|
|
|
2019-07-07 09:52:01 +02:00
|
|
|
void setColor(double r, double g, double b) { cairo_set_source_rgb(context, r, g, b); }
|
2017-10-12 22:36:56 +02:00
|
|
|
|
2019-07-07 09:52:01 +02:00
|
|
|
void setLineWidth(double size) { cairo_set_line_width(context, size); }
|
2017-10-12 22:36:56 +02:00
|
|
|
|
2019-07-07 09:52:01 +02:00
|
|
|
void paint() { cairo_paint(context); }
|
2017-10-12 22:36:56 +02:00
|
|
|
|
2019-07-07 09:52:01 +02:00
|
|
|
void moveTo(double x, double y) { cairo_move_to(context, x, y); }
|
2017-10-12 22:36:56 +02:00
|
|
|
|
|
|
|
void rectangleTo(double width, double height) {
|
|
|
|
double baseX;
|
|
|
|
double baseY;
|
|
|
|
|
|
|
|
cairo_get_current_point(context, &baseX, &baseY);
|
|
|
|
cairo_rectangle(context, baseX, baseY, width, height);
|
|
|
|
cairo_fill(context);
|
|
|
|
|
|
|
|
moveTo(baseX, baseY);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lineTo(double x, double y) {
|
|
|
|
cairo_line_to(context, x, y);
|
|
|
|
cairo_stroke(context);
|
|
|
|
moveTo(x, y);
|
|
|
|
}
|
|
|
|
|
2019-07-07 09:52:01 +02:00
|
|
|
void save(char const* filename) { cairo_surface_write_to_png(surface, filename); }
|
2017-10-12 22:36:56 +02:00
|
|
|
|
2019-07-07 09:52:01 +02:00
|
|
|
void save(std::string const& filename) { save(filename.c_str()); }
|
2015-03-10 01:21:45 +01:00
|
|
|
|
|
|
|
private:
|
2019-07-07 09:52:01 +02:00
|
|
|
cairo_surface_t* surface;
|
|
|
|
cairo_t* context;
|
2015-03-10 01:21:45 +01:00
|
|
|
};
|
|
|
|
|
2017-10-12 22:36:56 +02:00
|
|
|
static std::unique_ptr<Turtle> turtle;
|
2015-03-10 01:21:45 +01:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2017-10-12 22:36:56 +02:00
|
|
|
void cofandina(int32_t x, int32_t y) {
|
|
|
|
turtle.reset(new Turtle(x, y));
|
2015-03-10 01:21:45 +01:00
|
|
|
}
|
|
|
|
|
2017-10-12 22:36:56 +02:00
|
|
|
void pulitina(double r, double g, double b) {
|
|
|
|
turtle->setColor(r, g, b);
|
2015-03-10 01:21:45 +01:00
|
|
|
}
|
|
|
|
|
2017-10-12 22:36:56 +02:00
|
|
|
void pastene(double size) {
|
|
|
|
turtle->setLineWidth(size);
|
2015-03-10 01:21:45 +01:00
|
|
|
}
|
|
|
|
|
2017-10-12 22:36:56 +02:00
|
|
|
void muovi(double x, double y) {
|
|
|
|
turtle->moveTo(x, y);
|
2015-03-10 01:21:45 +01:00
|
|
|
}
|
|
|
|
|
2017-10-12 22:36:56 +02:00
|
|
|
void ispettore(double x, double y) {
|
|
|
|
turtle->lineTo(x, y);
|
2015-03-10 01:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void barilotto() {
|
2017-10-12 22:36:56 +02:00
|
|
|
turtle->save("barilotto.png");
|
2015-03-10 01:21:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // extern
|