Browse Source

Began implementing a Background class.

* Canvas is now an abstract class, with Sprite and Background
      inheriting it.
    * Backgrounds are now updated with texture locking like they should
      have been all along.
    * SdlWindow now creates its Background object using the Background
      class (don't worry--that system is going to be completely changing
      fairly soon as well ;)).
    * Changed Canvas.DestroyTexture() to be private.
    * Corrected some XMLDocs (yet again).
improved_timing
Ian Burgmyer 5 years ago
parent
commit
01672ba0bd
  1. 41
      DotSDL/Graphics/Background.cs
  2. 10
      DotSDL/Graphics/Canvas.cs
  3. 2
      DotSDL/Graphics/SdlWindow.cs
  4. 4
      DotSDL/Graphics/Sprite.cs
  5. 35
      DotSDL/Interop/Core/Render.cs

41
DotSDL/Graphics/Background.cs

@ -0,0 +1,41 @@
using DotSDL.Interop.Core;
using System;
namespace DotSDL.Graphics {
public class Background : Canvas {
/// <summary>
/// Initializes a new <see cref="Background"/>.
/// </summary>
/// <param name="textureWidth">The width of the new <see cref="Background"/>.</param>
/// <param name="textureHeight">The height of the new <see cref="Background"/>.</param>
public Background(int textureWidth, int textureHeight) : base(textureWidth, textureHeight) { }
/// <summary>
/// Initializes a new <see cref="Background"/>.
/// </summary>
/// <param name="textureWidth">The width of the new <see cref="Background"/>.</param>
/// <param name="textureHeight">The height of the new <see cref="Background"/>.</param>
/// <param name="clipping">A rectangle specifying which part of this <see cref="Background"/> should be drawn.</param>
public Background(int textureWidth, int textureHeight, Rectangle clipping) : base(textureWidth, textureHeight, clipping) { }
/// <inheritdoc/>
internal override void CreateTexture() {
CreateTexture(Render.TextureAccess.Streaming);
}
/// <inheritdoc/>
internal override bool UpdateTexture() {
if(!HasTexture) return false;
// TODO: Might be able to make this faster by leveraging memcpy(). Need to benchmark it.
unsafe {
var copySize = Width * Height * 4; // Width x Height at 4 bytes per pixel.
Render.LockTexture(Texture, IntPtr.Zero, out var pixels, out var _);
Buffer.MemoryCopy(GetCanvasPointer().ToPointer(), pixels, copySize, copySize);
Render.UnlockTexture(Texture);
}
return true;
}
}
}

10
DotSDL/Graphics/Canvas.cs

