-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomMonoGameUtils.cs
134 lines (116 loc) · 4.75 KB
/
CustomMonoGameUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_EcoSimApp
{
internal static class CustomMonoGameUtil
{
private static Texture2D _texture;
private static Texture2D GetTexture(SpriteBatch spriteBatch)
{
if (_texture == null)
{
_texture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
_texture.SetData(new[] { Microsoft.Xna.Framework.Color.White });
}
return _texture;
}
public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Microsoft.Xna.Framework.Color color, float thickness = 1f)
{
var distance = Vector2.Distance(point1, point2);
var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
DrawLine(spriteBatch, point1, distance, angle, color, thickness);
}
public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point, float length, float angle, Microsoft.Xna.Framework.Color color, float thickness = 1f)
{
var origin = new Vector2(0f, 0.5f);
var scale = new Vector2(length, thickness);
spriteBatch.Draw(GetTexture(spriteBatch), point, null, color, angle, origin, scale, SpriteEffects.None, 0);
}
public static System.Drawing.PointF Vector2ToPoint(Vector2 vector2)
{
System.Drawing.PointF pt = new System.Drawing.Point(
(int)(vector2.X + 0.5f), (int)(vector2.Y + 0.5f));
return pt;
}
public static Vector2 ToVector2(PointF point)
{
return new Vector2(point.X, point.Y);
}
//// str - the source string
//// index- the start location to replace at (0-based)
//// length - the number of characters to be removed before inserting
//// replace - the string that is replacing characters
public static string ReplaceAt(this string str, int index, int length, string replace)
{
return str.Remove(index, Math.Min(length, str.Length - index))
.Insert(index, replace);
}
}
public class SimpleFps
{
private double frames = 0;
private double updates = 0;
private double elapsed = 0;
private double last = 0;
private double now = 0;
public double msgFrequency = 1.0f;
public string msg = "";
/// <summary>
/// The msgFrequency here is the reporting time to update the message.
/// </summary>
public void Update(GameTime gameTime)
{
now = gameTime.TotalGameTime.TotalSeconds;
elapsed = (double)(now - last);
if (elapsed > msgFrequency)
{
msg = " Fps: " + Math.Round(frames / elapsed, 4).ToString() + "\n Elapsed time: " + Math.Round(elapsed, 4).ToString(); //+ "\n Updates: " + updates.ToString() + "\n Frames: " + frames.ToString();
//Console.WriteLine(msg);
elapsed = 0;
frames = 0;
updates = 0;
last = now;
}
updates++;
}
public void DrawFps(SpriteBatch spriteBatch, SpriteFont font, Vector2 fpsDisplayPosition, Microsoft.Xna.Framework.Color fpsTextColor)
{
spriteBatch.DrawString(font, msg, fpsDisplayPosition, fpsTextColor);
frames++;
}
}
public class Camera2D
{
public Camera2D()
{
Zoom = 1;
Position = Vector2.Zero;
Rotation = 0;
Origin = Vector2.Zero;
Position = Vector2.Zero;
}
public float Zoom { get; set; }
public Vector2 Position { get; set; }
public float Rotation { get; set; }
public Vector2 Origin { get; set; }
public void Move(Vector2 direction)
{
Position += direction;
}
public Matrix GetTransform()
{
var translationMatrix = Matrix.CreateTranslation(new Vector3(Position.X, Position.Y, 0));
var rotationMatrix = Matrix.CreateRotationZ(Rotation);
var scaleMatrix = Matrix.CreateScale(new Vector3(Zoom, Zoom, 1));
var originMatrix = Matrix.CreateTranslation(new Vector3(Origin.X, Origin.Y, 0));
return translationMatrix * rotationMatrix * scaleMatrix * originMatrix;
}
}
}