Skip to content

Commit 34e62ac

Browse files
committed
Sample step -> draw -> updateUI
1 parent 349cd15 commit 34e62ac

5 files changed

Lines changed: 90 additions & 87 deletions

File tree

‎src/Box2D.NET.Samples/Draw.cs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ public class Draw
1818
private Camera _camera;
1919
public bool m_showUI;
2020

21-
public GLBackground m_background;
22-
public GLPoints m_points;
23-
public GLLines m_lines;
24-
public GLTriangles m_triangles;
25-
public GLCircles m_circles;
26-
public GLSolidCircles m_solidCircles;
27-
public GLSolidCapsules m_solidCapsules;
28-
public GLSolidPolygons m_solidPolygons;
21+
private GLBackground m_background;
22+
private GLPoints m_points;
23+
private GLLines m_lines;
24+
private GLTriangles m_triangles;
25+
private GLCircles m_circles;
26+
private GLSolidCircles m_solidCircles;
27+
private GLSolidCapsules m_solidCapsules;
28+
private GLSolidPolygons m_solidPolygons;
2929
public B2DebugDraw m_debugDraw;
3030

3131
public ImFontPtr m_smallFont;

‎src/Box2D.NET.Samples/SampleApp.cs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,15 @@ private unsafe void OnWindowRender(double dt)
298298
ImGui.Begin("Overlay", ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoInputs | ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoScrollbar);
299299
ImGui.End();
300300

301-
s_sample.Draw(s_settings);
302-
303-
_ctx.draw.Flush();
304-
305301
if (_ctx.draw.m_showUI)
306302
{
307303
var title = SampleFactory.Shared.GetTitle(s_settings.sampleIndex);
308304
s_sample.DrawTitle(title);
309305
}
310306

307+
s_sample.Draw(s_settings);
308+
_ctx.draw.Flush();
309+
311310
UpdateUI();
312311

313312
//ImGui.ShowDemoWindow();

‎src/Box2D.NET.Samples/Samples/Collisions/DynamicTree.cs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Box2D.NET.Samples.Samples.Collisions;
2222
public class DynamicTree : Sample
2323
{
2424
private static readonly int SampleDynamicTree = SampleFactory.Shared.RegisterSample("Collision", "Dynamic Tree", Create);
25-
25+
2626
private B2DynamicTree m_tree;
2727
private int m_rowCount, m_columnCount;
2828
private Proxy[] m_proxies;
@@ -105,12 +105,14 @@ public DynamicTree(SampleAppContext ctx, Settings settings) : base(ctx, settings
105105
m_validate = true;
106106
}
107107

108-
public void Release()
108+
public override void Dispose()
109109
{
110110
m_proxies = null;
111111
m_moveBuffer = null;
112112
b2DynamicTree_Destroy(m_tree);
113113
m_tree = null;
114+
115+
base.Dispose();
114116
}
115117

116118
void BuildTree()
@@ -185,7 +187,7 @@ void BuildTree()
185187
public override void UpdateUI()
186188
{
187189
base.UpdateUI();
188-
190+
189191
float height = 320.0f;
190192
ImGui.SetNextWindowPos(new Vector2(10.0f, m_context.camera.m_height - height - 50.0f), ImGuiCond.Once);
191193
ImGui.SetNextWindowSize(new Vector2(200.0f, height));
@@ -441,4 +443,4 @@ public override void Step(Settings settings)
441443

442444
m_timeStamp += 1;
443445
}
444-
}
446+
}

‎src/Box2D.NET.Samples/Samples/Sample.cs‎

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -372,35 +372,8 @@ public virtual void Step(Settings settings)
372372
{
373373
timeStep = 0.0f;
374374
}
375-
376-
if (m_context.draw.m_showUI)
377-
{
378-
m_context.draw.DrawString(5, m_textLine, "****PAUSED****");
379-
m_textLine += m_textIncrement;
380-
}
381375
}
382376

