diff --git a/examples/turtle/.gitignore b/examples/turtle/.gitignore new file mode 100644 index 0000000..959feb0 --- /dev/null +++ b/examples/turtle/.gitignore @@ -0,0 +1,3 @@ +/tartaruga +/turtle.bc +/barilotto.png diff --git a/examples/turtle/Makefile b/examples/turtle/Makefile new file mode 100644 index 0000000..fa8e0f3 --- /dev/null +++ b/examples/turtle/Makefile @@ -0,0 +1,6 @@ +compile: + # Make sure to have mcc somewhere in path + mcc turtle.mm turtle.mc + llc turtle.bc + c++ turtle.s turtle.cpp -I../.. -lcairo -o tartaruga + rm -f turtle.s diff --git a/examples/turtle/turtle.cpp b/examples/turtle/turtle.cpp new file mode 100644 index 0000000..bf7d619 --- /dev/null +++ b/examples/turtle/turtle.cpp @@ -0,0 +1,103 @@ +#include "Runtime.h" + +#include +#include + + +class Turtle { +public: + Turtle(int sizeX, int sizeY) { + surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, sizeX, sizeY); + context = cairo_create(surface); + reset(); + } + + virtual ~Turtle() { + cairo_destroy(context); + cairo_surface_destroy(surface); + } + + void reset() { + setColor(1, 1, 1); + paint(); + setLineWidth(3); + setColor(1, 0, 0); + moveTo(0, 0); + } + + void setColor(double r, double g, double b) { + cairo_set_source_rgb(context, r, g, b); + } + + void setLineWidth(double size) { + cairo_set_line_width(context, size); + } + + void paint() { + cairo_paint(context); + } + + void moveTo(double x, double y) { + cairo_move_to(context, x, y); + } + + 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); + } + + void save(char const* filename) { + cairo_surface_write_to_png(surface, filename); + } + + void save(std::string const& filename) { + save(filename.c_str()); + } + +private: + cairo_surface_t *surface; + cairo_t *context; +}; + +static Turtle *turtle = 0; + +extern "C" { + +void cofandina(Monicelli_Int x, Monicelli_Int y) { + if (turtle != 0) delete turtle; + turtle = new Turtle(x, y); +} + +void pulitina(Monicelli_Double r, Monicelli_Double g, Monicelli_Double b) { + turtle->setColor(r, g, b); +} + +void pastene(Monicelli_Double size) { + turtle->setLineWidth(size); +} + +void muovi(Monicelli_Double x, Monicelli_Double y) { + turtle->moveTo(x, y); +} + +void ispettore(Monicelli_Double x, Monicelli_Double y) { + turtle->lineTo(x, y); +} + +void barilotto() { + turtle->save("barilotto.png"); +} + +} // extern diff --git a/examples/turtle/turtle.mc b/examples/turtle/turtle.mc new file mode 100644 index 0000000..5d450bf --- /dev/null +++ b/examples/turtle/turtle.mc @@ -0,0 +1,5 @@ +Lei ha clacsonato + prematurata la supercazzola cofandina con 200, 200 o scherziamo? + prematurata la supercazzola pastene con 3 o scherziamo? + prematurata la supercazzola ispettore con 200, 200 o scherziamo? + prematurata la supercazzola barilotto o scherziamo? diff --git a/examples/turtle/turtle.mm b/examples/turtle/turtle.mm new file mode 100644 index 0000000..6ebdffd --- /dev/null +++ b/examples/turtle/turtle.mm @@ -0,0 +1,26 @@ +help: + Turtle graphics in Monicelli! + +source: + - Turtle.cpp + +functions: + cofandina: + args: {x: int, y: int} + type: void + help: Crea una superficie di x per y pixel. + pulitina: + args: {r: double, g: double, b: double} + help: Imposta il colore dell'output. + pastene: + args: {size: double} + help: Imposta la dimensione in pixel della penna. + muovi: + args: {x: double, y: double} + help: Porta la penna nel punto specificato. + ispettore: + args: {x: double, y: double} + help: Traccia una linea dal punto corrente al punto specificato. + barilotto: + docs: Salva l'immagine come barilotto.png +