Skip to content

Commit f39fdd5

Browse files
committed
Documented slow-mo timestepping keybind and pulled it into the demo harness. LockMouse moved to the first control slot for visibility.
1 parent ed8b71a commit f39fdd5

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

Demos/Controls.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,14 @@ public struct Controls
220220
public float CameraMoveSpeed;
221221
public float CameraFastMoveSpeed;
222222

223+
public HoldableBind SlowTimesteps;
223224
public InstantBind LockMouse;
224225
public InstantBind Exit;
225226
public InstantBind ShowConstraints;
226227
public InstantBind ShowContacts;
227228
public InstantBind ShowBoundingBoxes;
228229
public InstantBind ChangeTimingDisplayMode;
229-
public HoldableBind ChangeDemo;
230+
public InstantBind ChangeDemo;
230231
public InstantBind ShowControls;
231232

232233
public static Controls Default
@@ -247,6 +248,7 @@ public static Controls Default
247248
CameraSlowMoveSpeed = 0.5f,
248249
CameraMoveSpeed = 5,
249250
CameraFastMoveSpeed = 50,
251+
SlowTimesteps = MouseButton.Middle,
250252

251253
LockMouse = Key.Tab,
252254
Exit = Key.Escape,

Demos/Demo.cs

+5-16
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,13 @@ protected Demo()
3535
}
3636

3737
public abstract void Initialize(Camera camera);
38-
39-
int frameCount = 0;
38+
4039
public virtual void Update(Input input, float dt)
4140
{
42-
++frameCount;
43-
//If we're running headless, there is no input.
44-
if (input != null && input.IsDown(OpenTK.Input.MouseButton.Middle))
45-
{
46-
if (frameCount % 20 == 0)
47-
Simulation.Timestep(1 / 60f, ThreadDispatcher);
48-
}
49-
else
50-
{
51-
//TODO: While for the sake of the demos, using one update per render is probably the easiest/best choice,
52-
//we can't assume that every monitor has a 60hz refresh rate. One simple option here is to just measure the primary display's refresh rate ahead of time
53-
//and use that as the simulation timestep duration. Different displays would affect the simulation, but it wouldn't be too bad, and it would be locally consistent.
54-
Simulation.Timestep(1 / 60f, ThreadDispatcher);
55-
}
41+
//TODO: While for the sake of the demos, using one update per render is probably the easiest/best choice,
42+
//we can't assume that every monitor has a 60hz refresh rate. One simple option here is to just measure the primary display's refresh rate ahead of time
43+
//and use that as the simulation timestep duration. Different displays would affect the simulation, but it wouldn't be too bad, and it would be locally consistent.
44+
Simulation.Timestep(1 / 60f, ThreadDispatcher);
5645
}
5746

5847
public virtual void Render(Renderer renderer, TextBuilder text, Font font)

Demos/DemoHarness.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class DemoHarness : IDisposable
2020
bool showConstraints = true;
2121
bool showContacts;
2222
bool showBoundingBoxes;
23+
int frameCount;
2324

2425
enum TimingDisplayMode
2526
{
@@ -259,7 +260,11 @@ public void Update(float dt)
259260
{
260261
input.MouseLocked = false;
261262
}
262-
demo.Update(input, dt);
263+
++frameCount;
264+
if (!controls.SlowTimesteps.IsDown(input) || frameCount % 20 == 0)
265+
{
266+
demo.Update(input, dt);
267+
}
263268
timeSamples.RecordFrame(demo.Simulation);
264269
}
265270

@@ -279,7 +284,7 @@ public void Render(Renderer renderer)
279284
if (showControls)
280285
{
281286
var penPosition = new Vector2(window.Resolution.X - textHeight * 6 - 25, window.Resolution.Y - 25);
282-
penPosition.Y -= 16 * lineSpacing;
287+
penPosition.Y -= 17 * lineSpacing;
283288
uiText.Clear().Append("Controls: ");
284289
var headerHeight = textHeight * 1.2f;
285290
renderer.TextBatcher.Write(uiText, penPosition - new Vector2(0.5f * GlyphBatch.MeasureLength(uiText, font, headerHeight), 0), headerHeight, textColor, font);
@@ -300,6 +305,7 @@ void WriteName(string controlName, string control)
300305
}
301306

302307
//Conveniently, enum strings are cached. Every (Key).ToString() returns the same reference for the same key, so no garbage worries.
308+
WriteName(nameof(controls.LockMouse), controls.LockMouse.ToString());
303309
WriteName(nameof(controls.MoveForward), controls.MoveForward.ToString());
304310
WriteName(nameof(controls.MoveBackward), controls.MoveBackward.ToString());
305311
WriteName(nameof(controls.MoveLeft), controls.MoveLeft.ToString());
@@ -308,7 +314,7 @@ void WriteName(string controlName, string control)
308314
WriteName(nameof(controls.MoveDown), controls.MoveDown.ToString());
309315
WriteName(nameof(controls.MoveSlower), controls.MoveSlower.ToString());
310316
WriteName(nameof(controls.MoveFaster), controls.MoveFaster.ToString());
311-
WriteName(nameof(controls.LockMouse), controls.LockMouse.ToString());
317+
WriteName(nameof(controls.SlowTimesteps), controls.SlowTimesteps.ToString());
312318
WriteName(nameof(controls.Exit), controls.Exit.ToString());
313319
WriteName(nameof(controls.ShowConstraints), controls.ShowConstraints.ToString());
314320
WriteName(nameof(controls.ShowContacts), controls.ShowContacts.ToString());

0 commit comments

Comments
 (0)