1.0.12 & 1.0.12.1

+ Utility Methods for texture transformations

~ Fixed an issue, where the horizontal & vertical flip was wrongly applied
~ Added missing summaries
This commit is contained in:
Michel Fedde 2021-05-05 17:36:12 +02:00
parent a29a2cab53
commit f11a954b5a
10 changed files with 116 additions and 21 deletions

View file

@ -18,17 +18,17 @@ namespace SM.Base.Drawing
/// <summary>
/// The camera, that was used last time the object was rendered.
/// </summary>
public GenericCamera LastDrawingCamera;
public GenericCamera LastDrawingCamera { get; private set; }
/// <summary>
/// The material it should use.
/// </summary>
public Material Material = new Material();
public Material Material { get; set; } = new Material();
/// <summary>
/// Transformation for the textures.
/// </summary>
public TextureTransformation TextureTransform = new TextureTransformation();
public TextureTransformation TextureTransform { get; set; } = new TextureTransformation();
/// <summary>
/// This allows custom shaders to add own arguments.
@ -71,7 +71,6 @@ namespace SM.Base.Drawing
DrawContext(ref context);
}
/// <inheritdoc />
public virtual void OnAdded(object sender)
{

View file

@ -18,6 +18,9 @@ namespace SM.Base.Drawing.Text
/// </summary>
public int Advance;
/// <summary>
/// The bearing for this char.
/// </summary>
public float BearingX;
/// <summary>
@ -26,6 +29,9 @@ namespace SM.Base.Drawing.Text
public float Width;
/// <summary>
/// Matrix for the texture.
/// </summary>
public Matrix3 TextureMatrix;
}
}

View file

