Skip to content

Commit

Permalink
replace Pointf with System.Numerics.Vector2
Browse files Browse the repository at this point in the history
  • Loading branch information
lodicolo committed Feb 25, 2025
1 parent a527be1 commit 2d7cb24
Show file tree
Hide file tree
Showing 24 changed files with 90 additions and 139 deletions.
5 changes: 5 additions & 0 deletions Framework/Intersect.Framework.Core/Point.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using MessagePack;

namespace Intersect;
Expand Down Expand Up @@ -81,4 +82,8 @@ public void Deconstruct(out int x, out int y)
x = X;
y = Y;
}

public static implicit operator Point(Vector2 vector) => new((int)vector.X, (int)vector.Y);

public static implicit operator Vector2(Point point) => new(point.X, point.Y);
}
11 changes: 6 additions & 5 deletions Intersect.Client.Core/Core/Graphics.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Intersect.Client.Entities;
using Intersect.Client.Entities.Events;
using Intersect.Client.Framework.Content;
Expand Down Expand Up @@ -1098,7 +1099,7 @@ private static void GenerateLightMap()
return;
}

var destRect = new FloatRect(new Pointf(), sDarknessTexture.Dimensions / Globals.Database.WorldZoom);
var destRect = new FloatRect(new Vector2(), sDarknessTexture.Dimensions / Globals.Database.WorldZoom);
if (map.IsIndoors)
{
DrawGameTexture(
Expand Down Expand Up @@ -1411,9 +1412,9 @@ public static void UpdatePlayerLight()
/// </summary>
/// <param name="windowPoint">The point to convert.</param>
/// <returns>The converted point.</returns>
public static Pointf ConvertToWorldPoint(Pointf windowPoint)
public static Vector2 ConvertToWorldPoint(Vector2 windowPoint)
{
return new Pointf(
return new Vector2(
(int)Math.Floor(windowPoint.X / Globals.Database.WorldZoom + CurrentView.Left),
(int)Math.Floor(windowPoint.Y / Globals.Database.WorldZoom + CurrentView.Top)
);
Expand All @@ -1424,9 +1425,9 @@ public static Pointf ConvertToWorldPoint(Pointf windowPoint)
/// </summary>
/// <param name="windowPoint">The point to convert.</param>
/// <returns>The converted point.</returns>
public static Pointf ConvertToWorldPointNoZoom(Pointf windowPoint)
public static Vector2 ConvertToWorldPointNoZoom(Vector2 windowPoint)
{
return new Pointf((int)Math.Floor(windowPoint.X + CurrentView.Left), (int)Math.Floor(windowPoint.Y + CurrentView.Top));
return new Vector2((int)Math.Floor(windowPoint.X + CurrentView.Left), (int)Math.Floor(windowPoint.Y + CurrentView.Top));
}

//Rendering Functions
Expand Down
11 changes: 6 additions & 5 deletions Intersect.Client.Core/Entities/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using Intersect.Client.Core;
using Intersect.Client.Entities.Events;
using Intersect.Client.Entities.Projectiles;
Expand Down Expand Up @@ -129,7 +130,7 @@ public Guid[] Equipment
IReadOnlyDictionary<Vital, long> IEntity.MaxVitals =>
Enum.GetValues<Vital>().ToDictionary(vital => vital, vital => MaxVital[(int)vital]);

protected Pointf mOrigin = Pointf.Empty;
protected Vector2 mOrigin = Vector2.Zero;

//Chat
private readonly List<ChatBubble> mChatBubbles = [];
Expand Down Expand Up @@ -276,11 +277,11 @@ public Entity(Guid id, EntityPacket? packet, EntityType entityType)

IReadOnlyList<IStatus> IEntity.Status => Status;

public Pointf Origin => LatestMap == default ? Pointf.Empty : mOrigin;
public Vector2 Origin => LatestMap == default ? Vector2.Zero : mOrigin;

protected virtual Pointf CenterOffset => (Texture == default) ? Pointf.Empty : (Pointf.UnitY * Texture.Center.Y / Options.Instance.Sprites.Directions);
protected virtual Vector2 CenterOffset => (Texture == default) ? Vector2.Zero : (Vector2.UnitY * Texture.Center.Y / Options.Instance.Sprites.Directions);

public Pointf Center => Origin - CenterOffset;
public Vector2 Center => Origin - CenterOffset;

public Direction Dir
{
Expand Down Expand Up @@ -1475,7 +1476,7 @@ protected virtual void CalculateOrigin()
return;
}

mOrigin = new Pointf(
mOrigin = new Vector2(
LatestMap.X + X * Options.Instance.Map.TileWidth + OffsetX + Options.Instance.Map.TileWidth / 2,
LatestMap.Y + Y * Options.Instance.Map.TileHeight + OffsetY + Options.Instance.Map.TileHeight
);
Expand Down
9 changes: 5 additions & 4 deletions Intersect.Client.Core/Entities/Events/Event.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Intersect.Client.Core;
using Intersect.Client.Framework.GenericClasses;
using Intersect.Client.Framework.Graphics;
Expand Down Expand Up @@ -29,24 +30,24 @@ public partial class Event : Entity

public EventTrigger Trigger { get; set; }

protected override Pointf CenterOffset
protected override Vector2 CenterOffset
{
get
{
switch (Graphic.Type)
{
case EventGraphicType.None:
return HasAnimations ? Pointf.UnitY * Options.Instance.Map.TileHeight / 2f : Pointf.Empty;
return HasAnimations ? Vector2.UnitY * Options.Instance.Map.TileHeight / 2f : Vector2.Zero;

case EventGraphicType.Sprite:
return base.CenterOffset;

case EventGraphicType.Tileset:
return Pointf.UnitY * Options.Instance.Map.TileHeight * (Graphic.Height + 1) / 2f;
return Vector2.UnitY * Options.Instance.Map.TileHeight * (Graphic.Height + 1) / 2f;

default:
ApplicationContext.Context.Value?.Logger.LogError($"Unimplemented graphic type: {Graphic.Type}");
return Pointf.Empty;
return Vector2.Zero;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Intersect.Client.Core/Interface/Shared/FPSPanel.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Intersect.Client.Core;
using Intersect.Client.Framework.File_Management;
using Intersect.Client.Framework.GenericClasses;
Expand Down Expand Up @@ -44,7 +45,7 @@ public FPSPanel(Base parent, string name = nameof(FPSPanel)) : base(parent: pare
size: 10,
fontScale: 1
) +
new Pointf(16, 8);
new Vector2(16, 8);

DelegateDataProvider<int> fpsProvider = new(() => Graphics.Renderer.FPS)
{
Expand Down
20 changes: 10 additions & 10 deletions Intersect.Client.Core/MonoGame/Graphics/MonoRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ protected override bool RecreateSpriteBatch()
return true;
}

public Pointf GetMouseOffset()
public Vector2 GetMouseOffset()
{
return new Pointf(
return new Vector2(
_graphicsDeviceManager.PreferredBackBufferWidth / (float)_gameWindow.ClientBounds.Width,
_graphicsDeviceManager.PreferredBackBufferHeight / (float)_gameWindow.ClientBounds.Height
);
Expand Down Expand Up @@ -703,10 +703,10 @@ public override void DrawTexture(
origin = new Vector2(sw / 2f, sh / 2f);

//TODO: Optimize in terms of memory AND performance.
var pnt = new Pointf(0, 0);
var pnt1 = new Pointf(tw, 0);
var pnt2 = new Pointf(0, th);
var cntr = new Pointf(tw / 2, th / 2);
var pnt = new Vector2(0, 0);
var pnt1 = new Vector2(tw, 0);
var pnt2 = new Vector2(0, th);
var cntr = new Vector2(tw / 2, th / 2);

var pntMod = Rotate(pnt, cntr, rotationDegrees);
var pntMod2 = Rotate(pnt1, cntr, rotationDegrees);
Expand Down Expand Up @@ -778,9 +778,9 @@ private static double GetDistance(double x1, double y1, double x2, double y2)
return root;
}

private Pointf Rotate(Pointf pnt, Pointf ctr, float angle)
private Vector2 Rotate(Vector2 pnt, Vector2 ctr, float angle)
{
return new Pointf(
return new Vector2(
(float)(pnt.X + (ctr.X * Math.Cos(angle)) - (ctr.Y * Math.Sin(angle))),
(float)(pnt.Y + (ctr.X * Math.Sin(angle)) + (ctr.Y * Math.Cos(angle)))
);
Expand Down Expand Up @@ -976,7 +976,7 @@ public override GameShader LoadShader(string shaderName)
return new MonoShader(shaderName, _contentManager);
}

public override Pointf MeasureText(string text, IFont? font, int size, float fontScale)
public override System.Numerics.Vector2 MeasureText(string text, IFont? font, int size, float fontScale)
{
if (font is not Font<SpriteFont> platformFont)
{
Expand All @@ -989,7 +989,7 @@ public override Pointf MeasureText(string text, IFont? font, int size, float fon

var textMeasurement = spriteFont.MeasureString(text);

return new Pointf(textMeasurement.X * fontScale, textMeasurement.Y * fontScale);
return new System.Numerics.Vector2(textMeasurement.X * fontScale, textMeasurement.Y * fontScale);
}

private readonly Dictionary<SpriteFont, char> _defaultCharacterForSpriteFont = [];
Expand Down
3 changes: 2 additions & 1 deletion Intersect.Client.Core/MonoGame/Graphics/MonoShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Vector2 = System.Numerics.Vector2;

namespace Intersect.Client.MonoGame.Graphics;

Expand Down Expand Up @@ -65,7 +66,7 @@ public override void SetColor(string key, Color val)
}
}

public override void SetVector2(string key, Pointf val)
public override void SetVector2(string key, Vector2 val)
{
//throw new NotImplementedException();
}
Expand Down
10 changes: 5 additions & 5 deletions Intersect.Client.Core/MonoGame/Input/MonoInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private void InputHandlerOnFocusChanged(Base? control, FocusSource focusSource)
Mouse.SetPosition((int)center.X, (int)center.Y);
var mouseState = Mouse.GetState();
Interface.Interface.GwenInput.ProcessMessage(
new GwenInputMessage(IntersectInput.InputEvent.MouseMove, new Pointf(mouseState.X, mouseState.Y), MouseButton.None, Keys.Alt)
new GwenInputMessage(IntersectInput.InputEvent.MouseMove, new System.Numerics.Vector2(mouseState.X, mouseState.Y), MouseButton.None, Keys.Alt)
);
}

Expand Down Expand Up @@ -197,9 +197,9 @@ public override bool IsKeyDown(Keys key) =>
public override bool WasKeyDown(Keys key) =>
_intersectToMonoGameKeyMap.TryGetValue(key, out var mappedKey) && _previousKeyboardState.IsKeyDown(mappedKey);

public override Pointf GetMousePosition()
public override System.Numerics.Vector2 GetMousePosition()
{
return new Pointf(mMouseX, mMouseY);
return new System.Numerics.Vector2(mMouseX, mMouseY);
}

private void CheckMouseButton(Keys modifier, ButtonState bs, MouseButton mb)
Expand Down Expand Up @@ -242,11 +242,11 @@ private void CheckMouseButton(Keys modifier, ButtonState bs, MouseButton mb)

private void CheckMouseScrollWheel(int scrlVValue, int scrlHValue)
{
Pointf p = new Pointf(0, 0);
System.Numerics.Vector2 p = default;

if (scrlVValue != mMouseVScroll || scrlHValue != mMouseHScroll)
{
p = new Pointf(scrlHValue - mMouseHScroll, scrlVValue - mMouseVScroll);
p = new System.Numerics.Vector2(scrlHValue - mMouseHScroll, scrlVValue - mMouseVScroll);

Interface.Interface.GwenInput.ProcessMessage(
new GwenInputMessage(IntersectInput.InputEvent.MouseScroll, p, MouseButton.Middle, Keys.Alt)
Expand Down
5 changes: 3 additions & 2 deletions Intersect.Client.Framework/Entities/IEntity.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Intersect.Client.Framework.GenericClasses;
using Intersect.Client.Framework.Graphics;
using Intersect.Client.Framework.Items;
Expand All @@ -24,8 +25,8 @@ public interface IEntity : IDisposable
FloatRect WorldPos { get; }
float OffsetX { get; }
float OffsetY { get; }
Pointf Center { get; }
Pointf Origin { get; }
Vector2 Center { get; }
Vector2 Origin { get; }
bool IsMoving { get; }
bool IsStealthed { get; }
bool IsBlocking { get; }
Expand Down
15 changes: 8 additions & 7 deletions Intersect.Client.Framework/GenericClasses/FloatRect.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using System.Numerics;
using System.Runtime.CompilerServices;

namespace Intersect.Client.Framework.GenericClasses;

Expand Down Expand Up @@ -38,27 +39,27 @@ public float Height
set => mHeight = value;
}

public Pointf Position
public Vector2 Position
{
get => new Pointf(X, Y);
get => new Vector2(X, Y);
set
{
X = value.X;
Y = value.Y;
}
}

public Pointf Size
public Vector2 Size
{
get => new Pointf(Width, Height);
get => new Vector2(Width, Height);
set
{
Width = value.X;
Height = value.Y;
}
}

public Pointf Center => Position + Size / 2f;
public Vector2 Center => Position + Size / 2f;

public float Left => X;

Expand All @@ -78,7 +79,7 @@ public FloatRect(float x, float y, float w, float h)
mHeight = h;
}

public FloatRect(Pointf position, Pointf size) : this(
public FloatRect(Vector2 position, Vector2 size) : this(
position.X,
position.Y,
size.X,
Expand Down
73 changes: 0 additions & 73 deletions Intersect.Client.Framework/GenericClasses/Pointf.cs

This file was deleted.

Loading

0 comments on commit 2d7cb24

Please sign in to comment.