Save screenshots via the frame buffer.
This commit is contained in:
@@ -287,15 +287,6 @@ namespace OpenRA.Platforms.Default
|
||||
public delegate void Scissor(int x, int y, int width, int height);
|
||||
public static Scissor glScissor { get; private set; }
|
||||
|
||||
public delegate void PushClientAttrib(int mask);
|
||||
public static PushClientAttrib glPushClientAttrib { get; private set; }
|
||||
|
||||
public delegate void PopClientAttrib();
|
||||
public static PopClientAttrib glPopClientAttrib { get; private set; }
|
||||
|
||||
public delegate void PixelStoref(int param, float pname);
|
||||
public static PixelStoref glPixelStoref { get; private set; }
|
||||
|
||||
public delegate void ReadPixels(int x, int y, int width, int height,
|
||||
int format, int type, IntPtr data);
|
||||
public static ReadPixels glReadPixels { get; private set; }
|
||||
@@ -429,9 +420,6 @@ namespace OpenRA.Platforms.Default
|
||||
glBlendFunc = Bind<BlendFunc>("glBlendFunc");
|
||||
glDepthFunc = Bind<DepthFunc>("glDepthFunc");
|
||||
glScissor = Bind<Scissor>("glScissor");
|
||||
glPushClientAttrib = Bind<PushClientAttrib>("glPushClientAttrib");
|
||||
glPopClientAttrib = Bind<PopClientAttrib>("glPopClientAttrib");
|
||||
glPixelStoref = Bind<PixelStoref>("glPixelStoref");
|
||||
glReadPixels = Bind<ReadPixels>("glReadPixels");
|
||||
glGenTextures = Bind<GenTextures>("glGenTextures");
|
||||
glDeleteTextures = Bind<DeleteTextures>("glDeleteTextures");
|
||||
|
||||
@@ -10,8 +10,6 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Primitives;
|
||||
using SDL2;
|
||||
@@ -118,51 +116,6 @@ namespace OpenRA.Platforms.Default
|
||||
OpenGL.CheckGLError();
|
||||
}
|
||||
|
||||
public void SaveScreenshot(string path)
|
||||
{
|
||||
var s = window.SurfaceSize;
|
||||
var raw = new byte[s.Width * s.Height * 4];
|
||||
|
||||
OpenGL.glPushClientAttrib(OpenGL.GL_CLIENT_PIXEL_STORE_BIT);
|
||||
|
||||
OpenGL.glPixelStoref(OpenGL.GL_PACK_ROW_LENGTH, s.Width);
|
||||
OpenGL.glPixelStoref(OpenGL.GL_PACK_ALIGNMENT, 1);
|
||||
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* pRaw = raw)
|
||||
OpenGL.glReadPixels(0, 0, s.Width, s.Height,
|
||||
OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE, (IntPtr)pRaw);
|
||||
}
|
||||
|
||||
OpenGL.glFinish();
|
||||
OpenGL.glPopClientAttrib();
|
||||
|
||||
ThreadPool.QueueUserWorkItem(_ =>
|
||||
{
|
||||
// Convert GL pixel data into format expected by png
|
||||
// - Flip vertically
|
||||
// - BGRA to RGBA
|
||||
// - Force A to 255 (no transparent pixels!)
|
||||
var data = new byte[raw.Length];
|
||||
for (var y = 0; y < s.Height; y++)
|
||||
{
|
||||
for (var x = 0; x < s.Width; x++)
|
||||
{
|
||||
var iData = 4 * (y * s.Width + x);
|
||||
var iRaw = 4 * ((s.Height - y - 1) * s.Width + x);
|
||||
data[iData] = raw[iRaw + 2];
|
||||
data[iData + 1] = raw[iRaw + 1];
|
||||
data[iData + 2] = raw[iRaw + 0];
|
||||
data[iData + 3] = byte.MaxValue;
|
||||
}
|
||||
}
|
||||
|
||||
var screenshot = new Png(data, window.SurfaceSize.Width, window.SurfaceSize.Height);
|
||||
screenshot.Save(path);
|
||||
});
|
||||
}
|
||||
|
||||
public void Present()
|
||||
{
|
||||
VerifyThreadAffinity();
|
||||
|
||||
@@ -49,7 +49,6 @@ namespace OpenRA.Platforms.Default
|
||||
Action<object> doDrawPrimitives;
|
||||
Action<object> doEnableScissor;
|
||||
Action<object> doSetBlendMode;
|
||||
Action<object> doSaveScreenshot;
|
||||
|
||||
public ThreadedGraphicsContext(Sdl2GraphicsContext context, int batchSize)
|
||||
{
|
||||
@@ -108,7 +107,6 @@ namespace OpenRA.Platforms.Default
|
||||
context.EnableScissor(t.Item1, t.Item2, t.Item3, t.Item4);
|
||||
};
|
||||
doSetBlendMode = mode => { context.SetBlendMode((BlendMode)mode); };
|
||||
doSaveScreenshot = path => context.SaveScreenshot((string)path);
|
||||
|
||||
Monitor.Pulse(syncObject);
|
||||
}
|
||||
@@ -447,11 +445,6 @@ namespace OpenRA.Platforms.Default
|
||||
{
|
||||
Post(doSetBlendMode, mode);
|
||||
}
|
||||
|
||||
public void SaveScreenshot(string path)
|
||||
{
|
||||
Post(doSaveScreenshot, path);
|
||||
}
|
||||
}
|
||||
|
||||
class ThreadedFrameBuffer : IFrameBuffer
|
||||
|
||||
Reference in New Issue
Block a user