@ -11,21 +11,45 @@ using SM.Base.Textures;
namespace SM.Base.Drawing.Text
{
/// <summary>
/// Represents a font to be used in DrawText-classes.
/// </summary>
public class Font : Texture
{
private static Library _lib;
private Face _fontFace;
/// <summary>
/// The amount the cursor should move forward when a space was found.
/// </summary>
public float SpaceWidth { get; private set; } = 0;
/// <summary>
/// The char set the font should contain.
/// <para>See <see cref="FontCharStorage"/> for some default values.</para>
/// <para>Default: <see cref="FontCharStorage.SimpleUTF8"/></para>
/// </summary>
public ICollection<char> CharSet { get; set; } = FontCharStorage.SimpleUTF8;
/// <summary>
/// The font-size defines how large the result texture should be.
/// <para>Lower equals less quality, but also less memory usage.</para>
/// <para>Higher equals high quality, but also high memory usage.</para>
/// <para>Default: 12</para>
/// </summary>
public float FontSize { get; set; } = 12;
/// <summary>
/// The character positions.
/// </summary>
public Dictionary<char, CharParameter> Positions = new Dictionary<char, CharParameter>();
/// <summary>
/// Creates a font, by using a path
/// </summary>
/// <param name="path">Path to the font-file.</param>
public Font(string path)
{
_lib ??= new Library();
@ -34,6 +58,9 @@ namespace SM.Base.Drawing.Text
UnpackAlignment = 1;
}
/// <summary>
/// (Re-)Generates the texture.
/// </summary>
public void RegenerateTexture()
{
Width = Height = 0;
@ -88,6 +115,7 @@ namespace SM.Base.Drawing.Text
}
}
/// <inheritdoc />
public override void Compile()
{
RegenerateTexture();

View file

@ -10,10 +10,22 @@ using SM.Base.Window;
namespace SM.Base.Drawing.Text
{
/// <summary>
/// Represents the options for <see cref="TextDrawingBasis{TTransform}.Origin"/>
/// </summary>
public enum TextOrigin
{
/// <summary>
/// The position equals (0,0) in the left side of the text.
/// </summary>
Left,
/// <summary>
/// The position equals (0,0) in the center of the text.
/// </summary>
Center,
/// <summary>
/// The position equals (0,0) in the right side of the text.
/// </summary>
Right
}

View file

@ -1,6 +1,8 @@
#region usings
using OpenTK;
using OpenTK.Graphics.ES10;
using SM.Base.Textures;
using SM.Base.Types;
#endregion
@ -34,6 +36,41 @@ namespace SM.Base.Drawing
return CalculateMatrix(Offset, Scale, Rotation);
}
/// <summary>
/// Sets the offset relative to the pixels of the texture.
/// </summary>
/// <param name="texture">The texture it should use.</param>
/// <param name="pixelLocation">The offset in pixel.</param>
public void SetOffsetRelative(Texture texture, Vector2 pixelLocation)
{
Vector2 textureSize = new Vector2(texture.Width, texture.Height);
Offset.Set( Vector2.Divide(pixelLocation, textureSize) );
}
/// <summary>
/// Sets the scale relative to the pixels of the texture.
/// </summary>
/// <param name="texture">The texture.</param>
/// <param name="rectangleSize">The scale in pixel.</param>
public void SetScaleRelative(Texture texture, Vector2 rectangleSize)
{
Vector2 textureSize = new Vector2(texture.Width, texture.Height);
Scale.Set( Vector2.Divide(rectangleSize, textureSize) );
}
/// <summary>
/// Sets the offset and scale relative to the pixels of the texture.
/// </summary>
/// <param name="texture">The texture.</param>
/// <param name="location">Offset in pixel</param>
/// <param name="rectangleSize">Scale in pixel.</param>
public void SetRectangleRelative(Texture texture, Vector2 location, Vector2 rectangleSize)
{
Vector2 textureSize = new Vector2(texture.Width, texture.Height);
Offset.Set(Vector2.Divide(location, textureSize));
Scale.Set(Vector2.Divide(rectangleSize, textureSize));
}
/// <summary>
/// Calculates a texture matrix.
/// </summary>

View file

@ -33,6 +33,9 @@ namespace SM.Base.Textures
private int? _height;
private int? _width;
/// <summary>
/// The unpack alignment for this texture.
/// </summary>
public int UnpackAlignment = 4;
/// <summary>
@ -120,6 +123,7 @@ namespace SM.Base.Textures
/// <param name="filter">The filter</param>
/// <param name="wrapMode">The wrap mode</param>
/// <param name="dispose">Auto dispose of the bitmap? Default: false</param>
/// <param name="unpackAlignment">The unpack alignment for this texture.</param>
/// <returns></returns>
public static int GenerateTexture(Bitmap map, TextureMinFilter filter, TextureWrapMode wrapMode,
bool dispose = false, int unpackAlignment = 4)

View file

@ -143,6 +143,9 @@ namespace SM.Base.Window
}
}
/// <summary>
/// This gets called, when the window completed with all initilization steps.
/// </summary>
protected virtual void OnLoaded()
{
Icon ??= new Icon(AssemblyUtility.GetAssemblyStream("SM.Base.Window.winIcon.ico"));

View file

@ -23,6 +23,10 @@ namespace SM2D.Drawing
Transform.Size = new CVector2(1);
}
/// <summary>
/// Sets the height of the text.
/// </summary>
/// <param name="desiredHeight">The height it should be.</param>
public void SetHeight(float desiredHeight)
{
if (!Font.WasCompiled) Font.Compile();

View file

@ -54,9 +54,7 @@ namespace SM2D.Types
{
float z = 1 / (float) ZIndexPercision * ZIndex;
return Matrix4.CreateScale(Size.X, Size.Y, 1) *
Matrix4.CreateRotationX(MathHelper.DegreesToRadians(HorizontalFlip ? 180 : 0)) *
Matrix4.CreateRotationY(MathHelper.DegreesToRadians(VerticalFlip ? 180 : 0)) *
return Matrix4.CreateScale(Size.X * (VerticalFlip ? -1 : 1), Size.Y * (HorizontalFlip ? -1 : 1), 1) *
Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation)) *
Matrix4.CreateTranslation(Position.X, Position.Y, z);
}
@ -100,5 +98,15 @@ namespace SM2D.Types
{
Size.Set(width, width / texture.Aspect);
}
/// <summary>
/// Adjusts <see cref="Size"/> for the texture transform.
/// <para>In this way you can make sure, the texture is not stretched.</para>
/// </summary>
/// <param name="transform"></param>
public void AdjustSizeToTextureTransform(TextureTransformation transform)
{
Size.Set(transform.Scale * Size);
}
}
}

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
@ -8,13 +9,13 @@ using SM.Base;
using SM.Base.Animation;
using SM.Base.Controls;
using SM.Base.Drawing;
using SM.Base.Drawing.Text;
using SM.Base.Time;
using SM.Base.Window;
using SM2D;
using SM2D.Drawing;
using SM2D.Object;
using SM2D.Scene;
using Font = SM.Base.Drawing.Text.Font;
namespace SM_TEST
{
@ -34,8 +35,6 @@ namespace SM_TEST
};
font.RegenerateTexture();
SMRenderer.DefaultMesh = new Polygon(new[] { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) });
window = new GLWindow(1280, 720, "0ms", WindowFlags.Window, VSyncMode.Off);
window.ApplySetup(new Window2DSetup());
@ -49,19 +48,14 @@ namespace SM_TEST
};
Material uvMaterial = new Material()
DrawObject2D test = new DrawObject2D()
{
Tint = new Color4(1f, 0, 0, .5f),
Blending = true,
Texture = font
Texture = new Bitmap("test.png")
};
DrawText test = new DrawText(font, "Level Completed")
{
Material = uvMaterial,
};
test.Transform.Position.Set(0, 2);
test.Transform.Size.Set(.5f);
test.Material.Blending = true;
test.Transform.Size.Set(100);
test.TextureTransform.SetRectangleRelative(test.Texture, new Vector2(234, 0), new Vector2(220, 201));
test.Transform.AdjustSizeToTextureTransform(test.TextureTransform);
scene.Objects.Add(test);