+3
.gitignore
+3
.gitignore
-1
src/Peridot.Demo/Peridot.Demo.csproj
-1
src/Peridot.Demo/Peridot.Demo.csproj
···
9
9
10
10
<ItemGroup>
11
11
<PackageReference Include="Ez.Numerics" Version="0.2.0-alpha" />
12
-
<PackageReference Include="StbImageSharp" Version="2.22.6" />
13
12
<PackageReference Include="System.Resources.Extensions" Version="6.0.0" />
14
13
<PackageReference Include="Veldrid.StartupUtilities" Version="4.8.0" />
15
14
</ItemGroup>
+76
-74
src/Peridot.Demo/Program.cs
+76
-74
src/Peridot.Demo/Program.cs
···
1
1
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
2
// This code is licensed under MIT license (see LICENSE for details)
3
-
4
-
using Peridot.Veldrid;
5
3
using System.Drawing;
6
4
using Veldrid;
7
5
using Veldrid.StartupUtilities;
8
6
using System.Numerics;
9
7
using Peridot.Demo;
10
8
11
-
using Font = Peridot.Veldrid.Font;
12
9
using StbImageSharp;
13
10
using System.Diagnostics;
14
11
using Rectangle = System.Drawing.Rectangle;
15
12
using Peridot;
13
+
using Peridot.Text;
16
14
17
15
var title = "Peridot.Demo";
18
16
var wci = new WindowCreateInfo(100, 100, 640, 480, WindowState.Normal, title);
19
17
20
18
var window = VeldridStartup.CreateWindow(wci);
21
-
var gd = VeldridStartup.CreateDefaultD3D11GraphicsDevice(
22
-
new(true,
23
-
Veldrid.PixelFormat.D32_Float_S8_UInt,
24
-
true,
25
-
ResourceBindingModel.Default,
26
-
true,
27
-
true),
28
-
window);
19
+
var gd = VeldridStartup.CreateVulkanGraphicsDevice(
20
+
new(true,
21
+
Veldrid.PixelFormat.D32_Float_S8_UInt,
22
+
true,
23
+
ResourceBindingModel.Default,
24
+
true,
25
+
true),
26
+
window);
29
27
30
28
window.Resized += () =>
31
29
{
32
-
gd.MainSwapchain.Resize((uint)window.Width, (uint)window.Height);
30
+
gd.MainSwapchain.Resize((uint)window.Width, (uint)window.Height);
33
31
};
34
32
35
-
var shaders = VeldridSpriteBatch.LoadDefaultShaders(gd);
36
-
var sb = new VeldridSpriteBatch(gd, gd.MainSwapchain.Framebuffer.OutputDescription, shaders);
37
-
var tr = new TextRenderer(gd, sb);
33
+
var peridot = new VPeridot(gd);
34
+
35
+
var shaders = peridot.LoadDefaultShaders();
36
+
var sbd = new SpriteBatchDescriptor(gd.MainSwapchain.Framebuffer.OutputDescription, shaders);
37
+
var sb = peridot.CreateSpriteBatch(sbd);
38
+
39
+
var tr = new TextRenderer(peridot, sb);
38
40
var cl = gd.ResourceFactory.CreateCommandList();
39
41
var fence = gd.ResourceFactory.CreateFence(false);
40
42
var texture = LoadTexture(Resource._4_2_07);
···
46
48
var ps = 0;
47
49
while (window.Exists)
48
50
{
49
-
count++;
51
+
count++;
50
52
51
-
var now = DateTime.Now;
52
-
var delta = now - time;
53
-
var deltaTime = delta.TotalSeconds;
54
-
time = now;
53
+
var now = DateTime.Now;
54
+
var delta = now - time;
55
+
var deltaTime = delta.TotalSeconds;
56
+
time = now;
55
57
56
-
if((count % 1000) == 0)
57
-
{
58
-
window.Title = title + " " + (1f / deltaTime);
59
-
}
58
+
if((count % 1000) == 0)
59
+
{
60
+
window.Title = title + " " + (1f / deltaTime);
61
+
}
60
62
61
-
window.PumpEvents();
63
+
window.PumpEvents();
62
64
63
-
sb.Begin();
64
-
sb.ViewMatrix = Matrix4x4.CreateOrthographic(window.Width, window.Height, 0.01f, -100f);
65
-
66
-
var size = new Vector2(texture.Width, texture.Height);
67
-
var pos = size * -0.5f;
68
-
var source = new System.Drawing.Rectangle((int)texture.Width / 2, (int)texture.Height / 2, (int)texture.Width / 2, (int)texture.Height / 2);
65
+
sb.Begin();
66
+
sb.ViewMatrix = Matrix4x4.CreateOrthographic(window.Width, window.Height, 0.01f, -100f);
67
+
68
+
var size = new Vector2(texture.Width, texture.Height);
69
+
var pos = size * -0.5f;
70
+
var source = new System.Drawing.Rectangle((int)texture.Width / 2, (int)texture.Height / 2, (int)texture.Width / 2, (int)texture.Height / 2);
69
71
70
-
ps += neg ? -1 : 1;
71
-
if (ps == 0 || ps == 100)
72
-
neg = !neg;
72
+
ps += neg ? -1 : 1;
73
+
if (ps == 0 || ps == 100)
74
+
neg = !neg;
73
75
74
-
var options = ps >= 0 && ps < 25 ? SpriteOptions.None :
75
-
ps >= 25 && ps < 50 ? SpriteOptions.FlipVertically :
76
-
ps >= 50 && ps < 75 ? SpriteOptions.FlipVertically | SpriteOptions.FlipHorizontally :
77
-
SpriteOptions.FlipHorizontally;
76
+
var options = ps >= 0 && ps < 25 ? SpriteOptions.None :
77
+
ps >= 25 && ps < 50 ? SpriteOptions.FlipVertically :
78
+
ps >= 50 && ps < 75 ? SpriteOptions.FlipVertically | SpriteOptions.FlipHorizontally :
79
+
SpriteOptions.FlipHorizontally;
78
80
79
-
sb.Draw(texture, default, source, Color.White, 0, new(-size.X / 6, 0), Vector2.One, options, 1f);
81
+
sb.Draw(texture, default, source, Color.White, 0, new(-size.X / 6, 0), Vector2.One, options, 1f);
80
82
81
-
var s = font.MeasureString("Hello World!", 32);
82
-
var strScissor = new Rectangle(0, 0, (int)s.X, (int)s.Y * ps / 100);
83
-
tr.DrawString(font, 32, "Hello World!", new Vector2(1, 1), Color.White, 0, new Vector2(0, 0), new Vector2(1), 2f, strScissor);
84
-
sb.End();
83
+
var s = font.MeasureString("Hello World!", 32);
84
+
var strScissor = new Rectangle(0, 0, (int)s.X, (int)s.Y * ps / 100);
85
+
tr.DrawString(font, 32, "Hello World!", new Vector2(1, 1), Color.White, 0, new Vector2(0, 0), new Vector2(1), 2f, strScissor);
86
+
sb.End();
85
87
86
-
cl.Begin();
87
-
cl.SetFramebuffer(gd.SwapchainFramebuffer);
88
-
cl.ClearColorTarget(0, RgbaFloat.CornflowerBlue);
89
-
cl.ClearDepthStencil(0f);
90
-
sb.DrawBatch(cl);
91
-
cl.End();
88
+
cl.Begin();
89
+
cl.SetFramebuffer(gd.SwapchainFramebuffer);
90
+
cl.ClearColorTarget(0, RgbaFloat.CornflowerBlue);
91
+
cl.ClearDepthStencil(0f);
92
+
cl.DrawBatch(sb);
93
+
cl.End();
92
94
93
-
fence.Reset();
94
-
gd.SubmitCommands(cl, fence);
95
-
gd.WaitForFence(fence);
96
-
gd.SwapBuffers();
95
+
fence.Reset();
96
+
gd.SubmitCommands(cl, fence);
97
+
gd.WaitForFence(fence);
98
+
gd.SwapBuffers();
97
99
}
98
100
99
101
Texture LoadTexture(byte[] data)
100
102
{
101
-
ImageResult image;
102
-
{
103
-
image = ImageResult.FromMemory(data, ColorComponents.Default);
104
-
Debug.Assert(image != null);
105
-
}
103
+
ImageResult image;
104
+
{
105
+
image = ImageResult.FromMemory(data, ColorComponents.Default);
106
+
Debug.Assert(image != null);
107
+
}
106
108
107
-
var td = new TextureDescription(
108
-
(uint)image.Width, (uint)image.Height, 1,
109
-
1, 1,
110
-
PixelFormat.R8_G8_B8_A8_UNorm,
111
-
TextureUsage.Sampled,
112
-
TextureType.Texture2D);
109
+
var td = new TextureDescription(
110
+
(uint)image.Width, (uint)image.Height, 1,
111
+
1, 1,
112
+
Veldrid.PixelFormat.R8_G8_B8_A8_UNorm,
113
+
TextureUsage.Sampled,
114
+
TextureType.Texture2D);
113
115
114
-
var texture = gd.ResourceFactory.CreateTexture(td);
115
-
gd.UpdateTexture(texture, image.Data, 0, 0, 0, td.Width, td.Height, td.Depth, 0, 0);
116
+
var texture = gd.ResourceFactory.CreateTexture(td);
117
+
gd.UpdateTexture(texture, image.Data, 0, 0, 0, td.Width, td.Height, td.Depth, 0, 0);
116
118
117
-
return texture;
119
+
return texture;
118
120
}
119
121
120
122
Font LoadFont(byte[] data)
121
123
{
122
-
using var stream = new MemoryStream();
123
-
stream.Write(data);
124
-
stream.Position = 0;
125
-
var font = new Font();
126
-
font.AddFont(data);
127
-
return font;
128
-
}
124
+
using var stream = new MemoryStream();
125
+
stream.Write(data);
126
+
stream.Position = 0;
127
+
var font = new Font();
128
+
font.AddFont(data);
129
+
return font;
130
+
}
-78
src/Peridot.Veldrid/Font.cs
-78
src/Peridot.Veldrid/Font.cs
···
1
-
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
-
// This code is licensed under MIT license (see LICENSE for details)
3
-
4
-
using FontStashSharp;
5
-
using System.Numerics;
6
-
7
-
namespace Peridot.Veldrid
8
-
{
9
-
/// <summary>
10
-
/// Represents a font that can be rendered with <see cref="TextRenderer"/>.
11
-
/// </summary>
12
-
public class Font : IFont
13
-
{
14
-
/// <summary>
15
-
/// Creates a new instance of <see cref="Font"/>.
16
-
/// </summary>
17
-
public Font()
18
-
{
19
-
var settings = new FontSystemSettings
20
-
{
21
-
Effect = FontSystemEffect.None,
22
-
EffectAmount = 2,
23
-
};
24
-
FontSystem = new FontSystem(settings);
25
-
}
26
-
27
-
/// <summary>
28
-
/// Destroys a instance of <see cref="TextureWrapper"/>.
29
-
/// </summary>
30
-
~Font()
31
-
{
32
-
Dispose(false);
33
-
}
34
-
35
-
internal FontSystem FontSystem { get; }
36
-
37
-
/// <inheritdoc/>
38
-
public bool IsDisposed { get; private set; }
39
-
40
-
/// <summary>
41
-
/// Add a character font to the font.
42
-
/// </summary>
43
-
/// <param name="stream">The stream that contains the font.</param>
44
-
public void AddFont(Stream stream)
45
-
{
46
-
FontSystem.AddFont(stream);
47
-
}
48
-
49
-
/// <summary>
50
-
/// Add a character font to the font.
51
-
/// </summary>
52
-
/// <param name="data">The data that contains the font.</param>
53
-
public void AddFont(byte[] data)
54
-
{
55
-
FontSystem.AddFont(data);
56
-
}
57
-
58
-
/// <inheritdoc/>
59
-
public Vector2 MeasureString(string text, int fontSize)
60
-
{
61
-
var font = FontSystem.GetFont(fontSize);
62
-
return font.MeasureString(text);
63
-
}
64
-
65
-
/// <inheritdoc/>
66
-
public void Dispose()
67
-
{
68
-
Dispose(true);
69
-
GC.SuppressFinalize(this);
70
-
}
71
-
72
-
private void Dispose(bool disposing)
73
-
{
74
-
if (disposing)
75
-
FontSystem.Dispose();
76
-
}
77
-
}
78
-
}
-66
src/Peridot.Veldrid/FontStashRenderer.cs
-66
src/Peridot.Veldrid/FontStashRenderer.cs
···
1
-
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
-
// This code is licensed under MIT license (see LICENSE for details)
3
-
4
-
using FontStashSharp.Interfaces;
5
-
using System.Drawing;
6
-
using System.Numerics;
7
-
using Veldrid;
8
-
9
-
namespace Peridot.Veldrid
10
-
{
11
-
internal class FontStashRenderer : IFontStashRenderer, IDisposable
12
-
{
13
-
private bool _disposed;
14
-
private readonly VeldridSpriteBatch _sb;
15
-
private readonly Texture2DManager _texture2DManager;
16
-
public FontStashRenderer(GraphicsDevice gd, VeldridSpriteBatch sb)
17
-
{
18
-
_texture2DManager = new Texture2DManager(gd);
19
-
_sb = sb;
20
-
ResetScissor();
21
-
}
22
-
23
-
~FontStashRenderer()
24
-
{
25
-
Dispose(false);
26
-
}
27
-
28
-
public Rectangle Scissor { get; set; }
29
-
30
-
public ITexture2DManager TextureManager => _texture2DManager;
31
-
32
-
public void Dispose()
33
-
{
34
-
Dispose(true);
35
-
}
36
-
37
-
private void Dispose(bool disposing)
38
-
{
39
-
if (_disposed)
40
-
return;
41
-
_disposed = true;
42
-
if (disposing)
43
-
_texture2DManager.Dispose();
44
-
}
45
-
46
-
public void ResetScissor()
47
-
{
48
-
const int v = 1 << 23;
49
-
const int s = -(1 << 22);
50
-
Scissor = new Rectangle(s, s, v, v);
51
-
}
52
-
53
-
public void Draw(object texture, Vector2 pos, Rectangle? src, Color color, float rotation, Vector2 origin, Vector2 scale, float depth)
54
-
{
55
-
if (texture is not Texture t)
56
-
throw new ArgumentException(nameof(texture));
57
-
58
-
var s = src ?? new(0, 0, (int)t.Width, (int)t.Height);
59
-
60
-
var oldScissor = _sb.Scissor;
61
-
_sb.Scissor = RectangleF.Intersect(_sb.Scissor, Scissor);
62
-
_sb.Draw(t, pos, s, color, rotation, origin, new Vector2(scale.X, scale.Y), depth);
63
-
_sb.Scissor = oldScissor;
64
-
}
65
-
}
66
-
}
+1
-1
src/Peridot.Veldrid/Peridot.Veldrid.csproj
+1
-1
src/Peridot.Veldrid/Peridot.Veldrid.csproj
···
4
4
<TargetFramework>net6.0</TargetFramework>
5
5
<ImplicitUsings>enable</ImplicitUsings>
6
6
<Nullable>enable</Nullable>
7
+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7
8
<Description>Peridot implementation using Veldrid lib.</Description>
8
9
</PropertyGroup>
9
10
···
11
12
12
13
<ItemGroup>
13
14
<PackageReference Include="Ez.Memory" Version="0.2.0-alpha" />
14
-
<PackageReference Include="FontStashSharp" Version="1.0.4" />
15
15
<PackageReference Include="Veldrid" Version="4.8.0" />
16
16
<PackageReference Include="Veldrid.SPIRV" Version="1.0.14" />
17
17
</ItemGroup>
+23
src/Peridot.Veldrid/PeridotCommandListExtensions.cs
+23
src/Peridot.Veldrid/PeridotCommandListExtensions.cs
···
1
+
using Veldrid;
2
+
3
+
namespace Peridot;
4
+
5
+
/// <summary>
6
+
/// Peridot command list extensions.
7
+
/// </summary>
8
+
public static class PeridotCommandListExtensions
9
+
{
10
+
/// <summary>
11
+
/// Draw this branch into a <see cref="CommandList"/>.
12
+
/// Call this after calling <see cref="SpriteBatch{TTexture}.End"/>
13
+
/// </summary>
14
+
/// <param name="spriteBatch"></param>
15
+
/// <param name="commandList"></param>
16
+
public static void DrawBatch(this CommandList commandList, ISpriteBatch spriteBatch)
17
+
{
18
+
if (spriteBatch is not VSpriteBatch sb)
19
+
throw new NotSupportedException();
20
+
21
+
sb.DrawBatch(commandList);
22
+
}
23
+
}
+1
-1
src/Peridot.Veldrid/Resource.Designer.cs
+1
-1
src/Peridot.Veldrid/Resource.Designer.cs
+75
src/Peridot.Veldrid/SpriteBatchDescriptor.cs
+75
src/Peridot.Veldrid/SpriteBatchDescriptor.cs
···
1
+
using System;
2
+
using System.Collections.Generic;
3
+
using System.Linq;
4
+
using System.Threading.Tasks;
5
+
using Veldrid;
6
+
7
+
namespace Peridot
8
+
{
9
+
/// <summary>
10
+
/// Describes a sprite batch.
11
+
/// </summary>
12
+
public struct SpriteBatchDescriptor
13
+
{
14
+
/// <summary>
15
+
/// Creates a sprite batch descriptior.
16
+
/// </summary>
17
+
/// <param name="outputDescription">The output description of target framebuffer.</param>
18
+
/// <param name="shaders">The shaders to use to render. Uses <seealso cref="VPeridot.LoadDefaultShaders(GraphicsDevice)"/> for default.</param>
19
+
/// <param name="sampler">The samppler used to sample.</param>
20
+
/// <param name="cullMode">Controls which face will be culled. By default the sprite are rendered with forward normal, negatives scales can flips that normal.</param>
21
+
/// <param name="blendState">The blend state description for creating the pipeline.</param>
22
+
/// <param name="depthStencil">The depth stencil state description for creating the pipeline.</param>
23
+
public SpriteBatchDescriptor(
24
+
OutputDescription outputDescription,
25
+
Shader[] shaders,
26
+
Sampler? sampler = null,
27
+
FaceCullMode cullMode = FaceCullMode.None,
28
+
BlendStateDescription? blendState = null,
29
+
DepthStencilStateDescription? depthStencil = null)
30
+
{
31
+
OutputDescription = outputDescription;
32
+
Shaders = shaders;
33
+
Sampler = sampler;
34
+
CullMode = cullMode;
35
+
BlendState = blendState;
36
+
DepthStencil = depthStencil;
37
+
}
38
+
39
+
/// <summary>
40
+
/// The output description of target framebuffer.
41
+
/// </summary>
42
+
public OutputDescription OutputDescription { init; get; }
43
+
44
+
/// <summary>
45
+
/// The shaders to use to render.
46
+
/// Uses <seealso cref="VPeridot.LoadDefaultShaders(GraphicsDevice)"/> for default.
47
+
/// </summary>
48
+
public Shader[] Shaders { init; get; }
49
+
50
+
/// <summary>
51
+
/// The samppler used to sample.
52
+
/// If null, will use LinearSampler.
53
+
/// </summary>
54
+
public Sampler? Sampler { init; get; }
55
+
56
+
/// <summary>
57
+
/// Controls which face will be culled.
58
+
/// By default the sprite are rendered with forward normal, negatives scales can
59
+
/// flips that normal.
60
+
/// </summary>
61
+
public FaceCullMode CullMode { init; get; }
62
+
63
+
/// <summary>
64
+
/// The blend state description for creating the pipeline.
65
+
/// If null, will use SingleAlphaBlend.
66
+
/// </summary>
67
+
public BlendStateDescription? BlendState { init; get; }
68
+
69
+
/// <summary>
70
+
/// The depth stencil state description for creating the pipeline.
71
+
/// If null, will use depth test, depth write enable and comparison kind GreaterEqual.
72
+
/// </summary>
73
+
public DepthStencilStateDescription? DepthStencil { init; get; }
74
+
}
75
+
}
-90
src/Peridot.Veldrid/TextRenderer.cs
-90
src/Peridot.Veldrid/TextRenderer.cs
···
1
-
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
-
// This code is licensed under MIT license (see LICENSE for details)
3
-
4
-
using System.Drawing;
5
-
using System.Numerics;
6
-
using Veldrid;
7
-
8
-
namespace Peridot.Veldrid
9
-
{
10
-
/// <inheritdoc/>
11
-
public class TextRenderer : ITextRenderer<Font>
12
-
{
13
-
private readonly VeldridSpriteBatch _sb;
14
-
private readonly FontStashRenderer _textRenderer;
15
-
16
-
/// <summary>
17
-
/// Creates a new instance of <see cref="TextRenderer"/>.
18
-
/// </summary>
19
-
/// <param name="gd"></param>
20
-
/// <param name="spriteBatch"></param>
21
-
public TextRenderer(GraphicsDevice gd, VeldridSpriteBatch spriteBatch)
22
-
{
23
-
_sb = spriteBatch;
24
-
_textRenderer = new(gd, _sb);
25
-
}
26
-
27
-
/// <inheritdoc/>
28
-
~TextRenderer()
29
-
{
30
-
Dispose(false);
31
-
}
32
-
33
-
/// <inheritdoc/>
34
-
public bool IsDisposed { get; private set; }
35
-
36
-
/// <inheritdoc/>
37
-
public void Dispose()
38
-
{
39
-
GC.SuppressFinalize(this);
40
-
Dispose(true);
41
-
}
42
-
43
-
private void Dispose(bool disposing)
44
-
{
45
-
if (IsDisposed)
46
-
return;
47
-
IsDisposed = true;
48
-
49
-
if (disposing)
50
-
{
51
-
52
-
}
53
-
}
54
-
55
-
/// <inheritdoc/>
56
-
public void DrawString(IFont font, int fontSize, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, float layerDepth)
57
-
{
58
-
DrawString(Cast(font), fontSize, text, position, color, rotation, origin, scale, layerDepth);
59
-
}
60
-
61
-
/// <inheritdoc/>
62
-
public void DrawString(IFont font, int fontSize, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, float layerDepth, Rectangle scissor)
63
-
{
64
-
DrawString(Cast(font), fontSize, text, position, color, rotation, origin, scale, layerDepth, scissor);
65
-
}
66
-
67
-
/// <inheritdoc/>
68
-
public void DrawString(Font font, int fontSize, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, float layerDepth)
69
-
{
70
-
var rf = font.FontSystem.GetFont(fontSize);
71
-
rf.DrawText(_textRenderer, text, position, color, scale, rotation, origin, layerDepth);
72
-
}
73
-
74
-
/// <inheritdoc/>
75
-
public void DrawString(Font font, int fontSize, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, float layerDepth, Rectangle scissor)
76
-
{
77
-
var rf = font.FontSystem.GetFont(fontSize);
78
-
_textRenderer.Scissor = scissor;
79
-
rf.DrawText(_textRenderer, text, position, color, scale, rotation, origin, layerDepth);
80
-
_textRenderer.ResetScissor();
81
-
}
82
-
83
-
private static Font Cast(IFont font)
84
-
{
85
-
if (font is not Font f)
86
-
throw new InvalidCastException($"The {font} is not supported by this implementation.");
87
-
return f;
88
-
}
89
-
}
90
-
}
-76
src/Peridot.Veldrid/Texture2DManager.cs
-76
src/Peridot.Veldrid/Texture2DManager.cs
···
1
-
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
-
// This code is licensed under MIT license (see LICENSE for details)
3
-
4
-
using FontStashSharp.Interfaces;
5
-
using System.Drawing;
6
-
using Veldrid;
7
-
8
-
namespace Peridot.Veldrid
9
-
{
10
-
internal class Texture2DManager : ITexture2DManager, IDisposable
11
-
{
12
-
private readonly GraphicsDevice _gd;
13
-
private readonly List<WeakReference<Texture>> _textures;
14
-
private bool _disposed;
15
-
public Texture2DManager(GraphicsDevice gd)
16
-
{
17
-
_gd = gd;
18
-
_textures = new();
19
-
}
20
-
21
-
~Texture2DManager()
22
-
{
23
-
Dispose(false);
24
-
}
25
-
26
-
public object CreateTexture(int width, int height)
27
-
{
28
-
var texture = _gd.ResourceFactory.CreateTexture(
29
-
new(
30
-
(uint)width, (uint)height, 1,
31
-
1, 1,
32
-
PixelFormat.B8_G8_R8_A8_UNorm,
33
-
TextureUsage.Sampled,
34
-
TextureType.Texture2D));
35
-
36
-
_textures.Add(new(texture));
37
-
return texture;
38
-
}
39
-
40
-
public void Dispose()
41
-
{
42
-
Dispose(true);
43
-
}
44
-
45
-
private void Dispose(bool disposing)
46
-
{
47
-
if (_disposed)
48
-
return;
49
-
_disposed = true;
50
-
51
-
if (disposing)
52
-
{
53
-
foreach (var wt in _textures)
54
-
if (wt.TryGetTarget(out var texture))
55
-
texture.Dispose();
56
-
}
57
-
}
58
-
59
-
public Point GetTextureSize(object texture)
60
-
{
61
-
var t = texture as Texture;
62
-
if (t is null)
63
-
return Point.Empty;
64
-
65
-
return new((int)t.Width, (int)t.Height);
66
-
}
67
-
68
-
public void SetTextureData(object texture, Rectangle bounds, byte[] data)
69
-
{
70
-
if (texture is not Texture t)
71
-
return;
72
-
73
-
_gd.UpdateTexture(t, data, (uint)bounds.X, (uint)bounds.Y, 0, (uint)bounds.Width, (uint)bounds.Height, 1, 0, 0);
74
-
}
75
-
}
76
-
}
-65
src/Peridot.Veldrid/TextureWrapper.cs
-65
src/Peridot.Veldrid/TextureWrapper.cs
···
1
-
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
-
// This code is licensed under MIT license (see LICENSE for details)
3
-
4
-
using System.Diagnostics.CodeAnalysis;
5
-
using System.Drawing;
6
-
using Veldrid;
7
-
8
-
namespace Peridot.Veldrid;
9
-
10
-
/// <summary>
11
-
/// A wrapper with <see cref="ITexture2D"/> interface.
12
-
/// </summary>
13
-
public class TextureWrapper : ITexture2D, IEquatable<TextureWrapper>
14
-
{
15
-
/// <summary>
16
-
/// Creates a new instance of <see cref="TextureWrapper"/>.
17
-
/// </summary>
18
-
/// <param name="texture">The texture.</param>
19
-
/// <exception cref="ArgumentNullException"></exception>
20
-
public TextureWrapper(Texture texture)
21
-
{
22
-
Texture = texture ?? throw new ArgumentNullException(nameof(texture));
23
-
}
24
-
25
-
/// <summary>
26
-
/// Texture
27
-
/// </summary>
28
-
public Texture Texture { get; }
29
-
30
-
/// <summary>
31
-
/// Size of texture
32
-
/// </summary>
33
-
public Size Size => new((int)Texture.Width, (int)Texture.Height);
34
-
35
-
/// <inheritdoc/>
36
-
public bool IsDisposed => Texture.IsDisposed;
37
-
38
-
/// <inheritdoc/>
39
-
public override int GetHashCode() => Texture.GetHashCode();
40
-
41
-
/// <inheritdoc/>
42
-
public bool Equals(TextureWrapper? other) => Texture == other?.Texture;
43
-
44
-
/// <inheritdoc/>
45
-
public override bool Equals([NotNullWhen(true)] object? obj) =>
46
-
obj is TextureWrapper tw && Equals(tw);
47
-
48
-
/// <inheritdoc/>
49
-
public override string? ToString() => Texture.ToString();
50
-
51
-
/// <inheritdoc/>
52
-
public void Dispose()
53
-
{
54
-
Texture.Dispose();
55
-
GC.SuppressFinalize(this);
56
-
}
57
-
58
-
public static implicit operator TextureWrapper(Texture t) => new(t);
59
-
60
-
public static implicit operator Texture(TextureWrapper t) => t.Texture;
61
-
62
-
public static bool operator ==(TextureWrapper left, TextureWrapper right) => left.Equals(right);
63
-
64
-
public static bool operator !=(TextureWrapper left, TextureWrapper right) => !(left == right);
65
-
}
+79
src/Peridot.Veldrid/VImage.cs
+79
src/Peridot.Veldrid/VImage.cs
···
1
+
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
+
// This code is licensed under MIT license (see LICENSE for details)
3
+
4
+
using System.Diagnostics.CodeAnalysis;
5
+
using System.Drawing;
6
+
using Veldrid;
7
+
8
+
namespace Peridot;
9
+
10
+
/// <summary>
11
+
/// A wrapper with <see cref="VImage"/> interface.
12
+
/// </summary>
13
+
internal class VImage : Image, IEquatable<VImage>
14
+
{
15
+
/// <summary>
16
+
/// Creates a new instance of <see cref="VImage"/>.
17
+
/// </summary>
18
+
/// <param name="texture">The texture.</param>
19
+
/// <exception cref="ArgumentNullException"></exception>
20
+
public VImage(Texture texture)
21
+
{
22
+
Texture = texture ?? throw new ArgumentNullException(nameof(texture));
23
+
}
24
+
25
+
/// <summary>
26
+
/// Texture
27
+
/// </summary>
28
+
public Texture Texture { get; }
29
+
30
+
/// <summary>
31
+
/// Size of texture
32
+
/// </summary>
33
+
public Size Size => new((int)Texture.Width, (int)Texture.Height);
34
+
35
+
/// <inheritdoc/>
36
+
public bool IsDisposed => Texture.IsDisposed;
37
+
38
+
/// <inheritdoc/>
39
+
public PixelFormat Format => Texture.Format switch
40
+
{
41
+
Veldrid.PixelFormat.R8_UNorm => PixelFormat.R8,
42
+
Veldrid.PixelFormat.R16_UNorm => PixelFormat.R16,
43
+
Veldrid.PixelFormat.B8_G8_R8_A8_UNorm => PixelFormat.BGRA8,
44
+
Veldrid.PixelFormat.R32_G32_B32_A32_Float => PixelFormat.RGBAF32,
45
+
Veldrid.PixelFormat.R8_G8_UNorm => PixelFormat.RG8,
46
+
Veldrid.PixelFormat.R16_G16_UNorm => PixelFormat.RG16,
47
+
Veldrid.PixelFormat.R32_G32_B32_A32_SInt => PixelFormat.RGBAI32,
48
+
Veldrid.PixelFormat.R8_G8_B8_A8_UNorm => PixelFormat.RGBA8,
49
+
_ => PixelFormat.Undefined
50
+
};
51
+
52
+
/// <inheritdoc/>
53
+
public override int GetHashCode() => Texture.GetHashCode();
54
+
55
+
/// <inheritdoc/>
56
+
public bool Equals(VImage? other) => Texture == other?.Texture;
57
+
58
+
/// <inheritdoc/>
59
+
public override bool Equals([NotNullWhen(true)] object? obj) =>
60
+
obj is VImage tw && Equals(tw);
61
+
62
+
/// <inheritdoc/>
63
+
public override string? ToString() => Texture.ToString();
64
+
65
+
/// <inheritdoc/>
66
+
public void Dispose()
67
+
{
68
+
Texture.Dispose();
69
+
GC.SuppressFinalize(this);
70
+
}
71
+
72
+
public static implicit operator VImage(Texture t) => new(t);
73
+
74
+
public static implicit operator Texture(VImage t) => t.Texture;
75
+
76
+
public static bool operator ==(VImage left, VImage right) => left.Equals(right);
77
+
78
+
public static bool operator !=(VImage left, VImage right) => !(left == right);
79
+
}
+108
src/Peridot.Veldrid/VPeridot.cs
+108
src/Peridot.Veldrid/VPeridot.cs
···
1
+
using System.Diagnostics;
2
+
using System.Text;
3
+
using Veldrid;
4
+
using Veldrid.SPIRV;
5
+
using VPF = Veldrid.PixelFormat;
6
+
namespace Peridot;
7
+
8
+
public class VPeridot : IPeridot<Texture, SpriteBatchDescriptor>
9
+
{
10
+
private readonly GraphicsDevice m_gd;
11
+
private readonly Image m_whiteImage;
12
+
public VPeridot(GraphicsDevice gd)
13
+
{
14
+
m_gd = gd;
15
+
m_whiteImage = CreateImage(new ImageDescription()
16
+
{
17
+
Format = PixelFormat.BGRA8,
18
+
Width = 1,
19
+
Height = 1,
20
+
});
21
+
22
+
UpdateImage<byte>(m_whiteImage, new byte[] { 255, 255, 255, 255 }, 0, 0, 1, 1);
23
+
}
24
+
25
+
internal GraphicsDevice GraphicsDevice => m_gd;
26
+
27
+
/// <inheritdoc/>
28
+
public Image CreateImage(ImageDescription description)
29
+
{
30
+
var pf = description.Format switch
31
+
{
32
+
PixelFormat.R8 => VPF.R8_UNorm,
33
+
PixelFormat.R16 => VPF.R16_UNorm,
34
+
PixelFormat.BGRA8 => VPF.B8_G8_R8_A8_UNorm,
35
+
PixelFormat.RGBAF32 => VPF.R32_G32_B32_A32_Float,
36
+
PixelFormat.RG8 => VPF.R8_G8_UNorm,
37
+
PixelFormat.RG16 => VPF.R16_G16_UNorm,
38
+
PixelFormat.RGBAI32 => VPF.R8_G8_B8_A8_SInt,
39
+
PixelFormat.RGBA8 => VPF.R8_G8_B8_A8_UNorm,
40
+
_ => throw new ArgumentOutOfRangeException(nameof(description.Format))
41
+
};
42
+
TextureDescription desc = new(description.Width, description.Height, 1, 1, 1, pf, TextureUsage.Sampled, TextureType.Texture2D);
43
+
return new VImage(m_gd.ResourceFactory.CreateTexture(desc));
44
+
}
45
+
46
+
/// <inheritdoc/>
47
+
public void UpdateImage<T>(Image image, ReadOnlySpan<T> source, uint x, uint y, uint width, uint height) where T : unmanaged
48
+
{
49
+
Debug.Assert(image is VImage);
50
+
if (image is not VImage vimage)
51
+
throw new ArgumentOutOfRangeException(nameof(image));
52
+
53
+
unsafe
54
+
{
55
+
uint size = (uint)source.Length * (uint)sizeof(T);
56
+
fixed (T* ptr = source)
57
+
{
58
+
m_gd.UpdateTexture(vimage, (IntPtr)ptr, size, x, y, 0, width, height, 1, 0, 0);
59
+
}
60
+
}
61
+
}
62
+
63
+
/// <inheritdoc/>
64
+
public ISpriteBatch<Texture> CreateSpriteBatch(SpriteBatchDescriptor descriptor)
65
+
{
66
+
var s = descriptor.Sampler ?? GraphicsDevice.LinearSampler;
67
+
var bs = descriptor.BlendState ?? BlendStateDescription.SingleAlphaBlend;
68
+
var ds = descriptor.DepthStencil ?? new(
69
+
depthTestEnabled: true,
70
+
depthWriteEnabled: true,
71
+
comparisonKind: ComparisonKind.GreaterEqual);
72
+
73
+
return new VSpriteBatch(
74
+
this,
75
+
m_whiteImage,
76
+
descriptor.OutputDescription,
77
+
descriptor.Shaders,
78
+
s,
79
+
descriptor.CullMode,
80
+
bs,
81
+
ds);
82
+
}
83
+
84
+
/// <summary>
85
+
/// Loads default shaders for <see cref="ISpriteBatch"/>.
86
+
/// </summary>
87
+
/// <returns>Shaders for use in <see cref="ISpriteBatch"/>.</returns>
88
+
public Shader[] LoadDefaultShaders()
89
+
{
90
+
91
+
var vertexShaderDesc = new ShaderDescription(
92
+
ShaderStages.Vertex,
93
+
Encoding.UTF8.GetBytes(Resource.shader_vert),
94
+
"main");
95
+
96
+
var fragmentShaderDesc = new ShaderDescription(
97
+
ShaderStages.Fragment,
98
+
Encoding.UTF8.GetBytes(Resource.shader_frag),
99
+
"main");
100
+
101
+
return GraphicsDevice.ResourceFactory.CreateFromSpirv(vertexShaderDesc, fragmentShaderDesc);
102
+
}
103
+
104
+
ISpriteBatch IPeridot<SpriteBatchDescriptor>.CreateSpriteBatch(SpriteBatchDescriptor description)
105
+
{
106
+
return CreateSpriteBatch(description);
107
+
}
108
+
}
+200
src/Peridot.Veldrid/VSpriteBatch.cs
+200
src/Peridot.Veldrid/VSpriteBatch.cs
···
1
+
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
+
// This code is licensed under MIT license (see LICENSE for details)
3
+
4
+
using Ez.Memory;
5
+
using System.Drawing;
6
+
using System.Numerics;
7
+
8
+
using Veldrid;
9
+
10
+
namespace Peridot
11
+
{
12
+
/// <inheritdoc/>
13
+
internal class VSpriteBatch : SpriteBatch<Texture>
14
+
{
15
+
private readonly GraphicsDevice m_device;
16
+
private readonly DeviceBuffer m_vertexBuffer;
17
+
private readonly Sampler m_sampler;
18
+
private Pipeline m_pipeline;
19
+
private readonly ResourceLayout[] m_resourceLayouts;
20
+
private Dictionary<Texture, (DeviceBuffer buffer, ResourceSet set, ResourceSet textureSet)> m_buffers;
21
+
22
+
internal VSpriteBatch(
23
+
VPeridot peridot,
24
+
Image whiteImage,
25
+
OutputDescription outputDescription,
26
+
Shader[] shaders,
27
+
Sampler sampler,
28
+
FaceCullMode cullMode,
29
+
BlendStateDescription blendState,
30
+
DepthStencilStateDescription depthStencil) : base(whiteImage)
31
+
{
32
+
m_device = peridot.GraphicsDevice;
33
+
m_sampler = sampler ?? m_device.LinearSampler;
34
+
m_vertexBuffer = CreateVertexBuffer(m_device);
35
+
m_resourceLayouts = CreateResourceLayouts(m_device);
36
+
m_buffers = new();
37
+
38
+
var bs = blendState;
39
+
var ds = depthStencil;
40
+
m_pipeline = CreatePipeline(m_device, outputDescription, cullMode, bs, ds, shaders, m_resourceLayouts);
41
+
}
42
+
43
+
/// <summary>
44
+
/// Draw this branch into a <see cref="CommandList"/>.
45
+
/// Call this after calling <see cref="SpriteBatch{TTexture}.End"/>
46
+
/// </summary>
47
+
/// <param name="commandList"></param>
48
+
public void DrawBatch(CommandList commandList)
49
+
{
50
+
var matrixSize = (int)MemUtil.SizeOf<Matrix4x4>();
51
+
commandList.SetPipeline(m_pipeline);
52
+
foreach (var item in m_batcher)
53
+
{
54
+
var texture = item.Key;
55
+
var group = item.Value;
56
+
57
+
var pair = GetBuffer(texture, group.Count + matrixSize);
58
+
var mapped = m_device.Map(pair.buffer, MapMode.Write);
59
+
60
+
MemUtil.Set(mapped.Data, ViewMatrix, 1);
61
+
MemUtil.Copy(mapped.Data + matrixSize, group.GetSpan());
62
+
63
+
m_device.Unmap(pair.buffer);
64
+
65
+
commandList.SetVertexBuffer(0, m_vertexBuffer);
66
+
commandList.SetGraphicsResourceSet(0, pair.set);
67
+
commandList.SetGraphicsResourceSet(1, pair.textureSet);
68
+
commandList.Draw(4, (uint)group.Count, 0, 0);
69
+
}
70
+
}
71
+
72
+
internal (DeviceBuffer buffer, ResourceSet set, ResourceSet textureSet) GetBuffer(Texture t, int count)
73
+
{
74
+
var texture = t as Texture;
75
+
var structSize = MemUtil.SizeOf<BatchItem>();
76
+
var size = ((count + 63) & (~63)) * structSize;
77
+
var bci = new BufferDescription((uint)size, BufferUsage.StructuredBufferReadOnly | BufferUsage.Dynamic, structSize);
78
+
79
+
if (!m_buffers.TryGetValue(texture, out var pair))
80
+
{
81
+
pair.buffer = m_device.ResourceFactory.CreateBuffer(bci);
82
+
pair.set = m_device.ResourceFactory.CreateResourceSet(new ResourceSetDescription(m_resourceLayouts[0], pair.buffer));
83
+
pair.textureSet = m_device.ResourceFactory.CreateResourceSet(new ResourceSetDescription(m_resourceLayouts[1], texture, m_sampler));
84
+
m_buffers[texture] = pair;
85
+
}
86
+
else if (size > pair.buffer.SizeInBytes)
87
+
{
88
+
pair.set.Dispose();
89
+
pair.buffer.Dispose();
90
+
91
+
pair.buffer = m_device.ResourceFactory.CreateBuffer(bci);
92
+
pair.set = m_device.ResourceFactory.CreateResourceSet(new ResourceSetDescription(m_resourceLayouts[0], pair.buffer));
93
+
pair.textureSet = m_device.ResourceFactory.CreateResourceSet(new ResourceSetDescription(m_resourceLayouts[1], texture, m_sampler));
94
+
m_buffers[texture] = pair;
95
+
}
96
+
97
+
return pair;
98
+
}
99
+
100
+
/// <inheritdoc/>
101
+
protected override void Dispose(bool disposing)
102
+
{
103
+
if (disposing)
104
+
{
105
+
m_vertexBuffer.Dispose();
106
+
}
107
+
}
108
+
109
+
private static DeviceBuffer CreateVertexBuffer(GraphicsDevice device)
110
+
{
111
+
var bd = new BufferDescription()
112
+
{
113
+
SizeInBytes = 4 * MemUtil.SizeOf<Vector2>(),
114
+
Usage = BufferUsage.VertexBuffer,
115
+
};
116
+
117
+
var buffer = device.ResourceFactory.CreateBuffer(bd);
118
+
119
+
var vertices = new Vector2[]
120
+
{
121
+
new( 0, 0),
122
+
new( 1, 0),
123
+
new( 0, 1),
124
+
new( 1, 1)
125
+
};
126
+
127
+
device.UpdateBuffer(buffer, 0, vertices);
128
+
129
+
return buffer;
130
+
}
131
+
132
+
private static ResourceLayout[] CreateResourceLayouts(GraphicsDevice device)
133
+
{
134
+
var layouts = new ResourceLayout[2];
135
+
136
+
var elements = new ResourceLayoutElementDescription[]
137
+
{
138
+
new("Items", ResourceKind.StructuredBufferReadOnly, ShaderStages.Vertex),
139
+
};
140
+
var rld = new ResourceLayoutDescription(elements);
141
+
142
+
layouts[0] = device.ResourceFactory.CreateResourceLayout(rld);
143
+
144
+
elements = new ResourceLayoutElementDescription[]
145
+
{
146
+
new("Tex", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
147
+
new("Sampler", ResourceKind.Sampler, ShaderStages.Fragment)
148
+
};
149
+
rld = new ResourceLayoutDescription(elements);
150
+
151
+
layouts[1] = device.ResourceFactory.CreateResourceLayout(rld);
152
+
return layouts;
153
+
}
154
+
155
+
private static Pipeline CreatePipeline(GraphicsDevice device,
156
+
OutputDescription outputDescription,
157
+
FaceCullMode cullMode,
158
+
BlendStateDescription blendState,
159
+
DepthStencilStateDescription depthStencil,
160
+
Shader[] shaders,
161
+
params ResourceLayout[] layouts)
162
+
{
163
+
var vertexLayout = new VertexLayoutDescription(
164
+
new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2));
165
+
166
+
var pipelineDescription = new GraphicsPipelineDescription
167
+
{
168
+
BlendState = blendState,
169
+
DepthStencilState = depthStencil,
170
+
RasterizerState = new(
171
+
cullMode: cullMode,
172
+
fillMode: PolygonFillMode.Solid,
173
+
frontFace: FrontFace.Clockwise,
174
+
depthClipEnabled: true,
175
+
scissorTestEnabled: false),
176
+
PrimitiveTopology = PrimitiveTopology.TriangleStrip,
177
+
ResourceLayouts = layouts,
178
+
ShaderSet = new(
179
+
vertexLayouts: new[] { vertexLayout },
180
+
shaders: shaders,
181
+
specializations: new[] { new SpecializationConstant(0, device.IsClipSpaceYInverted) }),
182
+
Outputs = outputDescription,
183
+
};
184
+
185
+
return device.ResourceFactory.CreateGraphicsPipeline(pipelineDescription);
186
+
}
187
+
188
+
protected override Size GetSize(Texture image)
189
+
{
190
+
return new((int)image.Width, (int)image.Height);
191
+
}
192
+
193
+
protected override Texture GetHandle(Image image)
194
+
{
195
+
if (image is not VImage vimage)
196
+
throw new InvalidCastException($"The {image} is not supported by this implementation.");
197
+
return vimage.Texture;
198
+
}
199
+
}
200
+
}
-222
src/Peridot.Veldrid/VeldridSpriteBatch.cs
-222
src/Peridot.Veldrid/VeldridSpriteBatch.cs
···
1
-
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
-
// This code is licensed under MIT license (see LICENSE for details)
3
-
4
-
using Ez.Memory;
5
-
6
-
using System.Numerics;
7
-
using System.Text;
8
-
9
-
using Veldrid;
10
-
using Veldrid.SPIRV;
11
-
12
-
namespace Peridot.Veldrid
13
-
{
14
-
/// <inheritdoc/>
15
-
public class VeldridSpriteBatch : SpriteBatch<TextureWrapper>
16
-
{
17
-
private readonly GraphicsDevice _device;
18
-
private readonly DeviceBuffer _vertexBuffer;
19
-
private readonly Sampler _sampler;
20
-
private Pipeline _pipeline;
21
-
private readonly ResourceLayout[] _resourceLayouts;
22
-
private Dictionary<Texture, (DeviceBuffer buffer, ResourceSet set, ResourceSet textureSet)> _buffers;
23
-
24
-
/// <summary>
25
-
/// Creates a new instance of <see cref="VeldridSpriteBatch"/>.
26
-
/// </summary>
27
-
/// <param name="device">The graphics device to create resources.</param>
28
-
/// <param name="outputDescription">The output description of target framebuffer.</param>
29
-
/// <param name="shaders">The shaders to use to render. Uses <seealso cref="LoadDefaultShaders(GraphicsDevice)"/> for default.</param>
30
-
/// <param name="sampler">The samppler used to sample.</param>
31
-
/// <param name="cullMode">Controls which face will be culled. By default the sprite are rendered with forward normal, negatives scales can flips that normal.</param>
32
-
/// <param name="blendState">The blend state description for creating the pipeline.</param>
33
-
/// <param name="depthStencil">The depth stencil state description for creating the pipeline.</param>
34
-
public VeldridSpriteBatch(GraphicsDevice device,
35
-
OutputDescription outputDescription,
36
-
Shader[] shaders,
37
-
Sampler? sampler = null,
38
-
FaceCullMode cullMode = FaceCullMode.None,
39
-
BlendStateDescription? blendState = null,
40
-
DepthStencilStateDescription? depthStencil = null) : base()
41
-
{
42
-
_sampler = sampler ?? device.LinearSampler;
43
-
_device = device;
44
-
_vertexBuffer = CreateVertexBuffer(device);
45
-
_resourceLayouts = CreateResourceLayouts(device);
46
-
_buffers = new();
47
-
48
-
var bs = blendState ?? BlendStateDescription.SingleAlphaBlend;
49
-
var ds = depthStencil ?? new(
50
-
depthTestEnabled: true,
51
-
depthWriteEnabled: true,
52
-
comparisonKind: ComparisonKind.GreaterEqual);
53
-
_pipeline = CreatePipeline(device, outputDescription, cullMode, bs, ds, shaders, _resourceLayouts);
54
-
}
55
-
56
-
/// <summary>
57
-
/// Draw this branch into a <see cref="CommandList"/>.
58
-
/// Call this after calling <see cref="SpriteBatch{TTexture}.End"/>
59
-
/// </summary>
60
-
/// <param name="commandList"></param>
61
-
public void DrawBatch(CommandList commandList)
62
-
{
63
-
var matrixSize = (int)MemUtil.SizeOf<Matrix4x4>();
64
-
commandList.SetPipeline(_pipeline);
65
-
foreach (var item in _batcher)
66
-
{
67
-
var texture = item.Key;
68
-
var group = item.Value;
69
-
70
-
var pair = GetBuffer(texture, group.Count + matrixSize);
71
-
var mapped = _device.Map(pair.buffer, MapMode.Write);
72
-
73
-
MemUtil.Set(mapped.Data, ViewMatrix, 1);
74
-
MemUtil.Copy(mapped.Data + matrixSize, group.GetSpan());
75
-
76
-
_device.Unmap(pair.buffer);
77
-
78
-
commandList.SetVertexBuffer(0, _vertexBuffer);
79
-
commandList.SetGraphicsResourceSet(0, pair.set);
80
-
commandList.SetGraphicsResourceSet(1, pair.textureSet);
81
-
commandList.Draw(4, (uint)group.Count, 0, 0);
82
-
}
83
-
}
84
-
85
-
internal (DeviceBuffer buffer, ResourceSet set, ResourceSet textureSet) GetBuffer(Texture t, int count)
86
-
{
87
-
var texture = t as Texture;
88
-
var structSize = MemUtil.SizeOf<BatchItem>();
89
-
var size = ((count + 63) & (~63)) * structSize;
90
-
var bci = new BufferDescription((uint)size, BufferUsage.StructuredBufferReadOnly | BufferUsage.Dynamic, structSize);
91
-
92
-
if (!_buffers.TryGetValue(texture, out var pair))
93
-
{
94
-
pair.buffer = _device.ResourceFactory.CreateBuffer(bci);
95
-
pair.set = _device.ResourceFactory.CreateResourceSet(new ResourceSetDescription(_resourceLayouts[0], pair.buffer));
96
-
pair.textureSet = _device.ResourceFactory.CreateResourceSet(new ResourceSetDescription(_resourceLayouts[1], texture, _sampler));
97
-
_buffers[texture] = pair;
98
-
}
99
-
else if (size > pair.buffer.SizeInBytes)
100
-
{
101
-
pair.set.Dispose();
102
-
pair.buffer.Dispose();
103
-
104
-
pair.buffer = _device.ResourceFactory.CreateBuffer(bci);
105
-
pair.set = _device.ResourceFactory.CreateResourceSet(new ResourceSetDescription(_resourceLayouts[0], pair.buffer));
106
-
pair.textureSet = _device.ResourceFactory.CreateResourceSet(new ResourceSetDescription(_resourceLayouts[1], texture, _sampler));
107
-
_buffers[texture] = pair;
108
-
}
109
-
110
-
return pair;
111
-
}
112
-
113
-
/// <inheritdoc/>
114
-
protected override void Dispose(bool disposing)
115
-
{
116
-
if (disposing)
117
-
{
118
-
_vertexBuffer.Dispose();
119
-
}
120
-
}
121
-
122
-
private static DeviceBuffer CreateVertexBuffer(GraphicsDevice device)
123
-
{
124
-
var bd = new BufferDescription()
125
-
{
126
-
SizeInBytes = 4 * MemUtil.SizeOf<Vector2>(),
127
-
Usage = BufferUsage.VertexBuffer,
128
-
};
129
-
130
-
var buffer = device.ResourceFactory.CreateBuffer(bd);
131
-
132
-
var vertices = new Vector2[]
133
-
{
134
-
new( 0, 0),
135
-
new( 1, 0),
136
-
new( 0, 1),
137
-
new( 1, 1)
138
-
};
139
-
140
-
device.UpdateBuffer(buffer, 0, vertices);
141
-
142
-
return buffer;
143
-
}
144
-
145
-
private static ResourceLayout[] CreateResourceLayouts(GraphicsDevice device)
146
-
{
147
-
var layouts = new ResourceLayout[2];
148
-
149
-
var elements = new ResourceLayoutElementDescription[]
150
-
{
151
-
new("Items", ResourceKind.StructuredBufferReadOnly, ShaderStages.Vertex),
152
-
};
153
-
var rld = new ResourceLayoutDescription(elements);
154
-
155
-
layouts[0] = device.ResourceFactory.CreateResourceLayout(rld);
156
-
157
-
elements = new ResourceLayoutElementDescription[]
158
-
{
159
-
new("Tex", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
160
-
new("Sampler", ResourceKind.Sampler, ShaderStages.Fragment)
161
-
};
162
-
rld = new ResourceLayoutDescription(elements);
163
-
164
-
layouts[1] = device.ResourceFactory.CreateResourceLayout(rld);
165
-
return layouts;
166
-
}
167
-
168
-
private static Pipeline CreatePipeline(GraphicsDevice device,
169
-
OutputDescription outputDescription,
170
-
FaceCullMode cullMode,
171
-
BlendStateDescription blendState,
172
-
DepthStencilStateDescription depthStencil,
173
-
Shader[] shaders,
174
-
params ResourceLayout[] layouts)
175
-
{
176
-
var vertexLayout = new VertexLayoutDescription(
177
-
new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2));
178
-
179
-
var pipelineDescription = new GraphicsPipelineDescription
180
-
{
181
-
BlendState = blendState,
182
-
DepthStencilState = depthStencil,
183
-
RasterizerState = new(
184
-
cullMode: cullMode,
185
-
fillMode: PolygonFillMode.Solid,
186
-
frontFace: FrontFace.Clockwise,
187
-
depthClipEnabled: true,
188
-
scissorTestEnabled: false),
189
-
PrimitiveTopology = PrimitiveTopology.TriangleStrip,
190
-
ResourceLayouts = layouts,
191
-
ShaderSet = new(
192
-
vertexLayouts: new[] { vertexLayout },
193
-
shaders: shaders,
194
-
specializations: new[] { new SpecializationConstant(0, device.IsClipSpaceYInverted) }),
195
-
Outputs = outputDescription,
196
-
};
197
-
198
-
return device.ResourceFactory.CreateGraphicsPipeline(pipelineDescription);
199
-
}
200
-
201
-
/// <summary>
202
-
/// Loads default shaders for <see cref="VeldridSpriteBatch"/>.
203
-
/// </summary>
204
-
/// <param name="graphicsDevice">The graphics device.</param>
205
-
/// <returns>Shaders for use in <see cref="VeldridSpriteBatch"/>.</returns>
206
-
public static Shader[] LoadDefaultShaders(GraphicsDevice graphicsDevice)
207
-
{
208
-
209
-
var vertexShaderDesc = new ShaderDescription(
210
-
ShaderStages.Vertex,
211
-
Encoding.UTF8.GetBytes(Resource.shader_vert),
212
-
"main");
213
-
214
-
var fragmentShaderDesc = new ShaderDescription(
215
-
ShaderStages.Fragment,
216
-
Encoding.UTF8.GetBytes(Resource.shader_frag),
217
-
"main");
218
-
219
-
return graphicsDevice.ResourceFactory.CreateFromSpirv(vertexShaderDesc, fragmentShaderDesc);
220
-
}
221
-
}
222
-
}
+49
-50
src/Peridot/BatchGroup.cs
+49
-50
src/Peridot/BatchGroup.cs
···
1
1
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
2
// This code is licensed under MIT license (see LICENSE for details)
3
3
4
-
namespace Peridot
4
+
namespace Peridot;
5
+
6
+
public class BatchGroup
5
7
{
6
-
public class BatchGroup
7
-
{
8
-
private BatchItem[] _items;
8
+
private BatchItem[] _items;
9
9
10
-
public BatchGroup()
11
-
{
12
-
_items = new BatchItem[64];
13
-
}
14
-
public int Count { get; private set; }
10
+
public BatchGroup()
11
+
{
12
+
_items = new BatchItem[64];
13
+
}
14
+
public int Count { get; private set; }
15
15
16
-
public ref BatchItem Add()
17
-
{
18
-
if (Count >= _items.Length)
19
-
{
20
-
var lastSize = _items.Length;
21
-
var newSize = (lastSize + lastSize / 2 + 63) & (~63);
22
-
Array.Resize(ref _items, newSize);
23
-
}
16
+
public ref BatchItem Add()
17
+
{
18
+
if (Count >= _items.Length)
19
+
{
20
+
var lastSize = _items.Length;
21
+
var newSize = (lastSize + lastSize / 2 + 63) & (~63);
22
+
Array.Resize(ref _items, newSize);
23
+
}
24
24
25
-
return ref _items[Count++];
26
-
}
25
+
return ref _items[Count++];
26
+
}
27
27
28
-
public void Clear()
29
-
{
30
-
Count = 0;
31
-
}
28
+
public void Clear()
29
+
{
30
+
Count = 0;
31
+
}
32
32
33
-
public ReadOnlySpan<BatchItem> GetSpan() => new ReadOnlySpan<BatchItem>(_items, 0, Count);
33
+
public ReadOnlySpan<BatchItem> GetSpan() => new ReadOnlySpan<BatchItem>(_items, 0, Count);
34
34
35
-
public Enumerator GetEnumerator() => new(_items, Count);
35
+
public Enumerator GetEnumerator() => new(_items, Count);
36
36
37
-
public struct Enumerator
38
-
{
39
-
private readonly BatchItem[] _items;
40
-
private readonly int _count;
41
-
private int _index;
37
+
public struct Enumerator
38
+
{
39
+
private readonly BatchItem[] _items;
40
+
private readonly int _count;
41
+
private int _index;
42
42
43
-
internal Enumerator(BatchItem[] items, int count)
44
-
{
45
-
_items = items;
46
-
_count = count;
47
-
_index = -1;
48
-
}
43
+
internal Enumerator(BatchItem[] items, int count)
44
+
{
45
+
_items = items;
46
+
_count = count;
47
+
_index = -1;
48
+
}
49
49
50
-
public BatchItem Current => _items[_index];
50
+
public BatchItem Current => _items[_index];
51
51
52
-
public bool MoveNext()
53
-
{
54
-
if (_index >= _count)
55
-
return false;
56
-
_index++;
57
-
return true;
58
-
}
52
+
public bool MoveNext()
53
+
{
54
+
if (_index >= _count)
55
+
return false;
56
+
_index++;
57
+
return true;
58
+
}
59
59
60
-
public void Reset()
61
-
{
62
-
_index = -1;
63
-
}
64
-
}
65
-
}
60
+
public void Reset()
61
+
{
62
+
_index = -1;
63
+
}
64
+
}
66
65
}
+54
-55
src/Peridot/BatchItem.cs
+54
-55
src/Peridot/BatchItem.cs
···
4
4
using System.Drawing;
5
5
using System.Numerics;
6
6
7
-
namespace Peridot
7
+
namespace Peridot;
8
+
9
+
public struct BatchItem
8
10
{
9
-
public struct BatchItem
11
+
public BatchItem(Vector2 textureSize, RectangleF destinationRectangle, RectangleF sourceRectangle, Color color,
12
+
float rotation, Vector2 origin, float layerDepth, RectangleF scissor, SpriteOptions options)
10
13
{
11
-
public BatchItem(Vector2 textureSize, RectangleF destinationRectangle, RectangleF sourceRectangle, Color color,
12
-
float rotation, Vector2 origin, float layerDepth, RectangleF scissor, SpriteOptions options)
13
-
{
14
-
var sourceSize = new Vector2(sourceRectangle.Width, sourceRectangle.Height) / textureSize;
15
-
var pos = new Vector2(sourceRectangle.X, sourceRectangle.Y) / textureSize;
14
+
var sourceSize = new Vector2(sourceRectangle.Width, sourceRectangle.Height) / textureSize;
15
+
var pos = new Vector2(sourceRectangle.X, sourceRectangle.Y) / textureSize;
16
16
17
-
UV = CreateUV(options, sourceSize, pos);
18
-
Color = ToVector(color);
19
-
Scale = destinationRectangle.Size.ToVector2();
20
-
Origin = origin;
21
-
Location = new(destinationRectangle.Location.ToVector2(), layerDepth);
22
-
Rotation = rotation;
23
-
Scissor = scissor;
24
-
}
17
+
UV = CreateUV(options, sourceSize, pos);
18
+
Color = ToVector(color);
19
+
Scale = destinationRectangle.Size.ToVector2();
20
+
Origin = origin;
21
+
Location = new(destinationRectangle.Location.ToVector2(), layerDepth);
22
+
Rotation = rotation;
23
+
Scissor = scissor;
24
+
}
25
25
26
-
public BatchItem(Vector2 textureSize, Vector2 position, RectangleF sourceRectangle, Color color, float rotation,
27
-
Vector2 origin, Vector2 scale, float layerDepth, RectangleF scissor, SpriteOptions options)
28
-
: this(textureSize, new RectangleF(position.X, position.Y, sourceRectangle.Width * scale.X, sourceRectangle.Height * scale.Y),
29
-
sourceRectangle,
30
-
color,
31
-
rotation,
32
-
origin,
33
-
layerDepth,
34
-
scissor,
35
-
options)
36
-
{
37
-
}
26
+
public BatchItem(Vector2 textureSize, Vector2 position, RectangleF sourceRectangle, Color color, float rotation,
27
+
Vector2 origin, Vector2 scale, float layerDepth, RectangleF scissor, SpriteOptions options)
28
+
: this(textureSize, new RectangleF(position.X, position.Y, sourceRectangle.Width * scale.X, sourceRectangle.Height * scale.Y),
29
+
sourceRectangle,
30
+
color,
31
+
rotation,
32
+
origin,
33
+
layerDepth,
34
+
scissor,
35
+
options)
36
+
{
37
+
}
38
38
39
-
public Vector4 UV { get; set; }
40
-
public Vector4 Color { get; set; }
41
-
public Vector2 Scale { get; set; }
42
-
public Vector2 Origin { get; set; }
43
-
public Vector3 Location { get; set; }
44
-
public float Rotation { get; set; }
45
-
public RectangleF Scissor { get; set; }
39
+
public Vector4 UV { get; set; }
40
+
public Vector4 Color { get; set; }
41
+
public Vector2 Scale { get; set; }
42
+
public Vector2 Origin { get; set; }
43
+
public Vector3 Location { get; set; }
44
+
public float Rotation { get; set; }
45
+
public RectangleF Scissor { get; set; }
46
46
47
-
private static Vector4 CreateUV(SpriteOptions options, Vector2 sourceSize, Vector2 sourceLocation)
47
+
private static Vector4 CreateUV(SpriteOptions options, Vector2 sourceSize, Vector2 sourceLocation)
48
+
{
49
+
if (options != SpriteOptions.None)
48
50
{
49
-
if (options != SpriteOptions.None)
51
+
// flipX
52
+
if (options.HasFlag(SpriteOptions.FlipHorizontally))
50
53
{
51
-
// flipX
52
-
if (options.HasFlag(SpriteOptions.FlipHorizontally))
53
-
{
54
-
sourceLocation.X += sourceSize.X;
55
-
sourceSize.X *= -1;
56
-
}
57
-
58
-
// flipY
59
-
if (options.HasFlag(SpriteOptions.FlipVertically))
60
-
{
61
-
sourceLocation.Y += sourceSize.Y;
62
-
sourceSize.Y *= -1;
63
-
}
54
+
sourceLocation.X += sourceSize.X;
55
+
sourceSize.X *= -1;
64
56
}
65
57
66
-
return new(sourceLocation.X, sourceLocation.Y, sourceSize.X, sourceSize.Y);
58
+
// flipY
59
+
if (options.HasFlag(SpriteOptions.FlipVertically))
60
+
{
61
+
sourceLocation.Y += sourceSize.Y;
62
+
sourceSize.Y *= -1;
63
+
}
67
64
}
68
65
69
-
private static Vector4 ToVector(Color color) => new Vector4(color.R, color.G, color.B, color.A) * (1f / 255f);
66
+
return new(sourceLocation.X, sourceLocation.Y, sourceSize.X, sourceSize.Y);
67
+
}
68
+
69
+
private static Vector4 ToVector(Color color) => new Vector4(color.R, color.G, color.B, color.A) * (1f / 255f);
70
70
71
-
public override string ToString()
72
-
{
73
-
return $"uv: {UV}, color: {Color}, scale: {Scale}, origin: {Origin}, location: {new Vector4(Location, Rotation)}, scissor: {Scissor}";
74
-
}
71
+
public override string ToString()
72
+
{
73
+
return $"uv: {UV}, color: {Color}, scale: {Scale}, origin: {Origin}, location: {new Vector4(Location, Rotation)}, scissor: {Scissor}";
75
74
}
76
75
}
+33
-34
src/Peridot/Batcher.cs
+33
-34
src/Peridot/Batcher.cs
···
1
1
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
2
// This code is licensed under MIT license (see LICENSE for details)
3
3
4
-
namespace Peridot
4
+
namespace Peridot;
5
+
6
+
public class Batcher<TTexture> where TTexture : notnull
5
7
{
6
-
public class Batcher<TTexture> where TTexture : notnull
8
+
private Stack<BatchGroup> _batchGroups;
9
+
private Dictionary<TTexture, BatchGroup> _batchItems;
10
+
11
+
public Batcher()
7
12
{
8
-
private Stack<BatchGroup> _batchGroups;
9
-
private Dictionary<TTexture, BatchGroup> _batchItems;
13
+
_batchItems = new();
14
+
_batchGroups = new();
15
+
}
10
16
11
-
public Batcher()
17
+
public ref BatchItem Add(TTexture texture)
18
+
{
19
+
if (!_batchItems.TryGetValue(texture, out var group))
12
20
{
13
-
_batchItems = new();
14
-
_batchGroups = new();
21
+
group = GetBatchGroup();
22
+
group.Clear();
23
+
_batchItems[texture] = group;
15
24
}
16
25
17
-
public ref BatchItem Add(TTexture texture)
18
-
{
19
-
if(!_batchItems.TryGetValue(texture, out var group))
20
-
{
21
-
group = GetBatchGroup();
22
-
group.Clear();
23
-
_batchItems[texture] = group;
24
-
}
26
+
return ref group.Add();
27
+
}
25
28
26
-
return ref group.Add();
27
-
}
29
+
public void Clear()
30
+
{
31
+
foreach (var group in this)
32
+
ReturnBatchGroup(group.Value);
33
+
_batchItems.Clear();
34
+
}
28
35
29
-
public void Clear()
30
-
{
31
-
foreach (var group in this)
32
-
ReturnBatchGroup(group.Value);
33
-
_batchItems.Clear();
34
-
}
36
+
public Dictionary<TTexture, BatchGroup>.Enumerator GetEnumerator() =>
37
+
_batchItems.GetEnumerator();
35
38
36
-
public Dictionary<TTexture, BatchGroup>.Enumerator GetEnumerator() =>
37
-
_batchItems.GetEnumerator();
38
-
39
-
private BatchGroup GetBatchGroup()
40
-
{
41
-
if (!_batchGroups.TryPop(out var group))
42
-
group = new();
43
-
return group;
44
-
}
45
-
46
-
private void ReturnBatchGroup(BatchGroup group) => _batchGroups.Push(group);
39
+
private BatchGroup GetBatchGroup()
40
+
{
41
+
if (!_batchGroups.TryPop(out var group))
42
+
group = new();
43
+
return group;
47
44
}
45
+
46
+
private void ReturnBatchGroup(BatchGroup group) => _batchGroups.Push(group);
48
47
}
+305
src/Peridot/ColorB.Static.cs
+305
src/Peridot/ColorB.Static.cs
···
1
+
using System.Runtime.CompilerServices;
2
+
3
+
namespace Peridot;
4
+
5
+
/// <summary>
6
+
/// Provides static methods to <see cref="ColorB"/> manipulation.
7
+
/// </summary>
8
+
public partial struct ColorB
9
+
{
10
+
#region Math
11
+
/// <summary>
12
+
/// Negates the specified color.
13
+
/// </summary>
14
+
/// <param name="color">The color to negate.</param>
15
+
/// <returns>The negated vector.</returns>
16
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17
+
public static ColorB Negate(ColorB color)
18
+
{
19
+
var r = -color.R;
20
+
var g = -color.G;
21
+
var b = -color.B;
22
+
var a = -color.A;
23
+
24
+
return new((byte)r, (byte)g, (byte)b, (byte)a);
25
+
}
26
+
27
+
/// <summary>
28
+
/// Adds two colors together.
29
+
/// </summary>
30
+
/// <param name="left">The first color.</param>
31
+
/// <param name="right">The second color.</param>
32
+
/// <returns>The summed color.</returns>
33
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34
+
public static ColorB Add(ColorB left, ColorB right)
35
+
{
36
+
var r = left.R + right.R;
37
+
var g = left.G + right.G;
38
+
var b = left.B + right.B;
39
+
var a = left.A + right.A;
40
+
41
+
return new((byte)r, (byte)g, (byte)b, (byte)a);
42
+
}
43
+
44
+
/// <summary>
45
+
/// Subtracts the second color from the first.
46
+
/// </summary>
47
+
/// <param name="left">The first color.</param>
48
+
/// <param name="right">The second color.</param>
49
+
/// <returns>The color that results from subracting <paramref name="right"/> from <paramref name="left"/>.</returns>
50
+
51
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
52
+
public static ColorB Subtract(ColorB left, ColorB right)
53
+
{
54
+
var r = left.R - right.R;
55
+
var g = left.G - right.G;
56
+
var b = left.B - right.B;
57
+
var a = left.A - right.A;
58
+
59
+
return new((byte)r, (byte)g, (byte)b, (byte)a);
60
+
}
61
+
62
+
/// <summary>
63
+
/// Multiplies two colors together.
64
+
/// </summary>
65
+
/// <param name="left">The first color.</param>
66
+
/// <param name="right">The second color.</param>
67
+
/// <returns>The product color.</returns>
68
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
69
+
public static ColorB Multiply(ColorB left, ColorB right)
70
+
{
71
+
var r = left.R * right.R;
72
+
var g = left.G * right.G;
73
+
var b = left.B * right.B;
74
+
var a = left.A * right.A;
75
+
76
+
return new((byte)r, (byte)g, (byte)b, (byte)a);
77
+
}
78
+
79
+
/// <summary>
80
+
/// Divides the first color by the second.
81
+
/// </summary>
82
+
/// <param name="left">The first color.</param>
83
+
/// <param name="right">The second color.</param>
84
+
/// <returns>The color that results from dividing <paramref name="left"/> by <paramref name="right"/>.</returns>
85
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
86
+
public static ColorB Divide(ColorB left, ColorB right)
87
+
{
88
+
var r = left.R / right.R;
89
+
var g = left.G / right.G;
90
+
var b = left.B / right.B;
91
+
var a = left.A / right.A;
92
+
93
+
return new((byte)r, (byte)g, (byte)b, (byte)a);
94
+
}
95
+
96
+
/// <summary>
97
+
/// Multiples the specified color by the specified scalar value.
98
+
/// </summary>
99
+
/// <param name="left">The color.</param>
100
+
/// <param name="right">The scalar value.</param>
101
+
/// <returns>The scaled color.</returns>
102
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
103
+
public static ColorB Multiply(ColorB left, float right)
104
+
{
105
+
var r = left.R * right;
106
+
var g = left.G * right;
107
+
var b = left.B * right;
108
+
var a = left.A * right;
109
+
110
+
return new((byte)r, (byte)g, (byte)b, (byte)a);
111
+
}
112
+
113
+
/// <summary>
114
+
/// Divides the specified color by a specified scalar value.
115
+
/// </summary>
116
+
/// <param name="left">The color.</param>
117
+
/// <param name="right">The scalar value.</param>
118
+
/// <returns>The result of the division.</returns>
119
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
120
+
public static ColorB Divide(ColorB left, float right)
121
+
{
122
+
var r = left.R / right;
123
+
var g = left.G / right;
124
+
var b = left.B / right;
125
+
var a = left.A / right;
126
+
127
+
return new((byte)r, (byte)g, (byte)b, (byte)a);
128
+
}
129
+
130
+
/// <summary>
131
+
/// Multiples the specified color by the specified scalar value.
132
+
/// </summary>
133
+
/// <param name="left">The scalar value.</param>
134
+
/// <param name="right">The color.</param>
135
+
/// <returns>The scaled color.</returns>
136
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
137
+
public static ColorB Multiply(float left, ColorB right) => Multiply(right, left);
138
+
139
+
/// <summary>
140
+
/// Linearly interpolates between two colors.
141
+
/// </summary>
142
+
/// <param name="a">The first color.</param>
143
+
/// <param name="b">The second color.</param>
144
+
/// <param name="t">Influence of the second color on the final result.</param>
145
+
/// <returns><paramref name="a"/> * (255 - <paramref name="t"/>) + <paramref name="b"/> * <paramref name="t"/>.</returns>
146
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
147
+
public static ColorB Lerp(in ColorB a, in ColorB b, float t) =>
148
+
a + (b - a) * t;
149
+
150
+
#endregion
151
+
#region Operators
152
+
/// <summary>
153
+
/// Element-wise equality.
154
+
/// </summary>
155
+
/// <param name="left">The first value.</param>
156
+
/// <param name="right">The second value.</param>
157
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
158
+
public static bool operator ==(ColorB left, ColorB right) =>
159
+
left.Equals(right);
160
+
161
+
/// <summary>
162
+
/// Element-wise inequality.
163
+
/// </summary>
164
+
/// <param name="left">The first value.</param>
165
+
/// <param name="right">The second value.</param>
166
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
167
+
public static bool operator !=(ColorB left, ColorB right) =>
168
+
!left.Equals(right);
169
+
170
+
/// <summary>
171
+
/// Negates the specified color.
172
+
/// </summary>
173
+
/// <param name="color">The color to negate.</param>
174
+
/// <returns>The negated vector.</returns>
175
+
public static ColorB operator -(ColorB color) => Negate(color);
176
+
177
+
/// <summary>
178
+
/// Adds two colors together.
179
+
/// </summary>
180
+
/// <param name="left">The first color.</param>
181
+
/// <param name="right">The second color.</param>
182
+
/// <returns>The summed color.</returns>
183
+
public static ColorB operator +(ColorB left, ColorB right) => Add(left, right);
184
+
185
+
/// <summary>
186
+
/// Subtracts the second color from the first.
187
+
/// </summary>
188
+
/// <param name="left">The first color.</param>
189
+
/// <param name="right">The second color.</param>
190
+
/// <returns>The color that results from subracting <paramref name="right"/> from <paramref name="left"/>.</returns>
191
+
public static ColorB operator -(ColorB left, ColorB right) => Subtract(left, right);
192
+
193
+
/// <summary>
194
+
/// Multiplies two colors together.
195
+
/// </summary>
196
+
/// <param name="left">The first color.</param>
197
+
/// <param name="right">The second color.</param>
198
+
/// <returns>The product color.</returns>
199
+
public static ColorB operator *(ColorB left, ColorB right) => Multiply(left, right);
200
+
201
+
/// <summary>
202
+
/// Divides the first color by the second.
203
+
/// </summary>
204
+
/// <param name="left">The first color.</param>
205
+
/// <param name="right">The second color.</param>
206
+
/// <returns>The color that results from dividing <paramref name="left"/> by <paramref name="right"/>.</returns>
207
+
public static ColorB operator /(ColorB left, ColorB right) => Divide(left, right);
208
+
209
+
/// <summary>
210
+
/// Multiples the specified color by the specified scalar value.
211
+
/// </summary>
212
+
/// <param name="left">The color.</param>
213
+
/// <param name="right">The scalar value.</param>
214
+
/// <returns>The scaled color.</returns>
215
+
public static ColorB operator *(ColorB left, float right) => Multiply(left, right);
216
+
217
+
/// <summary>
218
+
/// Divides the specified color by a specified scalar value.
219
+
/// </summary>
220
+
/// <param name="left">The color.</param>
221
+
/// <param name="right">The scalar value.</param>
222
+
/// <returns>The result of the division.</returns>
223
+
public static ColorB operator /(ColorB left, float right) => Divide(left, right);
224
+
225
+
/// <summary>
226
+
/// Multiples the specified color by the specified scalar value.
227
+
/// </summary>
228
+
/// <param name="left">The scalar value.</param>
229
+
/// <param name="right">The color.</param>
230
+
/// <returns>The scaled color.</returns>
231
+
public static ColorB operator *(float left, ColorB right) => Multiply(left, right);
232
+
#endregion
233
+
#region Colors
234
+
/// <summary>
235
+
/// Red (1, 0, 0, 1)
236
+
/// </summary>
237
+
public static readonly ColorB Red = new(255, 0, 0, 255);
238
+
public static readonly ColorB Transparent = new(0, 0, 0, 0);
239
+
240
+
/// <summary>
241
+
/// Dark Red (139, 0, 0, 255)
242
+
/// </summary>
243
+
public static readonly ColorB DarkRed = new(139, 0, 0, 255);
244
+
245
+
/// <summary>
246
+
/// Green (0, 255, 0, 255)
247
+
/// </summary>
248
+
public static readonly ColorB Green = new(0, 255, 0, 255);
249
+
250
+
/// <summary>
251
+
/// Blue (0, 0, 255, 255)
252
+
/// </summary>
253
+
public static readonly ColorB Blue = new(0, 0, 255, 255);
254
+
255
+
/// <summary>
256
+
/// Yellow (255, 255, 0, 255)
257
+
/// </summary>
258
+
public static readonly ColorB Yellow = new(255, 255, 0, 255);
259
+
260
+
/// <summary>
261
+
/// Grey (128, 128, 128 / 255f, 255)
262
+
/// </summary>
263
+
public static readonly ColorB Grey = new(128, 128, 128, 255);
264
+
265
+
/// <summary>
266
+
/// Light Grey (211, 211, 211, 255)
267
+
/// </summary>
268
+
public static readonly ColorB LightGrey = new(211, 211, 211, 255);
269
+
270
+
/// <summary>
271
+
/// Cyan (0, 255, 255, 255)
272
+
/// </summary>
273
+
public static readonly ColorB Cyan = new(0, 255, 255, 255);
274
+
275
+
/// <summary>
276
+
/// White (255, 255, 255, 255)
277
+
/// </summary>
278
+
public static readonly ColorB White = new(255, 255, 255, 255);
279
+
280
+
/// <summary>
281
+
/// Cornflower Blue (100, 149, 237, 255)
282
+
/// </summary>
283
+
public static readonly ColorB CornflowerBlue = new(100, 149, 237, 255);
284
+
285
+
/// <summary>
286
+
/// Clear (0, 0, 0, 0)
287
+
/// </summary>
288
+
public static readonly ColorB Clear = new(0, 0, 0, 0);
289
+
290
+
/// <summary>
291
+
/// Black (0, 0, 0, 255)
292
+
/// </summary>
293
+
public static readonly ColorB Black = new(0, 0, 0, 255);
294
+
295
+
/// <summary>
296
+
/// Pink (255, 192, 203, 255)
297
+
/// </summary>
298
+
public static readonly ColorB Pink = new(255, 192, 203, 255);
299
+
300
+
/// <summary>
301
+
/// Orange (255, 165, 0, 255)
302
+
/// </summary>
303
+
public static readonly ColorB Orange = new(255, 165, 0, 255);
304
+
#endregion
305
+
}
+84
src/Peridot/ColorB.cs
+84
src/Peridot/ColorB.cs
···
1
+
using System.Drawing;
2
+
3
+
namespace Peridot;
4
+
5
+
/// <summary>
6
+
/// Represetns a RGBA color with floating point values.
7
+
/// All components must be between 0.0 and 1.0.
8
+
/// </summary>
9
+
public partial struct ColorB : IEquatable<ColorB>
10
+
{
11
+
/// <summary>
12
+
/// Constructs a new ColorB from components.
13
+
/// </summary>
14
+
/// <param name="r">Red component.</param>
15
+
/// <param name="g">Green component.</param>
16
+
/// <param name="b">Blue component.</param>
17
+
/// <param name="a">Alpha component.</param>
18
+
public ColorB(byte r, byte g, byte b, byte a)
19
+
{
20
+
(R, G, B, A) = (r, g, b, a);
21
+
}
22
+
23
+
/// <summary>
24
+
/// Constructs a new ColorB from a Color.
25
+
/// </summary>
26
+
/// <param name="color">Color.</param>
27
+
public ColorB(Color color)
28
+
{
29
+
(R, G, B, A) = (color.R, color.G, color.B, color.A);
30
+
}
31
+
32
+
/// <summary>
33
+
/// Constructs a new ColorB from a Vector4.
34
+
/// </summary>
35
+
/// <param name="color">Color.</param>
36
+
public ColorB(ColorF color)
37
+
{
38
+
color *= 255f;
39
+
(R, G, B, A) = ((byte)color.R, (byte)color.G, (byte)color.B, (byte)color.A);
40
+
}
41
+
42
+
/// <summary>
43
+
/// The red color component.
44
+
/// </summary>
45
+
public byte R;
46
+
47
+
/// <summary>
48
+
/// The green color component.
49
+
/// </summary>
50
+
public byte G;
51
+
52
+
/// <summary>
53
+
/// The blue color component.
54
+
/// </summary>
55
+
public byte B;
56
+
57
+
/// <summary>
58
+
/// The alpha color component.
59
+
/// </summary>
60
+
public byte A;
61
+
62
+
public bool Equals(ColorB other)
63
+
{
64
+
return (R, G, B, A) == (other.R, other.G, other.B, other.A);
65
+
}
66
+
67
+
/// <inheritdoc/>
68
+
public override bool Equals(object obj)
69
+
{
70
+
return obj is ColorB other && Equals(other);
71
+
}
72
+
73
+
/// <inheritdoc/>
74
+
public override int GetHashCode()
75
+
{
76
+
return HashCode.Combine(R, G, B, A);
77
+
}
78
+
79
+
/// <inheritdoc/>
80
+
public override string ToString()
81
+
{
82
+
return $"{{R: {R}, G: {G}, B: {B}, A: {A}}}";
83
+
}
84
+
}
+272
src/Peridot/ColorF.Static.cs
+272
src/Peridot/ColorF.Static.cs
···
1
+
using System.Drawing;
2
+
using System.Numerics;
3
+
using System.Runtime.CompilerServices;
4
+
5
+
namespace Peridot;
6
+
7
+
/// <summary>
8
+
/// Provides static methods to <see cref="ColorF"/> manipulation.
9
+
/// </summary>
10
+
public partial struct ColorF
11
+
{
12
+
#region Cast operators
13
+
/// <summary>
14
+
/// Cast a <see cref="ColorF"/> to <see cref="Vector4"/>.
15
+
/// </summary>
16
+
/// <param name="color">The color to cast.</param>
17
+
public static implicit operator Vector4(ColorF color) => color.m_rgba;
18
+
19
+
/// <summary>
20
+
/// Cast a <see cref="Vector4"/> to <see cref="ColorF"/>.
21
+
/// </summary>
22
+
/// <param name="color"></param>
23
+
public static implicit operator ColorF(Vector4 color) => new(color);
24
+
25
+
/// <summary>
26
+
/// Cast a <see cref="Vector4"/> to <see cref="ColorF"/>.
27
+
/// </summary>
28
+
/// <param name="color"></param>
29
+
public static implicit operator Color(ColorF color)
30
+
{
31
+
color = Vector4.Clamp(color, Vector4.Zero, Vector4.One) * 255f;
32
+
return Color.FromArgb((byte)color.A, (byte)color.R, (byte)color.G, (byte)color.B);
33
+
}
34
+
#endregion
35
+
#region Math
36
+
/// <summary>
37
+
/// Negates the specified color.
38
+
/// </summary>
39
+
/// <param name="color">The color to negate.</param>
40
+
/// <returns>The negated vector.</returns>
41
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
42
+
public static ColorF Negate(ColorF color) => -color.m_rgba;
43
+
44
+
/// <summary>
45
+
/// Adds two colors together.
46
+
/// </summary>
47
+
/// <param name="left">The first color.</param>
48
+
/// <param name="right">The second color.</param>
49
+
/// <returns>The summed color.</returns>
50
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
51
+
public static ColorF Add(ColorF left, ColorF right) => left.m_rgba + right.m_rgba;
52
+
53
+
/// <summary>
54
+
/// Subtracts the second color from the first.
55
+
/// </summary>
56
+
/// <param name="left">The first color.</param>
57
+
/// <param name="right">The second color.</param>
58
+
/// <returns>The color that results from subracting <paramref name="right"/> from <paramref name="left"/>.</returns>
59
+
60
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
61
+
public static ColorF Subtract(ColorF left, ColorF right) => left.m_rgba - right.m_rgba;
62
+
63
+
/// <summary>
64
+
/// Multiplies two colors together.
65
+
/// </summary>
66
+
/// <param name="left">The first color.</param>
67
+
/// <param name="right">The second color.</param>
68
+
/// <returns>The product color.</returns>
69
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
70
+
public static ColorF Multiply(ColorF left, ColorF right) => left.m_rgba * right.m_rgba;
71
+
72
+
/// <summary>
73
+
/// Divides the first color by the second.
74
+
/// </summary>
75
+
/// <param name="left">The first color.</param>
76
+
/// <param name="right">The second color.</param>
77
+
/// <returns>The color that results from dividing <paramref name="left"/> by <paramref name="right"/>.</returns>
78
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
79
+
public static ColorF Divide(ColorF left, ColorF right) => left.m_rgba / right.m_rgba;
80
+
81
+
/// <summary>
82
+
/// Multiples the specified color by the specified scalar value.
83
+
/// </summary>
84
+
/// <param name="left">The color.</param>
85
+
/// <param name="right">The scalar value.</param>
86
+
/// <returns>The scaled color.</returns>
87
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
88
+
public static ColorF Multiply(ColorF left, float right) => left.m_rgba * right;
89
+
90
+
/// <summary>
91
+
/// Divides the specified color by a specified scalar value.
92
+
/// </summary>
93
+
/// <param name="left">The color.</param>
94
+
/// <param name="right">The scalar value.</param>
95
+
/// <returns>The result of the division.</returns>
96
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
97
+
public static ColorF Divide(ColorF left, float right) => left.m_rgba / right;
98
+
99
+
/// <summary>
100
+
/// Multiples the specified color by the specified scalar value.
101
+
/// </summary>
102
+
/// <param name="left">The scalar value.</param>
103
+
/// <param name="right">The color.</param>
104
+
/// <returns>The scaled color.</returns>
105
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
106
+
public static ColorF Multiply(float left, ColorF right) => left * right.m_rgba;
107
+
108
+
/// <summary>
109
+
/// Linearly interpolates between two colors.
110
+
/// </summary>
111
+
/// <param name="a">The first color.</param>
112
+
/// <param name="b">The second color.</param>
113
+
/// <param name="t">Influence of the second color on the final result.</param>
114
+
/// <returns><paramref name="a"/> * (1f - <paramref name="t"/>) + <paramref name="b"/> * <paramref name="t"/>.</returns>
115
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
116
+
public static ColorF Lerp(in ColorF a, in ColorF b, float t) =>
117
+
a + (b - a) * t;
118
+
#endregion
119
+
#region Operators
120
+
/// <summary>
121
+
/// Element-wise equality.
122
+
/// </summary>
123
+
/// <param name="left">The first value.</param>
124
+
/// <param name="right">The second value.</param>
125
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
126
+
public static bool operator ==(ColorF left, ColorF right) =>
127
+
left.Equals(right);
128
+
129
+
/// <summary>
130
+
/// Element-wise inequality.
131
+
/// </summary>
132
+
/// <param name="left">The first value.</param>
133
+
/// <param name="right">The second value.</param>
134
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
135
+
public static bool operator !=(ColorF left, ColorF right) =>
136
+
!left.Equals(right);
137
+
138
+
/// <summary>
139
+
/// Negates the specified color.
140
+
/// </summary>
141
+
/// <param name="color">The color to negate.</param>
142
+
/// <returns>The negated vector.</returns>
143
+
public static ColorF operator -(ColorF color) => Negate(color);
144
+
145
+
/// <summary>
146
+
/// Adds two colors together.
147
+
/// </summary>
148
+
/// <param name="left">The first color.</param>
149
+
/// <param name="right">The second color.</param>
150
+
/// <returns>The summed color.</returns>
151
+
public static ColorF operator +(ColorF left, ColorF right) => Add(left, right);
152
+
153
+
/// <summary>
154
+
/// Subtracts the second color from the first.
155
+
/// </summary>
156
+
/// <param name="left">The first color.</param>
157
+
/// <param name="right">The second color.</param>
158
+
/// <returns>The color that results from subracting <paramref name="right"/> from <paramref name="left"/>.</returns>
159
+
public static ColorF operator -(ColorF left, ColorF right) => Subtract(left, right);
160
+
161
+
/// <summary>
162
+
/// Multiplies two colors together.
163
+
/// </summary>
164
+
/// <param name="left">The first color.</param>
165
+
/// <param name="right">The second color.</param>
166
+
/// <returns>The product color.</returns>
167
+
public static ColorF operator *(ColorF left, ColorF right) => Multiply(left, right);
168
+
169
+
/// <summary>
170
+
/// Divides the first color by the second.
171
+
/// </summary>
172
+
/// <param name="left">The first color.</param>
173
+
/// <param name="right">The second color.</param>
174
+
/// <returns>The color that results from dividing <paramref name="left"/> by <paramref name="right"/>.</returns>
175
+
public static ColorF operator /(ColorF left, ColorF right) => Divide(left, right);
176
+
177
+
/// <summary>
178
+
/// Multiples the specified color by the specified scalar value.
179
+
/// </summary>
180
+
/// <param name="left">The color.</param>
181
+
/// <param name="right">The scalar value.</param>
182
+
/// <returns>The scaled color.</returns>
183
+
public static ColorF operator *(ColorF left, float right) => Multiply(left, right);
184
+
185
+
/// <summary>
186
+
/// Divides the specified color by a specified scalar value.
187
+
/// </summary>
188
+
/// <param name="left">The color.</param>
189
+
/// <param name="right">The scalar value.</param>
190
+
/// <returns>The result of the division.</returns>
191
+
public static ColorF operator /(ColorF left, float right) => Divide(left, right);
192
+
193
+
/// <summary>
194
+
/// Multiples the specified color by the specified scalar value.
195
+
/// </summary>
196
+
/// <param name="left">The scalar value.</param>
197
+
/// <param name="right">The color.</param>
198
+
/// <returns>The scaled color.</returns>
199
+
public static ColorF operator *(float left, ColorF right) => Multiply(left, right);
200
+
#endregion
201
+
#region Colors
202
+
/// <summary>
203
+
/// Red (1, 0, 0, 1)
204
+
/// </summary>
205
+
public static readonly ColorF Red = new(1f, 0f, 0f, 1f);
206
+
207
+
/// <summary>
208
+
/// Dark Red (139f / 255f, 0f, 0f, 1f)
209
+
/// </summary>
210
+
public static readonly ColorF DarkRed = new(139f / 255f, 0f, 0f, 1f);
211
+
212
+
/// <summary>
213
+
/// Green (0f, 1f, 0f, 1f)
214
+
/// </summary>
215
+
public static readonly ColorF Green = new(0f, 1f, 0f, 1f);
216
+
217
+
/// <summary>
218
+
/// Blue (0f, 0f, 1f, 1f)
219
+
/// </summary>
220
+
public static readonly ColorF Blue = new(0f, 0f, 1f, 1f);
221
+
222
+
/// <summary>
223
+
/// Yellow (1f, 1f, 0f, 1f)
224
+
/// </summary>
225
+
public static readonly ColorF Yellow = new(1f, 1f, 0f, 1f);
226
+
227
+
/// <summary>
228
+
/// Grey (128f / 255f, 128f / 255f, 128 / 255f, 1f)
229
+
/// </summary>
230
+
public static readonly ColorF Grey = new(128f / 255f, 128f / 255f, 128f / 255f, 1f);
231
+
232
+
/// <summary>
233
+
/// Light Grey (211f / 255f, 211f / 255f, 211f / 255f, 1f)
234
+
/// </summary>
235
+
public static readonly ColorF LightGrey = new(211f / 255f, 211f / 255f, 211f / 255f, 1f);
236
+
237
+
/// <summary>
238
+
/// Cyan (0f, 1f, 1f, 1f)
239
+
/// </summary>
240
+
public static readonly ColorF Cyan = new(0f, 1f, 1f, 1f);
241
+
242
+
/// <summary>
243
+
/// White (1f, 1f, 1f, 1f)
244
+
/// </summary>
245
+
public static readonly ColorF White = new(1f, 1f, 1f, 1f);
246
+
247
+
/// <summary>
248
+
/// Cornflower Blue (100f / 255f, 149f / 255f, 237f / 255f, 1f)
249
+
/// </summary>
250
+
public static readonly ColorF CornflowerBlue = new(100f / 255f, 149f / 255f, 237f / 255f, 1f);
251
+
252
+
/// <summary>
253
+
/// Clear (0f, 0f, 0f, 0f)
254
+
/// </summary>
255
+
public static readonly ColorF Clear = new(0f, 0f, 0f, 0f);
256
+
257
+
/// <summary>
258
+
/// Black (0f, 0f, 0f, 1f)
259
+
/// </summary>
260
+
public static readonly ColorF Black = new(0f, 0f, 0f, 1f);
261
+
262
+
/// <summary>
263
+
/// Pink (1f, 192f / 255f, 203f / 255f, 1f)
264
+
/// </summary>
265
+
public static readonly ColorF Pink = new(1f, 192f / 255f, 203f / 255f, 1f);
266
+
267
+
/// <summary>
268
+
/// Orange (1f, 165f / 255f, 0f, 1f)
269
+
/// </summary>
270
+
public static readonly ColorF Orange = new(1f, 165f / 255f, 0f, 1f);
271
+
#endregion
272
+
}
+88
src/Peridot/ColorF.cs
+88
src/Peridot/ColorF.cs
···
1
+
using System.Drawing;
2
+
using System.Numerics;
3
+
4
+
namespace Peridot;
5
+
6
+
/// <summary>
7
+
/// Represetns a RGBA color with floating point values.
8
+
/// All components must be between 0.0 and 1.0.
9
+
/// </summary>
10
+
public partial struct ColorF : IEquatable<ColorF>
11
+
{
12
+
private Vector4 m_rgba;
13
+
14
+
/// <summary>
15
+
/// Constructs a new ColorF from components.
16
+
/// </summary>
17
+
/// <param name="r">Red component.</param>
18
+
/// <param name="g">Green component.</param>
19
+
/// <param name="b">Blue component.</param>
20
+
/// <param name="a">Alpha component.</param>
21
+
public ColorF(float r, float g, float b, float a)
22
+
{
23
+
m_rgba = new(r, g, b, a);
24
+
}
25
+
26
+
/// <summary>
27
+
/// Constructs a new ColorF from a Color.
28
+
/// </summary>
29
+
/// <param name="color">Color.</param>
30
+
public ColorF(Color color)
31
+
{
32
+
m_rgba = new Vector4(color.R, color.G, color.B, color.A) * (1f / 255f);
33
+
}
34
+
35
+
/// <summary>
36
+
/// Constructs a new ColorF from a Vector4.
37
+
/// </summary>
38
+
/// <param name="color">Color.</param>
39
+
public ColorF(Vector4 color)
40
+
{
41
+
m_rgba = color;
42
+
}
43
+
44
+
/// <summary>
45
+
/// The red color component.
46
+
/// </summary>
47
+
public float R { get => m_rgba.X; set => m_rgba.X = value; }
48
+
49
+
/// <summary>
50
+
/// The green color component.
51
+
/// </summary>
52
+
public float G { get => m_rgba.Y; set => m_rgba.Y = value; }
53
+
54
+
/// <summary>
55
+
/// The blue color component.
56
+
/// </summary>
57
+
public float B { get => m_rgba.Z; set => m_rgba.Z = value; }
58
+
59
+
/// <summary>
60
+
/// The alpha color component.
61
+
/// </summary>
62
+
public float A { get => m_rgba.W; set => m_rgba.W = value; }
63
+
64
+
public Vector4 AsVector { get => m_rgba; set => m_rgba = value; }
65
+
66
+
public bool Equals(ColorF other)
67
+
{
68
+
return m_rgba.Equals(other.m_rgba);
69
+
}
70
+
71
+
/// <inheritdoc/>
72
+
public override bool Equals(object obj)
73
+
{
74
+
return obj is ColorF other && Equals(other);
75
+
}
76
+
77
+
/// <inheritdoc/>
78
+
public override int GetHashCode()
79
+
{
80
+
return m_rgba.GetHashCode();
81
+
}
82
+
83
+
/// <inheritdoc/>
84
+
public override string ToString()
85
+
{
86
+
return $"{{R: {R}, G: {G}, B: {B}, A: {A}}}";
87
+
}
88
+
}
-27
src/Peridot/IFont.cs
-27
src/Peridot/IFont.cs
···
1
-
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
-
// This code is licensed under MIT license (see LICENSE for details)
3
-
4
-
using System.Numerics;
5
-
6
-
namespace Peridot
7
-
{
8
-
/// <summary>
9
-
/// Represents a font to use in <see cref="SpriteBatch{TTexture}"/>.
10
-
/// </summary>
11
-
public interface IFont : IDisposable
12
-
{
13
-
/// <summary>
14
-
/// A bool indicating whether this instance has been disposed.
15
-
/// </summary>
16
-
public bool IsDisposed { get; }
17
-
18
-
/// <summary>
19
-
/// Gets the size of a string when rendered in this font.
20
-
/// </summary>
21
-
/// <param name="text">The text to measure.</param>
22
-
/// <param name="fontSize">The font size to measure.</param>
23
-
/// <returns>The size of <paramref name="text"/> rendered with a <see cref="ITextRenderer"/>
24
-
/// with <paramref name="fontSize"/> font size.</returns>
25
-
public Vector2 MeasureString(string text, int fontSize);
26
-
}
27
-
}
+52
src/Peridot/IPeridot.cs
+52
src/Peridot/IPeridot.cs
···
1
+
namespace Peridot;
2
+
3
+
/// <summary>
4
+
/// Represents a interface for implementation of Peridot.
5
+
/// </summary>
6
+
public interface IPeridot
7
+
{
8
+
/// <summary>
9
+
/// Creates a new image.
10
+
/// </summary>
11
+
/// <param name="description">The description of image.</param>
12
+
/// <returns>A new image.</returns>
13
+
public Image CreateImage(ImageDescription description);
14
+
15
+
/// <summary>
16
+
/// Updates a portion of Image with new data.
17
+
/// </summary>
18
+
/// <param name="image">The image to update.</param>
19
+
/// <param name="source">An span with data to upload.</param>
20
+
/// <param name="x">The minimum X value of the updated region.</param>
21
+
/// <param name="y">The minimum Y value of the updated region.</param>
22
+
/// <param name="width">The width of the updated region.</param>
23
+
/// <param name="height">The height of the updated region.</param>
24
+
/// <typeparam name="T">Type of data updated.</typeparam>
25
+
public void UpdateImage<T>(Image image, ReadOnlySpan<T> source, uint x, uint y, uint width, uint height) where T : unmanaged;
26
+
}
27
+
28
+
/// <summary>
29
+
/// Represents a interface for implementation of Peridot.
30
+
/// </summary>
31
+
public interface IPeridot<TSpriteBatchDescripton> : IPeridot
32
+
{
33
+
/// <summary>
34
+
/// Creates a new sprite batch.
35
+
/// </summary>
36
+
/// <param name="description">The description of sprite batch.</param>
37
+
/// <returns>A new sprite batch.</returns>
38
+
public ISpriteBatch CreateSpriteBatch(TSpriteBatchDescripton description);
39
+
}
40
+
41
+
/// <summary>
42
+
/// Represents a interface for implementation of Peridot.
43
+
/// </summary>
44
+
public interface IPeridot<TImage, TSpriteBatchDescripton> : IPeridot<TSpriteBatchDescripton>
45
+
{
46
+
/// <summary>
47
+
/// Creates a new sprite batch.
48
+
/// </summary>
49
+
/// <param name="description">The description of sprite batch.</param>
50
+
/// <returns>A new sprite batch.</returns>
51
+
public new ISpriteBatch<TImage> CreateSpriteBatch(TSpriteBatchDescripton description);
52
+
}
+671
-599
src/Peridot/ISpriteBatch.cs
+671
-599
src/Peridot/ISpriteBatch.cs
···
4
4
using System.Drawing;
5
5
using System.Numerics;
6
6
7
-
namespace Peridot
7
+
namespace Peridot;
8
+
9
+
/// <summary>
10
+
/// Represents a interface for drawing sprites in one or more optimized batches.
11
+
/// </summary>
12
+
public interface ISpriteBatch : IDisposable
8
13
{
9
14
/// <summary>
10
-
/// Represents a interface for drawing sprites in one or more optimized batches.
15
+
/// A bool indicating whether this instance has been disposed.
11
16
/// </summary>
12
-
public interface ISpriteBatch : IDisposable
13
-
{
14
-
/// <summary>
15
-
/// A bool indicating whether this instance has been disposed.
16
-
/// </summary>
17
-
public bool IsDisposed { get; }
17
+
public bool IsDisposed { get; }
18
18
19
-
/// <summary>
20
-
/// The view matrix to use to renderer.
21
-
/// </summary>
22
-
public Matrix4x4 ViewMatrix { get; set; }
19
+
/// <summary>
20
+
/// The view matrix to use to renderer.
21
+
/// </summary>
22
+
public Matrix4x4 ViewMatrix { get; set; }
23
23
24
-
/// <summary>
25
-
/// The scissor rectangle.
26
-
/// </summary>
27
-
public RectangleF Scissor { get; set; }
24
+
/// <summary>
25
+
/// The scissor rectangle.
26
+
/// </summary>
27
+
public RectangleF Scissor { get; set; }
28
28
29
-
/// <summary>
30
-
/// Resets the <see cref="Scissor"/> to default value.
31
-
/// </summary>
32
-
public void ResetScissor();
29
+
/// <summary>
30
+
/// Resets the <see cref="Scissor"/> to default value.
31
+
/// </summary>
32
+
public void ResetScissor();
33
33
34
-
/// <summary>
35
-
/// Replaces the <see cref="Scissor"/> with the intersection of itself
36
-
/// and the <paramref name="clip"/> rectangle.
37
-
/// </summary>
38
-
/// <param name="clip">The rectangle to intersects.</param>
39
-
public void IntersectScissor(RectangleF clip);
34
+
/// <summary>
35
+
/// Replaces the <see cref="Scissor"/> with the intersection of itself
36
+
/// and the <paramref name="clip"/> rectangle.
37
+
/// </summary>
38
+
/// <param name="clip">The rectangle to intersects.</param>
39
+
public void IntersectScissor(RectangleF clip);
40
40
41
-
/// <summary>
42
-
/// Begins the sprite branch.
43
-
/// </summary>
44
-
/// <exception cref="InvalidOperationException">Thrown if <see cref="Begin"/> is called next time without previous <see cref="End"/>.</exception>
45
-
public void Begin();
41
+
/// <summary>
42
+
/// Begins the sprite branch.
43
+
/// </summary>
44
+
/// <exception cref="InvalidOperationException">Thrown if <see cref="Begin"/> is called next time without previous <see cref="End"/>.</exception>
45
+
public void Begin();
46
46
47
-
/// <summary>
48
-
/// Flushes all batched text and sprites to the screen.
49
-
/// </summary>
50
-
/// <exception cref="InvalidOperationException">This command should be called after <see cref="Begin"/> and drawing commands.</exception>
51
-
public void End();
47
+
/// <summary>
48
+
/// Flushes all batched text and sprites to the screen.
49
+
/// </summary>
50
+
/// <exception cref="InvalidOperationException">This command should be called after <see cref="Begin"/> and drawing commands.</exception>
51
+
public void End();
52
52
53
-
/// <summary>
54
-
/// Submit sprite draw in the batch.
55
-
/// </summary>
56
-
/// <param name="texture">The source texture.</param>
57
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
58
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
59
-
/// <param name="color">Color multiplier.</param>
60
-
/// <param name="rotation">The rotation of the sprite.</param>
61
-
/// <param name="origin">Sprite center.</param>
62
-
/// <param name="options">Options that modify the drawing.</param>
63
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
64
-
public void Draw(ITexture2D texture,
65
-
RectangleF destinationRectangle,
66
-
RectangleF sourceRectangle,
67
-
Color color,
68
-
float rotation,
69
-
Vector2 origin,
70
-
SpriteOptions options,
71
-
float layerDepth);
72
-
73
-
/// <summary>
74
-
/// Submit sprite draw in the batch.
75
-
/// </summary>
76
-
/// <param name="texture">The source texture.</param>
77
-
/// <param name="position">The drawing location on screen.</param>
78
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
79
-
/// <param name="color">Color multiplier.</param>
80
-
/// <param name="rotation">The rotation of the sprite.</param>
81
-
/// <param name="origin">Sprite center.</param>
82
-
/// <param name="scale">A scaling of this sprite.</param>
83
-
/// <param name="options">Options that modify the drawing.</param>
84
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
85
-
public void Draw(ITexture2D texture,
86
-
Vector2 position,
87
-
RectangleF sourceRectangle,
88
-
Color color,
89
-
float rotation,
90
-
Vector2 origin,
91
-
Vector2 scale,
92
-
SpriteOptions options,
93
-
float layerDepth);
94
-
95
-
/// <summary>
96
-
/// Submit sprite draw in the batch.
97
-
/// </summary>
98
-
/// <param name="texture">The source texture.</param>
99
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
100
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
101
-
/// <param name="color">Color multiplier.</param>
102
-
/// <param name="rotation">The rotation of the sprite.</param>
103
-
/// <param name="origin">Sprite center.</param>
104
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
105
-
public void Draw(ITexture2D texture,
106
-
RectangleF destinationRectangle,
107
-
RectangleF sourceRectangle,
108
-
Color color,
109
-
float rotation,
110
-
Vector2 origin,
111
-
float layerDepth);
112
-
113
-
/// <summary>
114
-
/// Submit sprite draw in the batch.
115
-
/// </summary>
116
-
/// <param name="texture">The source texture.</param>
117
-
/// <param name="position">The drawing location on screen.</param>
118
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
119
-
/// <param name="color">Color multiplier.</param>
120
-
/// <param name="rotation">The rotation of the sprite.</param>
121
-
/// <param name="origin">Sprite center.</param>
122
-
/// <param name="scale">A scaling of this sprite.</param>
123
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
124
-
public void Draw(ITexture2D texture,
125
-
Vector2 position,
126
-
RectangleF sourceRectangle,
127
-
Color color,
128
-
float rotation,
129
-
Vector2 origin,
130
-
Vector2 scale,
131
-
float layerDepth);
132
-
133
-
/// <summary>
134
-
/// Submit a sprite for drawing in the current batch.
135
-
/// </summary>
136
-
/// <param name="texture">The source texture.</param>
137
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
138
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
139
-
/// <param name="color">Color multiplier.</param>
140
-
/// <param name="rotation">A rotation of this sprite.</param>
141
-
/// <param name="origin">Sprite center.</param>
142
-
/// <param name="options">Options that modify the drawing.</param>
143
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
144
-
public void Draw(ITexture2D texture,
145
-
RectangleF destinationRectangle,
146
-
RectangleF? sourceRectangle,
147
-
Color color,
148
-
float rotation,
149
-
Vector2 origin,
150
-
SpriteOptions options,
151
-
float layerDepth);
53
+
/// <summary>
54
+
/// Submit sprite draw in the batch.
55
+
/// </summary>
56
+
/// <param name="image">The source image.</param>
57
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
58
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
59
+
/// <param name="color">Color multiplier.</param>
60
+
/// <param name="rotation">The rotation of the sprite.</param>
61
+
/// <param name="origin">Sprite center.</param>
62
+
/// <param name="options">Options that modify the drawing.</param>
63
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
64
+
public void Draw(Image image,
65
+
RectangleF destinationRectangle,
66
+
RectangleF sourceRectangle,
67
+
Color color,
68
+
float rotation,
69
+
Vector2 origin,
70
+
SpriteOptions options,
71
+
float layerDepth);
152
72
153
-
/// <summary>
154
-
/// Submit a sprite for drawing in the current batch.
155
-
/// </summary>
156
-
/// <param name="texture">The source texture.</param>
157
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
158
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
159
-
/// <param name="color">Color multiplier.</param>
160
-
/// <param name="rotation">A rotation of this sprite.</param>
161
-
/// <param name="origin">Sprite center.</param>
162
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
163
-
public void Draw(ITexture2D texture,
164
-
RectangleF destinationRectangle,
165
-
RectangleF? sourceRectangle,
166
-
Color color,
167
-
float rotation,
168
-
Vector2 origin,
169
-
float layerDepth);
73
+
/// <summary>
74
+
/// Submit sprite draw in the batch.
75
+
/// </summary>
76
+
/// <param name="image">The source image.</param>
77
+
/// <param name="position">The drawing location on screen.</param>
78
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
79
+
/// <param name="color">Color multiplier.</param>
80
+
/// <param name="rotation">The rotation of the sprite.</param>
81
+
/// <param name="origin">Sprite center.</param>
82
+
/// <param name="scale">A scaling of this sprite.</param>
83
+
/// <param name="options">Options that modify the drawing.</param>
84
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
85
+
public void Draw(Image image,
86
+
Vector2 position,
87
+
RectangleF sourceRectangle,
88
+
Color color,
89
+
float rotation,
90
+
Vector2 origin,
91
+
Vector2 scale,
92
+
SpriteOptions options,
93
+
float layerDepth);
170
94
171
-
/// <summary>
172
-
/// Submit sprite draw in the batch.
173
-
/// </summary>
174
-
/// <param name="texture">The source texture.</param>
175
-
/// <param name="position">The drawing location on screen.</param>
176
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
177
-
/// <param name="color">Color multiplier.</param>
178
-
/// <param name="rotation">The rotation of the sprite.</param>
179
-
/// <param name="origin">Sprite center.</param>
180
-
/// <param name="scale">A scaling of this sprite.</param>
181
-
/// <param name="options">Options that modify the drawing.</param>
182
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
183
-
public void Draw(ITexture2D texture,
184
-
Vector2 position,
185
-
RectangleF? sourceRectangle,
186
-
Color color,
187
-
float rotation,
188
-
Vector2 origin,
189
-
Vector2 scale,
190
-
SpriteOptions options,
191
-
float layerDepth);
95
+
/// <summary>
96
+
/// Submit sprite draw in the batch.
97
+
/// </summary>
98
+
/// <param name="image">The source image.</param>
99
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
100
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
101
+
/// <param name="color">Color multiplier.</param>
102
+
/// <param name="rotation">The rotation of the sprite.</param>
103
+
/// <param name="origin">Sprite center.</param>
104
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
105
+
public void Draw(Image image,
106
+
RectangleF destinationRectangle,
107
+
RectangleF sourceRectangle,
108
+
Color color,
109
+
float rotation,
110
+
Vector2 origin,
111
+
float layerDepth);
192
112
193
-
/// <summary>
194
-
/// Submit sprite draw in the batch.
195
-
/// </summary>
196
-
/// <param name="texture">The source texture.</param>
197
-
/// <param name="position">The drawing location on screen.</param>
198
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
199
-
/// <param name="color">Color multiplier.</param>
200
-
/// <param name="rotation">The rotation of the sprite.</param>
201
-
/// <param name="origin">Sprite center.</param>
202
-
/// <param name="scale">A scaling of this sprite.</param>
203
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
204
-
public void Draw(ITexture2D texture,
205
-
Vector2 position,
206
-
RectangleF? sourceRectangle,
207
-
Color color,
208
-
float rotation,
209
-
Vector2 origin,
210
-
Vector2 scale,
211
-
float layerDepth);
212
-
213
-
/// <summary>
214
-
/// Submit a sprite for drawing in the current batch.
215
-
/// </summary>
216
-
/// <param name="texture">The source texture.</param>
217
-
/// <param name="position">The drawing location on screen.</param>
218
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
219
-
/// <param name="color">Color multiplier.</param>
220
-
/// <param name="rotation">A rotation of this sprite.</param>
221
-
/// <param name="origin">Sprite center.</param>
222
-
/// <param name="scale">A scaling of this sprite.</param>
223
-
/// <param name="options">Options that modify the drawing.</param>
224
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
225
-
public void Draw(ITexture2D texture,
226
-
Vector2 position,
227
-
RectangleF? sourceRectangle,
228
-
Color color,
229
-
float rotation,
230
-
Vector2 origin,
231
-
float scale,
232
-
SpriteOptions options,
233
-
float layerDepth);
234
-
235
-
/// <summary>
236
-
/// Submit a sprite for drawing in the current batch.
237
-
/// </summary>
238
-
/// <param name="texture">The source texture.</param>
239
-
/// <param name="position">The drawing location on screen.</param>
240
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
241
-
/// <param name="color">Color multiplier.</param>
242
-
/// <param name="rotation">A rotation of this sprite.</param>
243
-
/// <param name="origin">Sprite center.</param>
244
-
/// <param name="scale">A scaling of this sprite.</param>
245
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
246
-
public void Draw(ITexture2D texture,
247
-
Vector2 position,
248
-
RectangleF? sourceRectangle,
249
-
Color color,
250
-
float rotation,
251
-
Vector2 origin,
252
-
float scale,
253
-
float layerDepth);
254
-
255
-
/// <summary>
256
-
/// Submit a sprite for drawing in the current batch.
257
-
/// </summary>
258
-
/// <param name="texture">The source texture.</param>
259
-
/// <param name="position">The drawing location on screen.</param>
260
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
261
-
/// <param name="color">Color multiplier.</param>
262
-
/// <param name="options">Options that modify the drawing.</param>
263
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
264
-
public void Draw(ITexture2D texture,
265
-
Vector2 position,
266
-
RectangleF? sourceRectangle,
267
-
Color color,
268
-
SpriteOptions options,
269
-
float layerDepth);
270
-
271
-
/// <summary>
272
-
/// Submit a sprite for drawing in the current batch.
273
-
/// </summary>
274
-
/// <param name="texture">The source texture.</param>
275
-
/// <param name="position">The drawing location on screen.</param>
276
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
277
-
/// <param name="color">Color multiplier.</param>
278
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
279
-
public void Draw(ITexture2D texture,
280
-
Vector2 position,
281
-
RectangleF? sourceRectangle,
282
-
Color color,
283
-
float layerDepth);
284
-
285
-
/// <summary>
286
-
/// Submit a sprite for drawing in the current batch.
287
-
/// </summary>
288
-
/// <param name="texture">The source texture.</param>
289
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
290
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
291
-
/// <param name="color">Color multiplier.</param>
292
-
/// <param name="options">Options that modify the drawing.</param>
293
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
294
-
public void Draw(ITexture2D texture,
295
-
RectangleF destinationRectangle,
296
-
RectangleF? sourceRectangle,
297
-
Color color,
298
-
SpriteOptions options,
299
-
float layerDepth);
300
-
301
-
/// <summary>
302
-
/// Submit a sprite for drawing in the current batch.
303
-
/// </summary>
304
-
/// <param name="texture">The source texture.</param>
305
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
306
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
307
-
/// <param name="color">Color multiplier.</param>
308
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
309
-
public void Draw(ITexture2D texture,
310
-
RectangleF destinationRectangle,
311
-
RectangleF? sourceRectangle,
312
-
Color color,
313
-
float layerDepth);
314
-
315
-
/// <summary>
316
-
/// Submit a sprite for drawing in the current batch.
317
-
/// </summary>
318
-
/// <param name="texture">The source texture.</param>
319
-
/// <param name="position">The drawing location on screen.</param>
320
-
/// <param name="color">Color multiplier.</param>
321
-
/// <param name="options">Options that modify the drawing.</param>
322
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
323
-
public void Draw(ITexture2D texture,
324
-
Vector2 position,
325
-
Color color,
326
-
SpriteOptions options,
327
-
float layerDepth);
328
-
329
-
/// <summary>
330
-
/// Submit a sprite for drawing in the current batch.
331
-
/// </summary>
332
-
/// <param name="texture">The source texture.</param>
333
-
/// <param name="position">The drawing location on screen.</param>
334
-
/// <param name="color">Color multiplier.</param>
335
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
336
-
public void Draw(ITexture2D texture,
337
-
Vector2 position,
338
-
Color color,
339
-
float layerDepth);
340
-
341
-
/// <summary>
342
-
/// Submit a sprite for drawing in the current batch.
343
-
/// </summary>
344
-
/// <param name="texture">The source texture.</param>
345
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
346
-
/// <param name="color">Color multiplier.</param>
347
-
/// <param name="options">Options that modify the drawing.</param>
348
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
349
-
public void Draw(ITexture2D texture,
350
-
RectangleF destinationRectangle,
351
-
Color color,
352
-
SpriteOptions options,
353
-
float layerDepth);
354
-
355
-
/// <summary>
356
-
/// Submit a sprite for drawing in the current batch.
357
-
/// </summary>
358
-
/// <param name="texture">The source texture.</param>
359
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
360
-
/// <param name="color">Color multiplier.</param>
361
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
362
-
public void Draw(ITexture2D texture,
363
-
RectangleF destinationRectangle,
364
-
Color color,
365
-
float layerDepth);
366
-
}
113
+
/// <summary>
114
+
/// Submit sprite draw in the batch.
115
+
/// </summary>
116
+
/// <param name="image">The source image.</param>
117
+
/// <param name="position">The drawing location on screen.</param>
118
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
119
+
/// <param name="color">Color multiplier.</param>
120
+
/// <param name="rotation">The rotation of the sprite.</param>
121
+
/// <param name="origin">Sprite center.</param>
122
+
/// <param name="scale">A scaling of this sprite.</param>
123
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
124
+
public void Draw(Image image,
125
+
Vector2 position,
126
+
RectangleF sourceRectangle,
127
+
Color color,
128
+
float rotation,
129
+
Vector2 origin,
130
+
Vector2 scale,
131
+
float layerDepth);
367
132
368
133
/// <summary>
369
-
/// Represents a interface for drawing sprites in one or more optimized batches.
134
+
/// Submit a sprite for drawing in the current batch.
370
135
/// </summary>
371
-
public interface ISpriteBatch<TTexture> : ISpriteBatch where TTexture : notnull, ITexture2D
372
-
{
373
-
/// <summary>
374
-
/// Submit sprite draw in the batch.
375
-
/// </summary>
376
-
/// <param name="texture">The source texture.</param>
377
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
378
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
379
-
/// <param name="color">Color multiplier.</param>
380
-
/// <param name="rotation">The rotation of the sprite.</param>
381
-
/// <param name="origin">Sprite center.</param>
382
-
/// <param name="options">Options that modify the drawing.</param>
383
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
384
-
public void Draw(TTexture texture,
385
-
RectangleF destinationRectangle,
386
-
RectangleF sourceRectangle,
387
-
Color color,
388
-
float rotation,
389
-
Vector2 origin,
390
-
SpriteOptions options,
391
-
float layerDepth);
392
-
393
-
/// <summary>
394
-
/// Submit sprite draw in the batch.
395
-
/// </summary>
396
-
/// <param name="texture">The source texture.</param>
397
-
/// <param name="position">The drawing location on screen.</param>
398
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
399
-
/// <param name="color">Color multiplier.</param>
400
-
/// <param name="rotation">The rotation of the sprite.</param>
401
-
/// <param name="origin">Sprite center.</param>
402
-
/// <param name="scale">A scaling of this sprite.</param>
403
-
/// <param name="options">Options that modify the drawing.</param>
404
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
405
-
public void Draw(TTexture texture,
406
-
Vector2 position,
407
-
RectangleF sourceRectangle,
408
-
Color color,
409
-
float rotation,
410
-
Vector2 origin,
411
-
Vector2 scale,
412
-
SpriteOptions options,
413
-
float layerDepth);
414
-
415
-
/// <summary>
416
-
/// Submit sprite draw in the batch.
417
-
/// </summary>
418
-
/// <param name="texture">The source texture.</param>
419
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
420
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
421
-
/// <param name="color">Color multiplier.</param>
422
-
/// <param name="rotation">The rotation of the sprite.</param>
423
-
/// <param name="origin">Sprite center.</param>
424
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
425
-
public void Draw(TTexture texture,
426
-
RectangleF destinationRectangle,
427
-
RectangleF sourceRectangle,
428
-
Color color,
429
-
float rotation,
430
-
Vector2 origin,
431
-
float layerDepth);
432
-
433
-
/// <summary>
434
-
/// Submit sprite draw in the batch.
435
-
/// </summary>
436
-
/// <param name="texture">The source texture.</param>
437
-
/// <param name="position">The drawing location on screen.</param>
438
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
439
-
/// <param name="color">Color multiplier.</param>
440
-
/// <param name="rotation">The rotation of the sprite.</param>
441
-
/// <param name="origin">Sprite center.</param>
442
-
/// <param name="scale">A scaling of this sprite.</param>
443
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
444
-
public void Draw(TTexture texture,
445
-
Vector2 position,
446
-
RectangleF sourceRectangle,
447
-
Color color,
448
-
float rotation,
449
-
Vector2 origin,
450
-
Vector2 scale,
451
-
float layerDepth);
452
-
453
-
/// <summary>
454
-
/// Submit a sprite for drawing in the current batch.
455
-
/// </summary>
456
-
/// <param name="texture">The source texture.</param>
457
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
458
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
459
-
/// <param name="color">Color multiplier.</param>
460
-
/// <param name="rotation">A rotation of this sprite.</param>
461
-
/// <param name="origin">Sprite center.</param>
462
-
/// <param name="options">Options that modify the drawing.</param>
463
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
464
-
public void Draw(TTexture texture,
136
+
/// <param name="image">The source image.</param>
137
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
138
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
139
+
/// <param name="color">Color multiplier.</param>
140
+
/// <param name="rotation">A rotation of this sprite.</param>
141
+
/// <param name="origin">Sprite center.</param>
142
+
/// <param name="options">Options that modify the drawing.</param>
143
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
144
+
public void Draw(Image image,
465
145
RectangleF destinationRectangle,
466
146
RectangleF? sourceRectangle,
467
147
Color color,
···
470
150
SpriteOptions options,
471
151
float layerDepth);
472
152
473
-
/// <summary>
474
-
/// Submit a sprite for drawing in the current batch.
475
-
/// </summary>
476
-
/// <param name="texture">The source texture.</param>
477
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
478
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
479
-
/// <param name="color">Color multiplier.</param>
480
-
/// <param name="rotation">A rotation of this sprite.</param>
481
-
/// <param name="origin">Sprite center.</param>
482
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
483
-
public void Draw(TTexture texture,
153
+
/// <summary>
154
+
/// Submit a sprite for drawing in the current batch.
155
+
/// </summary>
156
+
/// <param name="image">The source image.</param>
157
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
158
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
159
+
/// <param name="color">Color multiplier.</param>
160
+
/// <param name="rotation">A rotation of this sprite.</param>
161
+
/// <param name="origin">Sprite center.</param>
162
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
163
+
public void Draw(Image image,
484
164
RectangleF destinationRectangle,
485
165
RectangleF? sourceRectangle,
486
166
Color color,
···
488
168
Vector2 origin,
489
169
float layerDepth);
490
170
491
-
/// <summary>
492
-
/// Submit sprite draw in the batch.
493
-
/// </summary>
494
-
/// <param name="texture">The source texture.</param>
495
-
/// <param name="position">The drawing location on screen.</param>
496
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
497
-
/// <param name="color">Color multiplier.</param>
498
-
/// <param name="rotation">The rotation of the sprite.</param>
499
-
/// <param name="origin">Sprite center.</param>
500
-
/// <param name="scale">A scaling of this sprite.</param>
501
-
/// <param name="options">Options that modify the drawing.</param>
502
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
503
-
public void Draw(TTexture texture,
504
-
Vector2 position,
505
-
RectangleF? sourceRectangle,
506
-
Color color,
507
-
float rotation,
508
-
Vector2 origin,
509
-
Vector2 scale,
510
-
SpriteOptions options,
511
-
float layerDepth);
171
+
/// <summary>
172
+
/// Submit sprite draw in the batch.
173
+
/// </summary>
174
+
/// <param name="image">The source image.</param>
175
+
/// <param name="position">The drawing location on screen.</param>
176
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
177
+
/// <param name="color">Color multiplier.</param>
178
+
/// <param name="rotation">The rotation of the sprite.</param>
179
+
/// <param name="origin">Sprite center.</param>
180
+
/// <param name="scale">A scaling of this sprite.</param>
181
+
/// <param name="options">Options that modify the drawing.</param>
182
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
183
+
public void Draw(Image image,
184
+
Vector2 position,
185
+
RectangleF? sourceRectangle,
186
+
Color color,
187
+
float rotation,
188
+
Vector2 origin,
189
+
Vector2 scale,
190
+
SpriteOptions options,
191
+
float layerDepth);
512
192
513
-
/// <summary>
514
-
/// Submit sprite draw in the batch.
515
-
/// </summary>
516
-
/// <param name="texture">The source texture.</param>
517
-
/// <param name="position">The drawing location on screen.</param>
518
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
519
-
/// <param name="color">Color multiplier.</param>
520
-
/// <param name="rotation">The rotation of the sprite.</param>
521
-
/// <param name="origin">Sprite center.</param>
522
-
/// <param name="scale">A scaling of this sprite.</param>
523
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
524
-
public void Draw(TTexture texture,
525
-
Vector2 position,
526
-
RectangleF? sourceRectangle,
527
-
Color color,
528
-
float rotation,
529
-
Vector2 origin,
530
-
Vector2 scale,
531
-
float layerDepth);
193
+
/// <summary>
194
+
/// Submit sprite draw in the batch.
195
+
/// </summary>
196
+
/// <param name="image">The source image.</param>
197
+
/// <param name="position">The drawing location on screen.</param>
198
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
199
+
/// <param name="color">Color multiplier.</param>
200
+
/// <param name="rotation">The rotation of the sprite.</param>
201
+
/// <param name="origin">Sprite center.</param>
202
+
/// <param name="scale">A scaling of this sprite.</param>
203
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
204
+
public void Draw(Image image,
205
+
Vector2 position,
206
+
RectangleF? sourceRectangle,
207
+
Color color,
208
+
float rotation,
209
+
Vector2 origin,
210
+
Vector2 scale,
211
+
float layerDepth);
532
212
533
-
/// <summary>
534
-
/// Submit a sprite for drawing in the current batch.
535
-
/// </summary>
536
-
/// <param name="texture">The source texture.</param>
537
-
/// <param name="position">The drawing location on screen.</param>
538
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
539
-
/// <param name="color">Color multiplier.</param>
540
-
/// <param name="rotation">A rotation of this sprite.</param>
541
-
/// <param name="origin">Sprite center.</param>
542
-
/// <param name="scale">A scaling of this sprite.</param>
543
-
/// <param name="options">Options that modify the drawing.</param>
544
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
545
-
public void Draw(TTexture texture,
546
-
Vector2 position,
547
-
RectangleF? sourceRectangle,
548
-
Color color,
549
-
float rotation,
550
-
Vector2 origin,
551
-
float scale,
552
-
SpriteOptions options,
553
-
float layerDepth);
213
+
/// <summary>
214
+
/// Submit a sprite for drawing in the current batch.
215
+
/// </summary>
216
+
/// <param name="image">The source image.</param>
217
+
/// <param name="position">The drawing location on screen.</param>
218
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
219
+
/// <param name="color">Color multiplier.</param>
220
+
/// <param name="rotation">A rotation of this sprite.</param>
221
+
/// <param name="origin">Sprite center.</param>
222
+
/// <param name="scale">A scaling of this sprite.</param>
223
+
/// <param name="options">Options that modify the drawing.</param>
224
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
225
+
public void Draw(Image image,
226
+
Vector2 position,
227
+
RectangleF? sourceRectangle,
228
+
Color color,
229
+
float rotation,
230
+
Vector2 origin,
231
+
float scale,
232
+
SpriteOptions options,
233
+
float layerDepth);
554
234
555
-
/// <summary>
556
-
/// Submit a sprite for drawing in the current batch.
557
-
/// </summary>
558
-
/// <param name="texture">The source texture.</param>
559
-
/// <param name="position">The drawing location on screen.</param>
560
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
561
-
/// <param name="color">Color multiplier.</param>
562
-
/// <param name="rotation">A rotation of this sprite.</param>
563
-
/// <param name="origin">Sprite center.</param>
564
-
/// <param name="scale">A scaling of this sprite.</param>
565
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
566
-
public void Draw(TTexture texture,
235
+
/// <summary>
236
+
/// Submit a sprite for drawing in the current batch.
237
+
/// </summary>
238
+
/// <param name="image">The source image.</param>
239
+
/// <param name="position">The drawing location on screen.</param>
240
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
241
+
/// <param name="color">Color multiplier.</param>
242
+
/// <param name="rotation">A rotation of this sprite.</param>
243
+
/// <param name="origin">Sprite center.</param>
244
+
/// <param name="scale">A scaling of this sprite.</param>
245
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
246
+
public void Draw(Image image,
567
247
Vector2 position,
568
248
RectangleF? sourceRectangle,
569
249
Color color,
···
572
252
float scale,
573
253
float layerDepth);
574
254
575
-
/// <summary>
576
-
/// Submit a sprite for drawing in the current batch.
577
-
/// </summary>
578
-
/// <param name="texture">The source texture.</param>
579
-
/// <param name="position">The drawing location on screen.</param>
580
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
581
-
/// <param name="color">Color multiplier.</param>
582
-
/// <param name="options">Options that modify the drawing.</param>
583
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
584
-
public void Draw(TTexture texture,
255
+
/// <summary>
256
+
/// Submit a sprite for drawing in the current batch.
257
+
/// </summary>
258
+
/// <param name="image">The source image.</param>
259
+
/// <param name="position">The drawing location on screen.</param>
260
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
261
+
/// <param name="color">Color multiplier.</param>
262
+
/// <param name="options">Options that modify the drawing.</param>
263
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
264
+
public void Draw(Image image,
585
265
Vector2 position,
586
266
RectangleF? sourceRectangle,
587
267
Color color,
588
268
SpriteOptions options,
589
269
float layerDepth);
590
270
591
-
/// <summary>
592
-
/// Submit a sprite for drawing in the current batch.
593
-
/// </summary>
594
-
/// <param name="texture">The source texture.</param>
595
-
/// <param name="position">The drawing location on screen.</param>
596
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
597
-
/// <param name="color">Color multiplier.</param>
598
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
599
-
public void Draw(TTexture texture,
271
+
/// <summary>
272
+
/// Submit a sprite for drawing in the current batch.
273
+
/// </summary>
274
+
/// <param name="image">The source image.</param>
275
+
/// <param name="position">The drawing location on screen.</param>
276
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
277
+
/// /// <param name="color">Color multiplier.</param>
278
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
279
+
public void Draw(Image image,
600
280
Vector2 position,
601
281
RectangleF? sourceRectangle,
602
282
Color color,
603
283
float layerDepth);
604
284
605
-
/// <summary>
606
-
/// Submit a sprite for drawing in the current batch.
607
-
/// </summary>
608
-
/// <param name="texture">The source texture.</param>
609
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
610
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
611
-
/// <param name="color">Color multiplier.</param>
612
-
/// <param name="options">Options that modify the drawing.</param>
613
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
614
-
public void Draw(TTexture texture,
285
+
/// <summary>
286
+
/// Submit a sprite for drawing in the current batch.
287
+
/// </summary>
288
+
/// <param name="image"> The source image.</param>
289
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
290
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
291
+
/// <param name="color">Color multiplier.</param>
292
+
/// <param name="options">Options that modify the drawing.</param>
293
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
294
+
public void Draw(Image image,
615
295
RectangleF destinationRectangle,
616
296
RectangleF? sourceRectangle,
617
297
Color color,
618
298
SpriteOptions options,
619
299
float layerDepth);
620
300
621
-
/// <summary>
622
-
/// Submit a sprite for drawing in the current batch.
623
-
/// </summary>
624
-
/// <param name="texture">The source texture.</param>
625
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
626
-
/// <param name="sourceRectangle">An optional region on the texture which will be rendered. If null - draws full texture.</param>
627
-
/// <param name="color">Color multiplier.</param>
628
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
629
-
public void Draw(TTexture texture,
301
+
/// <summary>
302
+
/// Submit a sprite for drawing in the current batch.
303
+
/// </summary>
304
+
/// <param name="image">The source image.</param>
305
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
306
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
307
+
/// <param name="color">Color multiplier.</param>
308
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
309
+
public void Draw(Image image,
630
310
RectangleF destinationRectangle,
631
311
RectangleF? sourceRectangle,
632
312
Color color,
633
313
float layerDepth);
634
314
635
-
/// <summary>
636
-
/// Submit a sprite for drawing in the current batch.
637
-
/// </summary>
638
-
/// <param name="texture">The source texture.</param>
639
-
/// <param name="position">The drawing location on screen.</param>
640
-
/// <param name="color">Color multiplier.</param>
641
-
/// <param name="options">Options that modify the drawing.</param>
642
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
643
-
public void Draw(TTexture texture,
315
+
/// <summary>
316
+
/// Submit a sprite for drawing in the current batch.
317
+
/// </summary>
318
+
/// <param name="image">The source image.</param>
319
+
/// <param name="position">The drawing location on screen.</param>
320
+
/// <param name="color">Color multiplier.</param>
321
+
/// <param name="options">Options that modify the drawing.</param>
322
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
323
+
public void Draw(Image image,
644
324
Vector2 position,
645
325
Color color,
646
326
SpriteOptions options,
647
327
float layerDepth);
648
328
649
-
/// <summary>
650
-
/// Submit a sprite for drawing in the current batch.
651
-
/// </summary>
652
-
/// <param name="texture">The source texture.</param>
653
-
/// <param name="position">The drawing location on screen.</param>
654
-
/// <param name="color">Color multiplier.</param>
655
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
656
-
public void Draw(TTexture texture,
329
+
/// <summary>
330
+
/// Submit a sprite for drawing in the current batch.
331
+
/// </summary>
332
+
/// <param name="image">The source image.</param>
333
+
/// <param name="position">The drawing location on screen.</param>
334
+
/// <param name="color">Color multiplier.</param>
335
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
336
+
public void Draw(Image image,
337
+
Vector2 position,
338
+
Color color,
339
+
float layerDepth);
340
+
341
+
/// <summary>
342
+
/// Submit a sprite for drawing in the current batch.
343
+
/// </summary>
344
+
/// <param name="image">The source image.</param>
345
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
346
+
/// <param name="color">Color multiplier.</param>
347
+
/// <param name="options">Options that modify the drawing.</param>
348
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
349
+
public void Draw(Image image,
350
+
RectangleF destinationRectangle,
351
+
Color color,
352
+
SpriteOptions options,
353
+
float layerDepth);
354
+
355
+
/// <summary>
356
+
/// Submit a sprite for drawing in the current batch.
357
+
/// </summary>
358
+
/// <param name="image">The source image.</param>
359
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
360
+
/// <param name="color">Color multiplier.</param>
361
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
362
+
public void Draw(Image image,
363
+
RectangleF destinationRectangle,
364
+
Color color,
365
+
float layerDepth);
366
+
367
+
#region Draw without image
368
+
/// <summary>
369
+
/// Submit a rectangle for drawing in the current batch.
370
+
/// </summary>
371
+
/// <param name="rectangle">The drawing bounds on screen.</param>
372
+
/// <param name="color">Color multiplier.</param>
373
+
/// <param name="rotation">The rotation of the sprite.</param>
374
+
/// <param name="origin">Sprite center.</param>
375
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
376
+
public void DrawRect(
377
+
RectangleF rectangle,
378
+
Color color,
379
+
float rotation,
380
+
Vector2 origin,
381
+
float layerDepth);
382
+
383
+
/// <summary>
384
+
/// Submit a rectangle for drawing in the current batch.
385
+
/// </summary>
386
+
/// <param name="rectangle">The drawing bounds on screen.</param>
387
+
/// <param name="color">Color multiplier.</param>
388
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
389
+
public void DrawRect(
390
+
RectangleF rectangle,
391
+
Color color,
392
+
float layerDepth);
393
+
394
+
/// <summary>
395
+
/// Submit a square for drawing in the current batch.Vector2.Zero
396
+
/// </summary>
397
+
/// <param name="position">The drawing location on screen.</param>
398
+
/// <param name="size">The size of quad</param>
399
+
/// <param name="color">Color multiplier.</param>
400
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
401
+
public void DrawDot(
657
402
Vector2 position,
403
+
float size,
658
404
Color color,
659
405
float layerDepth);
660
406
661
-
/// <summary>
662
-
/// Submit a sprite for drawing in the current batch.
663
-
/// </summary>
664
-
/// <param name="texture">The source texture.</param>
665
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
666
-
/// <param name="color">Color multiplier.</param>
667
-
/// <param name="options">Options that modify the drawing.</param>
668
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
669
-
public void Draw(TTexture texture,
407
+
/// <summary>
408
+
/// Submit a line segment for drawing in the current batch.
409
+
/// </summary>
410
+
/// <param name="a">Endpoint A.</param>
411
+
/// <param name="b">Endpoint B</param>
412
+
/// <param name="color">Color.</param>
413
+
/// <param name="thickness">Line segment thickness.</param>
414
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
415
+
public void DrawSegment(
416
+
Vector2 a,
417
+
Vector2 b,
418
+
Color color,
419
+
float thickness,
420
+
float layerDepth);
421
+
422
+
/// <summary>
423
+
/// Submit a line segment for drawing in the current batch.
424
+
/// </summary>
425
+
/// <param name="start">The reference endpoint.</param>
426
+
/// <param name="length">The segment length.</param>
427
+
/// <param name="angle">The segment angle.</param>
428
+
/// <param name="color">The angle at start.</param>
429
+
/// <param name="thickness">Line segment thickness.</param>
430
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
431
+
public void DrawSegment(
432
+
Vector2 start,
433
+
float length,
434
+
Color color,
435
+
float angle,
436
+
float thickness,
437
+
float layerDepth);
438
+
#endregion
439
+
}
440
+
441
+
/// <summary>
442
+
/// Represents a interface for drawing sprites in one or more optimized batches.
443
+
/// </summary>
444
+
public interface ISpriteBatch<TTexture> : ISpriteBatch
445
+
{
446
+
/// <summary>
447
+
/// Submit sprite draw in the batch.
448
+
/// </summary>
449
+
/// <param name="image">The source image.</param>
450
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
451
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
452
+
/// <param name="color">Color multiplier.</param>
453
+
/// <param name="rotation">The rotation of the sprite.</param>
454
+
/// <param name="origin">Sprite center.</param>
455
+
/// <param name="options">Options that modify the drawing.</param>
456
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
457
+
public void Draw(TTexture image,
670
458
RectangleF destinationRectangle,
459
+
RectangleF sourceRectangle,
671
460
Color color,
461
+
float rotation,
462
+
Vector2 origin,
672
463
SpriteOptions options,
673
464
float layerDepth);
674
465
675
-
/// <summary>
676
-
/// Submit a sprite for drawing in the current batch.
677
-
/// </summary>
678
-
/// <param name="texture">The source texture.</param>
679
-
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
680
-
/// <param name="color">Color multiplier.</param>
681
-
/// <param name="layerDepth">A depth of the layer of this sprite.</param>
682
-
public void Draw(TTexture texture,
466
+
/// <summary>
467
+
/// Submit sprite draw in the batch.
468
+
/// </summary>
469
+
/// <param name="image">The source image.</param>
470
+
/// <param name="position">The drawing location on screen.</param>
471
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
472
+
/// <param name="color">Color multiplier.</param>
473
+
/// <param name="rotation">The rotation of the sprite.</param>
474
+
/// <param name="origin">Sprite center.</param>
475
+
/// <param name="scale">A scaling of this sprite.</param>
476
+
/// <param name="options">Options that modify the drawing.</param>
477
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
478
+
public void Draw(TTexture image,
479
+
Vector2 position,
480
+
RectangleF sourceRectangle,
481
+
Color color,
482
+
float rotation,
483
+
Vector2 origin,
484
+
Vector2 scale,
485
+
SpriteOptions options,
486
+
float layerDepth);
487
+
488
+
/// <summary>
489
+
/// Submit sprite draw in the batch.
490
+
/// </summary>
491
+
/// <param name="image">The source image.</param>
492
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
493
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
494
+
/// <param name="color">Color multiplier.</param>
495
+
/// <param name="rotation">The rotation of the sprite.</param>
496
+
/// <param name="origin">Sprite center.</param>
497
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
498
+
public void Draw(TTexture image,
683
499
RectangleF destinationRectangle,
500
+
RectangleF sourceRectangle,
684
501
Color color,
502
+
float rotation,
503
+
Vector2 origin,
685
504
float layerDepth);
686
-
}
505
+
506
+
/// <summary>
507
+
/// Submit sprite draw in the batch.
508
+
/// </summary>
509
+
/// <param name="image">The source image.</param>
510
+
/// <param name="position">The drawing location on screen.</param>
511
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
512
+
/// <param name="color">Color multiplier.</param>
513
+
/// <param name="rotation">The rotation of the sprite.</param>
514
+
/// <param name="origin">Sprite center.</param>
515
+
/// <param name="scale">A scaling of this sprite.</param>
516
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
517
+
public void Draw(TTexture image,
518
+
Vector2 position,
519
+
RectangleF sourceRectangle,
520
+
Color color,
521
+
float rotation,
522
+
Vector2 origin,
523
+
Vector2 scale,
524
+
float layerDepth);
525
+
526
+
/// <summary>
527
+
/// Submit a sprite for drawing in the current batch.
528
+
/// </summary>
529
+
/// <param name="image">The source image.</param>
530
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
531
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
532
+
/// <param name="color">Color multiplier.</param>
533
+
/// <param name="rotation">A rotation of this sprite.</param>
534
+
/// <param name="origin">Sprite center.</param>
535
+
/// <param name="options">Options that modify the drawing.</param>
536
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
537
+
public void Draw(TTexture image,
538
+
RectangleF destinationRectangle,
539
+
RectangleF? sourceRectangle,
540
+
Color color,
541
+
float rotation,
542
+
Vector2 origin,
543
+
SpriteOptions options,
544
+
float layerDepth);
545
+
546
+
/// <summary>
547
+
/// Submit a sprite for drawing in the current batch.
548
+
/// </summary>
549
+
/// <param name="image">The source image.</param>
550
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
551
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
552
+
/// <param name="color">Color multiplier.</param>
553
+
/// <param name="rotation">A rotation of this sprite.</param>
554
+
/// <param name="origin">Sprite center.</param>
555
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
556
+
public void Draw(TTexture image,
557
+
RectangleF destinationRectangle,
558
+
RectangleF? sourceRectangle,
559
+
Color color,
560
+
float rotation,
561
+
Vector2 origin,
562
+
float layerDepth);
563
+
564
+
/// <summary>
565
+
/// Submit sprite draw in the batch.
566
+
/// </summary>
567
+
/// <param name="image">The source image.</param>
568
+
/// <param name="position">The drawing location on screen.</param>
569
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
570
+
/// <param name="color">Color multiplier.</param>
571
+
/// <param name="rotation">The rotation of the sprite.</param>
572
+
/// <param name="origin">Sprite center.</param>
573
+
/// <param name="scale">A scaling of this sprite.</param>
574
+
/// <param name="options">Options that modify the drawing.</param>
575
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
576
+
public void Draw(TTexture image,
577
+
Vector2 position,
578
+
RectangleF? sourceRectangle,
579
+
Color color,
580
+
float rotation,
581
+
Vector2 origin,
582
+
Vector2 scale,
583
+
SpriteOptions options,
584
+
float layerDepth);
585
+
586
+
/// <summary>
587
+
/// Submit sprite draw in the batch.
588
+
/// </summary>
589
+
/// <param name="image">The source image.</param>
590
+
/// <param name="position">The drawing location on screen.</param>
591
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
592
+
/// <param name="color">Color multiplier.</param>
593
+
/// <param name="rotation">The rotation of the sprite.</param>
594
+
/// <param name="origin">Sprite center.</param>
595
+
/// <param name="scale">A scaling of this sprite.</param>
596
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
597
+
public void Draw(TTexture image,
598
+
Vector2 position,
599
+
RectangleF? sourceRectangle,
600
+
Color color,
601
+
float rotation,
602
+
Vector2 origin,
603
+
Vector2 scale,
604
+
float layerDepth);
605
+
606
+
/// <summary>
607
+
/// Submit a sprite for drawing in the current batch.
608
+
/// </summary>
609
+
/// <param name="image">The source image.</param>
610
+
/// <param name="position">The drawing location on screen.</param>
611
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
612
+
/// <param name="color">Color multiplier.</param>
613
+
/// <param name="rotation">A rotation of this sprite.</param>
614
+
/// <param name="origin">Sprite center.</param>
615
+
/// <param name="scale">A scaling of this sprite.</param>
616
+
/// <param name="options">Options that modify the drawing.</param>
617
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
618
+
public void Draw(TTexture image,
619
+
Vector2 position,
620
+
RectangleF? sourceRectangle,
621
+
Color color,
622
+
float rotation,
623
+
Vector2 origin,
624
+
float scale,
625
+
SpriteOptions options,
626
+
float layerDepth);
627
+
628
+
/// <summary>
629
+
/// Submit a sprite for drawing in the current batch.
630
+
/// </summary>
631
+
/// <param name="image">The source image.</param>
632
+
/// <param name="position">The drawing location on screen.</param>
633
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
634
+
/// <param name="color">Color multiplier.</param>
635
+
/// <param name="rotation">A rotation of this sprite.</param>
636
+
/// <param name="origin">Sprite center.</param>
637
+
/// <param name="scale">A scaling of this sprite.</param>
638
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
639
+
public void Draw(TTexture image,
640
+
Vector2 position,
641
+
RectangleF? sourceRectangle,
642
+
Color color,
643
+
float rotation,
644
+
Vector2 origin,
645
+
float scale,
646
+
float layerDepth);
647
+
648
+
/// <summary>
649
+
/// Submit a sprite for drawing in the current batch.
650
+
/// </summary>
651
+
/// <param name="image">The source image.</param>
652
+
/// <param name="position">The drawing location on screen.</param>
653
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
654
+
/// <param name="color">Color multiplier.</param>
655
+
/// <param name="options">Options that modify the drawing.</param>
656
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
657
+
public void Draw(TTexture image,
658
+
Vector2 position,
659
+
RectangleF? sourceRectangle,
660
+
Color color,
661
+
SpriteOptions options,
662
+
float layerDepth);
663
+
664
+
/// <summary>
665
+
/// Submit a sprite for drawing in the current batch.
666
+
/// </summary>
667
+
/// <param name="image">The source image.</param>
668
+
/// <param name="position">The drawing location on screen.</param>
669
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
670
+
/// <param name="color">Color multiplier.</param>
671
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
672
+
public void Draw(TTexture image,
673
+
Vector2 position,
674
+
RectangleF? sourceRectangle,
675
+
Color color,
676
+
float layerDepth);
677
+
678
+
/// <summary>
679
+
/// Submit a sprite for drawing in the current batch.
680
+
/// </summary>
681
+
/// <param name="image">The source image.</param>
682
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
683
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
684
+
/// <param name="color">Color multiplier.</param>
685
+
/// <param name="options">Options that modify the drawing.</param>
686
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
687
+
public void Draw(TTexture image,
688
+
RectangleF destinationRectangle,
689
+
RectangleF? sourceRectangle,
690
+
Color color,
691
+
SpriteOptions options,
692
+
float layerDepth);
693
+
694
+
/// <summary>
695
+
/// Submit a sprite for drawing in the current batch.
696
+
/// </summary>
697
+
/// <param name="image">The source image.</param>
698
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
699
+
/// <param name="sourceRectangle">An optional region on the image which will be rendered. If null - draws full image.</param>
700
+
/// <param name="color">Color multiplier.</param>
701
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
702
+
public void Draw(TTexture image,
703
+
RectangleF destinationRectangle,
704
+
RectangleF? sourceRectangle,
705
+
Color color,
706
+
float layerDepth);
707
+
708
+
/// <summary>
709
+
/// Submit a sprite for drawing in the current batch.
710
+
/// </summary>
711
+
/// <param name="image">The source image.</param>
712
+
/// <param name="position">The drawing location on screen.</param>
713
+
/// <param name="color">Color multiplier.</param>
714
+
/// <param name="options">Options that modify the drawing.</param>
715
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
716
+
public void Draw(TTexture image,
717
+
Vector2 position,
718
+
Color color,
719
+
SpriteOptions options,
720
+
float layerDepth);
721
+
722
+
/// <summary>
723
+
/// Submit a sprite for drawing in the current batch.
724
+
/// </summary>
725
+
/// <param name="image">The source image.</param>
726
+
/// <param name="position">The drawing location on screen.</param>
727
+
/// <param name="color">Color multiplier.</param>
728
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
729
+
public void Draw(TTexture image,
730
+
Vector2 position,
731
+
Color color,
732
+
float layerDepth);
733
+
734
+
/// <summary>
735
+
/// Submit a sprite for drawing in the current batch.
736
+
/// </summary>
737
+
/// <param name="image">The source image.</param>
738
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
739
+
/// <param name="color">Color multiplier.</param>
740
+
/// <param name="options">Options that modify the drawing.</param>
741
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
742
+
public void Draw(TTexture image,
743
+
RectangleF destinationRectangle,
744
+
Color color,
745
+
SpriteOptions options,
746
+
float layerDepth);
747
+
748
+
/// <summary>
749
+
/// Submit a sprite for drawing in the current batch.
750
+
/// </summary>
751
+
/// <param name="image">The source image.</param>
752
+
/// <param name="destinationRectangle">The drawing bounds on screen.</param>
753
+
/// <param name="color">Color multiplier.</param>
754
+
/// <param name="layerDepth">A layer depth of this drawing.</param>
755
+
public void Draw(TTexture image,
756
+
RectangleF destinationRectangle,
757
+
Color color,
758
+
float layerDepth);
687
759
}
-243
src/Peridot/ITextRenderer.cs
-243
src/Peridot/ITextRenderer.cs
···
1
-
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
-
// This code is licensed under MIT license (see LICENSE for details)
3
-
4
-
using System.Drawing;
5
-
using System.Numerics;
6
-
7
-
namespace Peridot
8
-
{
9
-
/// <summary>
10
-
/// Represents an interface to a text renderer.
11
-
/// </summary>
12
-
public interface ITextRenderer : IDisposable
13
-
{
14
-
/// <summary>
15
-
/// A bool indicating whether this instance has been disposed.
16
-
/// </summary>
17
-
public bool IsDisposed { get; }
18
-
19
-
/// <summary>
20
-
/// Submit a text string for drawing.
21
-
/// </summary>
22
-
/// <param name="font">The font to draw.</param>
23
-
/// <param name="fontSize">The font size.</param>
24
-
/// <param name="text">The text which will be drawn.</param>
25
-
/// <param name="position">The drawing location on screen.</param>
26
-
/// <param name="color">The text color.</param>
27
-
/// <param name="rotation">The rotation of the drawing.</param>
28
-
/// <param name="origin">The center of the rotation. (0, 0) for default.</param>
29
-
/// <param name="scale">The scaling of this drawing.</param>
30
-
/// <param name="layerDepth">The layer depth of this drawing.</param>
31
-
public void DrawString(
32
-
IFont font,
33
-
int fontSize,
34
-
string text,
35
-
Vector2 position,
36
-
Color color,
37
-
float rotation,
38
-
Vector2 origin,
39
-
Vector2 scale,
40
-
float layerDepth);
41
-
42
-
/// <summary>
43
-
/// Submit a text string for drawing.
44
-
/// </summary>
45
-
/// <param name="font">The font to draw.</param>
46
-
/// <param name="fontSize">The font size.</param>
47
-
/// <param name="text">The text which will be drawn.</param>
48
-
/// <param name="position">The drawing location on screen.</param>
49
-
/// <param name="color">The text color.</param>
50
-
/// <param name="rotation">The rotation of the drawing.</param>
51
-
/// <param name="origin">The center of the rotation. (0, 0) for default.</param>
52
-
/// <param name="scale">The scaling of this drawing.</param>
53
-
/// <param name="layerDepth">The layer depth of this drawing.</param>
54
-
/// <param name="scissor">The scissor rectangle.</param>
55
-
public void DrawString(
56
-
IFont font,
57
-
int fontSize,
58
-
string text,
59
-
Vector2 position,
60
-
Color color,
61
-
float rotation,
62
-
Vector2 origin,
63
-
Vector2 scale,
64
-
float layerDepth,
65
-
Rectangle scissor);
66
-
67
-
/// <summary>
68
-
/// Submit a text string for drawing.
69
-
/// </summary>
70
-
/// <param name="font">The font to draw.</param>
71
-
/// <param name="fontSize">The font size.</param>
72
-
/// <param name="text">The text which will be drawn.</param>
73
-
/// <param name="position">The drawing location on screen</param>
74
-
/// <param name="color">The text color.</param>
75
-
public void DrawString(
76
-
IFont font,
77
-
int fontSize,
78
-
string text,
79
-
Vector2 position,
80
-
Color color)
81
-
{
82
-
DrawString(font, fontSize, text, position, color, 0, Vector2.Zero, Vector2.One, 0);
83
-
}
84
-
85
-
/// <summary>
86
-
/// Submit a text string for drawing.
87
-
/// </summary>
88
-
/// <param name="font">The font to draw.</param>
89
-
/// <param name="fontSize">The font size.</param>
90
-
/// <param name="text">The text which will be drawn.</param>
91
-
/// <param name="position">The drawing location on screen</param>
92
-
/// <param name="color">The text color.</param>
93
-
/// <param name="scissor">The scissor rectangle.</param>
94
-
public void DrawString(
95
-
IFont font,
96
-
int fontSize,
97
-
string text,
98
-
Vector2 position,
99
-
Color color,
100
-
Rectangle scissor)
101
-
{
102
-
DrawString(font, fontSize, text, position, color, 0, Vector2.Zero, Vector2.One, 0, scissor);
103
-
}
104
-
105
-
/// <summary>
106
-
/// Submit a text string for drawing.
107
-
/// </summary>
108
-
/// <param name="font">The font to draw.</param>
109
-
/// <param name="fontSize">The font size.</param>
110
-
/// <param name="text">The text which will be drawn.</param>
111
-
/// <param name="position">The drawing location on screen</param>
112
-
/// <param name="color">The text color.</param>
113
-
/// <param name="rotation">The rotation of the drawing.</param>
114
-
/// <param name="origin">The center of the rotation. (0, 0) for default.</param>
115
-
/// <param name="scale">The scaling of this drawing.</param>
116
-
/// <param name="layerDepth">The layer depth of this drawing.</param>
117
-
public void DrawString(
118
-
IFont font,
119
-
int fontSize,
120
-
string text,
121
-
Vector2 position,
122
-
Color color,
123
-
float rotation,
124
-
Vector2 origin,
125
-
float scale,
126
-
float layerDepth)
127
-
{
128
-
DrawString(font, fontSize, text, position, color, rotation, origin, new Vector2(scale), layerDepth);
129
-
}
130
-
131
-
/// <summary>
132
-
/// Submit a text string for drawing.
133
-
/// </summary>
134
-
/// <param name="font">The font to draw.</param>
135
-
/// <param name="fontSize">The font size.</param>
136
-
/// <param name="text">The text which will be drawn.</param>
137
-
/// <param name="position">The drawing location on screen</param>
138
-
/// <param name="color">The text color.</param>
139
-
/// <param name="rotation">The rotation of the drawing.</param>
140
-
/// <param name="origin">The center of the rotation. (0, 0) for default.</param>
141
-
/// <param name="scale">The scaling of this drawing.</param>
142
-
/// <param name="layerDepth">The layer depth of this drawing.</param>
143
-
/// <param name="scissor">The scissor rectangle.</param>
144
-
public void DrawString(
145
-
IFont font,
146
-
int fontSize,
147
-
string text,
148
-
Vector2 position,
149
-
Color color,
150
-
float rotation,
151
-
Vector2 origin,
152
-
float scale,
153
-
float layerDepth,
154
-
Rectangle scissor)
155
-
{
156
-
DrawString(font, fontSize, text, position, color, rotation, origin, new Vector2(scale), layerDepth, scissor);
157
-
}
158
-
}
159
-
160
-
/// <summary>
161
-
/// Represents an interface to a text renderer.
162
-
/// </summary>
163
-
public interface ITextRenderer<TFont> : ITextRenderer where TFont : notnull, IFont
164
-
{
165
-
/// <summary>
166
-
/// Submit a text string for drawing.
167
-
/// </summary>
168
-
/// <param name="font">The font to draw.</param>
169
-
/// <param name="fontSize">The font size.</param>
170
-
/// <param name="text">The text which will be drawn.</param>
171
-
/// <param name="position">The drawing location on screen.</param>
172
-
/// <param name="color">The text color.</param>
173
-
/// <param name="rotation">The rotation of the drawing.</param>
174
-
/// <param name="origin">The center of the rotation. (0, 0) for default.</param>
175
-
/// <param name="scale">The scaling of this drawing.</param>
176
-
/// <param name="layerDepth">The layer depth of this drawing.</param>
177
-
public abstract void DrawString(
178
-
TFont font,
179
-
int fontSize,
180
-
string text,
181
-
Vector2 position,
182
-
Color color,
183
-
float rotation,
184
-
Vector2 origin,
185
-
Vector2 scale,
186
-
float layerDepth);
187
-
188
-
/// <summary>
189
-
/// Submit a text string for drawing.
190
-
/// </summary>
191
-
/// <param name="font">The font to draw.</param>
192
-
/// <param name="fontSize">The font size.</param>
193
-
/// <param name="text">The text which will be drawn.</param>
194
-
/// <param name="position">The drawing location on screen.</param>
195
-
/// <param name="color">The text color.</param>
196
-
/// <param name="rotation">The rotation of the drawing.</param>
197
-
/// <param name="origin">The center of the rotation. (0, 0) for default.</param>
198
-
/// <param name="scale">The scaling of this drawing.</param>
199
-
/// <param name="layerDepth">The layer depth of this drawing.</param>
200
-
/// <param name="scissor">The scissor rectangle.</param>
201
-
public abstract void DrawString(
202
-
TFont font,
203
-
int fontSize,
204
-
string text,
205
-
Vector2 position,
206
-
Color color,
207
-
float rotation,
208
-
Vector2 origin,
209
-
Vector2 scale,
210
-
float layerDepth,
211
-
Rectangle scissor);
212
-
213
-
/// <summary>
214
-
/// Submit a text string for drawing.
215
-
/// </summary>
216
-
/// <param name="font">The font to draw.</param>
217
-
/// <param name="fontSize">The font size.</param>
218
-
/// <param name="text">The text which will be drawn.</param>
219
-
/// <param name="position">The drawing location on screen</param>
220
-
/// <param name="color">The text color.</param>
221
-
public void DrawString(TFont font, int fontSize, string text, Vector2 position, Color color)
222
-
{
223
-
DrawString(font, fontSize, text, position, color, 0, new Vector2(), new Vector2(1), 0);
224
-
}
225
-
226
-
/// <summary>
227
-
/// Submit a text string for drawing.
228
-
/// </summary>
229
-
/// <param name="font">The font to draw.</param>
230
-
/// <param name="fontSize">The font size.</param>
231
-
/// <param name="text">The text which will be drawn.</param>
232
-
/// <param name="position">The drawing location on screen</param>
233
-
/// <param name="color">The text color.</param>
234
-
/// <param name="rotation">The rotation of the drawing.</param>
235
-
/// <param name="origin">The center of the rotation. (0, 0) for default.</param>
236
-
/// <param name="scale">The scaling of this drawing.</param>
237
-
/// <param name="layerDepth">The layer depth of this drawing.</param>
238
-
public void DrawString(TFont font, int fontSize, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, float layerDepth)
239
-
{
240
-
DrawString(font, fontSize, text, position, color, rotation, origin, new Vector2(scale), layerDepth);
241
-
}
242
-
}
243
-
}
-23
src/Peridot/ITexture2D.cs
-23
src/Peridot/ITexture2D.cs
···
1
-
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
-
// This code is licensed under MIT license (see LICENSE for details)
3
-
4
-
using System.Drawing;
5
-
6
-
namespace Peridot
7
-
{
8
-
/// <summary>
9
-
/// Represents a 2D texture to use in <see cref="SpriteBatch{TTexture}"/>.
10
-
/// </summary>
11
-
public interface ITexture2D : IDisposable
12
-
{
13
-
/// <summary>
14
-
/// A bool indicating whether this instance has been disposed.
15
-
/// </summary>
16
-
public bool IsDisposed { get; }
17
-
18
-
/// <summary>
19
-
/// The total size of the texture.
20
-
/// </summary>
21
-
public Size Size { get; }
22
-
}
23
-
}
+26
src/Peridot/Image.cs
+26
src/Peridot/Image.cs
···
1
+
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
+
// This code is licensed under MIT license (see LICENSE for details)
3
+
4
+
using System.Drawing;
5
+
6
+
namespace Peridot;
7
+
8
+
/// <summary>
9
+
/// Represents a 2D texture to use in <see cref="SpriteBatch{TTexture}"/>.
10
+
/// </summary>
11
+
public interface Image : IDisposable
12
+
{
13
+
/// <summary>
14
+
/// A bool indicating whether this instance has been disposed.
15
+
/// </summary>
16
+
public bool IsDisposed { get; }
17
+
18
+
/// <summary>
19
+
/// The total size of the texture.
20
+
/// </summary>
21
+
public Size Size { get; }
22
+
public PixelFormat Format { get; }
23
+
24
+
public int Width => Size.Width;
25
+
public int Height => Size.Height;
26
+
}
+25
src/Peridot/ImageDescription.cs
+25
src/Peridot/ImageDescription.cs
···
1
+
namespace Peridot;
2
+
3
+
/// <summary>
4
+
/// Describes a image in Peridot.
5
+
/// </summary>
6
+
public struct ImageDescription
7
+
{
8
+
/// <summary>
9
+
/// Format of the image data.
10
+
/// </summary>
11
+
/// <value>Internal pixel format of image data.</value>
12
+
public PixelFormat Format { get; set; }
13
+
14
+
/// <summary>
15
+
/// The width of image.
16
+
/// </summary>
17
+
/// <value>Image width.</value>
18
+
public uint Width { get; set; }
19
+
20
+
/// <summary>
21
+
/// The height of image.
22
+
/// </summary>
23
+
/// <value>Image height.</value>
24
+
public uint Height { get; set; }
25
+
}
+5
src/Peridot/Peridot.csproj
+5
src/Peridot/Peridot.csproj
+41
src/Peridot/PixelFormat.cs
+41
src/Peridot/PixelFormat.cs
···
1
+
namespace Peridot;
2
+
3
+
/// <summary>
4
+
/// Specifies the pixel format.
5
+
/// </summary>
6
+
public enum PixelFormat
7
+
{
8
+
Undefined,
9
+
/// <summary>
10
+
/// One-channel, byte storage.
11
+
/// </summary>
12
+
R8,
13
+
/// <summary>
14
+
/// One-channel, short storage.
15
+
/// </summary>
16
+
R16,
17
+
/// <summary>
18
+
/// Four channels, byte storage.
19
+
/// </summary>
20
+
BGRA8,
21
+
/// <summary>
22
+
/// Four channels, float storage.
23
+
/// </summary>
24
+
RGBAF32,
25
+
/// <summary>
26
+
/// Two-channels, byte storage.
27
+
/// </summary>
28
+
RG8,
29
+
/// <summary>
30
+
/// Two-channels, short storage.
31
+
/// </summary>
32
+
RG16,
33
+
/// <summary>
34
+
/// Four channels, signed integer storage.
35
+
/// </summary>
36
+
RGBAI32,
37
+
/// <summary>
38
+
/// Four channels, byte storage.
39
+
/// </summary>
40
+
RGBA8,
41
+
}
+660
-614
src/Peridot/SpriteBatch.cs
+660
-614
src/Peridot/SpriteBatch.cs
···
4
4
using System.Drawing;
5
5
using System.Numerics;
6
6
7
-
namespace Peridot
7
+
namespace Peridot;
8
+
9
+
/// <summary>
10
+
/// A class for drawing sprites in one or more optimized batches.
11
+
/// </summary>
12
+
/// <typeparam name="TImage">The image type to renderer.</typeparam>
13
+
public abstract class SpriteBatch<TImage> : ISpriteBatch<TImage>
8
14
{
9
-
/// <summary>
10
-
/// A class for drawing sprites in one or more optimized batches.
11
-
/// </summary>
12
-
/// <typeparam name="TTexture">The texture type to renderer.</typeparam>
13
-
public abstract class SpriteBatch<TTexture> : ISpriteBatch<TTexture> where TTexture : notnull, ITexture2D
14
-
{
15
-
/// <summary>
16
-
/// The batcher with all entities to renderer.
17
-
/// </summary>
18
-
protected readonly Batcher<TTexture> _batcher;
15
+
/// <summary>
16
+
/// The batcher with all entities to renderer.
17
+
/// </summary>
18
+
protected readonly Batcher<TImage> m_batcher;
19
19
20
-
private bool _beginCalled;
20
+
protected readonly Image m_whiteImage;
21
21
22
-
/// <summary>
23
-
/// Creates a new <see cref="SpriteBatch{TTexture}"/>.
24
-
/// </summary>
25
-
public SpriteBatch()
26
-
{
27
-
_batcher = new();
28
-
_beginCalled = false;
29
-
IsDisposed = false;
30
-
ResetScissor();
31
-
}
22
+
private bool m_beginCalled;
32
23
33
-
/// <summary>
34
-
/// Deconstructor of <see cref="SpriteBatch{TTexture}"/>.
35
-
/// </summary>
36
-
~SpriteBatch()
37
-
{
38
-
CoreDispose(false);
39
-
}
24
+
/// <summary>
25
+
/// Creates a new <see cref="SpriteBatch{TImage}"/>.
26
+
/// </summary>
27
+
public SpriteBatch(Image whiteImage)
28
+
{
29
+
m_batcher = new();
30
+
m_whiteImage = whiteImage;
31
+
m_beginCalled = false;
32
+
IsDisposed = false;
33
+
ResetScissor();
34
+
}
40
35
41
-
/// <summary>
42
-
/// The view matrix to use to renderer.
43
-
/// </summary>
44
-
public Matrix4x4 ViewMatrix { get; set; }
36
+
/// <summary>
37
+
/// Deconstructor of <see cref="SpriteBatch{TImage}"/>.
38
+
/// </summary>
39
+
~SpriteBatch()
40
+
{
41
+
CoreDispose(false);
42
+
}
45
43
46
-
/// <inheritdoc/>
47
-
public bool IsDisposed { get; protected set; }
44
+
/// <summary>
45
+
/// The view matrix to use to renderer.
46
+
/// </summary>
47
+
public Matrix4x4 ViewMatrix { get; set; }
48
48
49
-
/// <inheritdoc/>
50
-
public RectangleF Scissor { get; set; }
49
+
/// <inheritdoc/>
50
+
public bool IsDisposed { get; protected set; }
51
51
52
-
/// <summary>
53
-
/// Begins the sprite branch.
54
-
/// </summary>
55
-
/// <exception cref="InvalidOperationException">Thrown if <see cref="Begin"/> is called next time without previous <see cref="End"/>.</exception>
56
-
public void Begin()
57
-
{
58
-
if (_beginCalled)
59
-
throw new InvalidOperationException("Begin cannot be called again until End has been successfully called.");
52
+
/// <inheritdoc/>
53
+
public RectangleF Scissor { get; set; }
60
54
61
-
ViewMatrix = Matrix4x4.Identity;
62
-
_beginCalled = true;
63
-
_batcher.Clear();
64
-
}
55
+
/// <summary>
56
+
/// Begins the sprite branch.
57
+
/// </summary>
58
+
/// <exception cref="InvalidOperationException">Thrown if <see cref="Begin"/> is called next time without previous <see cref="End"/>.</exception>
59
+
public void Begin()
60
+
{
61
+
if (m_beginCalled)
62
+
throw new InvalidOperationException("Begin cannot be called again until End has been successfully called.");
65
63
66
-
/// <summary>
67
-
/// Flushes all batched text and sprites to the screen.
68
-
/// </summary>
69
-
/// <exception cref="InvalidOperationException">This command should be called after <see cref="Begin"/> and drawing commands.</exception>
70
-
public void End()
71
-
{
72
-
if (!_beginCalled)
73
-
throw new InvalidOperationException("Begin must be called before calling End.");
64
+
ViewMatrix = Matrix4x4.Identity;
65
+
m_beginCalled = true;
66
+
m_batcher.Clear();
67
+
}
74
68
75
-
_beginCalled = false;
76
-
}
69
+
/// <summary>
70
+
/// Flushes all batched text and sprites to the screen.
71
+
/// </summary>
72
+
/// <exception cref="InvalidOperationException">This command should be called after <see cref="Begin"/> and drawing commands.</exception>
73
+
public void End()
74
+
{
75
+
if (!m_beginCalled)
76
+
throw new InvalidOperationException("Begin must be called before calling End.");
77
77
78
-
/// <inheritdoc/>
79
-
public void Draw(ITexture2D texture, RectangleF destinationRectangle, RectangleF sourceRectangle, Color color, float rotation, Vector2 origin, SpriteOptions options, float layerDepth) =>
80
-
Draw(SpriteBatch<TTexture>.Cast(texture), destinationRectangle, sourceRectangle, color, rotation, origin, options, layerDepth);
78
+
m_beginCalled = false;
79
+
}
81
80
82
-
/// <inheritdoc/>
83
-
public void Draw(ITexture2D texture, Vector2 position, RectangleF sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteOptions options, float layerDepth) =>
84
-
Draw(SpriteBatch<TTexture>.Cast(texture), position, sourceRectangle, color, rotation, origin, scale, options, layerDepth);
81
+
/// <inheritdoc/>
82
+
public void Draw(Image image, RectangleF destinationRectangle, RectangleF sourceRectangle, Color color, float rotation, Vector2 origin, SpriteOptions options, float layerDepth) =>
83
+
Draw(GetHandle(image), destinationRectangle, sourceRectangle, color, rotation, origin, options, layerDepth);
85
84
86
-
/// <inheritdoc/>
87
-
public void Draw(TTexture texture, RectangleF destinationRectangle, RectangleF sourceRectangle, Color color, float rotation, Vector2 origin, SpriteOptions options, float layerDepth)
88
-
{
89
-
CheckValid(texture);
90
-
ref var item = ref _batcher.Add(texture);
85
+
/// <inheritdoc/>
86
+
public void Draw(Image image, Vector2 position, RectangleF sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteOptions options, float layerDepth) =>
87
+
Draw(GetHandle(image), position, sourceRectangle, color, rotation, origin, scale, options, layerDepth);
91
88
92
-
var size = new Vector2(texture.Size.Width, texture.Size.Height);
93
-
item = new(size, destinationRectangle, sourceRectangle, color, rotation, origin, layerDepth, Transform(Scissor, ViewMatrix), options);
94
-
}
89
+
/// <inheritdoc/>
90
+
public void Draw(TImage image, RectangleF destinationRectangle, RectangleF sourceRectangle, Color color, float rotation, Vector2 origin, SpriteOptions options, float layerDepth)
91
+
{
92
+
CheckValid(image);
93
+
ref var item = ref m_batcher.Add(image);
94
+
var size = GetSize(image);
95
+
var vsize = new Vector2(size.Width, size.Height);
95
96
96
-
/// <inheritdoc/>
97
-
public void Draw(TTexture texture, Vector2 position, RectangleF sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteOptions options, float layerDepth)
98
-
{
99
-
CheckValid(texture);
100
-
ref var item = ref _batcher.Add(texture);
97
+
item = new(vsize, destinationRectangle, sourceRectangle, color, rotation, origin, layerDepth, Transform(Scissor, ViewMatrix), options);
98
+
}
101
99
102
-
var size = new Vector2(texture.Size.Width, texture.Size.Height);
103
-
item = new(size, position, sourceRectangle, color, rotation, origin, scale, layerDepth, Transform(Scissor, ViewMatrix), options);
104
-
}
100
+
/// <inheritdoc/>
101
+
public void Draw(TImage image, Vector2 position, RectangleF sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteOptions options, float layerDepth)
102
+
{
103
+
CheckValid(image);
104
+
ref var item = ref m_batcher.Add(image);
105
+
var size = GetSize(image);
106
+
var vsize = new Vector2(size.Width, size.Height);
105
107
106
-
/// <inheritdoc/>
107
-
public void Dispose()
108
-
{
109
-
CoreDispose(true);
110
-
GC.SuppressFinalize(this);
111
-
}
108
+
item = new(vsize, position, sourceRectangle, color, rotation, origin, scale, layerDepth, Transform(Scissor, ViewMatrix), options);
109
+
}
112
110
113
-
private void CoreDispose(bool disposing)
114
-
{
115
-
if (IsDisposed)
116
-
return;
117
-
IsDisposed = true;
111
+
/// <inheritdoc/>
112
+
public void Dispose()
113
+
{
114
+
CoreDispose(true);
115
+
GC.SuppressFinalize(this);
116
+
}
118
117
119
-
Dispose(disposing);
120
-
}
118
+
private void CoreDispose(bool disposing)
119
+
{
120
+
if (IsDisposed)
121
+
return;
122
+
IsDisposed = true;
121
123
122
-
/// <summary>
123
-
/// Disposes resources.
124
-
/// </summary>
125
-
/// <param name="disposing">If called by <see cref="Dispose()"/></param>
126
-
protected abstract void Dispose(bool disposing);
124
+
Dispose(disposing);
125
+
}
127
126
128
-
private void CheckValid(ITexture2D texture)
129
-
{
130
-
if (texture == null)
131
-
throw new ArgumentNullException(nameof(texture));
132
-
if (!_beginCalled)
133
-
throw new InvalidOperationException("Draw was called, but Begin has not yet been called. Begin must be called successfully before you can call Draw.");
134
-
}
127
+
/// <summary>
128
+
/// Disposes resources.
129
+
/// </summary>
130
+
/// <param name="disposing">If called by <see cref="Dispose()"/></param>
131
+
protected abstract void Dispose(bool disposing);
135
132
136
-
private static TTexture Cast(ITexture2D texture)
137
-
{
138
-
if (texture is not TTexture tt)
139
-
throw new InvalidCastException($"The {texture} is not supported by this implementation.");
140
-
return tt;
141
-
}
133
+
private void CheckValid(TImage image)
134
+
{
135
+
if (image == null)
136
+
throw new ArgumentNullException(nameof(image));
137
+
if (!m_beginCalled)
138
+
throw new InvalidOperationException("Draw was called, but Begin has not yet been called. Begin must be called successfully before you can call Draw.");
139
+
}
142
140
143
-
/// <inheritdoc/>
144
-
public void ResetScissor()
145
-
{
146
-
const float v = 1 << 23;
147
-
const float s = -(1 << 22);
148
-
Scissor = new RectangleF(s, s, v, v);
149
-
}
141
+
/// <inheritdoc/>
142
+
public void ResetScissor()
143
+
{
144
+
const float v = 1 << 23;
145
+
const float s = -(1 << 22);
146
+
Scissor = new RectangleF(s, s, v, v);
147
+
}
150
148
151
-
/// <inheritdoc/>
152
-
public void IntersectScissor(RectangleF clip)
153
-
{
154
-
var scissor = Scissor;
155
-
scissor.Intersect(clip);
156
-
Scissor = scissor;
157
-
}
149
+
/// <inheritdoc/>
150
+
public void IntersectScissor(RectangleF clip)
151
+
{
152
+
var scissor = Scissor;
153
+
scissor.Intersect(clip);
154
+
Scissor = scissor;
155
+
}
158
156
159
-
private static RectangleF Transform(RectangleF rect, Matrix4x4 matrix)
160
-
{
161
-
var pos = Vector4.Transform(new Vector4(rect.X, rect.Y, 0, 1), matrix);
162
-
var size = Vector4.Transform(new Vector4(rect.X + rect.Width, rect.Y + rect.Height, 0, 1), matrix);
163
-
return new(pos.X, pos.Y, size.X - pos.X, size.Y - pos.Y);
164
-
}
157
+
private static RectangleF Transform(RectangleF rect, Matrix4x4 matrix)
158
+
{
159
+
var pos = Vector4.Transform(new Vector4(rect.X, rect.Y, 0, 1), matrix);
160
+
var size = Vector4.Transform(new Vector4(rect.X + rect.Width, rect.Y + rect.Height, 0, 1), matrix);
161
+
return new(pos.X, pos.Y, size.X - pos.X, size.Y - pos.Y);
162
+
}
165
163
166
-
#region ISpriteBatch
167
-
/// <inheritdoc/>
168
-
public void Draw(ITexture2D texture,
169
-
RectangleF destinationRectangle,
170
-
RectangleF sourceRectangle,
171
-
Color color,
172
-
float rotation,
173
-
Vector2 origin,
174
-
float layerDepth)
175
-
{
176
-
Draw(texture, destinationRectangle, sourceRectangle, color, rotation, origin, SpriteOptions.None, layerDepth);
177
-
}
164
+
#region ISpriteBatch
165
+
/// <inheritdoc/>
166
+
public void Draw(Image image,
167
+
RectangleF destinationRectangle,
168
+
RectangleF sourceRectangle,
169
+
Color color,
170
+
float rotation,
171
+
Vector2 origin,
172
+
float layerDepth)
173
+
{
174
+
Draw(image, destinationRectangle, sourceRectangle, color, rotation, origin, SpriteOptions.None, layerDepth);
175
+
}
178
176
179
-
/// <inheritdoc/>
180
-
public void Draw(ITexture2D texture,
181
-
Vector2 position,
182
-
RectangleF sourceRectangle,
183
-
Color color,
184
-
float rotation,
185
-
Vector2 origin,
186
-
Vector2 scale,
187
-
float layerDepth)
188
-
{
189
-
Draw(texture, position, sourceRectangle, color, rotation, origin, scale, SpriteOptions.None, layerDepth);
190
-
}
177
+
/// <inheritdoc/>
178
+
public void Draw(Image image,
179
+
Vector2 position,
180
+
RectangleF sourceRectangle,
181
+
Color color,
182
+
float rotation,
183
+
Vector2 origin,
184
+
Vector2 scale,
185
+
float layerDepth)
186
+
{
187
+
Draw(image, position, sourceRectangle, color, rotation, origin, scale, SpriteOptions.None, layerDepth);
188
+
}
191
189
192
-
/// <inheritdoc/>
193
-
public void Draw(ITexture2D texture,
194
-
RectangleF destinationRectangle,
195
-
RectangleF? sourceRectangle,
196
-
Color color,
197
-
float rotation,
198
-
Vector2 origin,
199
-
SpriteOptions options,
200
-
float layerDepth)
201
-
{
202
-
var srcRect = sourceRectangle ?? new(0, 0, texture.Size.Width, texture.Size.Height);
203
-
Draw(texture, destinationRectangle, srcRect, color, rotation, origin, options, layerDepth);
204
-
}
190
+
/// <inheritdoc/>
191
+
public void Draw(Image image,
192
+
RectangleF destinationRectangle,
193
+
RectangleF? sourceRectangle,
194
+
Color color,
195
+
float rotation,
196
+
Vector2 origin,
197
+
SpriteOptions options,
198
+
float layerDepth)
199
+
{
200
+
var srcRect = sourceRectangle ?? new(0, 0, image.Width, image.Height);
201
+
Draw(image, destinationRectangle, srcRect, color, rotation, origin, options, layerDepth);
202
+
}
205
203
206
-
/// <inheritdoc/>
207
-
public void Draw(ITexture2D texture,
208
-
RectangleF destinationRectangle,
209
-
RectangleF? sourceRectangle,
210
-
Color color,
211
-
float rotation,
212
-
Vector2 origin,
213
-
float layerDepth)
214
-
{
215
-
Draw(texture, destinationRectangle, sourceRectangle, color, rotation, origin, SpriteOptions.None, layerDepth);
216
-
}
204
+
/// <inheritdoc/>
205
+
public void Draw(Image image,
206
+
RectangleF destinationRectangle,
207
+
RectangleF? sourceRectangle,
208
+
Color color,
209
+
float rotation,
210
+
Vector2 origin,
211
+
float layerDepth)
212
+
{
213
+
Draw(image, destinationRectangle, sourceRectangle, color, rotation, origin, SpriteOptions.None, layerDepth);
214
+
}
217
215
218
-
/// <inheritdoc/>
219
-
public void Draw(ITexture2D texture,
220
-
Vector2 position,
221
-
RectangleF? sourceRectangle,
222
-
Color color,
223
-
float rotation,
224
-
Vector2 origin,
225
-
Vector2 scale,
226
-
SpriteOptions options,
227
-
float layerDepth)
228
-
{
229
-
Draw(texture: texture,
230
-
position: position,
231
-
sourceRectangle: sourceRectangle ?? new()
232
-
{
233
-
X = 0,
234
-
Y = 0,
235
-
Width = texture.Size.Width,
236
-
Height = texture.Size.Height,
237
-
},
238
-
color: color,
239
-
rotation: rotation,
240
-
origin: origin,
241
-
scale: scale,
242
-
options: options,
243
-
layerDepth: layerDepth);
244
-
}
216
+
/// <inheritdoc/>
217
+
public void Draw(Image image,
218
+
Vector2 position,
219
+
RectangleF? sourceRectangle,
220
+
Color color,
221
+
float rotation,
222
+
Vector2 origin,
223
+
Vector2 scale,
224
+
SpriteOptions options,
225
+
float layerDepth)
226
+
{
227
+
Draw(image: image,
228
+
position: position,
229
+
sourceRectangle: sourceRectangle ?? new()
230
+
{
231
+
X = 0,
232
+
Y = 0,
233
+
Width = image.Width,
234
+
Height = image.Height,
235
+
},
236
+
color: color,
237
+
rotation: rotation,
238
+
origin: origin,
239
+
scale: scale,
240
+
options: options,
241
+
layerDepth: layerDepth);
242
+
}
245
243
246
-
/// <inheritdoc/>
247
-
public void Draw(ITexture2D texture,
248
-
Vector2 position,
249
-
RectangleF? sourceRectangle,
250
-
Color color,
251
-
float rotation,
252
-
Vector2 origin,
253
-
Vector2 scale,
254
-
float layerDepth)
255
-
{
256
-
Draw(texture: texture,
257
-
position: position,
258
-
sourceRectangle: sourceRectangle,
259
-
color: color,
260
-
rotation: rotation,
261
-
origin: origin,
262
-
scale: scale,
263
-
options: SpriteOptions.None,
264
-
layerDepth: layerDepth);
265
-
}
244
+
/// <inheritdoc/>
245
+
public void Draw(Image image,
246
+
Vector2 position,
247
+
RectangleF? sourceRectangle,
248
+
Color color,
249
+
float rotation,
250
+
Vector2 origin,
251
+
Vector2 scale,
252
+
float layerDepth)
253
+
{
254
+
Draw(image: image,
255
+
position: position,
256
+
sourceRectangle: sourceRectangle,
257
+
color: color,
258
+
rotation: rotation,
259
+
origin: origin,
260
+
scale: scale,
261
+
options: SpriteOptions.None,
262
+
layerDepth: layerDepth);
263
+
}
266
264
267
-
/// <inheritdoc/>
268
-
public void Draw(ITexture2D texture,
269
-
Vector2 position,
270
-
RectangleF? sourceRectangle,
271
-
Color color,
272
-
float rotation,
273
-
Vector2 origin,
274
-
float scale,
275
-
SpriteOptions options,
276
-
float layerDepth)
277
-
{
278
-
Draw(texture: texture,
279
-
position: position,
280
-
sourceRectangle: sourceRectangle,
281
-
color: color,
282
-
rotation: rotation,
283
-
origin: origin,
284
-
scale: new Vector2(scale),
285
-
options: options,
286
-
layerDepth: layerDepth);
287
-
}
265
+
/// <inheritdoc/>
266
+
public void Draw(Image image,
267
+
Vector2 position,
268
+
RectangleF? sourceRectangle,
269
+
Color color,
270
+
float rotation,
271
+
Vector2 origin,
272
+
float scale,
273
+
SpriteOptions options,
274
+
float layerDepth)
275
+
{
276
+
Draw(image: image,
277
+
position: position,
278
+
sourceRectangle: sourceRectangle,
279
+
color: color,
280
+
rotation: rotation,
281
+
origin: origin,
282
+
scale: new Vector2(scale),
283
+
options: options,
284
+
layerDepth: layerDepth);
285
+
}
288
286
289
-
/// <inheritdoc/>
290
-
public void Draw(ITexture2D texture,
291
-
Vector2 position,
292
-
RectangleF? sourceRectangle,
293
-
Color color,
294
-
float rotation,
295
-
Vector2 origin,
296
-
float scale,
297
-
float layerDepth)
298
-
{
299
-
Draw(texture: texture,
300
-
position: position,
301
-
sourceRectangle: sourceRectangle,
302
-
color: color,
303
-
rotation: rotation,
304
-
origin: origin,
305
-
scale: scale,
306
-
options: SpriteOptions.None,
307
-
layerDepth: layerDepth);
308
-
}
287
+
/// <inheritdoc/>
288
+
public void Draw(Image image,
289
+
Vector2 position,
290
+
RectangleF? sourceRectangle,
291
+
Color color,
292
+
float rotation,
293
+
Vector2 origin,
294
+
float scale,
295
+
float layerDepth)
296
+
{
297
+
Draw(image: image,
298
+
position: position,
299
+
sourceRectangle: sourceRectangle,
300
+
color: color,
301
+
rotation: rotation,
302
+
origin: origin,
303
+
scale: scale,
304
+
options: SpriteOptions.None,
305
+
layerDepth: layerDepth);
306
+
}
309
307
310
-
/// <inheritdoc/>
311
-
public void Draw(ITexture2D texture,
312
-
Vector2 position,
313
-
RectangleF? sourceRectangle,
314
-
Color color,
315
-
SpriteOptions options,
316
-
float layerDepth)
317
-
{
318
-
Draw(texture: texture,
319
-
position: position,
320
-
sourceRectangle: sourceRectangle,
321
-
color: color,
322
-
rotation: 0f,
323
-
origin: default,
324
-
scale: 0f,
325
-
options: options,
326
-
layerDepth: layerDepth);
327
-
}
308
+
/// <inheritdoc/>
309
+
public void Draw(Image image,
310
+
Vector2 position,
311
+
RectangleF? sourceRectangle,
312
+
Color color,
313
+
SpriteOptions options,
314
+
float layerDepth)
315
+
{
316
+
Draw(image: image,
317
+
position: position,
318
+
sourceRectangle: sourceRectangle,
319
+
color: color,
320
+
rotation: 0f,
321
+
origin: default,
322
+
scale: 0f,
323
+
options: options,
324
+
layerDepth: layerDepth);
325
+
}
328
326
329
-
/// <inheritdoc/>
330
-
public void Draw(ITexture2D texture,
331
-
Vector2 position,
332
-
RectangleF? sourceRectangle,
333
-
Color color,
334
-
float layerDepth)
335
-
{
336
-
Draw(texture: texture,
337
-
position: position,
338
-
sourceRectangle: sourceRectangle,
339
-
color: color,
340
-
options: SpriteOptions.None,
341
-
layerDepth: layerDepth);
342
-
}
327
+
/// <inheritdoc/>
328
+
public void Draw(Image image,
329
+
Vector2 position,
330
+
RectangleF? sourceRectangle,
331
+
Color color,
332
+
float layerDepth)
333
+
{
334
+
Draw(image: image,
335
+
position: position,
336
+
sourceRectangle: sourceRectangle,
337
+
color: color,
338
+
options: SpriteOptions.None,
339
+
layerDepth: layerDepth);
340
+
}
343
341
344
-
/// <inheritdoc/>
345
-
public void Draw(ITexture2D texture,
346
-
RectangleF destinationRectangle,
347
-
RectangleF? sourceRectangle,
348
-
Color color,
349
-
SpriteOptions options,
350
-
float layerDepth)
351
-
{
352
-
Draw(texture: texture,
353
-
destinationRectangle: destinationRectangle,
354
-
sourceRectangle: sourceRectangle,
355
-
color: color,
356
-
rotation: 0,
357
-
origin: default,
358
-
options: options,
359
-
layerDepth: layerDepth);
360
-
}
342
+
/// <inheritdoc/>
343
+
public void Draw(Image image,
344
+
RectangleF destinationRectangle,
345
+
RectangleF? sourceRectangle,
346
+
Color color,
347
+
SpriteOptions options,
348
+
float layerDepth)
349
+
{
350
+
Draw(image: image,
351
+
destinationRectangle: destinationRectangle,
352
+
sourceRectangle: sourceRectangle,
353
+
color: color,
354
+
rotation: 0,
355
+
origin: default,
356
+
options: options,
357
+
layerDepth: layerDepth);
358
+
}
361
359
362
-
/// <inheritdoc/>
363
-
public void Draw(ITexture2D texture,
364
-
RectangleF destinationRectangle,
365
-
RectangleF? sourceRectangle,
366
-
Color color,
367
-
float layerDepth)
368
-
{
369
-
Draw(texture: texture,
370
-
destinationRectangle:
371
-
destinationRectangle,
372
-
sourceRectangle: sourceRectangle,
373
-
color: color,
374
-
options: SpriteOptions.None,
375
-
layerDepth: layerDepth);
376
-
}
360
+
/// <inheritdoc/>
361
+
public void Draw(Image image,
362
+
RectangleF destinationRectangle,
363
+
RectangleF? sourceRectangle,
364
+
Color color,
365
+
float layerDepth)
366
+
{
367
+
Draw(image: image,
368
+
destinationRectangle:
369
+
destinationRectangle,
370
+
sourceRectangle: sourceRectangle,
371
+
color: color,
372
+
options: SpriteOptions.None,
373
+
layerDepth: layerDepth);
374
+
}
375
+
376
+
/// <inheritdoc/>
377
+
public void Draw(Image image,
378
+
Vector2 position,
379
+
Color color,
380
+
SpriteOptions options,
381
+
float layerDepth)
382
+
{
383
+
Draw(image: image,
384
+
position: position,
385
+
sourceRectangle: new Rectangle(default, image.Size),
386
+
color: color,
387
+
rotation: 0,
388
+
origin: default,
389
+
scale: default,
390
+
options: options,
391
+
layerDepth: layerDepth);
392
+
}
393
+
394
+
/// <inheritdoc/>
395
+
public void Draw(Image image,
396
+
Vector2 position,
397
+
Color color,
398
+
float layerDepth)
399
+
{
400
+
Draw(image: image,
401
+
position: position,
402
+
color: color,
403
+
options: SpriteOptions.None,
404
+
layerDepth: layerDepth);
405
+
}
406
+
407
+
/// <inheritdoc/>
408
+
public void Draw(Image image,
409
+
RectangleF destinationRectangle,
410
+
Color color,
411
+
SpriteOptions options,
412
+
float layerDepth)
413
+
{
414
+
Draw(image: image,
415
+
destinationRectangle: destinationRectangle,
416
+
sourceRectangle: new Rectangle(default, image.Size),
417
+
color: color,
418
+
rotation: 0,
419
+
origin: default,
420
+
options: options,
421
+
layerDepth: layerDepth);
422
+
}
423
+
424
+
/// <inheritdoc/>
425
+
public void Draw(Image image,
426
+
RectangleF destinationRectangle,
427
+
Color color,
428
+
float layerDepth)
429
+
{
430
+
Draw(image: image,
431
+
destinationRectangle: destinationRectangle,
432
+
color: color,
433
+
options: SpriteOptions.None,
434
+
layerDepth: layerDepth);
435
+
}
436
+
#endregion
437
+
438
+
#region ISpriteBatch<TImage>
439
+
440
+
/// <inheritdoc/>
441
+
public void Draw(TImage image,
442
+
RectangleF destinationRectangle,
443
+
RectangleF sourceRectangle,
444
+
Color color,
445
+
float rotation,
446
+
Vector2 origin,
447
+
float layerDepth)
448
+
{
449
+
Draw(image: image,
450
+
destinationRectangle: destinationRectangle,
451
+
sourceRectangle: sourceRectangle,
452
+
color: color,
453
+
rotation: rotation,
454
+
origin: origin,
455
+
options: SpriteOptions.None,
456
+
layerDepth: layerDepth);
457
+
}
458
+
459
+
/// <inheritdoc/>
460
+
public void Draw(TImage image,
461
+
Vector2 position,
462
+
RectangleF sourceRectangle,
463
+
Color color,
464
+
float rotation,
465
+
Vector2 origin,
466
+
Vector2 scale,
467
+
float layerDepth)
468
+
{
469
+
Draw(image: image,
470
+
position: position,
471
+
sourceRectangle: sourceRectangle,
472
+
color: color,
473
+
rotation: rotation,
474
+
origin: origin,
475
+
scale: scale,
476
+
options: SpriteOptions.None,
477
+
layerDepth: layerDepth);
478
+
}
479
+
480
+
/// <inheritdoc/>
481
+
public void Draw(TImage image,
482
+
RectangleF destinationRectangle,
483
+
RectangleF? sourceRectangle,
484
+
Color color,
485
+
float rotation,
486
+
Vector2 origin,
487
+
SpriteOptions options,
488
+
float layerDepth)
489
+
{
490
+
var size = GetSize(image);
491
+
Draw(image: image,
492
+
destinationRectangle: destinationRectangle,
493
+
sourceRectangle: sourceRectangle ?? new()
494
+
{
495
+
X = 0,
496
+
Y = 0,
497
+
Width = size.Width,
498
+
Height = size.Height,
499
+
},
500
+
color: color,
501
+
rotation: rotation,
502
+
origin: origin,
503
+
options: options,
504
+
layerDepth: layerDepth);
505
+
}
506
+
507
+
/// <inheritdoc/>
508
+
public void Draw(TImage image,
509
+
RectangleF destinationRectangle,
510
+
RectangleF? sourceRectangle,
511
+
Color color,
512
+
float rotation,
513
+
Vector2 origin,
514
+
float layerDepth)
515
+
{
516
+
Draw(image, destinationRectangle, sourceRectangle, color, rotation, origin, SpriteOptions.None, layerDepth);
517
+
}
377
518
378
-
/// <inheritdoc/>
379
-
public void Draw(ITexture2D texture,
380
-
Vector2 position,
381
-
Color color,
382
-
SpriteOptions options,
383
-
float layerDepth)
384
-
{
385
-
Draw(texture: texture,
386
-
position: position,
387
-
sourceRectangle: new Rectangle(default, texture.Size),
388
-
color: color,
389
-
rotation: 0,
390
-
origin: default,
391
-
scale: default,
392
-
options: options,
393
-
layerDepth: layerDepth);
394
-
}
519
+
/// <inheritdoc/>
520
+
public void Draw(TImage image,
521
+
Vector2 position,
522
+
RectangleF? sourceRectangle,
523
+
Color color,
524
+
float rotation,
525
+
Vector2 origin,
526
+
Vector2 scale,
527
+
SpriteOptions options,
528
+
float layerDepth)
529
+
{
530
+
var size = GetSize(image);
531
+
Draw(image: image,
532
+
position: position,
533
+
sourceRectangle: sourceRectangle ?? new()
534
+
{
535
+
X = 0,
536
+
Y = 0,
537
+
Width = size.Width,
538
+
Height = size.Height,
539
+
},
540
+
color: color,
541
+
rotation: rotation,
542
+
origin: origin,
543
+
scale: scale,
544
+
options: options,
545
+
layerDepth: layerDepth);
546
+
}
395
547
396
-
/// <inheritdoc/>
397
-
public void Draw(ITexture2D texture,
398
-
Vector2 position,
399
-
Color color,
400
-
float layerDepth)
401
-
{
402
-
Draw(texture: texture,
403
-
position: position,
404
-
color: color,
405
-
options: SpriteOptions.None,
406
-
layerDepth: layerDepth);
407
-
}
548
+
/// <inheritdoc/>
549
+
public void Draw(TImage image,
550
+
Vector2 position,
551
+
RectangleF? sourceRectangle,
552
+
Color color,
553
+
float rotation,
554
+
Vector2 origin,
555
+
Vector2 scale,
556
+
float layerDepth)
557
+
{
558
+
Draw(image: image,
559
+
position: position,
560
+
sourceRectangle: sourceRectangle,
561
+
color: color,
562
+
rotation: rotation,
563
+
origin: origin,
564
+
scale: scale,
565
+
options: SpriteOptions.None,
566
+
layerDepth: layerDepth);
567
+
}
408
568
409
-
/// <inheritdoc/>
410
-
public void Draw(ITexture2D texture,
411
-
RectangleF destinationRectangle,
412
-
Color color,
413
-
SpriteOptions options,
414
-
float layerDepth)
415
-
{
416
-
Draw(texture: texture,
417
-
destinationRectangle: destinationRectangle,
418
-
sourceRectangle: new Rectangle(default, texture.Size),
419
-
color: color,
420
-
rotation: 0,
421
-
origin: default,
422
-
options: options,
423
-
layerDepth: layerDepth);
424
-
}
569
+
/// <inheritdoc/>
570
+
public void Draw(TImage image,
571
+
Vector2 position,
572
+
RectangleF? sourceRectangle,
573
+
Color color,
574
+
float rotation,
575
+
Vector2 origin,
576
+
float scale,
577
+
SpriteOptions options,
578
+
float layerDepth)
579
+
{
580
+
Draw(image, position, sourceRectangle, color, rotation, origin, new Vector2(scale), options, layerDepth);
581
+
}
425
582
426
-
/// <inheritdoc/>
427
-
public void Draw(ITexture2D texture,
428
-
RectangleF destinationRectangle,
429
-
Color color,
430
-
float layerDepth)
431
-
{
432
-
Draw(texture: texture,
433
-
destinationRectangle: destinationRectangle,
434
-
color: color,
435
-
options: SpriteOptions.None,
436
-
layerDepth: layerDepth);
437
-
}
438
-
#endregion
583
+
/// <inheritdoc/>
584
+
public void Draw(TImage image,
585
+
Vector2 position,
586
+
RectangleF? sourceRectangle,
587
+
Color color,
588
+
float rotation,
589
+
Vector2 origin,
590
+
float scale,
591
+
float layerDepth)
592
+
{
593
+
Draw(image, position, sourceRectangle, color, rotation, origin, scale, SpriteOptions.None, layerDepth);
594
+
}
439
595
440
-
#region ISpriteBatch<TTexture>
441
-
442
-
/// <inheritdoc/>
443
-
public void Draw(TTexture texture,
444
-
RectangleF destinationRectangle,
445
-
RectangleF sourceRectangle,
446
-
Color color,
447
-
float rotation,
448
-
Vector2 origin,
449
-
float layerDepth)
450
-
{
451
-
Draw(texture: texture,
452
-
destinationRectangle: destinationRectangle,
453
-
sourceRectangle: sourceRectangle,
454
-
color: color,
455
-
rotation: rotation,
456
-
origin: origin,
457
-
options: SpriteOptions.None,
458
-
layerDepth: layerDepth);
459
-
}
596
+
/// <inheritdoc/>
597
+
public void Draw(TImage image,
598
+
Vector2 position,
599
+
RectangleF? sourceRectangle,
600
+
Color color,
601
+
SpriteOptions options,
602
+
float layerDepth)
603
+
{
604
+
Draw(image, position, sourceRectangle, color, 0f, default, 0f, options, layerDepth);
605
+
}
460
606
461
-
/// <inheritdoc/>
462
-
public void Draw(TTexture texture,
463
-
Vector2 position,
464
-
RectangleF sourceRectangle,
465
-
Color color,
466
-
float rotation,
467
-
Vector2 origin,
468
-
Vector2 scale,
469
-
float layerDepth)
470
-
{
471
-
Draw(texture: texture,
472
-
position: position,
473
-
sourceRectangle: sourceRectangle,
474
-
color: color,
475
-
rotation: rotation,
476
-
origin: origin,
477
-
scale: scale,
478
-
options: SpriteOptions.None,
479
-
layerDepth: layerDepth);
480
-
}
607
+
/// <inheritdoc/>
608
+
public void Draw(TImage image,
609
+
Vector2 position,
610
+
RectangleF? sourceRectangle,
611
+
Color color,
612
+
float layerDepth)
613
+
{
614
+
Draw(image, position, sourceRectangle, color, SpriteOptions.None, layerDepth);
615
+
}
481
616
482
-
/// <inheritdoc/>
483
-
public void Draw(TTexture texture,
484
-
RectangleF destinationRectangle,
485
-
RectangleF? sourceRectangle,
486
-
Color color,
487
-
float rotation,
488
-
Vector2 origin,
489
-
SpriteOptions options,
490
-
float layerDepth)
491
-
{
492
-
//var srcRect = sourceRectangleF ?? new(0, 0, texture.Size.Width, texture.Size.Height);
493
-
Draw(texture: texture,
494
-
destinationRectangle: destinationRectangle,
495
-
sourceRectangle: sourceRectangle ?? new()
496
-
{
497
-
X = 0,
498
-
Y = 0,
499
-
Width = texture.Size.Width,
500
-
Height = texture.Size.Height,
501
-
},
502
-
color: color,
503
-
rotation: rotation,
504
-
origin: origin,
505
-
options: options,
506
-
layerDepth: layerDepth);
507
-
}
617
+
/// <inheritdoc/>
618
+
public void Draw(TImage image,
619
+
RectangleF destinationRectangle,
620
+
RectangleF? sourceRectangle,
621
+
Color color,
622
+
SpriteOptions options,
623
+
float layerDepth)
624
+
{
625
+
Draw(image, destinationRectangle, sourceRectangle, color, 0, default, options, layerDepth);
626
+
}
508
627
509
-
/// <inheritdoc/>
510
-
public void Draw(TTexture texture,
511
-
RectangleF destinationRectangle,
512
-
RectangleF? sourceRectangle,
513
-
Color color,
514
-
float rotation,
515
-
Vector2 origin,
516
-
float layerDepth)
517
-
{
518
-
Draw(texture, destinationRectangle, sourceRectangle, color, rotation, origin, SpriteOptions.None, layerDepth);
519
-
}
628
+
/// <inheritdoc/>
629
+
public void Draw(TImage image,
630
+
RectangleF destinationRectangle,
631
+
RectangleF? sourceRectangle,
632
+
Color color,
633
+
float layerDepth)
634
+
{
635
+
Draw(image, destinationRectangle, sourceRectangle, color, SpriteOptions.None, layerDepth);
636
+
}
520
637
521
-
/// <inheritdoc/>
522
-
public void Draw(TTexture texture,
523
-
Vector2 position,
524
-
RectangleF? sourceRectangle,
525
-
Color color,
526
-
float rotation,
527
-
Vector2 origin,
528
-
Vector2 scale,
529
-
SpriteOptions options,
530
-
float layerDepth)
531
-
{
532
-
//var srcRect = sourceRectangleF ?? new(0, 0, texture.Size.Width, texture.Size.Height);
533
-
Draw(texture: texture,
534
-
position: position,
535
-
sourceRectangle: sourceRectangle ?? new()
536
-
{
537
-
X = 0,
538
-
Y = 0,
539
-
Width = texture.Size.Width,
540
-
Height = texture.Size.Height,
541
-
},
542
-
color: color,
543
-
rotation: rotation,
544
-
origin: origin,
545
-
scale: scale,
546
-
options: options,
547
-
layerDepth: layerDepth);
548
-
}
638
+
/// <inheritdoc/>
639
+
public void Draw(TImage image,
640
+
Vector2 position,
641
+
Color color,
642
+
SpriteOptions options,
643
+
float layerDepth)
644
+
{
645
+
Draw(image, position, new Rectangle(default, GetSize(image)), color, 0, default, default, options, layerDepth);
646
+
}
549
647
550
-
/// <inheritdoc/>
551
-
public void Draw(TTexture texture,
552
-
Vector2 position,
553
-
RectangleF? sourceRectangle,
554
-
Color color,
555
-
float rotation,
556
-
Vector2 origin,
557
-
Vector2 scale,
558
-
float layerDepth)
559
-
{
560
-
Draw(texture: texture,
561
-
position: position,
562
-
sourceRectangle: sourceRectangle,
563
-
color: color,
564
-
rotation: rotation,
565
-
origin: origin,
566
-
scale: scale,
567
-
options: SpriteOptions.None,
568
-
layerDepth: layerDepth);
569
-
}
648
+
/// <inheritdoc/>
649
+
public void Draw(TImage image,
650
+
Vector2 position,
651
+
Color color,
652
+
float layerDepth)
653
+
{
654
+
Draw(image, position, new Rectangle(default, GetSize(image)), color, 0, default, default, SpriteOptions.None, layerDepth);
655
+
}
570
656
571
-
/// <inheritdoc/>
572
-
public void Draw(TTexture texture,
573
-
Vector2 position,
574
-
RectangleF? sourceRectangle,
575
-
Color color,
576
-
float rotation,
577
-
Vector2 origin,
578
-
float scale,
579
-
SpriteOptions options,
580
-
float layerDepth)
581
-
{
582
-
Draw(texture, position, sourceRectangle, color, rotation, origin, new Vector2(scale), options, layerDepth);
583
-
}
657
+
/// <inheritdoc/>
658
+
public void Draw(TImage image,
659
+
RectangleF destinationRectangle,
660
+
Color color,
661
+
SpriteOptions options,
662
+
float layerDepth)
663
+
{
664
+
Draw(image, destinationRectangle, new Rectangle(default, GetSize(image)), color, 0, default, options, layerDepth);
665
+
}
584
666
585
-
/// <inheritdoc/>
586
-
public void Draw(TTexture texture,
587
-
Vector2 position,
588
-
RectangleF? sourceRectangle,
589
-
Color color,
590
-
float rotation,
591
-
Vector2 origin,
592
-
float scale,
593
-
float layerDepth)
594
-
{
595
-
Draw(texture, position, sourceRectangle, color, rotation, origin, scale, SpriteOptions.None, layerDepth);
596
-
}
667
+
/// <inheritdoc/>
668
+
public void Draw(TImage image,
669
+
RectangleF destinationRectangle,
670
+
Color color,
671
+
float layerDepth)
672
+
{
673
+
Draw(image, destinationRectangle, color, SpriteOptions.None, layerDepth);
674
+
}
597
675
598
-
/// <inheritdoc/>
599
-
public void Draw(TTexture texture,
600
-
Vector2 position,
601
-
RectangleF? sourceRectangle,
602
-
Color color,
603
-
SpriteOptions options,
604
-
float layerDepth)
605
-
{
606
-
Draw(texture, position, sourceRectangle, color, 0f, default, 0f, options, layerDepth);
607
-
}
676
+
/// <inheritdoc/>
677
+
public void DrawRect(RectangleF rectangle, Color color, float rotation, Vector2 origin, float layerDepth)
678
+
{
679
+
Draw(m_whiteImage, rectangle, new RectangleF(0, 0, 1, 1), color, rotation, origin, layerDepth);
680
+
}
608
681
609
-
/// <inheritdoc/>
610
-
public void Draw(TTexture texture,
611
-
Vector2 position,
612
-
RectangleF? sourceRectangle,
613
-
Color color,
614
-
float layerDepth)
615
-
{
616
-
Draw(texture, position, sourceRectangle, color, SpriteOptions.None, layerDepth);
617
-
}
682
+
/// <inheritdoc/>
683
+
public void DrawRect(RectangleF rectangle, Color color, float layerDepth)
684
+
{
685
+
Draw(m_whiteImage, rectangle, new RectangleF(0, 0, 1, 1), color, layerDepth);
686
+
}
618
687
619
-
/// <inheritdoc/>
620
-
public void Draw(TTexture texture,
621
-
RectangleF destinationRectangle,
622
-
RectangleF? sourceRectangle,
623
-
Color color,
624
-
SpriteOptions options,
625
-
float layerDepth)
626
-
{
627
-
Draw(texture, destinationRectangle, sourceRectangle, color, 0, default, options, layerDepth);
628
-
}
688
+
/// <inheritdoc/>
689
+
public void DrawDot(Vector2 position, float size, Color color, float layerDepth)
690
+
{
691
+
Draw(m_whiteImage, new RectangleF(0, 0, size, size), new RectangleF(0, 0, 1, 1), color, layerDepth);
692
+
}
629
693
630
-
/// <inheritdoc/>
631
-
public void Draw(TTexture texture,
632
-
RectangleF destinationRectangle,
633
-
RectangleF? sourceRectangle,
634
-
Color color,
635
-
float layerDepth)
636
-
{
637
-
Draw(texture, destinationRectangle, sourceRectangle, color, SpriteOptions.None, layerDepth);
638
-
}
694
+
/// <inheritdoc/>
695
+
public void DrawSegment(Vector2 a, Vector2 b, Color color, float thickness, float layerDepth)
696
+
{
697
+
var diff = b - a;
698
+
var length = MathF.Sqrt(Vector2.Dot(diff, diff));
699
+
var angle = MathF.Atan2(diff.Y, diff.X);
700
+
DrawSegment(a, length, color, angle, thickness, layerDepth);
701
+
}
639
702
640
-
/// <inheritdoc/>
641
-
public void Draw(TTexture texture,
642
-
Vector2 position,
643
-
Color color,
644
-
SpriteOptions options,
645
-
float layerDepth)
646
-
{
647
-
Draw(texture, position, new Rectangle(default, texture.Size), color, 0, default, default, options, layerDepth);
648
-
}
703
+
/// <inheritdoc/>
704
+
public void DrawSegment(Vector2 start, float length, Color color, float angle, float thickness, float layerDepth)
705
+
{
706
+
var rect = new RectangleF(start.X, start.Y, length, thickness);
707
+
DrawRect(rect, color, angle, new Vector2(0, 0.5f), layerDepth);
708
+
}
649
709
650
-
/// <inheritdoc/>
651
-
public void Draw(TTexture texture,
652
-
Vector2 position,
653
-
Color color,
654
-
float layerDepth)
655
-
{
656
-
Draw(texture, position, new Rectangle(default, texture.Size), color, 0, default, default, SpriteOptions.None, layerDepth);
657
-
}
710
+
#endregion
658
711
659
-
/// <inheritdoc/>
660
-
public void Draw(TTexture texture,
661
-
RectangleF destinationRectangle,
662
-
Color color,
663
-
SpriteOptions options,
664
-
float layerDepth)
665
-
{
666
-
Draw(texture, destinationRectangle, new Rectangle(default, texture.Size), color, 0, default, options, layerDepth);
667
-
}
712
+
/// <summary>
713
+
/// Gets size of a image.
714
+
/// </summary>
715
+
/// <param name="image">Image.</param>
716
+
/// <returns>Image Size.</returns>
717
+
protected abstract Size GetSize(TImage image);
668
718
669
-
/// <inheritdoc/>
670
-
public void Draw(TTexture texture,
671
-
RectangleF destinationRectangle,
672
-
Color color,
673
-
float layerDepth)
674
-
{
675
-
Draw(texture, destinationRectangle, color, SpriteOptions.None, layerDepth);
676
-
}
677
-
#endregion
678
-
}
719
+
/// <summary>
720
+
/// Gets TImage.
721
+
/// </summary>
722
+
/// <param name="image"></param>
723
+
/// <returns></returns>
724
+
protected abstract TImage GetHandle(Image image);
679
725
}
+17
-18
src/Peridot/SpriteOptions.cs
+17
-18
src/Peridot/SpriteOptions.cs
···
1
-
namespace Peridot
1
+
namespace Peridot;
2
+
3
+
/// <summary>
4
+
/// Defines sprite options.
5
+
/// </summary>
6
+
[Flags]
7
+
public enum SpriteOptions
2
8
{
3
9
/// <summary>
4
-
/// Defines sprite options.
10
+
/// Nothing is applied in sprite rendering.
11
+
/// </summary>
12
+
None,
13
+
/// <summary>
14
+
/// Horizontal flip is applied in sprite rendering.
15
+
/// </summary>
16
+
FlipHorizontally,
17
+
/// <summary>
18
+
/// Vertical flip is applied in sprite rendering.
5
19
/// </summary>
6
-
[Flags]
7
-
public enum SpriteOptions
8
-
{
9
-
/// <summary>
10
-
/// Nothing is applied in sprite rendering.
11
-
/// </summary>
12
-
None,
13
-
/// <summary>
14
-
/// Horizontal flip is applied in sprite rendering.
15
-
/// </summary>
16
-
FlipHorizontally,
17
-
/// <summary>
18
-
/// Vertical flip is applied in sprite rendering.
19
-
/// </summary>
20
-
FlipVertically,
21
-
}
20
+
FlipVertically,
22
21
}
+82
src/Peridot/Text/Font.cs
+82
src/Peridot/Text/Font.cs
···
1
+
using System.Numerics;
2
+
using FontStashSharp;
3
+
4
+
namespace Peridot.Text
5
+
{
6
+
/// <summary>
7
+
/// Represents a font that can be rendered with <see cref="TextRenderer"/>.
8
+
/// </summary>
9
+
public class Font
10
+
{
11
+
/// <summary>
12
+
/// Creates a new instance of <see cref="Font"/>.
13
+
/// </summary>
14
+
public Font()
15
+
{
16
+
var settings = new FontSystemSettings
17
+
{
18
+
Effect = FontSystemEffect.None,
19
+
EffectAmount = 2,
20
+
};
21
+
FontSystem = new FontSystem(settings);
22
+
}
23
+
24
+
/// <summary>
25
+
/// Destroys font.
26
+
/// </summary>
27
+
~Font()
28
+
{
29
+
Dispose(false);
30
+
}
31
+
32
+
internal FontSystem FontSystem { get; }
33
+
34
+
/// <summary>
35
+
/// A bool indicating whether this instance has been disposed.
36
+
/// </summary>
37
+
public bool IsDisposed { get; private set; }
38
+
39
+
/// <summary>
40
+
/// Add a character font to the font.
41
+
/// </summary>
42
+
/// <param name="stream">The stream that contains the font.</param>
43
+
public void AddFont(Stream stream)
44
+
{
45
+
FontSystem.AddFont(stream);
46
+
}
47
+
48
+
/// <summary>
49
+
/// Add a character font to the font.
50
+
/// </summary>
51
+
/// <param name="data">The data that contains the font.</param>
52
+
public void AddFont(byte[] data)
53
+
{
54
+
FontSystem.AddFont(data);
55
+
}
56
+
57
+
/// <summary>
58
+
/// Gets the size of a string when rendered in this font.
59
+
/// </summary>
60
+
/// <param name="text">The text to measure.</param>
61
+
/// <param name="fontSize">The font size to measure.</param>
62
+
/// <returns>The size of <paramref name="text"/> rendered with <paramref name="fontSize"/> font size.</returns>
63
+
public Vector2 MeasureString(string text, int fontSize)
64
+
{
65
+
var font = FontSystem.GetFont(fontSize);
66
+
return font.MeasureString(text);
67
+
}
68
+
69
+
/// <inheritdoc/>
70
+
public void Dispose()
71
+
{
72
+
Dispose(true);
73
+
GC.SuppressFinalize(this);
74
+
}
75
+
76
+
private void Dispose(bool disposing)
77
+
{
78
+
if (disposing)
79
+
FontSystem.Dispose();
80
+
}
81
+
}
82
+
}
+145
src/Peridot/Text/FontRenderer.cs
+145
src/Peridot/Text/FontRenderer.cs
···
1
+
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
+
// This code is licensed under MIT license (see LICENSE for details)
3
+
4
+
using FontStashSharp;
5
+
using FontStashSharp.Interfaces;
6
+
using System.Diagnostics;
7
+
using System.Drawing;
8
+
using System.Numerics;
9
+
10
+
namespace Peridot
11
+
{
12
+
internal class FontStashRenderer : IFontStashRenderer, IDisposable
13
+
{
14
+
private bool m_disposed;
15
+
private readonly ISpriteBatch m_spriteBatch;
16
+
private readonly Texture2DManager m_textureManager;
17
+
public FontStashRenderer(IPeridot peridot, ISpriteBatch spriteBatch)
18
+
{
19
+
m_spriteBatch = spriteBatch;
20
+
m_textureManager = new Texture2DManager(peridot);
21
+
ResetScissor();
22
+
}
23
+
24
+
~FontStashRenderer()
25
+
{
26
+
Dispose(false);
27
+
}
28
+
29
+
public Rectangle Scissor { get; set; }
30
+
31
+
public ITexture2DManager TextureManager => m_textureManager;
32
+
33
+
private void Dispose(bool disposing)
34
+
{
35
+
if (m_disposed)
36
+
return;
37
+
m_disposed = true;
38
+
if (disposing)
39
+
m_textureManager.Dispose();
40
+
}
41
+
42
+
public void Dispose()
43
+
{
44
+
Dispose(true);
45
+
}
46
+
47
+
public void ResetScissor()
48
+
{
49
+
const int v = 1 << 23;
50
+
const int s = -(1 << 22);
51
+
Scissor = new Rectangle(s, s, v, v);
52
+
}
53
+
54
+
public void Draw(object texture, Vector2 pos, Rectangle? src, FSColor color, float rotation, Vector2 scale, float depth)
55
+
{
56
+
Debug.Assert(texture is Image, "The texture object must be a Image.");
57
+
if (texture is not Image t)
58
+
throw new ArgumentException(nameof(texture));
59
+
60
+
var s = src ?? new(0, 0, (int)t.Width, (int)t.Height);
61
+
62
+
var oldScissor = m_spriteBatch.Scissor;
63
+
m_spriteBatch.Scissor = RectangleF.Intersect(m_spriteBatch.Scissor, Scissor);
64
+
m_spriteBatch.Draw(t, pos, s, ToColor(color), rotation, Vector2.Zero, new Vector2(scale.X, scale.Y), depth);
65
+
m_spriteBatch.Scissor = oldScissor;
66
+
}
67
+
68
+
private static Color ToColor(in FSColor color)
69
+
{
70
+
return Color.FromArgb(color.A, color.R, color.G, color.B);
71
+
}
72
+
73
+
private class Texture2DManager : ITexture2DManager, IDisposable
74
+
{
75
+
private readonly List<WeakReference<Image>> m_images;
76
+
private readonly IPeridot m_peridot;
77
+
private bool m_disposed;
78
+
public Texture2DManager(IPeridot peridot)
79
+
{
80
+
m_images = new();
81
+
m_peridot = peridot;
82
+
m_disposed = false;
83
+
}
84
+
85
+
~Texture2DManager()
86
+
{
87
+
Dispose(false);
88
+
}
89
+
90
+
public object CreateTexture(int width, int height)
91
+
{
92
+
var image = m_peridot.CreateImage(new ImageDescription()
93
+
{
94
+
Format = PixelFormat.BGRA8,
95
+
Width = (uint)width,
96
+
Height = (uint)height,
97
+
});
98
+
m_images.Add(new WeakReference<Image>(image));
99
+
return image;
100
+
}
101
+
102
+
public Point GetTextureSize(object texture)
103
+
{
104
+
Debug.Assert(texture is Image);
105
+
Image image = (texture as Image)!;
106
+
return new Point(image.Width, image.Height);
107
+
}
108
+
109
+
public void SetTextureData(object texture, Rectangle bounds, byte[] data)
110
+
{
111
+
Debug.Assert(texture is Image);
112
+
113
+
if (texture is not Image)
114
+
throw new NotImplementedException();
115
+
116
+
m_peridot.UpdateImage<byte>(
117
+
(texture as Image)!,
118
+
data,
119
+
(uint)bounds.X,
120
+
(uint)bounds.Y,
121
+
(uint)bounds.Width,
122
+
(uint)bounds.Height);
123
+
}
124
+
125
+
public void Dispose()
126
+
{
127
+
Dispose(true);
128
+
}
129
+
130
+
private void Dispose(bool disposing)
131
+
{
132
+
if (m_disposed)
133
+
return;
134
+
m_disposed = true;
135
+
136
+
if (disposing)
137
+
{
138
+
foreach (var wt in m_images)
139
+
if (wt.TryGetTarget(out var texture))
140
+
texture.Dispose();
141
+
}
142
+
}
143
+
}
144
+
}
145
+
}
+212
src/Peridot/Text/TextRenderer.cs
+212
src/Peridot/Text/TextRenderer.cs
···
1
+
// Copyright (c) 2021 ezequias2d <ezequiasmoises@gmail.com> and the Peridot contributors
2
+
// This code is licensed under MIT license (see LICENSE for details)
3
+
4
+
using System.Drawing;
5
+
using System.Numerics;
6
+
7
+
namespace Peridot.Text
8
+
{
9
+
/// <summary>
10
+
/// Represents an interface to a text renderer.
11
+
/// </summary>
12
+
public class TextRenderer : IDisposable
13
+
{
14
+
private readonly ISpriteBatch m_spriteBatch;
15
+
private readonly FontStashRenderer _textRenderer;
16
+
17
+
/// <summary>
18
+
/// Creates a new instance of <see cref="TextRenderer"/>.
19
+
/// </summary>
20
+
/// <param name="peridot">Peridot implementation.</param>
21
+
/// <param name="spriteBatch">SpriteBatch to use to render.</param>
22
+
public TextRenderer(IPeridot peridot, ISpriteBatch spriteBatch)
23
+
{
24
+
m_spriteBatch = spriteBatch;
25
+
_textRenderer = new(peridot, spriteBatch);
26
+
}
27
+
28
+
/// <inheritdoc/>
29
+
~TextRenderer()
30
+
{
31
+
Dispose(false);
32
+
}
33
+
34
+
/// <summary>
35
+
/// A bool indicating whether this instance has been disposed.
36
+
/// </summary>
37
+
public bool IsDisposed { get; private set; }
38
+
39
+
/// <summary>
40
+
/// Submit a text string for drawing.
41
+
/// </summary>
42
+
/// <param name="font">The font to draw.</param>
43
+
/// <param name="fontSize">The font size.</param>
44
+
/// <param name="text">The text which will be drawn.</param>
45
+
/// <param name="position">The drawing location on screen.</param>
46
+
/// <param name="color">The text color.</param>
47
+
/// <param name="rotation">The rotation of the drawing.</param>
48
+
/// <param name="origin">The center of the rotation. (0, 0) for default.</param>
49
+
/// <param name="scale">The scaling of this drawing.</param>
50
+
/// <param name="layerDepth">The layer depth of this drawing.</param>
51
+
public void DrawString(
52
+
Font font,
53
+
int fontSize,
54
+
string text,
55
+
Vector2 position,
56
+
Color color,
57
+
float rotation,
58
+
Vector2 origin,
59
+
Vector2 scale,
60
+
float layerDepth)
61
+
{
62
+
var rf = font.FontSystem.GetFont(fontSize);
63
+
var fcolor = new FontStashSharp.FSColor(color.R, color.G, color.B, color.A);
64
+
65
+
rf.DrawText(_textRenderer, text, position, fcolor, scale, rotation, origin, layerDepth);
66
+
}
67
+
68
+
/// <summary>
69
+
/// Submit a text string for drawing.
70
+
/// </summary>
71
+
/// <param name="font">The font to draw.</param>
72
+
/// <param name="fontSize">The font size.</param>
73
+
/// <param name="text">The text which will be drawn.</param>
74
+
/// <param name="position">The drawing location on screen.</param>
75
+
/// <param name="color">The text color.</param>
76
+
/// <param name="rotation">The rotation of the drawing.</param>
77
+
/// <param name="origin">The center of the rotation. (0, 0) for default.</param>
78
+
/// <param name="scale">The scaling of this drawing.</param>
79
+
/// <param name="layerDepth">The layer depth of this drawing.</param>
80
+
/// <param name="scissor">The scissor rectangle.</param>
81
+
public void DrawString(
82
+
Font font,
83
+
int fontSize,
84
+
string text,
85
+
Vector2 position,
86
+
Color color,
87
+
float rotation,
88
+
Vector2 origin,
89
+
Vector2 scale,
90
+
float layerDepth,
91
+
Rectangle scissor)
92
+
{
93
+
var rf = font.FontSystem.GetFont(fontSize);
94
+
var fcolor = new FontStashSharp.FSColor(color.R, color.G, color.B, color.A);
95
+
96
+
_textRenderer.Scissor = scissor;
97
+
rf.DrawText(_textRenderer, text, position, fcolor, scale, rotation, origin, layerDepth);
98
+
_textRenderer.ResetScissor();
99
+
}
100
+
101
+
/// <summary>
102
+
/// Submit a text string for drawing.
103
+
/// </summary>
104
+
/// <param name="font">The font to draw.</param>
105
+
/// <param name="fontSize">The font size.</param>
106
+
/// <param name="text">The text which will be drawn.</param>
107
+
/// <param name="position">The drawing location on screen</param>
108
+
/// <param name="color">The text color.</param>
109
+
public void DrawString(
110
+
Font font,
111
+
int fontSize,
112
+
string text,
113
+
Vector2 position,
114
+
Color color)
115
+
{
116
+
DrawString(font, fontSize, text, position, color, 0, Vector2.Zero, Vector2.One, 0);
117
+
}
118
+
119
+
/// <summary>
120
+
/// Submit a text string for drawing.
121
+
/// </summary>
122
+
/// <param name="font">The font to draw.</param>
123
+
/// <param name="fontSize">The font size.</param>
124
+
/// <param name="text">The text which will be drawn.</param>
125
+
/// <param name="position">The drawing location on screen</param>
126
+
/// <param name="color">The text color.</param>
127
+
/// <param name="scissor">The scissor rectangle.</param>
128
+
public void DrawString(
129
+
Font font,
130
+
int fontSize,
131
+
string text,
132
+
Vector2 position,
133
+
Color color,
134
+
Rectangle scissor)
135
+
{
136
+
DrawString(font, fontSize, text, position, color, 0, Vector2.Zero, Vector2.One, 0, scissor);
137
+
}
138
+
139
+
/// <summary>
140
+
/// Submit a text string for drawing.
141
+
/// </summary>
142
+
/// <param name="font">The font to draw.</param>
143
+
/// <param name="fontSize">The font size.</param>
144
+
/// <param name="text">The text which will be drawn.</param>
145
+
/// <param name="position">The drawing location on screen</param>
146
+
/// <param name="color">The text color.</param>
147
+
/// <param name="rotation">The rotation of the drawing.</param>
148
+
/// <param name="origin">The center of the rotation. (0, 0) for default.</param>
149
+
/// <param name="scale">The scaling of this drawing.</param>
150
+
/// <param name="layerDepth">The layer depth of this drawing.</param>
151
+
public void DrawString(
152
+
Font font,
153
+
int fontSize,
154
+
string text,
155
+
Vector2 position,
156
+
Color color,
157
+
float rotation,
158
+
Vector2 origin,
159
+
float scale,
160
+
float layerDepth)
161
+
{
162
+
DrawString(font, fontSize, text, position, color, rotation, origin, new Vector2(scale), layerDepth);
163
+
}
164
+
165
+
/// <summary>
166
+
/// Submit a text string for drawing.
167
+
/// </summary>
168
+
/// <param name="font">The font to draw.</param>
169
+
/// <param name="fontSize">The font size.</param>
170
+
/// <param name="text">The text which will be drawn.</param>
171
+
/// <param name="position">The drawing location on screen</param>
172
+
/// <param name="color">The text color.</param>
173
+
/// <param name="rotation">The rotation of the drawing.</param>
174
+
/// <param name="origin">The center of the rotation. (0, 0) for default.</param>
175
+
/// <param name="scale">The scaling of this drawing.</param>
176
+
/// <param name="layerDepth">The layer depth of this drawing.</param>
177
+
/// <param name="scissor">The scissor rectangle.</param>
178
+
public void DrawString(
179
+
Font font,
180
+
int fontSize,
181
+
string text,
182
+
Vector2 position,
183
+
Color color,
184
+
float rotation,
185
+
Vector2 origin,
186
+
float scale,
187
+
float layerDepth,
188
+
Rectangle scissor)
189
+
{
190
+
DrawString(font, fontSize, text, position, color, rotation, origin, new Vector2(scale), layerDepth, scissor);
191
+
}
192
+
193
+
/// <inheritdoc/>
194
+
public void Dispose()
195
+
{
196
+
GC.SuppressFinalize(this);
197
+
Dispose(true);
198
+
}
199
+
200
+
private void Dispose(bool disposing)
201
+
{
202
+
if (IsDisposed)
203
+
return;
204
+
IsDisposed = true;
205
+
206
+
if (disposing)
207
+
{
208
+
209
+
}
210
+
}
211
+
}
212
+
}