use of playn.core.SurfaceImage in project playn by threerings.
the class SubImageTest method init.
@Override
public void init() {
// create a canvas image and draw subimages of that
int r = 30;
CanvasImage cimg = graphics().createImage(2 * r, 2 * r);
Canvas canvas = cimg.canvas();
canvas.setFillColor(0xFF99CCFF);
canvas.fillCircle(r, r, r);
fragment("CanvasImage", cimg, 250, 160);
// draw subimages of a simple static image
Image orange = assets().getImage("images/orange.png");
orange.addCallback(new Callback<Image>() {
public void onSuccess(Image orange) {
fragment("Image", orange, 250, 10);
float pw = orange.width(), ph = orange.height(), phw = pw / 2, phh = ph / 2;
final Image.Region orangerep = orange.subImage(0, phh / 2, pw, phh);
orangerep.setRepeat(true, true);
// tile a sub-image, oh my!
ImageLayer tiled = graphics().createImageLayer(orangerep);
tiled.setSize(100, 100);
addTest(10, 10, tiled, "ImageLayer tiled with subimage of Image");
// use a subimage as a fill pattern
CanvasImage pat = graphics().createImage(100, 100);
pat.canvas().setFillPattern(orangerep.toPattern());
pat.canvas().fillRect(0, 0, 100, 100);
addTest(10, 160, graphics().createImageLayer(pat), "Canvas filled with subimage");
// tile a sub-image of a surface image, oh my!
SurfaceImage surf = graphics().createSurface(orange.width(), orange.height());
surf.surface().drawImage(orange, 0, 0);
Image.Region surfrep = surf.subImage(0, phh / 2, pw, phh);
surfrep.setRepeat(true, true);
ImageLayer surftiled = graphics().createImageLayer(surfrep);
surftiled.setSize(100, 100);
addTest(10, 300, surftiled, "ImageLayer tiled with subimage of SurfaceImage");
// draw a subimage to a canvas
CanvasImage split = graphics().createImage(orange.width(), orange.height());
split.canvas().drawImage(orange.subImage(0, 0, phw, phh), phw, phh);
split.canvas().drawImage(orange.subImage(phw, 0, phw, phh), 0, phh);
split.canvas().drawImage(orange.subImage(0, phh, phw, phh), phw, 0);
split.canvas().drawImage(orange.subImage(phw, phh, phw, phh), 0, 0);
addTest(140, 10, graphics().createImageLayer(split), "draw subimg into Canvas", 80);
// draw a subimage in an immediate layer
final Image.Region orangemid = orange.subImage(0, phh / 2, pw, phh);
ImmediateLayer imm = graphics().createImmediateLayer(new ImmediateLayer.Renderer() {
public void render(Surface surf) {
surf.drawImage(orangemid, 0, 0);
surf.drawImage(orangemid, orangemid.width(), 0);
surf.drawImage(orangemid, 0, orangemid.height());
surf.drawImage(orangemid, orangemid.width(), orangemid.height());
}
});
addTest(130, 100, imm, 2 * orangemid.width(), 2 * orangemid.height(), "draw subimg into Surface", 100);
// draw a subimage whose bounds oscillate
osci = orange.subImage(0, 0, orange.width(), orange.height());
addTest(130, 190, graphics().createImageLayer(osci), "ImageLayer with subimage with changing width", 100);
}
public void onFailure(Throwable err) {
log().warn("Failed to load orange image", err);
}
});
}
use of playn.core.SurfaceImage in project playn by threerings.
the class AlphaLayerTest method init.
@Override
public void init() {
final GroupLayer rootLayer = graphics().rootLayer();
final float fullWidth = 6 * width, fullHeight = 3 * height;
// add a half white, half blue background
SurfaceImage bg = graphics().createSurface(fullWidth, fullHeight);
bg.surface().setFillColor(Color.rgb(255, 255, 255));
bg.surface().fillRect(0, 0, fullWidth, fullHeight);
bg.surface().setFillColor(Color.rgb(0, 0, 255));
bg.surface().fillRect(0, 2 * height, fullWidth, height);
rootLayer.add(graphics().createImageLayer(bg));
addDescrip("all layers contained in group layer with a=0.5\n" + "thus, fully composited a=0.25", offset, fullHeight + 5, fullWidth);
// add a 50% transparent group layer
final GroupLayer groupLayer = graphics().createGroupLayer();
groupLayer.setAlpha(0.5f);
rootLayer.add(groupLayer);
assets().getImage("images/alphalayertest.png").addCallback(new Callback<Image>() {
@Override
public void onSuccess(Image image) {
// add the layers over the white background
float x = offset;
groupLayer.addAt(graphics().createImageLayer(image).setAlpha(0.5f), x, offset);
addDescrip("image\nimg layer a=0.5", x, offset + height, width);
x += width;
SurfaceImage surf1 = graphics().createSurface(image.width(), image.height());
surf1.surface().setAlpha(0.5f).drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(surf1), x, offset);
addDescrip("surface a=0.5\nimg layer a=1", x, offset + height, width);
x += width;
SurfaceImage surf2 = graphics().createSurface(image.width(), image.height());
surf2.surface().drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(surf2).setAlpha(0.5f), x, offset);
addDescrip("surface a=1\nimg layer a=0.5", x, offset + height, width);
x += width;
CanvasImage canvas1 = graphics().createImage(image.width(), image.height());
canvas1.canvas().drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(canvas1).setAlpha(0.5f), x, offset);
addDescrip("canvas a=1\nimg layer a=0.5", x, offset + height, width);
x += width;
CanvasImage canvas2 = graphics().createImage(image.width(), image.height());
canvas2.canvas().setAlpha(0.5f).drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(canvas2), x, offset);
addDescrip("canvas a=0.5\nimg layer a=1", x, offset + height, width);
x += width;
// add the same layers over the blue background
x = offset;
groupLayer.addAt(graphics().createImageLayer(image).setAlpha(0.5f), x, offset + 2 * height);
x += width;
SurfaceImage surf1b = graphics().createSurface(image.width(), image.height());
surf1b.surface().setAlpha(0.5f).drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(surf1b), x, offset + 2 * height);
x += width;
SurfaceImage surf2b = graphics().createSurface(image.width(), image.height());
surf2b.surface().drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(surf2b).setAlpha(0.5f), x, offset + 2 * height);
x += width;
CanvasImage canvas1b = graphics().createImage(image.width(), image.height());
canvas1b.canvas().drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(canvas1b).setAlpha(0.5f), x, offset + 2 * height);
x += width;
CanvasImage canvas2b = graphics().createImage(image.width(), image.height());
canvas2b.canvas().setAlpha(0.5f).drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(canvas2b), x, offset + 2 * height);
// add some copies of the image at 1, 0.5, 0.25 and 0.125 alpha
x = offset + width;
for (float alpha : new float[] { 1, 1 / 2f, 1 / 4f, 1 / 8f }) {
float y = fullHeight + 50;
rootLayer.addAt(graphics().createImageLayer(image).setAlpha(alpha), x, y);
addDescrip("image a=" + alpha, x, y + height / 2, width / 2);
x += width;
}
}
@Override
public void onFailure(Throwable err) {
log().error("Error loading image", err);
}
});
// add ground truth of 25% opaque image
assets().getImage("images/alphalayertest_expected.png").addCallback(new Callback<Image>() {
@Override
public void onSuccess(Image image) {
rootLayer.addAt(graphics().createImageLayer(image), 5 * width, 0);
addDescrip("ground truth", 5 * width, offset + height, width);
}
@Override
public void onFailure(Throwable err) {
log().error("Error loading image", err);
}
});
}
use of playn.core.SurfaceImage in project playn by threerings.
the class ClearBackgroundTest method init.
@Override
public void init() {
// remove the background layer added by default
graphics().rootLayer().removeAll();
// add a grey square
SurfaceImage surf = graphics().createSurface(width, height);
surf.surface().setFillColor(Color.rgb(200, 200, 200));
surf.surface().fillRect(0, 0, width, height);
square = graphics().createImageLayer(surf);
graphics().rootLayer().add(square);
}
use of playn.core.SurfaceImage in project playn by threerings.
the class SurfaceDrawLayerTest method addTests.
protected void addTests(Image orange) {
float owidth = orange.width(), oheight = orange.height();
float twidth = 3 * owidth, theight = oheight;
// create a small subgraph of a couple of image layers
final GroupLayer group = graphics().createGroupLayer();
group.addAt(graphics().createImageLayer(orange), owidth / 2, 0);
rotator = graphics().createImageLayer(orange);
group.addAt(rotator.setOrigin(owidth / 2, oheight / 2), 2 * owidth, oheight / 2);
rotator.setRotation(FloatMath.PI / 2);
// add the subgraph to the scene directly
final float gap = 25;
float x = gap, y = gap;
graphics().rootLayer().addAt(group, x, y);
addDescrip("Added to rootLayer", x, y + theight + gap, twidth);
x += twidth + gap;
// render the subgraph in an immediate layer, with a few variants
graphics().rootLayer().addAt(graphics().createImmediateLayer(new ImmediateLayer.Renderer() {
public void render(Surface surf) {
// since the group layer has a +gap+gap offset, we adjust for that here
surf.translate(-gap, -gap).drawLayer(group);
}
}), x, y);
addDescrip("ImmediateLayer drawLayer", x, y + theight + gap, twidth);
x += twidth + gap;
graphics().rootLayer().addAt(graphics().createImmediateLayer(new ImmediateLayer.Renderer() {
public void render(Surface surf) {
surf.translate(-gap, -gap).drawLayer(group);
}
}).setTint(0xFFDD0000), x, y);
addDescrip("ImmediateLayer drawLayer (with tint)", x, y + theight + gap, twidth);
x += twidth + gap;
graphics().rootLayer().addAt(graphics().createImmediateLayer(new ImmediateLayer.Renderer() {
public void render(Surface surf) {
surf.translate(-gap, -gap).drawLayer(group);
}
}).setShader(createSepiaShader()), x, y);
addDescrip("ImmediateLayer drawLayer (with sepia shader)", x, y + theight + gap, twidth);
x += twidth + gap;
// lastly create a surface image and render the layer thereinto; this one won't animate
SurfaceImage image = graphics().createSurface(twidth, theight);
image.surface().translate(-gap, -gap).drawLayer(group);
graphics().rootLayer().addAt(graphics().createImageLayer(image), x, y);
addDescrip("SurfaceImage drawLayer (won't animate)", x, y + theight + gap, twidth);
x += twidth + gap;
}
use of playn.core.SurfaceImage in project playn by threerings.
the class SurfaceTest method addTests.
protected void addTests(final Image orange, Image tile) {
final Pattern pattern = tile.toPattern();
// make samples big enough to force a buffer size increase
final int samples = 128, hsamples = samples / 2;
final float[] verts = new float[(samples + 1) * 4];
final int[] indices = new int[samples * 6];
tessellateCurve(0, 40 * (float) Math.PI, verts, indices, new F() {
public float apply(float x) {
return (float) Math.sin(x / 20) * 50;
}
});
float ygap = 20, ypos = 10;
// draw some wide lines
ypos = ygap + addTest(10, ypos, new ImmediateLayer.Renderer() {
public void render(Surface surf) {
drawLine(surf, 0, 0, 50, 50, 15);
drawLine(surf, 70, 50, 120, 0, 10);
drawLine(surf, 0, 70, 120, 120, 10);
}
}, 120, 120, "drawLine with width");
ypos = ygap + addTest(20, ypos, new ImmediateLayer.Renderer() {
public void render(Surface surf) {
surf.setFillColor(0xFF0000FF).fillRect(0, 0, 100, 25);
// these two alpha fills should look the same
surf.setFillColor(0x80FF0000).fillRect(0, 0, 50, 25);
surf.setAlpha(0.5f).setFillColor(0xFFFF0000).fillRect(50, 0, 50, 25).setAlpha(1f);
}
}, 100, 25, "left and right half both same color");
ypos = ygap + addTest(20, ypos, new ImmediateLayer.Renderer() {
public void render(Surface surf) {
surf.setFillColor(0xFF0000FF).fillRect(0, 0, 100, 50);
surf.setAlpha(0.5f);
surf.drawImage(orange, 55, 5);
surf.fillRect(0, 50, 50, 50);
surf.drawImage(orange, 55, 55);
surf.setAlpha(1f);
}
}, 100, 100, "fillRect and drawImage at 50% alpha");
ypos = 10;
ypos = ygap + addTest(160, ypos, new ImmediateLayer.Renderer() {
public void render(Surface surf) {
// fill some shapes with patterns
surf.setFillPattern(pattern).fillRect(10, 0, 100, 100);
// use same fill pattern for the triangles
surf.translate(0, 160);
// render a sliding window of half of our triangles to test the slice rendering
surf.fillTriangles(verts, offset * 4, (hsamples + 1) * 4, indices, offset * 6, hsamples * 6, offset * 2);
offset += doff;
if (offset == 0)
doff = 1;
else if (offset == hsamples)
doff = -1;
}
private int offset = 0, doff = 1;
}, 120, 210, "ImmediateLayer patterned fillRect, fillTriangles");
SurfaceImage patted = graphics().createSurface(100, 100);
patted.surface().setFillPattern(pattern).fillRect(0, 0, 100, 100);
ypos = ygap + addTest(170, ypos, graphics().createImageLayer(patted), "SurfaceImage patterned fillRect");
ypos = 10;
// fill a patterned quad in a clipped group layer
final int twidth = 150, theight = 75;
GroupLayer group = graphics().createGroupLayer();
ypos = ygap + addTest(315, 10, group, twidth, theight, "Clipped pattern should not exceed grey rectangle");
group.add(graphics().createImmediateLayer(new ImmediateLayer.Renderer() {
public void render(Surface surf) {
surf.setFillColor(0xFFCCCCCC).fillRect(0, 0, twidth, theight);
}
}));
group.add(graphics().createImmediateLayer(twidth, theight, new ImmediateLayer.Renderer() {
public void render(Surface surf) {
surf.setFillPattern(pattern).fillRect(-10, -10, twidth + 20, theight + 20);
}
}));
// draw some randomly jiggling dots inside a bounded region
dotBox = new Rectangle(315, ypos, 200, 100);
ypos = ygap + addTest(dotBox.x, dotBox.y, new ImmediateLayer.Renderer() {
public void render(Surface surf) {
surf.setFillColor(0xFFCCCCCC).fillRect(0, 0, dotBox.width, dotBox.height);
}
}, dotBox.width, dotBox.height, "Randomly positioned SurfaceImages");
for (int ii = 0; ii < 10; ii++) {
SurfaceImage dot = graphics().createSurface(10, 10);
dot.surface().setFillColor(0xFFFF0000);
dot.surface().fillRect(0, 0, 5, 5);
dot.surface().fillRect(5, 5, 5, 5);
dot.surface().setFillColor(0xFF0000FF);
dot.surface().fillRect(5, 0, 5, 5);
dot.surface().fillRect(0, 5, 5, 5);
ImageLayer dotl = graphics().createImageLayer(dot);
dotl.setTranslation(dotBox.x + random() * (dotBox.width - 10), dotBox.y + random() * (dotBox.height - 10));
dots.add(dotl);
// System.err.println("Created dot at " + dotl.transform());
graphics().rootLayer().add(dotl);
}
// add a surface layer that is updated on every call to paint (a bad practice, but one that
// should actually work)
paintUpped = graphics().createSurface(100, 100);
ypos = ygap + addTest(315, ypos, graphics().createImageLayer(paintUpped), "SurfaceImage updated in paint()");
}
Aggregations