Skip to content

Commit 9f8bd26

Browse files
"resize was laggy if framerate was to high, it was too high, because vsync wasn't working, so no we seep ourselves :)
1 parent 1cbf09e commit 9f8bd26

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
**/*.received.cs
77

88
Avalonia
9+
*.trace
910

1011
# User-specific files
1112
*.rsuser

Flamui/FlamuiApp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void CreateWindow<TRootComponent>(string title, FlamuiWindowOptions? opti
4949
{
5050
Size = new Vector2D<int>(options.Width, options.Height),
5151
Title = title,
52-
VSync = true,
52+
VSync = false, //for some reason this doesn't work on my laptop, so we just sleep ourselves
5353
ShouldSwapAutomatically = false
5454
};
5555

Flamui/UiWindow.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ private void OnRender(double obj)
9292
if (!isInitialized)
9393
return;
9494

95+
var start = Stopwatch.GetTimestamp();
96+
9597
using var _ = Systrace.BeginEvent(nameof(OnRender));
9698

9799
Render();
@@ -105,6 +107,13 @@ private void OnRender(double obj)
105107
OldHoveredElements.Add(uiContainer);
106108
}
107109
HoveredElements.Clear();
110+
111+
var end = Stopwatch.GetElapsedTime(start);
112+
if (end.TotalMilliseconds < 16) //todo we should probably also detect the refresh rate of monitor, to know how long to sleep for (or we can try to get vsync working)s
113+
{
114+
// Console.WriteLine($"Sleeping for {end.TotalMilliseconds}");
115+
Thread.Sleep(TimeSpan.FromMilliseconds(16 - end.TotalMilliseconds));
116+
}
108117
}
109118

110119
private void OnUpdate(double obj)
@@ -321,6 +330,7 @@ private void RenderToCanvas()
321330

322331
if (!RenderContextAreSame(RenderContext, LastRenderContext))
323332
{
333+
_renderer.Gl.Viewport(Window.Size);
324334
RenderContext.Rerender(_renderer);
325335
Window.GLContext.SwapBuffers();
326336
}
@@ -427,6 +437,8 @@ private void BuildUi()
427437

428438
_rootComponent.Build(Ui);
429439

440+
Console.WriteLine(Window.Size.X);
441+
430442
RootContainer.PrepareLayout(Dir.Vertical);
431443
RootContainer.Layout(new BoxConstraint(0, Window.Size.X / GetCompleteScaling().X, 0, Window.Size.Y / GetCompleteScaling().Y));
432444
}

SilkNetTest/Program.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Silk.NET.Input;
2+
using Silk.NET.Maths;
3+
using Silk.NET.Windowing;
4+
using Silk.NET.OpenGL;
5+
using System.Drawing;
6+
7+
namespace MySilkProgram;
8+
9+
public class Program
10+
{
11+
private static IWindow _window;
12+
private static GL _gl;
13+
14+
public static void Main(string[] args)
15+
{
16+
WindowOptions options = WindowOptions.Default;
17+
options.Size = new Vector2D<int>(800, 600);
18+
options.Title = "My first Silk.NET program!";
19+
options.VSync = true;
20+
21+
_window = Window.Create(options);
22+
23+
_window.Load += OnLoad;
24+
_window.Update += OnUpdate;
25+
_window.Render += OnRender;
26+
27+
_window.Run();
28+
}
29+
30+
private static void OnLoad()
31+
{
32+
Console.WriteLine("Load!");
33+
34+
IInputContext input = _window.CreateInput();
35+
for (int i = 0; i < input.Keyboards.Count; i++)
36+
input.Keyboards[i].KeyDown += KeyDown;
37+
38+
_gl = _window.CreateOpenGL();
39+
_gl.ClearColor(Color.CornflowerBlue);
40+
}
41+
42+
// These two methods are unused for this tutorial, aside from the logging we added earlier.
43+
private static void OnUpdate(double deltaTime)
44+
{
45+
}
46+
47+
private static void OnRender(double deltaTime)
48+
{
49+
_gl.Clear(ClearBufferMask.ColorBufferBit);
50+
}
51+
52+
private static void KeyDown(IKeyboard keyboard, Key key, int keyCode)
53+
{
54+
if (key == Key.Escape)
55+
_window.Close();
56+
}
57+
}

SilkNetTest/SilkNetTest.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Silk.NET.Input" Version="2.22.0" />
12+
<PackageReference Include="Silk.NET.OpenGL" Version="2.22.0" />
13+
<PackageReference Include="Silk.NET.Windowing" Version="2.22.0" />
14+
</ItemGroup>
15+
16+
</Project>

0 commit comments

Comments
 (0)