1using System.Numerics;
2using Fjord.Graphics;
3using Fjord.Ui;
4using static SDL2.SDL;
5
6namespace Fjord.Scenes;
7
8public class PerformanceScene : Scene
9{
10 float InputFPS = 0f;
11 float UpdateFPS = 0f;
12 float ProgramFPS = 0f;
13 ulong setFps = 0;
14
15 List<float> recentInputFPS = new();
16 List<float> recentUpdateFPS = new();
17 List<float> recentProgramFPS = new();
18
19 [Export(0, 3)]
20 public int Position = 0;
21
22 int LastPosition = 0;
23
24 [Export]
25 public bool Size = false;
26
27 bool LastSize = false;
28
29 public PerformanceScene(int width, int height) : base(width, height)
30 {
31 }
32
33 public override void Awake()
34 {
35 ClearColor = UiStyles.Background;
36 }
37
38 public override void Update()
39 {
40 if (SDL_GetTicks64() - setFps > 100)
41 {
42 setFps = SDL_GetTicks64();
43
44 InputFPS = Game.InputFPS;
45 UpdateFPS = Game.UpdateFPS;
46 ProgramFPS = Game.ProgramFPS;
47
48 recentInputFPS.Add(InputFPS);
49 recentProgramFPS.Add(ProgramFPS);
50
51 recentUpdateFPS.Add(UpdateFPS);
52 }
53
54 if (LastPosition != Position)
55 {
56 if (Position == 0)
57 {
58 SetRelativeWindowSize(0f, 0.89f, 0.10f, 1.001f);
59 }
60 else if (Position == 1)
61 {
62 SetRelativeWindowSize(0f, 0f, 0.1f, 0.11f);
63 }
64 else if (Position == 2)
65 {
66 SetRelativeWindowSize(0.9f, 0f, 1.001f, 0.11f);
67 }
68 else if (Position == 3)
69 {
70 SetRelativeWindowSize(0.9f, 0.89f, 1.001f, 1.001f);
71 }
72 Size = false;
73 LastSize = false;
74 }
75
76 if(LastSize != Size)
77 {
78 PerformanceScene scene = SceneHandler.Get<PerformanceScene>()!;
79 if (Size)
80 {
81 if (scene.Position < 2)
82 scene.SetRelativeWindowSize(scene.RelativeWindowSize.x, scene.RelativeWindowSize.y, scene.RelativeWindowSize.w + 0.14f, scene.RelativeWindowSize.h);
83 else
84 scene.SetRelativeWindowSize(scene.RelativeWindowSize.x - 0.14f, scene.RelativeWindowSize.y, scene.RelativeWindowSize.w, scene.RelativeWindowSize.h);
85 } else
86 {
87 if (scene.Position < 2)
88 scene.SetRelativeWindowSize(scene.RelativeWindowSize.x, scene.RelativeWindowSize.y, scene.RelativeWindowSize.w - 0.14f, scene.RelativeWindowSize.h);
89 else
90 scene.SetRelativeWindowSize(scene.RelativeWindowSize.x + 0.14f, scene.RelativeWindowSize.y, scene.RelativeWindowSize.w, scene.RelativeWindowSize.h);
91 }
92 }
93
94
95 //new Rectangle(new(0, 0, WindowSize.X, WindowSize.Y))
96 // .Color(new(0, 0, 0, 120))
97 // .Fill(true)
98 // .Render();
99
100 new UiBuilder(new Vector2(0, 0), Mouse.Position)
101 .Title("FPS")
102 .Text(((int)ProgramFPS).ToString() + " FPS")
103 .Title("Input")
104 .Text(((int)InputFPS).ToString() + " FPS")
105 .Render();
106
107 new UiBuilder(new Vector2(WindowSize.X / 2, 0), Mouse.Position)
108 .Title("Update")
109 .Text(((int)UpdateFPS).ToString() + " FPS")
110 .Render();
111
112 if(recentInputFPS.Count > 65)
113 {
114 recentInputFPS.RemoveAt(0);
115 recentProgramFPS.RemoveAt(0);
116
117 recentUpdateFPS.RemoveAt(0);
118 }
119
120 if(WindowSize.X > 450)
121 {
122 for(var i = 0; i < recentInputFPS.Count; i++)
123 {
124 new Rectangle(new(95 + i * 4, 10, 4, (recentProgramFPS[i] / recentProgramFPS.Max()) * 30))
125 .Color(UiStyles.ContainerIdleColor)
126 .Fill(true)
127 .Render();
128
129 new Rectangle(new(95 + i * 4, 65, 4, (recentInputFPS[i] / recentInputFPS.Max()) * 30))
130 .Color(UiStyles.ContainerIdleColor)
131 .Fill(true)
132 .Render();
133
134
135 new Rectangle(new(95 + i * 4 + WindowSize.X / 2, 10, 4, (recentUpdateFPS[i] / recentUpdateFPS.Max()) * 30))
136 .Color(UiStyles.ContainerIdleColor)
137 .Fill(true)
138 .Render();
139 }
140 }
141
142 LastPosition = Position;
143 LastSize = Size;
144
145 //Draw.Text(new(10, 10), Font.DefaultFont, Game.inputFPS.ToString(), 32, new(255, 255, 255, 255));
146 //Draw.Text(new(10, 10), Font.DefaultFont, Game.updateFPS.ToString(), 32, new(255, 255, 255, 255));
147 //Draw.Text(new(10, 10), Font.DefaultFont, Game.renderFPS.ToString(), 32, new(255, 255, 255, 255));
148 }
149}