Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 30 additions & 90 deletions src/Box2D.NET.Samples/SampleApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,18 @@ public int Run(string[] args)
Monitor* primaryMonitor = _context.glfw.GetPrimaryMonitor();
if (null != primaryMonitor)
{
float contentScale = 1.0f;
_context.glfw.GetMonitorContentScale(primaryMonitor, out contentScale, out contentScale);

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
_context.glfw.GetMonitorContentScale(primaryMonitor, out s_framebufferScale, out s_framebufferScale);
_context.uiScale = 1.0f;
s_framebufferScale = contentScale;
}
else
{
float uiScale = 1.0f;
_context.glfw.GetMonitorContentScale(primaryMonitor, out uiScale, out uiScale);
_context.uiScale = uiScale;
_context.uiScale = contentScale;
s_framebufferScale = 1.0f;
}
}
}
Expand Down Expand Up @@ -288,26 +291,27 @@ private void OnWindowUpdate(double dt)

int bufferWidth = 0;
int bufferHeight = 0;
double cursorPosX = 0.0d;
double cursorPosY = 0.0d;
unsafe
{
_context.glfw.GetFramebufferSize(_context.window, out bufferWidth, out bufferHeight);

// _ctx.draw.DrawBackground();

_context.glfw.GetCursorPos(_context.window, out cursorPosX, out cursorPosY);
}

// ImGui_ImplGlfw_CursorPosCallback(_ctx.g_mainWindow, cursorPosX / s_windowScale, cursorPosY / s_windowScale);
// ImGui_ImplOpenGL3_NewFrame();
// ImGui_ImplGlfw_NewFrame();
// ImGui_ImplGlfw_CursorPosCallback(_ctx.g_mainWindow, cursorPosX / s_windowScale, cursorPosY / s_windowScale);
if (null != _imgui)
{
var io = ImGui.GetIO();
io.DisplaySize = new Vector2(_context.camera.width, _context.camera.height);
io.DisplayFramebufferScale = new Vector2(bufferWidth / (float)_context.camera.width, bufferHeight / (float)_context.camera.height);

// These can be zero if the window is minimized
if (_context.camera.width > 0.0f && _context.camera.height > 0.0f)
{
// Framebuffer/window ratio: 1 on Windows/Linux, 2 on a Retina display. Drives
// both UI magnification and font rasterizer density.
io.DisplayFramebufferScale = new Vector2(bufferWidth / (float)_context.camera.width,
bufferHeight / (float)_context.camera.height);
}

io.DeltaTime = (float)dt;
_imgui.Update((float)dt);
}
Expand All @@ -319,20 +323,6 @@ private void OnWindowUpdate(double dt)
}

_context.sample.ResetText();

if (_context.showUI)
{
_context.sample.DrawColoredTextLine(B2HexColor.b2_colorGoldenRod, SampleFactory.Shared.GetName(_context.sampleIndex));
_context.sample.DrawColoredTextLine(B2HexColor.b2_colorLightGray, SampleFactory.Shared.GetCategory(_context.sampleIndex));
_context.sample.DrawTextLine("");
_context.sample.DrawColoredTextLine(B2HexColor.b2_colorSeaGreen, $"{1000.0f * _frameTime:0.0} ms");
_context.sample.DrawColoredTextLine(B2HexColor.b2_colorSeaGreen, $"step {_context.sample.m_stepCount}");
_context.sample.DrawTextLine("");
_context.sample.DrawColoredTextLine(B2HexColor.b2_colorSeaGreen,
$"cam ({_context.camera.center.X:0.0}, {_context.camera.center.Y:0.0})");
_context.sample.DrawColoredTextLine(B2HexColor.b2_colorSeaGreen, $"zoom {_context.camera.zoom:0.00}");
}