383-
m_context.draw.m_debugDraw.drawingBounds = m_context.camera.GetViewBounds();
384-
m_context.draw.m_debugDraw.useDrawingBounds = settings.useCameraBounds;
385-
386-
// todo testing
387-
// b2Transform t1 = {m_context.g_draw.m_debugDraw.drawingBounds.lowerBound, b2Rot_identity};
388-
// b2Transform t2 = {m_context.g_draw.m_debugDraw.drawingBounds.upperBound, b2Rot_identity};
389-
// m_context.g_draw.DrawSolidCircle(ref t1, b2Vec2_zero, 1.0f, {1.0f, 0.0f, 0.0f, 1.0f});
390-
// m_context.g_draw.DrawSolidCircle(ref t2, b2Vec2_zero, 1.0f, {1.0f, 0.0f, 0.0f, 1.0f});
391-
392-
m_context.draw.m_debugDraw.drawShapes = settings.drawShapes;
393-
m_context.draw.m_debugDraw.drawJoints = settings.drawJoints;
394-
m_context.draw.m_debugDraw.drawJointExtras = settings.drawJointExtras;
395-
m_context.draw.m_debugDraw.drawAABBs = settings.drawAABBs;
396-
m_context.draw.m_debugDraw.drawMass = settings.drawMass;
397-
m_context.draw.m_debugDraw.drawBodyNames = settings.drawBodyNames;
398-
m_context.draw.m_debugDraw.drawContacts = settings.drawContactPoints;
399-
m_context.draw.m_debugDraw.drawGraphColors = settings.drawGraphColors;
400-
m_context.draw.m_debugDraw.drawContactNormals = settings.drawContactNormals;
401-
m_context.draw.m_debugDraw.drawContactImpulses = settings.drawContactImpulses;
402-
m_context.draw.m_debugDraw.drawFrictionImpulses = settings.drawFrictionImpulses;
403-
404377
b2World_EnableSleeping(m_worldId, settings.enableSleep);
405378
b2World_EnableWarmStarting(m_worldId, settings.enableWarmStarting);
406379
b2World_EnableContinuous(m_worldId, settings.enableContinuous);
@@ -411,49 +384,11 @@ public virtual void Step(Settings settings)
411384
m_taskCount = 0;
412385
}
413386

414-
b2World_Draw(m_worldId, m_context.draw.m_debugDraw);
415-
416387
if (timeStep > 0.0f)
417388
{
418389
++m_stepCount;
419390
}
420391

