use of playn.core.CanvasImage in project playn by threerings.
the class DepthTest method init.
@Override
public void init() {
GroupLayer rootLayer = graphics().rootLayer();
CanvasImage image = graphics().createImage(250, 25);
image.canvas().drawText(rootLayer.getClass().getName(), 0, 15);
ImageLayer info = graphics().createImageLayer(image);
info.setTranslation(5, 5);
rootLayer.add(info);
int[] depths = { 0, -1, 1, 3, 2, -4, -3, 4, -2 };
int[] fills = { 0xFF99CCFF, 0xFFFFFF33, 0xFF9933FF, 0xFF999999, 0xFFFF0033, 0xFF00CC00, 0xFFFF9900, 0xFF0066FF, 0x0FFCC6666 };
int width = 200, height = 200;
for (int ii = 0; ii < depths.length; ii++) {
int depth = depths[ii];
image = graphics().createImage(width, height);
image.canvas().setFillColor(fills[ii]);
image.canvas().fillRect(0, 0, width, height);
image.canvas().setFillColor(0xFF000000);
image.canvas().drawText(depth + "/" + ii, 5, 15);
ImageLayer layer = graphics().createImageLayer(image);
layer.setDepth(depth);
layer.setTranslation(225 - 50 * depth, 125 + 25 * depth);
rootLayer.add(layer);
}
}
use of playn.core.CanvasImage in project playn by threerings.
the class PauseResumeTest method updateDisplay.
protected void updateDisplay() {
StringBuffer buf = new StringBuffer();
if (notifications.isEmpty()) {
buf.append("No notifications. Pause and resume the game to generate some.");
} else {
buf.append("Notifications:\n");
for (String note : notifications) buf.append(note).append("\n");
}
TextLayout layout = graphics().layoutText(buf.toString(), new TextFormat());
CanvasImage image = graphics().createImage(layout.width(), layout.height());
image.canvas().setFillColor(0xFF000000);
image.canvas().fillText(layout, 0, 0);
layer.setImage(image);
}
use of playn.core.CanvasImage in project playn by threerings.
the class ScaledTextTest method addInfo.
protected void addInfo(CanvasImage image, float cx, float y) {
TextLayout ilayout = graphics().layoutText(image.width() + "x" + image.height(), infoFormat);
CanvasImage iimage = graphics().createImage(ilayout.width(), ilayout.height());
iimage.canvas().setFillColor(0xFF000000).fillText(ilayout, 0, 0);
graphics().rootLayer().addAt(graphics().createImageLayer(iimage), cx - iimage.width() / 2, y);
}
use of playn.core.CanvasImage in project playn by threerings.
the class ScaledTextTest method init.
@Override
public void init() {
String text = "The quick brown fox jumped over the lazy dog.";
TextFormat format = new TextFormat().withFont(graphics().createFont("Helvetica", Font.Style.PLAIN, 18));
TextBlock block = new TextBlock(graphics().layoutText(text, format, new TextWrap(100)));
float x = 5;
for (float scale : new float[] { 1f, 2f, 3f }) {
float swidth = block.bounds.width() * scale, sheight = block.bounds.height() * scale;
CanvasImage image = graphics().createImage(swidth, sheight);
image.canvas().setStrokeColor(0xFFFFCCCC).strokeRect(0, 0, swidth - 0.5f, sheight - 0.5f);
image.canvas().scale(scale, scale);
image.canvas().setFillColor(0xFF000000);
block.fill(image.canvas(), TextBlock.Align.RIGHT, 0, 0);
graphics().rootLayer().addAt(graphics().createImageLayer(image), x, 5);
addInfo(image, x + swidth / 2, sheight + 10);
x += swidth + 5;
}
}
use of playn.core.CanvasImage in project playn by threerings.
the class ShaderTest method init.
protected void init(final Image orange) {
// add the normal orange
float dx = orange.width() + 25;
graphics().rootLayer().addAt(graphics().createImageLayer(orange), 25, 25);
// add a sepia toned orange
ImageLayer olayer = graphics().createImageLayer(orange);
olayer.setShader(createSepiaShader());
graphics().rootLayer().addAt(olayer, 25 + dx, 25);
// create a shader that rotates things around the (3D) y axis
IndexedTrisShader rotShader = new IndexedTrisShader(graphics().ctx()) {
@Override
protected String vertexShader() {
return VERT_UNIFS + "uniform float u_Angle;\n" + "uniform vec2 u_Eye;\n" + VERT_ATTRS + PER_VERT_ATTRS + VERT_VARS + "void main(void) {\n" + // Rotate the vertex per our 3D rotation
" float cosa = cos(u_Angle);\n" + " float sina = sin(u_Angle);\n" + " mat4 rotmat = mat4(\n" + " cosa, 0, sina, 0,\n" + " 0, 1, 0, 0,\n" + " -sina, 0, cosa, 0,\n" + " 0, 0, 0, 1);\n" + " vec4 pos = rotmat * vec4(a_Position - u_Eye, 0, 1);\n" + // Perspective project the vertex back into the plane
" mat4 persp = mat4(\n" + " 1, 0, 0, 0,\n" + " 0, 1, 0, 0,\n" + " 0, 0, 1, -1.0/200.0,\n" + " 0, 0, 0, 1);\n" + " pos = persp * pos;\n" + " pos /= pos.w;\n" + " pos += vec4(u_Eye, 0, 0);\n;" + // Transform the vertex per the normal screen transform
" mat4 transform = mat4(\n" + " a_Matrix[0], a_Matrix[1], 0, 0,\n" + " a_Matrix[2], a_Matrix[3], 0, 0,\n" + " 0, 0, 1, 0,\n" + " a_Translation[0], a_Translation[1], 0, 1);\n" + " pos = transform * pos;\n" + " pos.x /= (u_ScreenSize.x / 2.0);\n" + " pos.y /= (u_ScreenSize.y / 2.0);\n" + " pos.z /= (u_ScreenSize.y / 2.0);\n" + " pos.x -= 1.0;\n" + " pos.y = 1.0 - pos.y;\n" + " gl_Position = pos;\n" + VERT_SETTEX + VERT_SETCOLOR + "}";
}
@Override
protected Core createTextureCore() {
return new RotCore(vertexShader(), textureFragmentShader());
}
class RotCore extends ITCore {
private final Uniform1f uAngle = prog.getUniform1f("u_Angle");
private final Uniform2f uEye = prog.getUniform2f("u_Eye");
public RotCore(String vertShader, String fragShader) {
super(vertShader, fragShader);
}
@Override
public void activate(int fbufWidth, int fbufHeight) {
super.activate(fbufWidth, fbufHeight);
uAngle.bind(elapsed * FloatMath.PI);
uEye.bind(0, orange.height() / 2);
}
}
};
// add an image that is rotated around the (3D) y axis
CanvasImage image = graphics().createImage(orange.width(), orange.height());
image.canvas().setFillColor(0xFF99CCFF);
image.canvas().fillRect(0, 0, image.width(), image.height());
image.canvas().drawImage(orange, 0, 0);
ImageLayer rotlayer = graphics().createImageLayer(image);
rotlayer.setShader(rotShader);
graphics().rootLayer().addAt(rotlayer, 25 + 2 * dx + orange.width(), 25);
// add an immediate layer that draws a quad and an image (which should rotate)
ImmediateLayer irotlayer = graphics().createImmediateLayer(new ImmediateLayer.Renderer() {
public void render(Surface surf) {
surf.setFillColor(0xFFCC99FF);
surf.fillRect(0, 0, orange.width(), orange.height());
surf.drawImage(orange, 0, 0);
}
});
irotlayer.setShader(rotShader);
graphics().rootLayer().addAt(irotlayer, 25 + 3 * dx + orange.width(), 25);
}
Aggregations