1 module cairo.example; 2 3 import cairo; 4 import std.getopt, std.stdio, core.runtime; 5 version(GTK) 6 import cairo.examples.gtk; 7 8 9 alias DrawFunction = void function(Context ctx); 10 11 enum Output 12 { 13 _auto, 14 gtk, 15 png 16 } 17 18 void runExample(DrawFunction draw) 19 { 20 auto args = Runtime.args; 21 22 Output output = Output._auto; 23 getopt( 24 args, 25 "output", &output); 26 27 final switch(output) 28 { 29 case Output._auto: 30 version(GTK) 31 gtkRunExample(draw); 32 else static if(CAIRO_HAS_PNG_FUNCTIONS) 33 pngRunExample(draw); 34 else 35 writeln("No useable backend compiled in"); 36 break; 37 case Output.gtk: 38 version(GTK) 39 gtkRunExample(draw); 40 else 41 writeln("GTK support not compiled in"); 42 break; 43 case Output.png: 44 static if(CAIRO_HAS_PNG_FUNCTIONS) 45 pngRunExample(draw); 46 else 47 writeln("PNG support not compiled in"); 48 break; 49 } 50 } 51 52 static if(CAIRO_HAS_PNG_FUNCTIONS) 53 { 54 void pngRunExample(DrawFunction draw) 55 { 56 auto surface = new ImageSurface(Format.CAIRO_FORMAT_ARGB32, 400, 400); 57 auto context = Context(surface); 58 draw(context); 59 surface.writeToPNG("example.png"); 60 surface.dispose(); 61 writeln("Wrote result to example.png"); 62 } 63 }