421-
if (settings.drawCounters)
422-
{
423-
B2Counters s = b2World_GetCounters(m_worldId);
424-
425-
m_context.draw.DrawString(5, m_textLine, $"bodies/shapes/contacts/joints = {s.bodyCount}/{s.shapeCount}/{s.contactCount}/{s.jointCount}");
426-
m_textLine += m_textIncrement;
427-
428-
m_context.draw.DrawString(5, m_textLine, $"islands/tasks = {s.islandCount}/{s.taskCount}");
429-
m_textLine += m_textIncrement;
430-
431-
m_context.draw.DrawString(5, m_textLine, $"tree height static/movable = {s.staticTreeHeight}/{s.treeHeight}");
432-
m_textLine += m_textIncrement;
433-
434-
int totalCount = 0;
435-
var buffer = new StringBuilder();
436-
Debug.Assert(s.colorCounts.Length == 12);
437-
438-
// todo fix this
439-
buffer.Append("colors: ");
440-
for (int i = 0; i < 12; ++i)
441-
{
442-
buffer.Append($"{s.colorCounts[i]}/");
443-
totalCount += s.colorCounts[i];
444-
}
445-
446-
buffer.Append($"[{totalCount}]");
447-
m_context.draw.DrawString(5, m_textLine, buffer.ToString());
448-
m_textLine += m_textIncrement;
449-
450-
m_context.draw.DrawString(5, m_textLine, $"stack allocator size = {s.stackUsed / 1024} K");
451-
m_textLine += m_textIncrement;
452-
453-
m_context.draw.DrawString(5, m_textLine, $"total allocation = {s.byteCount / 1024} K");
454-
m_textLine += m_textIncrement;
455-
}
456-
457392
// Track maximum profile times
458393
{
459394
B2Profile p = b2World_GetProfile(m_worldId);
@@ -507,6 +442,73 @@ public virtual void Step(Settings settings)
507442

508443
public virtual void Draw(Settings settings)
509444
{
445+
if (settings.pause)
446+
{
447+
if (m_context.draw.m_showUI)
448+
{
449+
m_context.draw.DrawString(5, m_textLine, "****PAUSED****");
450+
m_textLine += m_textIncrement;
451+
}
452+
}
453+
454+
m_context.draw.m_debugDraw.drawingBounds = m_context.camera.GetViewBounds();
455+
m_context.draw.m_debugDraw.useDrawingBounds = settings.useCameraBounds;
456+
457+
// todo testing
458+
// b2Transform t1 = {m_context.g_draw.m_debugDraw.drawingBounds.lowerBound, b2Rot_identity};
459+
// b2Transform t2 = {m_context.g_draw.m_debugDraw.drawingBounds.upperBound, b2Rot_identity};
460+
// m_context.g_draw.DrawSolidCircle(ref t1, b2Vec2_zero, 1.0f, {1.0f, 0.0f, 0.0f, 1.0f});
461+
// m_context.g_draw.DrawSolidCircle(ref t2, b2Vec2_zero, 1.0f, {1.0f, 0.0f, 0.0f, 1.0f});
462+
463+
m_context.draw.m_debugDraw.drawShapes = settings.drawShapes;
464+
m_context.draw.m_debugDraw.drawJoints = settings.drawJoints;
465+
m_context.draw.m_debugDraw.drawJointExtras = settings.drawJointExtras;
466+
m_context.draw.m_debugDraw.drawAABBs = settings.drawAABBs;
467+
m_context.draw.m_debugDraw.drawMass = settings.drawMass;
468+
m_context.draw.m_debugDraw.drawBodyNames = settings.drawBodyNames;
469+
m_context.draw.m_debugDraw.drawContacts = settings.drawContactPoints;
470+
m_context.draw.m_debugDraw.drawGraphColors = settings.drawGraphColors;
471+
m_context.draw.m_debugDraw.drawContactNormals = settings.drawContactNormals;
472+
m_context.draw.m_debugDraw.drawContactImpulses = settings.drawContactImpulses;
473+
m_context.draw.m_debugDraw.drawFrictionImpulses = settings.drawFrictionImpulses;
474+
475+
b2World_Draw(m_worldId, m_context.draw.m_debugDraw);
476+
477+
if (settings.drawCounters)
478+
{
479+
B2Counters s = b2World_GetCounters(m_worldId);
480+
481+
m_context.draw.DrawString(5, m_textLine, $"bodies/shapes/contacts/joints = {s.bodyCount}/{s.shapeCount}/{s.contactCount}/{s.jointCount}");
482+
m_textLine += m_textIncrement;
483+
484+
m_context.draw.DrawString(5, m_textLine, $"islands/tasks = {s.islandCount}/{s.taskCount}");
485+
m_textLine += m_textIncrement;
486+
487+
m_context.draw.DrawString(5, m_textLine, $"tree height static/movable = {s.staticTreeHeight}/{s.treeHeight}");
488+
m_textLine += m_textIncrement;
489+
490+
int totalCount = 0;
491+
var buffer = new StringBuilder();
492+
Debug.Assert(s.colorCounts.Length == 12);
493+
494+
// todo fix this
495+
buffer.Append("colors: ");
496+
for (int i = 0; i < 12; ++i)
497+
{
498+
buffer.Append($"{s.colorCounts[i]}/");
499+
totalCount += s.colorCounts[i];
500+
}
501+
502+
buffer.Append($"[{totalCount}]");
503+
m_context.draw.DrawString(5, m_textLine, buffer.ToString());
504+
m_textLine += m_textIncrement;
505+
506+
m_context.draw.DrawString(5, m_textLine, $"stack allocator size = {s.stackUsed / 1024} K");
507+
m_textLine += m_textIncrement;
508+
509+
m_context.draw.DrawString(5, m_textLine, $"total allocation = {s.byteCount / 1024} K");
510+
m_textLine += m_textIncrement;
511+
}
510512
}
511513

512514
public void ShiftOrigin(B2Vec2 newOrigin)

‎test/Box2D.NET.Samples.Test/Samples/DynamicTreeTest.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ public void TestDynamicTree()
1111
var ctx = SampleAppContext.Create();
1212
var settings = Helpers.CreateSettings();
1313

14-
// using var testObject = new DynamicTree(ctx, settings);
15-
//
16-
// for (int i = 0; i < 37; ++i)
17-
// {
18-
// testObject.Step(settings);
19-
// }
14+
using var testObject = new DynamicTree(ctx, settings);
15+
16+
for (int i = 0; i < 37; ++i)
17+
{
18+
testObject.Step(settings);
19+
}
2020
}
2121
}

0 commit comments

Comments
 (0)