_context.sample.Step();
_context.sample.Draw();
}
Expand All @@ -353,18 +343,9 @@ private void OnWindowRender(double dt)
{
_context.gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

if (_context.showUI == false)
{
float fontSize = ImGui.GetFontSize();
DrawScreenString(_context.draw, 5.0f, 1.5f * fontSize, B2HexColor.b2_colorYellow,
$"{SampleFactory.Shared.GetCategory(_context.sampleIndex)} : {SampleFactory.Shared.GetName(_context.sampleIndex)}");
DrawScreenString(_context.draw, 5.0f, _context.camera.height - 0.5f * fontSize, B2HexColor.b2_colorSeaGreen,
$"{1000.0f * _frameTime:0.0} ms step {_context.sample.m_stepCount}");
}

FlushDraw(_context.draw, _context.camera);

UpdateSampleUI(_context);
DrawUI(_context, _frameTime);

//ImGui.ShowDemoWindow();

Expand Down Expand Up @@ -541,7 +522,7 @@ private void CreateUI(string glslVersion)
// }
//

if (_context.uiScale != 1.0f)
if (_context.uiScale != 1.0f || s_framebufferScale != 1.0f)
{
// ImGui.NET 1.90 does not expose AddFontDefaultVector, so use the existing font as an embedded resource.
using Stream stream = typeof(SampleApp).Assembly.GetManifestResourceStream("Box2D.NET.Samples.Fonts.droid_sans.ttf")!;
Expand All @@ -556,8 +537,11 @@ private void CreateUI(string glslVersion)
{
ApplyUIStyle();

ImGuiStylePtr style = ImGui.GetStyle();
style.ScaleAllSizes(_context.uiScale);

ImGuiIOPtr io = ImGui.GetIO();
if (_context.uiScale == 1.0f)
if (_context.uiScale == 1.0f && s_framebufferScale == 1.0f)
{
io.Fonts.AddFontDefault();
}
Expand All @@ -566,21 +550,15 @@ private void CreateUI(string glslVersion)
unsafe
{
ImFontConfigPtr fontConfig = new ImFontConfigPtr(ImGuiNative.ImFontConfig_ImFontConfig());
// This brightens the font, improving readability when it is small.
fontConfig.RasterizerMultiply = _context.uiScale * s_framebufferScale;
fontConfig.FontDataOwnedByAtlas = false;

float regularSize = MathF.Floor(13.0f * _context.uiScale);
float regularSize = MathF.Floor(13.0f * _context.uiScale * s_framebufferScale);
io.Fonts.AddFontFromMemoryTTF(_fontDataHandle.AddrOfPinnedObject(), _fontData.Length, regularSize, fontConfig);
io.FontGlobalScale = 1.0f / s_framebufferScale;
ImGuiNative.ImFontConfig_destroy(fontConfig.NativePtr);
}
}
});

if (_context.uiScale != 1.0f)
{
ImGui.GetStyle().ScaleAllSizes(_context.uiScale);
}
}

public void DestroyUI()
Expand Down Expand Up @@ -619,58 +597,20 @@ private unsafe void KeyCallback(WindowHandle* window, Keys key, int scancode, In

case Keys.Left:
// Pan left
if (0 != ((uint)mods & (uint)KeyModifiers.Control))
{
B2Vec2 newOrigin = new B2Vec2(2.0f, 0.0f);
_context.sample.ShiftOrigin(newOrigin);
}
else
{
_context.camera.center.X -= 0.5f;
}

_context.camera.center.X -= 0.5f;
break;

case Keys.Right:
// Pan right
if (0 != ((uint)mods & (uint)KeyModifiers.Control))
{
B2Vec2 newOrigin = new B2Vec2(-2.0f, 0.0f);
_context.sample.ShiftOrigin(newOrigin);
}
else
{
_context.camera.center.X += 0.5f;
}

_context.camera.center.X += 0.5f;
break;

case Keys.Down:
// Pan down
if (0 != ((uint)mods & (uint)KeyModifiers.Control))
{
B2Vec2 newOrigin = new B2Vec2(0.0f, 2.0f);
_context.sample.ShiftOrigin(newOrigin);
}
else
{
_context.camera.center.Y -= 0.5f;
}

_context.camera.center.Y -= 0.5f;
break;

case Keys.Up:
// Pan up
if (0 != ((uint)mods & (uint)KeyModifiers.Control))
{
B2Vec2 newOrigin = new B2Vec2(0.0f, -2.0f);
_context.sample.ShiftOrigin(newOrigin);
}
else
{
_context.camera.center.Y += 0.5f;
}

_context.camera.center.Y += 0.5f;
break;

case Keys.Home:
Expand Down Expand Up @@ -730,7 +670,7 @@ private unsafe void KeyCallback(WindowHandle* window, Keys key, int scancode, In
break;

case Keys.M:
_context.showDiagnostics = !_context.showDiagnostics;
_context.showMetrics = !_context.showMetrics;
break;

default:
Expand Down
4 changes: 2 additions & 2 deletions src/Box2D.NET.Samples/SampleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class SampleContext
public bool showUI = true;

// Diagnostics drawer visibility. D toggles.
public bool showDiagnostics = false;
public bool showMetrics = false;

// Set by Ctrl+O; consumed by UpdateSampleUI to open the fuzzy sample picker.
public bool openSamplePicker = false;
Expand Down Expand Up @@ -95,7 +95,7 @@ public void Load()
sampleIndex = settings.sampleIndex;
debugDraw.drawShapes = settings.drawShapes;
debugDraw.drawJoints = settings.drawJoints;
showDiagnostics = settings.showDiagnostics;
showMetrics = settings.showDiagnostics;
}


Expand Down
4 changes: 2 additions & 2 deletions src/Box2D.NET.Samples/SampleText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Box2D.NET.Samples;

public static class SampleText
{
public static string FormatFloat(float value)
public static string FormatFloat(float value, int precision = 6)
{
if (float.IsNaN(value))
{
Expand All @@ -25,6 +25,6 @@ public static string FormatFloat(float value)
}

// C printf("%g") defaults to six significant digits and is not affected by the current .NET culture.
return value.ToString("g6", CultureInfo.InvariantCulture);
return value.ToString($"g{precision}", CultureInfo.InvariantCulture);
}
}
4 changes: 3 additions & 1 deletion src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkBarrel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ void CreateScene()
}
}

