diff --git a/OpenRA.Game/Graphics/IGraphicsDevice.cs b/OpenRA.Game/Graphics/IGraphicsDevice.cs index d8214007f7..a66c199165 100755 --- a/OpenRA.Game/Graphics/IGraphicsDevice.cs +++ b/OpenRA.Game/Graphics/IGraphicsDevice.cs @@ -79,6 +79,7 @@ namespace OpenRA void Render(Action a); } + public enum TextureScaleFilter { Nearest, Linear } public interface ITexture { void SetData(Bitmap bitmap); @@ -86,6 +87,7 @@ namespace OpenRA void SetData(byte[] colors, int width, int height); byte[] GetData(); Size Size { get; } + TextureScaleFilter ScaleFilter { get; set; } } public interface IFrameBuffer diff --git a/OpenRA.Game/Widgets/VqaPlayerWidget.cs b/OpenRA.Game/Widgets/VqaPlayerWidget.cs index eb52a87396..e42801f9ff 100644 --- a/OpenRA.Game/Widgets/VqaPlayerWidget.cs +++ b/OpenRA.Game/Widgets/VqaPlayerWidget.cs @@ -18,6 +18,12 @@ namespace OpenRA.Widgets { 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; VqaReader video = null; string cachedVideo; @@ -29,17 +35,14 @@ namespace OpenRA.Widgets Action onComplete; - public bool Paused { get { return paused; } } - public VqaReader Video { get { return video; } } - readonly World world; + [ObjectCreator.UseCtor] public VqaPlayerWidget(World world) { this.world = world; } - public bool DrawOverlay = true; public void Load(string filename) { if (filename == cachedVideo) @@ -58,25 +61,29 @@ namespace OpenRA.Widgets var size = Math.Max(video.Width, video.Height); var textureSize = Exts.NextPowerOf2(size); var videoSheet = new Sheet(new Size(textureSize, textureSize), false); + + videoSheet.Texture.ScaleFilter = TextureScaleFilter.Linear; videoSheet.Texture.SetData(video.FrameData); 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); - videoOrigin = new float2(RenderBounds.X + (RenderBounds.Width - scale * video.Width) / 2, RenderBounds.Y + (RenderBounds.Height - scale * video.Height) / 2); - videoSize = new float2(video.Width * scale, video.Height * scale); + 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 * AspectRatio * video.Height) / 2); + + // 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) 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; - for (var y = 0; y < video.Height; y++) - for (var x = 0; x < video.Width; x++) - overlay[2 * y, x] = black; + for (var y = 0; y < scaledHeight; y += 2) + overlay[y, 0] = 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); - 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() @@ -84,7 +91,7 @@ namespace OpenRA.Widgets if (video == null) return; - if (!(stopped || paused)) + if (!stopped && !paused) { var nextFrame = (int)float2.Lerp(0, video.Frames, Sound.VideoSeekPosition * invLength); if (nextFrame > video.Frames) diff --git a/OpenRA.Renderer.Null/NullGraphicsDevice.cs b/OpenRA.Renderer.Null/NullGraphicsDevice.cs index 3e2d8d6f67..5e2da9c886 100644 --- a/OpenRA.Renderer.Null/NullGraphicsDevice.cs +++ b/OpenRA.Renderer.Null/NullGraphicsDevice.cs @@ -80,6 +80,7 @@ namespace OpenRA.Renderer.Null public class NullTexture : ITexture { + public TextureScaleFilter ScaleFilter { get { return TextureScaleFilter.Nearest; } set { } } public void SetData(Bitmap bitmap) { } public void SetData(uint[,] colors) { } public void SetData(byte[] colors, int width, int height) { } diff --git a/OpenRA.Renderer.Sdl2/Texture.cs b/OpenRA.Renderer.Sdl2/Texture.cs index 8590a6242e..de82a06e8e 100644 --- a/OpenRA.Renderer.Sdl2/Texture.cs +++ b/OpenRA.Renderer.Sdl2/Texture.cs @@ -19,11 +19,29 @@ namespace OpenRA.Renderer.Sdl2 public class Texture : ITexture { int texture; + TextureScaleFilter scaleFilter; + Size size; + public int ID { get { return texture; } } - Size size; public Size Size { get { return size; } } + public TextureScaleFilter ScaleFilter + { + get + { + return scaleFilter; + } + set + { + if (scaleFilter == value) + return; + + scaleFilter = value; + PrepareTexture(); + } + } + public Texture() { GL.GenTextures(1, out texture); @@ -45,9 +63,11 @@ namespace OpenRA.Renderer.Sdl2 ErrorHandler.CheckGlError(); GL.BindTexture(TextureTarget.Texture2D, texture); 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(); - GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); + GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, filter); ErrorHandler.CheckGlError(); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureWrapMode.ClampToEdge);