mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-16 16:46:07 +09:00
taking screenshot
This commit is contained in:
@@ -11,33 +11,33 @@ import java.io.OutputStream;
|
|||||||
*/
|
*/
|
||||||
public class PixmapIO2 {
|
public class PixmapIO2 {
|
||||||
|
|
||||||
public static void writeTGAHappy(FileHandle file, Pixmap pixmap) throws IOException {
|
// REMEMBER: to the GL's perspective, this game's FBOs are always Y-flipped. //
|
||||||
|
|
||||||
|
public static void writeTGAHappy(FileHandle file, Pixmap pixmap, boolean flipY) throws IOException {
|
||||||
OutputStream output = file.write(false);
|
OutputStream output = file.write(false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_writeTGA(output, pixmap, false);
|
_writeTGA(output, pixmap, false, flipY);
|
||||||
} finally {
|
} finally {
|
||||||
StreamUtils.closeQuietly(output);
|
StreamUtils.closeQuietly(output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void writeTGA(FileHandle file, Pixmap pixmap) throws IOException {
|
public static void writeTGA(FileHandle file, Pixmap pixmap, boolean flipY) throws IOException {
|
||||||
OutputStream output = file.write(false);
|
OutputStream output = file.write(false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_writeTGA(output, pixmap, true);
|
_writeTGA(output, pixmap, true, flipY);
|
||||||
} finally {
|
} finally {
|
||||||
StreamUtils.closeQuietly(output);
|
StreamUtils.closeQuietly(output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void _writeTGA(OutputStream out, Pixmap pixmap, Boolean verbatim) throws IOException {
|
private static void _writeTGA(OutputStream out, Pixmap pixmap, boolean verbatim, boolean flipY) throws IOException {
|
||||||
byte[] width = toShortLittle(pixmap.getWidth());
|
byte[] width = toShortLittle(pixmap.getWidth());
|
||||||
byte[] height = toShortLittle(pixmap.getHeight());
|
byte[] height = toShortLittle(pixmap.getHeight());
|
||||||
byte[] zero = toShortLittle(0);
|
byte[] zero = toShortLittle(0);
|
||||||
|
|
||||||
byte[] zeroalpha = new byte[]{0,0,0,0};
|
|
||||||
|
|
||||||
out.write(0); // ID field: empty
|
out.write(0); // ID field: empty
|
||||||
out.write(0); // no colour map, but should be ignored anyway as it being unmapped RGB
|
out.write(0); // no colour map, but should be ignored anyway as it being unmapped RGB
|
||||||
out.write(2); // 2 means unmapped RGB
|
out.write(2); // 2 means unmapped RGB
|
||||||
@@ -55,16 +55,17 @@ public class PixmapIO2 {
|
|||||||
// 1. BGRA order
|
// 1. BGRA order
|
||||||
// 2. Y-Flipped but not X-Flipped
|
// 2. Y-Flipped but not X-Flipped
|
||||||
|
|
||||||
for (int y = pixmap.getHeight() - 1; y >= 0; y--) {
|
if (!flipY) {
|
||||||
for (int x = 0; x < pixmap.getWidth(); x++) {
|
for (int y = pixmap.getHeight() - 1; y >= 0; y--) {
|
||||||
int color = pixmap.getPixel(x, y);
|
for (int x = 0; x < pixmap.getWidth(); x++) {
|
||||||
|
writeTga(x, y, verbatim, pixmap, out);
|
||||||
// if alpha == 0, write special value instead
|
|
||||||
if (verbatim && (color & 0xFF) == 0) {
|
|
||||||
out.write(zeroalpha);
|
|
||||||
}
|
}
|
||||||
else {
|
}
|
||||||
out.write(RGBAtoBGRA(color));
|
}
|
||||||
|
else {
|
||||||
|
for (int y = 0; y < pixmap.getHeight(); y++) {
|
||||||
|
for (int x = 0; x < pixmap.getWidth(); x++) {
|
||||||
|
writeTga(x, y, verbatim, pixmap, out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,6 +85,19 @@ public class PixmapIO2 {
|
|||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static byte[] zeroalpha = new byte[]{0,0,0,0};
|
||||||
|
private static void writeTga(int x, int y, boolean verbatim, Pixmap pixmap, OutputStream out) throws IOException {
|
||||||
|
int color = pixmap.getPixel(x, y);
|
||||||
|
|
||||||
|
// if alpha == 0, write special value instead
|
||||||
|
if (verbatim && (color & 0xFF) == 0) {
|
||||||
|
out.write(zeroalpha);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
out.write(RGBAtoBGRA(color));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static byte[] toShortLittle(int i) {
|
private static byte[] toShortLittle(int i) {
|
||||||
return new byte[]{
|
return new byte[]{
|
||||||
(byte) (i & 0xFF),
|
(byte) (i & 0xFF),
|
||||||
|
|||||||
@@ -272,7 +272,7 @@ class SpriteAssemblerPreview: Game() {
|
|||||||
|
|
||||||
if (doExport && image != null) {
|
if (doExport && image != null) {
|
||||||
doExport = false
|
doExport = false
|
||||||
PixmapIO2.writeTGAHappy(Gdx.files.absolute(exportPath), image)
|
PixmapIO2.writeTGAHappy(Gdx.files.absolute(exportPath), image, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
|||||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||||
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
|
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
|
||||||
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
|
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
|
||||||
|
import com.badlogic.gdx.utils.ScreenUtils;
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
@@ -140,6 +141,7 @@ public class AppLoader implements ApplicationListener {
|
|||||||
|
|
||||||
private static boolean splashDisplayed = false;
|
private static boolean splashDisplayed = false;
|
||||||
private static boolean postInitFired = false;
|
private static boolean postInitFired = false;
|
||||||
|
private static boolean screenshotRequested = false;
|
||||||
|
|
||||||
public static LwjglApplicationConfiguration appConfig;
|
public static LwjglApplicationConfiguration appConfig;
|
||||||
public static GameFontBase fontGame;
|
public static GameFontBase fontGame;
|
||||||
@@ -246,10 +248,6 @@ public class AppLoader implements ApplicationListener {
|
|||||||
|
|
||||||
FrameBufferManager.begin(renderFBO);
|
FrameBufferManager.begin(renderFBO);
|
||||||
gdxClearAndSetBlend(.094f, .094f, .094f, 0f);
|
gdxClearAndSetBlend(.094f, .094f, .094f, 0f);
|
||||||
FrameBufferManager.end();
|
|
||||||
|
|
||||||
|
|
||||||
FrameBufferManager.begin(renderFBO);
|
|
||||||
setCameraPosition(0, 0);
|
setCameraPosition(0, 0);
|
||||||
|
|
||||||
// draw splash screen when predefined screen is null
|
// draw splash screen when predefined screen is null
|
||||||
@@ -294,6 +292,20 @@ public class AppLoader implements ApplicationListener {
|
|||||||
PostProcessor.INSTANCE.draw(camera.combined, renderFBO);
|
PostProcessor.INSTANCE.draw(camera.combined, renderFBO);
|
||||||
|
|
||||||
|
|
||||||
|
// process screenshot request
|
||||||
|
if (screenshotRequested) {
|
||||||
|
screenshotRequested = false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Pixmap p = ScreenUtils.getFrameBufferPixmap(0, 0, appConfig.width, appConfig.height);
|
||||||
|
PixmapIO2.writeTGA(Gdx.files.absolute(defaultDir + "/Screenshot.tga"), p, true);
|
||||||
|
p.dispose();
|
||||||
|
}
|
||||||
|
catch (Throwable e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
splashDisplayed = true;
|
splashDisplayed = true;
|
||||||
GLOBAL_RENDER_TIMER += 1;
|
GLOBAL_RENDER_TIMER += 1;
|
||||||
}
|
}
|
||||||
@@ -417,13 +429,20 @@ public class AppLoader implements ApplicationListener {
|
|||||||
fullscreenQuad.setIndices(new short[]{0, 1, 2, 2, 3, 0});
|
fullscreenQuad.setIndices(new short[]{0, 1, 2, 2, 3, 0});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void requestScreenshot() {
|
||||||
|
screenshotRequested = true;
|
||||||
|
}
|
||||||
|
|
||||||
// DEFAULT DIRECTORIES //
|
// DEFAULT DIRECTORIES //
|
||||||
|
|
||||||
public static String OSName = System.getProperty("os.name");
|
public static String OSName = System.getProperty("os.name");
|
||||||
public static String OSVersion = System.getProperty("os.version");
|
public static String OSVersion = System.getProperty("os.version");
|
||||||
public static String operationSystem;
|
public static String operationSystem;
|
||||||
|
/** %appdata%/Terrarum, without trailing slash */
|
||||||
public static String defaultDir;
|
public static String defaultDir;
|
||||||
|
/** defaultDir + "/Saves", without trailing slash */
|
||||||
public static String defaultSaveDir;
|
public static String defaultSaveDir;
|
||||||
|
/** defaultDir + "/config.json" */
|
||||||
public static String configDir;
|
public static String configDir;
|
||||||
public static RunningEnvironment environment;
|
public static RunningEnvironment environment;
|
||||||
|
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ object CommandDict {
|
|||||||
"setscale" to SetScale,
|
"setscale" to SetScale,
|
||||||
"kill" to KillActor,
|
"kill" to KillActor,
|
||||||
"money" to MoneyDisp,
|
"money" to MoneyDisp,
|
||||||
|
"screenshot" to TakeScreenshot,
|
||||||
|
|
||||||
// Test codes
|
// Test codes
|
||||||
"bulletintest" to SetBulletin,
|
"bulletintest" to SetBulletin,
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package net.torvald.terrarum.console
|
package net.torvald.terrarum.console
|
||||||
|
|
||||||
import net.torvald.terrarum.*
|
import net.torvald.terrarum.ccG
|
||||||
|
import net.torvald.terrarum.ccW
|
||||||
|
import net.torvald.terrarum.ccY
|
||||||
import net.torvald.terrarum.langpack.Lang
|
import net.torvald.terrarum.langpack.Lang
|
||||||
import java.time.ZonedDateTime
|
import java.time.ZonedDateTime
|
||||||
import java.util.ArrayList
|
import java.util.*
|
||||||
import java.util.Formatter
|
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,7 +21,8 @@ internal object CommandInterpreter {
|
|||||||
"getlocale",
|
"getlocale",
|
||||||
"help",
|
"help",
|
||||||
"version",
|
"version",
|
||||||
"tips"
|
"tips",
|
||||||
|
"screenshot"
|
||||||
)
|
)
|
||||||
|
|
||||||
internal fun execute(command: String) {
|
internal fun execute(command: String) {
|
||||||
|
|||||||
13
src/net/torvald/terrarum/console/TakeScreenshot.kt
Normal file
13
src/net/torvald/terrarum/console/TakeScreenshot.kt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package net.torvald.terrarum.console
|
||||||
|
|
||||||
|
import net.torvald.terrarum.AppLoader
|
||||||
|
|
||||||
|
object TakeScreenshot: ConsoleCommand {
|
||||||
|
override fun execute(args: Array<String>) {
|
||||||
|
AppLoader.requestScreenshot()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun printUsage() {
|
||||||
|
Echo("Takes screenshot and save it to the default directory as 'screenshot.tga'")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,7 +22,9 @@ import net.torvald.terrarum.worlddrawer.WorldCamera
|
|||||||
import javax.swing.JFileChooser
|
import javax.swing.JFileChooser
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This will be rendered to a postprocessor FBO
|
* This will be rendered to a postprocessor FBO.
|
||||||
|
*
|
||||||
|
* For the entire render path, see AppLoader.
|
||||||
*/
|
*/
|
||||||
object IngameRenderer {
|
object IngameRenderer {
|
||||||
/** for non-private use, use with care! */
|
/** for non-private use, use with care! */
|
||||||
@@ -282,7 +284,8 @@ object IngameRenderer {
|
|||||||
if (fileChooser.selectedFile != null) {
|
if (fileChooser.selectedFile != null) {
|
||||||
fboRGB.inAction(null, null) {
|
fboRGB.inAction(null, null) {
|
||||||
val p = ScreenUtils.getFrameBufferPixmap(0, 0, fboRGB.width, fboRGB.height)
|
val p = ScreenUtils.getFrameBufferPixmap(0, 0, fboRGB.width, fboRGB.height)
|
||||||
PixmapIO2.writeTGA(Gdx.files.absolute(fileChooser.selectedFile.absolutePath), p)
|
PixmapIO2.writeTGA(Gdx.files.absolute(fileChooser.selectedFile.absolutePath), p, false)
|
||||||
|
p.dispose()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user