Pinstripe/Printer: Difference between revisions
Content added Content deleted
m (→{{header|Nim}}) |
(Added Wren) |
||
Line 354: | Line 354: | ||
# Explicit exit; no GUI desired |
# Explicit exit; no GUI desired |
||
exit</lang> |
exit</lang> |
||
=={{header|Wren}}== |
|||
{{trans|Go}} |
|||
{{libheader|DOME}} |
|||
<br> |
|||
As DOME doesn't have a method to print an image to a printer, we first need to create a small plug-in in C to add this functionality. |
|||
<lang c>/* gcc -O3 -std=c11 -shared -o printer.so -fPIC -I./include printer.c */ |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include "dome.h" |
|||
static DOME_API_v0* core; |
|||
static WREN_API_v0* wren; |
|||
static const char* source = "" |
|||
"class Printer {\n" |
|||
"foreign static printFile(name) \n" |
|||
"} \n"; |
|||
void C_printFile(WrenVM* vm) { |
|||
const char *arg = wren->getSlotString(vm, 1); |
|||
char command[strlen(arg) + 4]; |
|||
strcpy(command, "lp "); |
|||
strcat(command, arg); |
|||
int res = system(command); |
|||
} |
|||
DOME_EXPORT DOME_Result PLUGIN_onInit(DOME_getAPIFunction DOME_getAPI, DOME_Context ctx) { |
|||
core = DOME_getAPI(API_DOME, DOME_API_VERSION); |
|||
wren = DOME_getAPI(API_WREN, WREN_API_VERSION); |
|||
core->registerModule(ctx, "printer", source); |
|||
core->registerClass(ctx, "printer", "Printer", NULL, NULL); |
|||
core->registerFn(ctx, "printer", "static Printer.printFile(_)", C_printFile); |
|||
return DOME_RESULT_SUCCESS; |
|||
} |
|||
DOME_EXPORT DOME_Result PLUGIN_preUpdate(DOME_Context ctx) { |
|||
return DOME_RESULT_SUCCESS; |
|||
} |
|||
DOME_EXPORT DOME_Result PLUGIN_postUpdate(DOME_Context ctx) { |
|||
return DOME_RESULT_SUCCESS; |
|||
} |
|||
DOME_EXPORT DOME_Result PLUGIN_preDraw(DOME_Context ctx) { |
|||
return DOME_RESULT_SUCCESS; |
|||
} |
|||
DOME_EXPORT DOME_Result PLUGIN_postDraw(DOME_Context ctx) { |
|||
return DOME_RESULT_SUCCESS; |
|||
} |
|||
DOME_EXPORT DOME_Result PLUGIN_onShutdown(DOME_Context ctx) { |
|||
return DOME_RESULT_SUCCESS; |
|||
}</lang> |
|||
This assumes that the ''dome.h'' header file is copied to an ''include'' sub-directory of the current one and that the resulting ''printer.so'' shared library file is created in the latter. |
|||
We can now use this plug-in in the following script which draws the image to the canvas, copies it to an ''ImageData'' object, saves it as a ''.png'' file and prints it to the default printer. |
|||
<lang ecmascript>import "graphics" for Canvas, Color, ImageData |
|||
import "dome" for Window |
|||
import "plugin" for Plugin |
|||
Plugin.load("printer") |
|||
import "printer" for Printer |
|||
class Main { |
|||
construct new() { |
|||
Window.title = "Pinstripe - printer" |
|||
_width = 842 |
|||
_height = 595 |
|||
Canvas.resize(_width, _height) |
|||
Window.resize(_width, _height) |
|||
var colors = [ |
|||
Color.hex("FFFFFF"), // white |
|||
Color.hex("000000") // black |
|||
] |
|||
pinstripe(colors) |
|||
} |
|||
pinstripe(colors) { |
|||
var w = _width |
|||
var h = (_height/7).floor |
|||
for (b in 1..11) { |
|||
var x = 0 |
|||
var ci = 0 |
|||
while (x < w) { |
|||
var y = h * (b - 1) |
|||
Canvas.rectfill(x, y, b, h, colors[ci%2]) |
|||
x = x + b |
|||
ci = ci + 1 |
|||
} |
|||
} |
|||
} |
|||
init() { |
|||
var img = ImageData.create("pinstripe", _width, _height) |
|||
for (x in 0..._width) { |
|||
for (y in 0..._height) img.pset(x, y, Canvas.pget(x, y)) |
|||
} |
|||
img.saveToFile("pinstripe.png") |
|||
Printer.printFile("pinstripe.png") |
|||
} |
|||
update() {} |
|||
draw(alpha) {} |
|||
} |
|||
var Game = Main.new()</lang> |
|||
{{omit from|DWScript}} |
{{omit from|DWScript}} |