Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2d drawing improvements #133

Merged
merged 20 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions OpenKh.Engine/DrawingHelpers.cs

This file was deleted.

36 changes: 36 additions & 0 deletions OpenKh.Engine/Extensions/SpriteDrawingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using OpenKh.Engine.Renders;

namespace OpenKh.Engine.Extensions
{
public static class SpriteDrawingExtensions
{
public static void SetProjection(this ISpriteDrawing spriteDrawing,
float width, float height, float internalWidth, float internalHeight, float ratio)
{
var heightRatio = internalHeight / height;
width *= heightRatio;
height *= heightRatio;
width *= ratio;

var left = (internalWidth - width) / 2;
spriteDrawing.SetViewport(left, width + left, 0, height);
}

public static void FillRectangle(this ISpriteDrawing drawing, float x, float y, float width, float height, ColorF color)
{
drawing.AppendSprite(new SpriteDrawingContext()
.Source(0, 0, 1, 1)
.Position(x, y)
.DestinationSize(width, height)
.Color(color));
}

public static void DrawRectangle(this ISpriteDrawing drawing, float x, float y, float width, float height, ColorF color, float thickness = 1.0f)
{
drawing.FillRectangle(x, y, width, thickness, color);
drawing.FillRectangle(x, y + height - 1, width - 1, thickness, color);
drawing.FillRectangle(x, y, thickness, height, color);
drawing.FillRectangle(x + width - 1, y, thickness, height, color);
}
}
}
3 changes: 1 addition & 2 deletions OpenKh.Engine/OpenKh.Engine.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\OpenKh.Kh2\OpenKh.Kh2.csproj" />
<ProjectReference Include="..\XeEngine.Tools.Public\Xe.Drawing\Xe.Drawing.csproj" />
</ItemGroup>

</Project>
4 changes: 1 addition & 3 deletions OpenKh.Engine/Renders/IMessageRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Xe.Drawing;

