-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScreenshotFactory.java
More file actions
75 lines (67 loc) · 2.12 KB
/
ScreenshotFactory.java
File metadata and controls
75 lines (67 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.bc.memorytest;
import java.nio.ByteBuffer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.PixmapIO;
/**
* Class used to take screenshots.
*
* @author https://github.com/libgdx/libgdx/wiki/Take-a-Screenshot
*/
public class ScreenshotFactory
{
public static void saveScreenshot(String filename)
{
try
{
Gdx.files.local(filename + ".png").delete();
FileHandle fh = Gdx.files.local(filename + ".png");
if (fh.exists())
{
fh.delete(); // delete file if it exists
}
// do
// {
// fh = new FileHandle(filename + ".png");
// fh.delete(); // delete file if it exists
// }
// while (fh.exists());
Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
PixmapIO.writePNG(fh, pixmap);
pixmap.dispose();
}
catch (Exception e)
{
System.out.println(e);
}
}
private static Pixmap getScreenshot(int x, int y, int w, int h, boolean flipY)
{
Gdx.gl.glPixelStorei(GL20.GL_PACK_ALIGNMENT, 1);
final Pixmap pixmap = new Pixmap(w, h, Format.RGBA8888);
ByteBuffer pixels = pixmap.getPixels();
Gdx.gl.glReadPixels(x, y, w, h, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, pixels);
final int numBytes = w * h * 4;
byte[] lines = new byte[numBytes];
if (flipY)
{
pixels.clear();
pixels.get(lines);
}
else
{
final int numBytesPerLine = w * 4;
for (int i = 0; i < h; i++)
{
pixels.position((h - i - 1) * numBytesPerLine);
pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
}
pixels.clear();
pixels.put(lines);
}
return pixmap;
}
}