List of usage examples for com.badlogic.gdx.graphics Pixmap setBlending
public static void setBlending(Blending blending)
From source file:com.kotcrab.vis.editor.module.physicseditor.util.Tracer.java
License:Apache License
public static Vector2[][] trace(String path, float hullTolerance, int alphaTolerance, boolean multiPartDetection, boolean holeDetection) { Blending blending = Pixmap.getBlending(); Pixmap.setBlending(Blending.None); Pixmap pixmap = new Pixmap(Gdx.files.absolute(path)); int w = pixmap.getWidth(); int h = pixmap.getHeight(); int size = w * h; int[] array = new int[size]; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int color = pixmap.getPixel(x, y); array[x + y * w] = color;/* ww w.j a v a 2 s.co m*/ } } pixmap.dispose(); Pixmap.setBlending(blending); Array<Array<Vector2>> outlines; try { outlines = TextureConverter.createPolygon(array, w, h, hullTolerance, alphaTolerance, multiPartDetection, holeDetection); } catch (Exception e) { return null; } TextureRegion region = new TextureRegion(new Texture(path)); float tw = region.getRegionWidth(); float th = region.getRegionHeight(); Vector2[][] polygons = new Vector2[outlines.size][]; for (int i = 0; i < outlines.size; i++) { Array<Vector2> outline = outlines.get(i); polygons[i] = new Vector2[outline.size]; for (int ii = 0; ii < outline.size; ii++) { polygons[i][ii] = outline.get(ii); polygons[i][ii].x /= tw; polygons[i][ii].y /= tw; polygons[i][ii].y = 1 * th / tw - polygons[i][ii].y; } } return polygons; }
From source file:com.o2d.pkayjava.editor.utils.poly.tracer.Tracer.java
License:Apache License
public static Vector2[][] trace(Texture texture, float hullTolerance, int alphaTolerance, boolean multiPartDetection, boolean holeDetection) { Blending blending = Pixmap.getBlending(); Pixmap.setBlending(Blending.None); Pixmap pixmap = TextureUtils.getPOTPixmap(texture); int w = pixmap.getWidth(); int h = pixmap.getHeight(); int size = w * h; int[] array = new int[size]; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int color = pixmap.getPixel(x, y); array[x + y * w] = color;// ww w. ja va2 s . c o m } } pixmap.dispose(); Pixmap.setBlending(blending); Array<Array<Vector2>> outlines; try { outlines = TextureConverter.createPolygon(array, w, h, hullTolerance, alphaTolerance, multiPartDetection, holeDetection); } catch (Exception e) { return null; } TextureRegion region = TextureUtils.getPOTTexture(texture); float tw = region.getRegionWidth(); float th = region.getRegionHeight(); Vector2[][] polygons = new Vector2[outlines.size][]; for (int i = 0; i < outlines.size; i++) { Array<Vector2> outline = outlines.get(i); polygons[i] = new Vector2[outline.size]; for (int ii = 0; ii < outline.size; ii++) { polygons[i][ii] = outline.get(ii); polygons[i][ii].x /= tw; polygons[i][ii].y /= tw; polygons[i][ii].y = 1 * th / tw - polygons[i][ii].y; } } return polygons; }
From source file:com.ray3k.skincomposer.data.AtlasData.java
License:Open Source License
public void readAtlas(FileHandle fileHandle) throws Exception { if (fileHandle.exists()) { FileHandle saveFile = main.getProjectData().getSaveFile(); FileHandle targetDirectory;/*w w w . j ava2s . c o m*/ if (saveFile != null) { targetDirectory = saveFile.sibling(saveFile.nameWithoutExtension() + "_data/"); } else { targetDirectory = Gdx.files.local("temp/" + main.getProjectData().getId() + "_data/"); } targetDirectory.mkdirs(); TextureAtlas atlas = new TextureAtlas(fileHandle); Array<AtlasRegion> regions = atlas.getRegions(); for (AtlasRegion region : regions) { Texture texture = region.getTexture(); if (!texture.getTextureData().isPrepared()) { texture.getTextureData().prepare(); } Pixmap.setBlending(Pixmap.Blending.None); Pixmap pixmap = texture.getTextureData().consumePixmap(); Pixmap savePixmap; String name; if (region.splits == null && region.pads == null) { name = region.name + ".png"; savePixmap = new Pixmap(region.getRegionWidth(), region.getRegionHeight(), Pixmap.Format.RGBA8888); for (int x = 0; x < region.getRegionWidth(); x++) { for (int y = 0; y < region.getRegionHeight(); y++) { int colorInt = pixmap.getPixel(region.getRegionX() + x, region.getRegionY() + y); savePixmap.drawPixel(x, y, colorInt); } } } else { name = region.name + ".9.png"; savePixmap = new Pixmap(region.getRegionWidth() + 2, region.getRegionHeight() + 2, pixmap.getFormat()); int x; int y; //draw 9 patch lines savePixmap.setColor(Color.BLACK); if (region.splits != null) { x = 0; for (y = region.splits[2] + 1; y < savePixmap.getHeight() - region.splits[3] - 1; y++) { savePixmap.drawPixel(x, y); } y = 0; for (x = region.splits[0] + 1; x < savePixmap.getWidth() - region.splits[1] - 1; x++) { savePixmap.drawPixel(x, y); } } if (region.pads != null) { x = savePixmap.getWidth() - 1; for (y = region.pads[2] + 1; y < savePixmap.getHeight() - region.pads[3] - 1; y++) { savePixmap.drawPixel(x, y); } y = savePixmap.getHeight() - 1; for (x = region.pads[0] + 1; x < savePixmap.getWidth() - region.pads[1] - 1; x++) { savePixmap.drawPixel(x, y); } } for (x = 0; x < region.getRegionWidth(); x++) { for (y = 0; y < region.getRegionHeight(); y++) { int colorInt = pixmap.getPixel(region.getRegionX() + x, region.getRegionY() + y); savePixmap.drawPixel(x + 1, y + 1, colorInt); } } } FileHandle outputFile = targetDirectory.child(name); PixmapIO.writePNG(outputFile, savePixmap); DrawableData drawable = new DrawableData(outputFile); //delete drawables with the same name for (DrawableData originalData : new Array<>(main.getProjectData().getAtlasData().getDrawables())) { if (originalData.name.equals(drawable.name)) { main.getProjectData().getAtlasData().getDrawables().removeValue(originalData, true); } } drawables.add(drawable); } } else { throw new FileNotFoundException(); } }
From source file:com.ridiculousRPG.GameBase.java
License:Apache License
/** * Takes a screenshot from the current frame buffer. The screenshot will be * stretched to fit into the Rectangle specified by dstW and dstH * //ww w .j a v a 2 s . c o m * @param srcX * @param srcY * @param srcW * @param srcH * @param dstW * @param dstH * @return A Pixmap containing the screenshot. The screenshot will be * flipped at the y-axis. * @throws IOException */ public Pixmap takeScreenshot(int srcX, int srcY, int srcW, int srcH, int dstW, int dstH) throws IOException { Gdx.gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 1); Pixmap pixmap = new Pixmap(srcW, srcH, Format.RGBA8888); Gdx.gl.glReadPixels(srcX, srcY, srcW, srcH, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixmap.getPixels()); // scale the picture if (srcW != dstW || srcH != dstH) { Pixmap scale = new Pixmap(dstW, dstH, Format.RGBA8888); Blending old = Pixmap.getBlending(); Pixmap.setBlending(Blending.None); scale.drawPixmap(pixmap, 0, 0, srcW, srcH, 0, 0, dstW, dstH); Pixmap.setBlending(old); pixmap.dispose(); pixmap = scale; } return pixmap; }
From source file:releasethekraken.ui.renderer.GameRenderer.java
/** * Constructs a new GameRenderer//from ww w . j a va 2 s . co m * @param rtk The ReleaseTheKraken instance. This is final so that the * anonymous inner classes can access it. * @param world The world to be rendered */ public GameRenderer(final ReleaseTheKraken rtk, GameWorld world) { super(); this.world = world; //Populate the list of connected paths this.world.getFirstPath().getAllConnectedPaths(this.seaCreaturePaths); //Create the camera to view the world this.camera = new OrthographicCamera(); float cameraWidth = 80.0F; float cameraHeight = (Gdx.graphics.getHeight() * 1F / Gdx.graphics.getWidth()) * cameraWidth; this.camera.setToOrtho(false, cameraWidth, cameraHeight); Gdx.app.log("GameRenderer", "Calculated camera dimensions: " + cameraWidth + " x " + cameraHeight); this.worldSpriteBatch = new SpriteBatch(); this.worldShapeRenderer = new ShapeRenderer(); //Set the world renderers to render to the camera's projection matrix this.worldSpriteBatch.setProjectionMatrix(this.camera.combined); this.worldShapeRenderer.setProjectionMatrix(this.camera.combined); //Create the Box2D debug renderer this.box2DDebugRenderer = new Box2DDebugRenderer(); this.box2DDebugRenderer.setDrawVelocities(true); //Create the tile map renderer this.tiledMapRenderer = new OrthogonalTiledMapRenderer(world.getTiledMap(), 1 / (world.getTiledMapUnitScale())); //this.tiledMapRenderer.getBatch().setShader(GameAssets.tilemapShader); //Set the shader //Don't use the shader for now this.debugOverlay = new DebugOverlay(this); this.uiObjects.add(this.debugOverlay); UiButton pauseButton = new UiButton(this, Gdx.graphics.getWidth() - 0.075F * Gdx.graphics.getWidth(), Gdx.graphics.getHeight() - 0.05F * Gdx.graphics.getHeight(), 0.075F, 0.05F, "Pause", Color.GRAY.cpy().sub(0.1F, 0.1F, 0.1F, 0)) { @Override public void onClick(int mouseButton) { super.onClick(mouseButton); //Take a screenshot of the game Pixmap pixmap = Screenshots.getScreenshot(true); //Push a new pause screen onto the screen stack rtk.pushScreen(new PauseScreen(rtk, pixmap)); } }; pauseButton.setToolTip(new TextToolTip(this, "Pause the game")); this.uiObjects.add(pauseButton); UiButton debugMenuButton = new UiButton(this, Gdx.graphics.getWidth() - 0.2323F * Gdx.graphics.getWidth(), Gdx.graphics.getHeight() - 0.05F * Gdx.graphics.getHeight(), 0.157F, 0.05F, "Debug Screen", Color.GRAY.cpy().sub(0.1F, 0.1F, 0.1F, 0)) { @Override public void onClick(int mouseButton) { super.onClick(mouseButton); if (this.renderer instanceof GameRenderer) { ((GameRenderer) this.renderer).debugScreenVisible = !((GameRenderer) this.renderer).debugScreenVisible; //Toggle visibility Gdx.app.log("Debug Menu Button", "Debug screen " + (((GameRenderer) this.renderer).debugScreenVisible ? "ON" : "OFF")); } } }; debugMenuButton.setToolTip(new TextToolTip(this, "Show/Hide Debug Screen")); this.uiObjects.add(debugMenuButton); this.uiObjects.add(new Sidebar(this)); //Add the sidebar this.uiObjects.sort(); //Sort the UI objects so that they render in the order of their render depths //Generate path pixmap and texture Pixmap.setBlending(Pixmap.Blending.None); //Disable Pixmap blending, because it causes weird lines this.pathPixmap = new Pixmap((int) (this.world.getWidth() * this.world.getTiledMapUnitScale()), (int) (this.world.getHeight() * this.world.getTiledMapUnitScale()), Pixmap.Format.RGBA8888); //Make sure the pixmap is clear this.pathPixmap.setColor(new Color(0, 0, 0, 0)); //Completely clear this.pathPixmap.fill(); this.pathPixmap.setColor(Color.valueOf("61B5FF66")); //Partial transparency with color //Draw the path for every path for (SeaCreaturePath path : this.seaCreaturePaths) { CatmullRomSpline smoothPath = path.getSmoothPath(); Vector2 pathPoint = new Vector2(); //Move across the path from 0 to 1, drawing circles along the way for (float f = -0.1F; f < 1.1F; f += 0.001F) { smoothPath.valueAt(pathPoint, f); //Stores the value of the path at the point in the vector this.pathPixmap.fillCircle((int) (pathPoint.x * this.world.getTiledMapUnitScale()), (int) (this.world.getHeight() * this.world.getTiledMapUnitScale() - (pathPoint.y * this.world.getTiledMapUnitScale())), (int) this.world.getTiledMapUnitScale()); } } //Create texture to hold pixmap data this.pathTexture = new Texture(this.pathPixmap); this.pathTexture.draw(this.pathPixmap, 0, 0); //Draw the pixmap to the texture this.pathTexture.bind(1); //Bind the texture to texture unit 1 Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0); //Set the active texture back to the normal one }