@ -7,7 +7,7 @@ namespace DotSDL.Graphics {
/// A representation of the contents of the SDL window, with a number of
/// helper routines.
/// </summary>
public class Canvas {
public abstract class Canvas {
private int _width, _height;
private Color _colorMod = new Color { R = 255, G = 255, B = 255, A = 255 };
private byte _opacity = 255;
@ -186,9 +186,7 @@ namespace DotSDL.Graphics {
/// <summary>
/// Creates a texture or recreates it if it already exists.
/// </summary>
internal virtual void CreateTexture() {
CreateTexture(Render.TextureAccess.Streaming);
}
internal abstract void CreateTexture();
/// <summary>
/// Creates a texture or recreates it if it already exists.
@ -210,7 +208,7 @@ namespace DotSDL.Graphics {
/// <summary>
/// Destroys the texture associated with this <see cref="Sprite"/>.
/// </summary>
internal void DestroyTexture() {
private void DestroyTexture() {
if(!HasTexture) return;
Render.DestroyTexture(Texture);
@ -267,7 +265,7 @@ namespace DotSDL.Graphics {
/// <see cref="Canvas.Pixels"/> array is changed.
/// </summary>
/// <returns><c>true</c> if the texture was successfully updated, otherwise <c>false</c>. This will return <c>false</c> if this <see cref="Sprite"/> hasn't been added to the sprite list.</returns>
internal bool UpdateTexture() {
internal virtual bool UpdateTexture() {
if(!HasTexture) return false;
Render.UpdateTexture(Texture, IntPtr.Zero, GetCanvasPointer(), Width * 4);
return true;

2
DotSDL/Graphics/SdlWindow.cs

@ -171,7 +171,7 @@ namespace DotSDL.Graphics {
ScalingQuality = scalingQuality;
CreateTexture();
Background = new Canvas(textureWidth, textureHeight) {
Background = new Background(textureWidth, textureHeight) {
Renderer = _renderer,
BlendMode = BlendMode.None
};

4
DotSDL/Graphics/Sprite.cs

@ -132,9 +132,7 @@ namespace DotSDL.Graphics {
RotationCenter = new Point(clipping.Size.X / 2, clipping.Size.Y / 2);
}
/// <summary>
/// Creates a texture or recreates it if it already exists.
/// </summary>
/// <inheritdoc/>
internal override void CreateTexture() {
CreateTexture(Render.TextureAccess.Static);
}

35
DotSDL/Interop/Core/Render.cs

@ -28,7 +28,7 @@ namespace DotSDL.Interop.Core {
/// <summary>
/// The access pattern allowed for a texture.
/// </summary>
internal enum TextureAccess : int {
internal enum TextureAccess {
/// <summary>Changes rarely, not lockable.</summary>
Static,
@ -77,6 +77,28 @@ namespace DotSDL.Interop.Core {
[DllImport(Meta.CoreLib, EntryPoint = "SDL_DestroyTexture", CallingConvention = CallingConvention.Cdecl)]
internal static extern void DestroyTexture(IntPtr texture);
/// <summary>
/// Locks a portion of a texture for write-only pixel access.
/// </summary>
/// <param name="texture">The texture to lock for access. This texture must have been created with <see cref="TextureAccess.Streaming"/>.</param>
/// <param name="rect">An <see cref="Rect.SdlRect"/> structure representing the area to lock for access, or <see cref="IntPtr.Zero"/> to lock the entire texture.</param>
/// <param name="pixels">A pointer to the locked pixels, offset by the locked area.</param>
/// <param name="pitch">The pitch of the locked pixels. The pitch is the length of one row in bytes.</param>
/// <returns>0 on success or a negative error code if the texture is not valid or was not created with <see cref="TextureAccess.Streaming"/>.</returns>
[DllImport(Meta.CoreLib, EntryPoint = "SDL_LockTexture", CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe int LockTexture(IntPtr texture, IntPtr rect, out void* pixels, out int pitch);
/// <summary>
/// Locks a portion of a texture for write-only pixel access.
/// </summary>
/// <param name="texture">The texture to lock for access. This texture must have been created with <see cref="TextureAccess.Streaming"/>.</param>
/// <param name="rect">An <see cref="Rect.SdlRect"/> structure representing the area to lock for access, or <see cref="IntPtr.Zero"/> to lock the entire texture.</param>
/// <param name="pixels">A pointer to the locked pixels, offset by the locked area.</param>
/// <param name="pitch">The pitch of the locked pixels. The pitch is the length of one row in bytes.</param>
/// <returns>0 on success or a negative error code if the texture is not valid or was not created with <see cref="TextureAccess.Streaming"/>.</returns>
[DllImport(Meta.CoreLib, EntryPoint = "SDL_LockTexture", CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe int LockTexture(IntPtr texture, Rect.SdlRect rect, out void* pixels, out int pitch);
/// <summary>
/// Clear the current rendering target with the drawing color.
///
@ -119,7 +141,7 @@ namespace DotSDL.Interop.Core {
/// <param name="angle">An angle, in degrees, that indicates the rotation that will be applied to <paramref name="dstRect"/>.</param>
/// <param name="center">An <see cref="Rect.SdlPoint"/> indicating the point around which <paramref name="dstRect"/> will be rotated
/// (if NULL, rotation will be done around dstRect.w/2, dstRect.h/2).</param>
/// <param name="flip">A <see cref="RendererFlip"/> value indicating which flipping actions should be performed.</param>
/// <param name="flip">A <see cref="FlipDirection"/> value indicating which flipping actions should be performed.</param>
/// <returns>0 on success, or -1 on error.</returns>
[DllImport(Meta.CoreLib, EntryPoint = "SDL_RenderCopyEx", CallingConvention = CallingConvention.Cdecl)]
internal static extern int RenderCopyEx(IntPtr renderer, IntPtr texture, Rect.SdlRect srcRect, Rect.SdlRect dstRect, double angle, Rect.SdlPoint center, FlipDirection flip);
@ -134,7 +156,7 @@ namespace DotSDL.Interop.Core {
/// <param name="angle">An angle, in degrees, that indicates the rotation that will be applied to <paramref name="dstRect"/>.</param>
/// <param name="center">An <see cref="Rect.SdlPoint"/> indicating the point around which <paramref name="dstRect"/> will be rotated
/// (if NULL, rotation will be done around dstRect.w/2, dstRect.h/2).</param>
/// <param name="flip">A <see cref="RendererFlip"/> value indicating which flipping actions should be performed.</param>
/// <param name="flip">A <see cref="FlipDirection"/> value indicating which flipping actions should be performed.</param>
/// <returns>0 on success, or -1 on error.</returns>
[DllImport(Meta.CoreLib, EntryPoint = "SDL_RenderCopyEx", CallingConvention = CallingConvention.Cdecl)]
internal static extern int RenderCopyEx(IntPtr renderer, IntPtr texture, IntPtr srcRect, IntPtr dstRect, double angle, Rect.SdlPoint center, FlipDirection flip);
@ -185,6 +207,13 @@ namespace DotSDL.Interop.Core {
[DllImport(Meta.CoreLib, EntryPoint = "SDL_SetTextureBlendMode", CallingConvention = CallingConvention.Cdecl)]
internal static extern int SetTextureBlendMode(IntPtr texture, BlendMode blendMode);
/// <summary>
/// Unlocks a texture, uploading the changes to video memory if needed.
/// </summary>
/// <param name="texture">A locked texture.</param>
[DllImport(Meta.CoreLib, EntryPoint = "SDL_UnlockTexture", CallingConvention = CallingConvention.Cdecl)]
internal static extern void UnlockTexture(IntPtr texture);
/// <summary>
/// Update the given texture rectangle with new pixel data.
/// </summary>

Loading…
Cancel
Save