1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests; 18 19 import java.io.IOException; 20 import java.util.zip.Deflater; 21 22 import com.badlogic.gdx.Gdx; 23 import com.badlogic.gdx.files.FileHandle; 24 import com.badlogic.gdx.graphics.GL20; 25 import com.badlogic.gdx.graphics.Pixmap; 26 import com.badlogic.gdx.graphics.PixmapIO.PNG; 27 import com.badlogic.gdx.graphics.Texture; 28 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 29 import com.badlogic.gdx.math.MathUtils; 30 import com.badlogic.gdx.tests.utils.GdxTest; 31 import com.badlogic.gdx.utils.ScreenUtils; 32 33 public class PngTest extends GdxTest { 34 SpriteBatch batch; 35 Texture badlogic, screenshot; 36 create()37 public void create () { 38 batch = new SpriteBatch(); 39 badlogic = new Texture(Gdx.files.internal("data/badlogic.jpg")); 40 } 41 render()42 public void render () { 43 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 44 batch.begin(); 45 if (screenshot == null) { 46 int width = Gdx.graphics.getWidth(), height = Gdx.graphics.getHeight(); 47 for (int i = 0; i < 100; i++) 48 batch.draw(badlogic, MathUtils.random(width), MathUtils.random(height)); 49 batch.flush(); 50 51 FileHandle file = FileHandle.tempFile("screenshot-"); 52 System.out.println(file.file().getAbsolutePath()); 53 Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 54 try { 55 PNG writer = new PNG((int)(pixmap.getWidth() * pixmap.getHeight() * 1.5f)); 56 // writer.setCompression(Deflater.NO_COMPRESSION); 57 writer.write(file, pixmap); 58 writer.write(file, pixmap); // Write twice to make sure the object is reusable. 59 writer.dispose(); 60 } catch (IOException ex) { 61 throw new RuntimeException(ex); 62 } 63 screenshot = new Texture(file); 64 } 65 batch.draw(screenshot, 0, 0); 66 batch.end(); 67 } 68 } 69