Browse Source

Added scaling support for the rendering texture.

* Nearest (default) and linear filtering are supported.
    * Scaling is done by SDL, naturally.
    *** WE'RE BACK, BABY! ***
improved_timing
Ian Burgmyer 5 years ago
parent
commit
a11ef8df3c
  1. 1
      DotSDL.sln.DotSettings
  2. 16
      DotSDL/Graphics/ScalingQuality.cs
  3. 46
      DotSDL/Graphics/SdlWindow.cs
  4. 34
      DotSDL/Interop/Core/Hints.cs

1
DotSDL.sln.DotSettings

@ -101,6 +101,7 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpRenamePlacementToArrangementMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>

16
DotSDL/Graphics/ScalingQuality.cs

@ -0,0 +1,16 @@
namespace DotSDL.Graphics {
/// <summary>
/// The scaling (filtering) style that should be used for the application.</summary>
public enum ScalingQuality {
/// <summary>
/// Nearest pixel sampling. This should be used if crisp, square pixels are desired.
/// </summary>
Nearest = 0,
/// <summary>
/// Linear filtering. This will filter the surrounding pixels together, resulting
/// in a blurrier look.
/// </summary>
Linear = 1
}
}

46
DotSDL/Graphics/SdlWindow.cs

@ -24,7 +24,7 @@ namespace DotSDL.Graphics {
/// <summary><c>true</c> if this <see cref="SdlWindow"/> instance has been destroyed, othersize <c>false</c>.</summary>
public bool IsDestroyed { get; set; }
/// <summary><c>true</c> if this <see cref="SdlWindow"/> has been minimized, othersize <c>false</c>.</summary>
public bool IsMinimized { get; set; }
@ -98,7 +98,28 @@ namespace DotSDL.Graphics {
/// <param name="position">A <see cref="Point"/> representing the starting position of the window. The X and Y coordinates of the Point can be set to <see cref="WindowPosUndefined"/> or <see cref="WindowPosCentered"/>.</param>
/// <param name="windowWidth">The width of the window.</param>
/// <param name="windowHeight">The height of the window.</param>
public SdlWindow(string title, Point position, int windowWidth, int windowHeight) : this(title, position, windowWidth, windowHeight, windowWidth, windowHeight) { }
public SdlWindow(string title, Point position, int windowWidth, int windowHeight) : this(title, position, windowWidth, windowHeight, windowWidth, windowHeight, ScalingQuality.Nearest) { }
/// <summary>
/// Creates a new <see cref="SdlWindow"/>.
/// </summary>
/// <param name="title">The text that is displayed on the window's title bar.</param>
/// <param name="position">A <see cref="Point"/> representing the starting position of the window. The X and Y coordinates of the Point can be set to <see cref="WindowPosUndefined"/> or <see cref="WindowPosCentered"/>.</param>
/// <param name="windowWidth">The width of the window.</param>
/// <param name="windowHeight">The height of the window.</param>
/// <param name="scalingQuality">The scaling (filtering) method to use for the background canvas texture.</param>
public SdlWindow(string title, Point position, int windowWidth, int windowHeight, ScalingQuality scalingQuality) : this(title, position, windowWidth, windowHeight, windowWidth, windowHeight, scalingQuality) { }
/// <summary>
/// Creates a new <see cref="SdlWindow"/>.
/// </summary>
/// <param name="title">The text that is displayed on the window's title bar.</param>
/// <param name="position">A <see cref="Point"/> representing the starting position of the window. The X and Y coordinates of the Point can be set to <see cref="WindowPosUndefined"/> or <see cref="WindowPosCentered"/>.</param>
/// <param name="windowWidth">The width of the window.</param>
/// <param name="windowHeight">The height of the window.</param>
/// <param name="textureWidth">The width of the window's texture.</param>
/// <param name="textureHeight">The height of the window's texture.</param>
public SdlWindow(string title, Point position, int windowWidth, int windowHeight, int textureWidth, int textureHeight) : this(title, position, windowWidth, windowHeight, textureWidth, textureHeight, ScalingQuality.Nearest) { }
/// <summary>
/// Creates a new <see cref="SdlWindow"/>.
@ -109,13 +130,17 @@ namespace DotSDL.Graphics {
/// <param name="windowHeight">The height of the window.</param>
/// <param name="textureWidth">The width of the window's texture.</param>
/// <param name="textureHeight">The height of the window's texture.</param>
/// <param name="updateType">The method used to update the canvas.</param>
public SdlWindow(string title, Point position, int windowWidth, int windowHeight, int textureWidth, int textureHeight) {
/// <param name="scalingQuality">The scaling (filtering) method to use for the background canvas texture.</param>
public SdlWindow(string title, Point position, int windowWidth, int windowHeight, int textureWidth, int textureHeight, ScalingQuality scalingQuality) {
_sdlInit.InitSubsystem(Init.SubsystemFlags.Video);
_window = Video.CreateWindow(title, position.X, position.Y, windowWidth, windowHeight, Video.WindowFlags.Hidden);
_renderer = Render.CreateRenderer(_window, -1, Render.RendererFlags.Accelerated);
// Everything should be kept as nearest *except* for the target texture.
SetScalingQuality(scalingQuality);
_texture = Render.CreateTexture(_renderer, Pixels.PixelFormatArgb8888, Render.TextureAccess.Streaming, textureWidth, textureHeight);
SetScalingQuality(ScalingQuality.Nearest);
_canvas = new Canvas(textureWidth, textureHeight);
@ -144,7 +169,7 @@ namespace DotSDL.Graphics {
/// </summary>
private void BaseDraw() {
if(IsDestroyed || IsMinimized) return;
Render.UpdateTexture(_texture, IntPtr.Zero, GetCanvasPointer(), TextureWidth * 4);
Render.RenderCopy(_renderer, _texture, IntPtr.Zero, IntPtr.Zero);
Render.RenderPresent(_renderer);
@ -279,7 +304,7 @@ namespace DotSDL.Graphics {
/// </summary>
/// <param name="canvas">The active canvas for the window.</param>
protected virtual void OnDraw(ref Canvas canvas) { }
/// <summary>
/// Called before the window is shown.
/// </summary>
@ -306,6 +331,15 @@ namespace DotSDL.Graphics {
/// </summary>
protected virtual void OnUpdate() { }
/// <summary>
/// Sets the scaling/filter quality. This is set globally within SDL, so
/// for correctness sake it should be called before every texture is created.
/// </summary>
/// <param name="quality"></param>
private void SetScalingQuality(ScalingQuality quality) {
Hints.SetHint(Hints.RenderScaleQuality, quality.ToString());
}
/// <summary>
/// Displays the window and begins executing code that's associated with it.
/// </summary>

34
DotSDL/Interop/Core/Hints.cs

@ -0,0 +1,34 @@
using System.Runtime.InteropServices;
namespace DotSDL.Interop.Core {
internal static class Hints {
/// <summary>
/// Represents a variable controlling the scaling quality. This variable can be set
/// to one of the following values:
///
/// "0" or "nearest" - Nearest pixel sampling.
/// "1" or "linear" - Linear filtering (supported by OpenGL and Direct3D)
/// "2" or "best" - Anisotropic filtering (supported by Direct3D)
///
/// By default, nearest pixel sampling is used.
/// </summary>
internal const string RenderScaleQuality = "SDL_RENDER_SCALE_QUALITY";
/// <summary>
/// Gets the value of a hint.
/// </summary>
/// <param name="name">The name of the hint to set.</param>
/// <returns><c>true</c> if the hint was set, otherwise <c>false</c>.</returns>
[DllImport(Meta.CoreLib, EntryPoint = "SDL_SetHint", CallingConvention = CallingConvention.Cdecl)]
internal static extern string GetHint(string name);
/// <summary>
/// Sets a hint with normal priority.
/// </summary>
/// <param name="name">The name of the hint to set.</param>
/// <param name="value">The value to set the hint to.</param>
/// <returns><c>true</c> if the hint was set, otherwise <c>false</c>.</returns>
[DllImport(Meta.CoreLib, EntryPoint = "SDL_SetHint", CallingConvention = CallingConvention.Cdecl)]
internal static extern bool SetHint(string name, string value);
}
}
Loading…
Cancel
Save