Add an aspect ratio correction flag to VqaPlayerWidget.

This commit is contained in:
Paul Chote
2014-10-08 13:20:42 +13:00
parent df5b7d4497
commit 6b1505b71d
4 changed files with 47 additions and 17 deletions

View File

@@ -79,6 +79,7 @@ namespace OpenRA
void Render(Action a); void Render(Action a);
} }
public enum TextureScaleFilter { Nearest, Linear }
public interface ITexture public interface ITexture
{ {
void SetData(Bitmap bitmap); void SetData(Bitmap bitmap);
@@ -86,6 +87,7 @@ namespace OpenRA
void SetData(byte[] colors, int width, int height); void SetData(byte[] colors, int width, int height);
byte[] GetData(); byte[] GetData();
Size Size { get; } Size Size { get; }
TextureScaleFilter ScaleFilter { get; set; }
} }
public interface IFrameBuffer public interface IFrameBuffer

View File

@@ -18,6 +18,12 @@ namespace OpenRA.Widgets
{ {
public class VqaPlayerWidget : Widget public class VqaPlayerWidget : Widget
{ {
public float AspectRatio = 1.2f;
public bool DrawOverlay = true;
public bool Paused { get { return paused; } }
public VqaReader Video { get { return video; } }
Sprite videoSprite, overlaySprite; Sprite videoSprite, overlaySprite;
VqaReader video = null; VqaReader video = null;
string cachedVideo; string cachedVideo;
@@ -29,17 +35,14 @@ namespace OpenRA.Widgets
Action onComplete; Action onComplete;
public bool Paused { get { return paused; } }
public VqaReader Video { get { return video; } }
readonly World world; readonly World world;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public VqaPlayerWidget(World world) public VqaPlayerWidget(World world)
{ {
this.world = world; this.world = world;
} }
public bool DrawOverlay = true;
public void Load(string filename) public void Load(string filename)
{ {
if (filename == cachedVideo) if (filename == cachedVideo)
@@ -58,25 +61,29 @@ namespace OpenRA.Widgets
var size = Math.Max(video.Width, video.Height); var size = Math.Max(video.Width, video.Height);
var textureSize = Exts.NextPowerOf2(size); var textureSize = Exts.NextPowerOf2(size);
var videoSheet = new Sheet(new Size(textureSize, textureSize), false); var videoSheet = new Sheet(new Size(textureSize, textureSize), false);
videoSheet.Texture.ScaleFilter = TextureScaleFilter.Linear;
videoSheet.Texture.SetData(video.FrameData); videoSheet.Texture.SetData(video.FrameData);
videoSprite = new Sprite(videoSheet, new Rectangle(0, 0, video.Width, video.Height), TextureChannel.Alpha); videoSprite = new Sprite(videoSheet, new Rectangle(0, 0, video.Width, video.Height), TextureChannel.Alpha);
var scale = Math.Min(RenderBounds.Width / video.Width, RenderBounds.Height / video.Height); var scale = Math.Min(RenderBounds.Width / video.Width, RenderBounds.Height / (video.Height * AspectRatio));
videoOrigin = new float2(RenderBounds.X + (RenderBounds.Width - scale * video.Width) / 2, RenderBounds.Y + (RenderBounds.Height - scale * video.Height) / 2); videoOrigin = new float2(RenderBounds.X + (RenderBounds.Width - scale * video.Width) / 2, RenderBounds.Y + (RenderBounds.Height - scale * AspectRatio * video.Height) / 2);
videoSize = new float2(video.Width * scale, video.Height * scale);
// Round size to integer pixels. Round up to be consistent with the scale calcuation.
videoSize = new float2((int)Math.Ceiling(video.Width * scale), (int)Math.Ceiling(video.Height * scale * AspectRatio));
if (!DrawOverlay) if (!DrawOverlay)
return; return;
overlay = new uint[2 * textureSize, 2 * textureSize]; var scaledHeight = (int)videoSize.Y;
overlay = new uint[Exts.NextPowerOf2(scaledHeight), 1];
var black = (uint)255 << 24; var black = (uint)255 << 24;
for (var y = 0; y < video.Height; y++) for (var y = 0; y < scaledHeight; y += 2)
for (var x = 0; x < video.Width; x++) overlay[y, 0] = black;
overlay[2 * y, x] = black;
var overlaySheet = new Sheet(new Size(2 * textureSize, 2 * textureSize), false); var overlaySheet = new Sheet(new Size(1, Exts.NextPowerOf2(scaledHeight)), false);
overlaySheet.Texture.SetData(overlay); overlaySheet.Texture.SetData(overlay);
overlaySprite = new Sprite(overlaySheet, new Rectangle(0, 0, video.Width, 2 * video.Height), TextureChannel.Alpha); overlaySprite = new Sprite(overlaySheet, new Rectangle(0, 0, 1, scaledHeight), TextureChannel.Alpha);
} }
public override void Draw() public override void Draw()
@@ -84,7 +91,7 @@ namespace OpenRA.Widgets
if (video == null) if (video == null)
return; return;
if (!(stopped || paused)) if (!stopped && !paused)
{ {
var nextFrame = (int)float2.Lerp(0, video.Frames, Sound.VideoSeekPosition * invLength); var nextFrame = (int)float2.Lerp(0, video.Frames, Sound.VideoSeekPosition * invLength);
if (nextFrame > video.Frames) if (nextFrame > video.Frames)

View File

@@ -80,6 +80,7 @@ namespace OpenRA.Renderer.Null
public class NullTexture : ITexture public class NullTexture : ITexture
{ {
public TextureScaleFilter ScaleFilter { get { return TextureScaleFilter.Nearest; } set { } }
public void SetData(Bitmap bitmap) { } public void SetData(Bitmap bitmap) { }
public void SetData(uint[,] colors) { } public void SetData(uint[,] colors) { }
public void SetData(byte[] colors, int width, int height) { } public void SetData(byte[] colors, int width, int height) { }

View File

@@ -19,11 +19,29 @@ namespace OpenRA.Renderer.Sdl2
public class Texture : ITexture public class Texture : ITexture
{ {
int texture; int texture;
TextureScaleFilter scaleFilter;
Size size;
public int ID { get { return texture; } } public int ID { get { return texture; } }
Size size;
public Size Size { get { return size; } } public Size Size { get { return size; } }
public TextureScaleFilter ScaleFilter
{
get
{
return scaleFilter;
}
set
{
if (scaleFilter == value)
return;
scaleFilter = value;
PrepareTexture();
}
}
public Texture() public Texture()
{ {
GL.GenTextures(1, out texture); GL.GenTextures(1, out texture);
@@ -45,9 +63,11 @@ namespace OpenRA.Renderer.Sdl2
ErrorHandler.CheckGlError(); ErrorHandler.CheckGlError();
GL.BindTexture(TextureTarget.Texture2D, texture); GL.BindTexture(TextureTarget.Texture2D, texture);
ErrorHandler.CheckGlError(); ErrorHandler.CheckGlError();
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Nearest);
var filter = scaleFilter == TextureScaleFilter.Linear ? (int)TextureMinFilter.Linear : (int)TextureMinFilter.Nearest;
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, filter);
ErrorHandler.CheckGlError(); ErrorHandler.CheckGlError();
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, filter);
ErrorHandler.CheckGlError(); ErrorHandler.CheckGlError();
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureWrapMode.ClampToEdge);