public override void BuildSamplePanel()
public override bool DrawControls()
{


Expand All @@ -327,5 +327,7 @@ public override void BuildSamplePanel()
}



return true;
}
}
13 changes: 8 additions & 5 deletions src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkCast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using static Box2D.NET.B2Timers;
using static Box2D.NET.B2Distances;
using static Box2D.NET.Samples.Graphics.Draws;
using static Box2D.NET.Samples.SampleText;

namespace Box2D.NET.Samples.Samples.Benchmarks;

Expand All @@ -32,7 +33,7 @@
private float m_buildTime;

private int m_rowCount, m_columnCount;
private int m_updateType;

Check warning on line 36 in src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkCast.cs

View workflow job for this annotation

GitHub Actions / test-windows-latest-9

The field 'BenchmarkCast.m_updateType' is never used

Check warning on line 36 in src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkCast.cs

View workflow job for this annotation

GitHub Actions / test-macos-latest-9

The field 'BenchmarkCast.m_updateType' is never used
private int m_drawIndex;
private float m_radius;
private float m_fill;
Expand Down Expand Up @@ -321,7 +322,7 @@
}
}

public override void BuildSamplePanel()
public override bool DrawControls()
{


Expand Down Expand Up @@ -387,25 +388,27 @@
{
BuildScene();
}

return true;
}

public override void Draw()
{
base.Draw();

DrawTextLine($"build time ms = {m_buildTime:g}");
DrawScreenTextLine($"build time ms = {FormatFloat(m_buildTime)}");


DrawScreenTextLine($"hit count = {hitCount}, node visits = {nodeVisits}, leaf visits = {leafVisits}");


DrawTextLine($"total ms = {ms:F3}");
DrawScreenTextLine(System.FormattableString.Invariant($"total ms = {ms:F3}"));


DrawTextLine($"min total ms = {m_minTime:F3}");
DrawScreenTextLine(System.FormattableString.Invariant($"min total ms = {m_minTime:F3}"));


float aveRayCost = 1000.0f * m_minTime / (float)sampleCount;
DrawTextLine($"average us = {aveRayCost:F2}");
DrawScreenTextLine(System.FormattableString.Invariant($"average us = {aveRayCost:F2}"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void CreateScene()
m_bodyIndex = 0;
}

public override void BuildSamplePanel()
public override bool DrawControls()
{

ImGui.PushItemWidth(6.0f * ImGui.GetFontSize());
Expand All @@ -163,6 +163,8 @@ public override void BuildSamplePanel()

ImGui.PopItemWidth();


return true;
}

public override void Step()
Expand All @@ -189,4 +191,4 @@ public override void Step()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ public override void Dispose()
m_outputs = null;
}

public override void BuildSamplePanel()
public override bool DrawControls()
{
ImGui.PushItemWidth(6.0f * ImGui.GetFontSize());
ImGui.SliderInt("draw index", ref m_drawIndex, 0, m_count - 1);
ImGui.PopItemWidth();

return true;
}

public override void Step()
Expand Down Expand Up @@ -139,9 +141,9 @@ public override void Draw()

if (m_context.pause == false || m_context.singleStep == true)
{
DrawTextLine($"count = {m_count}");
DrawScreenTextLine($"count = {m_count}");
DrawScreenTextLine($"min ms = {FormatFloat(m_minMilliseconds)}, ave us = {FormatFloat(1000.0f * m_minMilliseconds / (float)m_count)}");
DrawTextLine($"average iterations = {totalIterations / (float)m_count}");
DrawScreenTextLine($"average iterations = {FormatFloat(totalIterations / (float)m_count)}");
}

B2Transform xfA = m_transformAs[m_drawIndex];
Expand All @@ -153,6 +155,6 @@ public override void Draw()
DrawPoint(m_draw, output.pointA, 10.0f, B2HexColor.b2_colorWhite);
DrawPoint(m_draw, output.pointB, 10.0f, B2HexColor.b2_colorWhite);
DrawLine(m_draw, output.pointA, output.pointA + 0.5f * output.normal, B2HexColor.b2_colorYellow);
DrawTextLine($"distance = {output.distance}");
DrawScreenTextLine($"distance = {FormatFloat(output.distance)}");
}
}
Loading
Loading