namespace OpenKh.Engine.Renders
namespace OpenKh.Engine.Renders
{
public class DrawContext
{
Expand Down
264 changes: 264 additions & 0 deletions OpenKh.Engine/Renders/ISpriteDrawing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
using OpenKh.Imaging;
using System;

namespace OpenKh.Engine.Renders
{
public enum BlendMode
{
Default,
Add,
Subtract,
}

public enum TextureWrapMode
{
Default = 0,
Clamp = 1,
Repeat = 2,
}

public struct ColorF
{
public static readonly ColorF Black = new ColorF(0.0f, 0.0f, 0.0f, 1.0f);
public static readonly ColorF White = new ColorF(1.0f, 1.0f, 1.0f, 1.0f);

public float R, G, B, A;

public ColorF(float r, float g, float b, float a)
{
R = r;
G = g;
B = b;
A = a;
}

public static ColorF operator +(ColorF colorA, ColorF colorB) => new ColorF
{
R = colorA.R + colorB.R,
G = colorA.G + colorB.G,
B = colorA.B + colorB.B,
A = colorA.A + colorB.A
};

public static ColorF operator *(ColorF colorA, ColorF colorB) => new ColorF
{
R = colorA.R * colorB.R,
G = colorA.G * colorB.G,
B = colorA.B * colorB.B,
A = colorA.A * colorB.A
};

public static ColorF FromRgba(int r, int g, int b, int a) => new ColorF
{
R = r / 255.0f,
G = g / 255.0f,
B = b / 255.0f,
A = a / 255.0f,
};

public static ColorF FromRgba(int rgba) => new ColorF
{
R = ((rgba >> 0) & 0xff) / 255.0f,
G = ((rgba >> 8) & 0xff) / 255.0f,
B = ((rgba >> 16) & 0xff) / 255.0f,
A = ((rgba >> 24) & 0xff) / 255.0f,
};

public static ColorF FromRgba(uint rgba) => new ColorF
{
R = ((rgba >> 0) & 0xff) / 255.0f,
G = ((rgba >> 8) & 0xff) / 255.0f,
B = ((rgba >> 16) & 0xff) / 255.0f,
A = ((rgba >> 24) & 0xff) / 255.0f,
};

public override string ToString() => $"({R}, {G}, {B}, {A})";
}

public class SpriteDrawingContext
{
public float SourceLeft { get; set; }
public float SourceTop { get; set; }
public float SourceRight { get; set; }
public float SourceBottom { get; set; }

public float DestinationX { get; set; }
public float DestinationY { get; set; }
public float DestinationWidth { get; set; }
public float DestinationHeight { get; set; }

public ColorF Color0 { get; set; }
public ColorF Color1 { get; set; }
public ColorF Color2 { get; set; }
public ColorF Color3 { get; set; }

public ISpriteTexture SpriteTexture { get; set; }
public BlendMode BlendMode { get; set; }

public TextureWrapMode TextureWrapU { get; set; } = TextureWrapMode.Default;
public TextureWrapMode TextureWrapV { get; set; } = TextureWrapMode.Default;
public float TextureHorizontalShift { get; set; }
public float TextureVerticalShift { get; set; }
public float TextureRegionLeft { get; set; }
public float TextureRegionRight { get; set; }
public float TextureRegionTop { get; set; }
public float TextureRegionBottom { get; set; }
}

public static class SpriteDrawingContextExtensions
{
private static readonly ColorF ColorWhite = new ColorF(1.0f, 1.0f, 1.0f, 1.0f);

public static SpriteDrawingContext SourceLTRB(this SpriteDrawingContext context, float left, float top, float right, float bottom)
{
context.SourceLeft = left;
context.SourceTop = top;
context.SourceRight = right;
context.SourceBottom = bottom;
return context;
}

public static SpriteDrawingContext Source(this SpriteDrawingContext context, float x, float y, float width, float height)
{
context.SourceLeft = x;
context.SourceTop = y;
context.SourceRight = x + width;
context.SourceBottom = y + height;
return context;
}

public static SpriteDrawingContext Position(this SpriteDrawingContext context, float x, float y)
{
context.DestinationX = x;
context.DestinationY = y;
return context;
}

public static SpriteDrawingContext Traslate(this SpriteDrawingContext context, float x, float y)
{
context.DestinationX += x;
context.DestinationY += y;
return context;
}

public static SpriteDrawingContext MatchSourceSize(this SpriteDrawingContext context)
{
context.DestinationWidth = Math.Abs(context.SourceRight - context.SourceLeft);
context.DestinationHeight = Math.Abs(context.SourceBottom - context.SourceTop);
return context;
}

public static SpriteDrawingContext DestinationSize(this SpriteDrawingContext context, float width, float height)
{
context.DestinationWidth = width;
context.DestinationHeight = height;
return context;
}

public static SpriteDrawingContext ScaleSize(this SpriteDrawingContext context, float scale)
{
context.DestinationWidth *= scale;
context.DestinationHeight *= scale;
return context;
}

public static SpriteDrawingContext ScaleSize(this SpriteDrawingContext context, float scaleX, float scaleY)
{
context.DestinationWidth *= scaleX;
context.DestinationHeight *= scaleY;
return context;
}

public static SpriteDrawingContext ColorDefault(this SpriteDrawingContext context)
{
context.Color0 = ColorWhite;
context.Color1 = ColorWhite;
context.Color2 = ColorWhite;
context.Color3 = ColorWhite;
return context;
}

public static SpriteDrawingContext Color(this SpriteDrawingContext context, ColorF color)
{
context.Color0 = color;
context.Color1 = color;
context.Color2 = color;
context.Color3 = color;
return context;
}

public static SpriteDrawingContext ColorAdd(this SpriteDrawingContext context, ColorF color)
{
context.Color0 += color;
context.Color1 += color;
context.Color2 += color;
context.Color3 += color;
return context;
}

public static SpriteDrawingContext ColorMultiply(this SpriteDrawingContext context, ColorF color)
{
context.Color0 *= color;
context.Color1 *= color;
context.Color2 *= color;
context.Color3 *= color;
return context;
}

public static SpriteDrawingContext SpriteTexture(this SpriteDrawingContext context, ISpriteTexture spriteTexture)
{
context.SpriteTexture = spriteTexture;
return context;
}

public static SpriteDrawingContext TextureWrapHorizontal(this SpriteDrawingContext context, TextureWrapMode mode, float left, float right)
{
context.TextureWrapU = mode;
context.TextureRegionLeft = left;
context.TextureRegionRight = right;
return context;
}

public static SpriteDrawingContext TextureWrapVertical(this SpriteDrawingContext context, TextureWrapMode mode, float top, float bottom)
{
context.TextureWrapV = mode;
context.TextureRegionTop = top;
context.TextureRegionBottom = bottom;
return context;
}
}

public interface IMappedResource : IDisposable
{
IntPtr Data { get; }

int Stride { get; }

int Length { get; }
}

public interface ISpriteTexture : IDisposable
{
int Width { get; }
int Height { get; }

IMappedResource Map();
}

public interface ISpriteDrawing : IDisposable
{
ISpriteTexture DestinationTexture { get; set; }

ISpriteTexture CreateSpriteTexture(IImageRead image);

ISpriteTexture CreateSpriteTexture(int width, int height);

void SetViewport(float left, float right, float top, float bottom);

void Clear(ColorF color);

void AppendSprite(SpriteDrawingContext context);

void Flush();
}
}
Loading