Save screenshots via the frame buffer.

This commit is contained in:
Paul Chote
2019-08-31 18:04:41 +01:00
committed by teinarss
parent c0ee346c1c
commit 1d106e71c4
6 changed files with 31 additions and 68 deletions

View File

@@ -550,7 +550,7 @@ namespace OpenRA
var path = Path.Combine(directory, string.Concat(filename, ".png"));
Log.Write("debug", "Taking screenshot " + path);
Renderer.Context.SaveScreenshot(path);
Renderer.SaveScreenshot(path);
Debug("Saved screenshot " + filename);
}
}

View File

@@ -66,7 +66,6 @@ namespace OpenRA
IShader CreateShader(string name);
void EnableScissor(int x, int y, int width, int height);
void DisableScissor();
void SaveScreenshot(string path);
void Present();
void DrawPrimitives(PrimitiveType pt, int firstVertex, int numVertices);
void Clear();

View File

@@ -12,6 +12,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Support;
@@ -308,6 +310,34 @@ namespace OpenRA
Window.ReleaseWindowMouseFocus();
}
public void SaveScreenshot(string path)
{
// Pull the data from the Texture directly to prevent the sheet from buffering it
var src = screenBuffer.Texture.GetData();
var srcWidth = screenSprite.Sheet.Size.Width;
var destWidth = screenSprite.Bounds.Width;
var destHeight = -screenSprite.Bounds.Height;
var channelOrder = new[] { 2, 1, 0, 3 };
ThreadPool.QueueUserWorkItem(_ =>
{
// Convert BGRA to RGBA
var dest = new byte[4 * destWidth * destHeight];
for (var y = 0; y < destHeight; y++)
{
for (var x = 0; x < destWidth; x++)
{
var destOffset = 4 * (y * destWidth + x);
var srcOffset = 4 * (y * srcWidth + x);
for (var i = 0; i < 4; i++)
dest[destOffset + i] = src[srcOffset + channelOrder[i]];
}
}
new Png(dest, destWidth, destHeight).Save(path);
});
}
public void Dispose()
{
WorldModelRenderer.Dispose();