Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
PerformanceManager.cs
浏览该文件的文档.
1using System.Text;
2using Engine;
4using Engine.Media;
5
6namespace Game {
7 public static class PerformanceManager {
8 public struct FrameData {
9 public float CpuTime;
10
11 public float TotalTime;
12 }
13
15
17
19
20 public static float? m_longTermAverageFrameTime;
21
22 public static long m_totalMemoryUsed;
23
24 public static long m_totalGpuMemoryUsed;
25
26 public static long m_totalGraphicResourcesCount;
27
29
30 public static double m_totalGameTime;
31
32 public static double m_totalFrameTime;
33
34 public static double m_totalCpuFrameTime;
35
36 public static int m_frameCount;
37
38 public static string m_statsString;
39
40 public static readonly List<string> m_extraStats = [];
41
42 public static FrameData[] m_frameData;
43
44 public static int m_frameDataIndex;
45
46 public static FontBatch2D m_fontBatch;
47
49
50 public static float AverageFrameTime => m_averageFrameTime.Value;
51
52 public static float AverageCpuFrameTime => m_averageCpuFrameTime.Value;
53
54 public static long TotalMemoryUsed => m_totalMemoryUsed;
55
57
59
66 m_statsString = string.Empty;
67 m_stateMachine.AddState(
68 "PreMeasure",
69 delegate { m_totalGameTime = 0.0; },
70 delegate {
71 if (GameManager.Project != null) {
73 if (m_totalGameTime > 60.0) {
74 m_stateMachine.TransitionTo("Measuring");
75 }
76 }
77 },
78 null
79 );
80 m_stateMachine.AddState(
81 "Measuring",
82 delegate {
83 m_totalFrameTime = 0.0;
85 m_frameCount = 0;
86 },
87 delegate {
88 if (GameManager.Project != null) {
90 && ScreensManager.CurrentScreen.GetType() == typeof(GameScreen)) {
91 float lastFrameTime = Program.LastFrameTime;
92 float lastCpuFrameTime = Program.LastCpuFrameTime;
93 if (lastFrameTime > 0f
94 && lastFrameTime < 1f
95 && lastCpuFrameTime > 0f
96 && lastCpuFrameTime < 1f) {
97 m_totalFrameTime += lastFrameTime;
98 m_totalCpuFrameTime += lastCpuFrameTime;
100 }
101 if (m_totalFrameTime > 180.0) {
102 m_stateMachine.TransitionTo("PostMeasure");
103 }
104 }
105 }
106 else {
107 m_stateMachine.TransitionTo("PreMeasure");
108 }
109 },
110 null
111 );
112 m_stateMachine.AddState(
113 "PostMeasure",
114 delegate {
115 if (m_frameCount > 0) {
117 float num = (int)Math.Round(
118 Math.Round(m_totalFrameTime / m_frameCount / 0.004999999888241291) * 0.004999999888241291 * 1000.0
119 );
120 float num2 = (int)Math.Round(
121 Math.Round(m_totalCpuFrameTime / m_frameCount / 0.004999999888241291) * 0.004999999888241291 * 1000.0
122 );
123 Log.Information($"PerformanceManager Measurement: frames={m_frameCount}, avgFrameTime={num}ms, avgFrameCpuTime={num2}ms");
124 }
125 },
126 delegate {
127 if (GameManager.Project == null) {
128 m_stateMachine.TransitionTo("PreMeasure");
129 }
130 },
131 null
132 );
133 m_stateMachine.TransitionTo("PreMeasure");
134 }
135
136 public static void Update() {
139 if (Time.PeriodicEvent(1.0, 0.0)) {
140 m_totalMemoryUsed = GC.GetTotalMemory(false);
143 }
144 m_stateMachine.Update();
145 }
146
147 public static void Draw() {
148 Vector2 scale = new(MathF.Round(Math.Clamp(ScreensManager.RootWidget.GlobalScale, 1.0f, 2.0f)));
149 Viewport viewport = Display.Viewport;
151 if (Time.PeriodicEvent(1.0, 0.0)
152 && ScreensManager.CurrentScreen != null) {
154 $"CPUMEM {TotalMemoryUsed / 1024f / 1024f:0}MB, GPUMEM {TotalGpuMemoryUsed / 1024f / 1024f:0}MB({TotalGraphicResourcesCount}), CPU {AverageCpuFrameTime / AverageFrameTime * 100f:0}%, {1f / AverageFrameTime:0.0} FPS";
155#if DEBUG
156 string wname = ScreensManager.RootWidget.Input.MousePosition.HasValue
158 : string.Empty;
159 m_statsString += "\nScreen:[" + ScreensManager.CurrentScreen.GetType().Name + "] [" + wname + "]";
160#endif
161 }
162 StringBuilder stringBuilder = new();
163 stringBuilder.AppendLine(m_statsString);
164 if (m_extraStats.Count > 0) {
165 stringBuilder.AppendJoin('\n', m_extraStats);
166 }
167 m_fontBatch.QueueText(
168 stringBuilder.ToString(),
170 0f,
171 Color.White,
172 TextAnchor.Default,
173 scale,
175 );
176 m_extraStats.Clear();
177 }
179 float num = viewport.Width / scale.X > 480f ? scale.X * 2f : scale.X;
180 float num2 = viewport.Height / -0.1f;
181 float num3 = viewport.Height - 1;
182 float s = 0.5f;
183 int num4 = MathUtils.Max((int)(viewport.Width / num), 1);
184 if (m_frameData == null
185 || m_frameData.Length != num4) {
186 m_frameData = new FrameData[num4];
188 }
191 FlatBatch2D flatBatch2D = m_primitivesRenderer.FlatBatch();
192 Color color = Color.Orange * s;
193 Color color2 = Color.Red * s;
194 for (int num5 = m_frameData.Length - 1; num5 >= 0; num5--) {
195 int num6 = (num5 - m_frameData.Length + 1 + m_frameDataIndex + m_frameData.Length) % m_frameData.Length;
196 FrameData frameData = m_frameData[num6];
197 float x = num5 * num;
198 float x2 = (num5 + 1) * num;
199 flatBatch2D.QueueQuad(new Vector2(x, num3), new Vector2(x2, num3 + frameData.CpuTime * num2), 0f, color);
200 flatBatch2D.QueueQuad(
201 new Vector2(x, num3 + frameData.CpuTime * num2),
202 new Vector2(x2, num3 + frameData.TotalTime * num2),
203 0f,
204 color2
205 );
206 }
207 flatBatch2D.QueueLine(
208 new Vector2(0f, num3 + 0.0166666675f * num2),
209 new Vector2(viewport.Width, num3 + 0.0166666675f * num2),
210 0f,
212 );
213 }
214 else {
215 m_frameData = null;
216 }
217 m_primitivesRenderer.Flush();
218 }
219
223 public static void AddExtraStat(string stat) {
224 m_extraStats.Add(stat);
225 }
226 }
227}
static Viewport Viewport
static long GetGpuMemoryUsage()
void QueueQuad(Vector2 corner1, Vector2 corner2, float depth, Color color)
void QueueLine(Vector2 p1, Vector2 p2, float depth, Color color)
static HashSet< GraphicsResource > m_resources
static void Information(object message)
定义 Log.cs:56
static int Max(int x1, int x2)
static BitmapFont DebugFont
static float FrameDuration
定义 Time.cs:46
static bool PeriodicEvent(double period, double offset)
定义 Time.cs:63
static Vector4 DisplayCutoutInsets
刘海/水滴/挖孔在屏幕边缘的宽度。X: 左边,Y: 顶部,Z: 右边,W: 底部
static Project Project
static RunningAverage m_averageFrameTime
static void AddExtraStat(string stat)
在性能信息下方添加一行信息,需每帧添加,不支持中文
static readonly List< string > m_extraStats
static PrimitivesRenderer2D m_primitivesRenderer
static RunningAverage m_averageCpuFrameTime
static float LastFrameTime
static float LastCpuFrameTime
static ContainerWidget RootWidget
virtual string Name
float GlobalScale
virtual Widget HitTestGlobal(Vector2 point, Func< Widget, bool > predicate=null)
WidgetInput Input
Matrix GlobalTransform
static Color White
static Color Green
static readonly Vector2 Zero
static Vector2 Transform(Vector2 v, Matrix m)