Add F1 debug menu with paused diagnostics and scene controls

This commit is contained in:
UnixNight committed 2026-10-25 01:49:36 -04:00
1 parent 8d60a68511
commit 3078b8afd0
21 files changed
+809 -36

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
using System.Globalization;
using System.Numerics;
using GamePort.Assets;
namespace GamePort.Application;
public sealed record DebugScene(string LevelFile, string Code, string[] Starts);
public enum DebugAction { None, Resume, Camera, SkipCutscene, RefillEnergy, GiveJetpack, ApplySceneTask, LoadScene, Reset }
public sealed record DebugMenuView(string[] Lines, int SelectedLine);
public sealed record DebugStatus(string Level, string Start, Vector3 Root, float Yaw, float Speed,
bool Grounded, float Energy, int Score, int SceneTask, string Cutscene, bool Controlled,
int Issues, float FarPlane, bool Inspecting);
/// <summary>Modern investigation tools, deliberately separate from recovered game UI.</summary>
public sealed class DebugMenu
{
public const uint ToggleKey = 58; // SDL scancode F1; F11 remains fullscreen.
public const int ItemCount = 10;
public bool Open { get; private set; }
public int Selected { get; private set; }
public int SceneTask { get; private set; }
public string Message { get; set; } = "";
private readonly DebugScene[] scenes;
private int sceneIndex, startIndex;
public DebugScene? Scene => scenes.Length == 0 ? null : scenes[sceneIndex];
public string? Start => Scene is { } scene ? scene.Starts[startIndex] : null;
public DebugMenu(IEnumerable<DebugScene> scenes, string? currentLevel, string? currentStart)
{
this.scenes = scenes.ToArray();
if (this.scenes.Any(s => s.Starts.Length == 0)) throw new ArgumentException("Debug scenes require a startpoint");
sceneIndex = Math.Max(0, Array.FindIndex(this.scenes,
s => string.Equals(s.LevelFile, currentLevel, StringComparison.OrdinalIgnoreCase)));
if (Scene is { } selected)
startIndex = Math.Max(0, Array.FindIndex(selected.Starts,
s => string.Equals(s, currentStart, StringComparison.OrdinalIgnoreCase)));
}
public void Toggle(int currentSceneTask)
{
Open = !Open;
if (Open) { SceneTask = currentSceneTask; Message = ""; }
}
public void Close() => Open = false;
public DebugAction Key(uint scancode, bool repeat = false)
{
if (!Open) return DebugAction.None;
if (scancode == 41) { Close(); return DebugAction.None; }
if (scancode is 82 or 81)
Selected = (Selected + (scancode == 82 ? ItemCount - 1 : 1)) % ItemCount;
else if (scancode is 80 or 79 or 75 or 78)
{
var direction = scancode is 80 or 78 ? -1 : 1;
if (Selected == 5)
SceneTask = (int)Math.Clamp((long)SceneTask + direction * (scancode is 75 or 78 ? 10 : 1), 0, 999);
else if (Selected == 6 && scenes.Length > 0)
{ sceneIndex = (sceneIndex + scenes.Length + direction) % scenes.Length; startIndex = 0; }
else if (Selected == 7 && Scene is { } scene)
startIndex = (startIndex + scene.Starts.Length + direction) % scene.Starts.Length;
}
else if (scancode is 40 or 88 && !repeat)
return Selected switch
{
0 => DebugAction.Resume, 1 => DebugAction.Camera, 2 => DebugAction.SkipCutscene,
3 => DebugAction.RefillEnergy, 4 => DebugAction.GiveJetpack, 5 => DebugAction.ApplySceneTask,
8 => Scene == null ? DebugAction.None : DebugAction.LoadScene, 9 => DebugAction.Reset,
_ => DebugAction.None
};
return DebugAction.None;
}
public DebugMenuView View(DebugStatus status)
{
if (!Open) return new([], -1);
var c = CultureInfo.InvariantCulture;
string F(float value) => value.ToString("0.0", c);
return new([
"NEUTRON DEBUG MENU - F1",
"PAUSED - MODERN DEBUG TOOLS",
$"LEVEL {status.Level} / START {status.Start}",
$"ROOT X {F(status.Root.X)} Y {F(status.Root.Y)} Z {F(status.Root.Z)}",
$"YAW {F(status.Yaw)} SPEED {F(status.Speed)} GROUNDED {status.Grounded}",
$"ENERGY {F(status.Energy)} SCORE {status.Score} SCENE {status.SceneTask}",
$"CONTROL {status.Controlled} CUTSCENE {status.Cutscene}",
$"ISSUES {status.Issues} FAR {F(status.FarPlane)} CAMERA {(status.Inspecting ? "ORBIT" : "GAMEPLAY")}",
"",
"RESUME",
$"CAMERA: {(status.Inspecting ? "SWITCH TO GAMEPLAY" : "SWITCH TO ORBIT")}",
"SKIP CURRENT CUTSCENE",
"REFILL ENERGY TO 100",
"GIVE / SELECT JET PACK",
$"SCENE TASK: {SceneTask} (ENTER APPLIES)",
$"LEVEL: {Scene?.LevelFile ?? "UNAVAILABLE"}",
$"START: {Start ?? "UNAVAILABLE"}",
"LOAD SELECTED LEVEL / START",
"RESET CURRENT START",
"",
"UP/DOWN SELECT LEFT/RIGHT CHANGE ENTER APPLY",
"SCENE TASK: PAGE UP/DOWN CHANGES BY 10",
"F1 / ESC CLOSE - GAME INPUT IS CAPTURED WHILE OPEN",
Message
], 9 + Selected);
}
public static DebugScene[] AvailableScenes(string assets, string prepared)
{
var scenes = new List<DebugScene>();
foreach (var path in Directory.EnumerateFiles(AssetPath.Resolve(assets, "gam"))
.Where(p => p.EndsWith(".gam", StringComparison.OrdinalIgnoreCase)).OrderBy(Path.GetFileName, StringComparer.OrdinalIgnoreCase))
{
try { AssetPath.Resolve(prepared, Path.GetFileNameWithoutExtension(path) + "/scene.json"); }
catch (FileNotFoundException) { continue; }
var level = GamLevel.Load(path);
var starts = level.Entities.Where(e => e.Kind == "STRT" && e.Tag is { Length: > 0 }).Select(e => e.Tag!).ToArray();
if (level.Player != null && starts.Length > 0) scenes.Add(new(Path.GetFileName(path), level.Code, starts));
}
return scenes.ToArray();
}
}
@@ -0,0 +1,164 @@
using System.Numerics;
using GamePort.Assets;
namespace GamePort.Application;
/// <summary>
/// Modern developer overlay, separate from recovered game UI. Uses a code-built
/// 5x7 font and the existing alpha-tested screen-space pipeline; no asset files.
/// Allocate once before renderer creation, then update the final merged mesh.
/// </summary>
public sealed class DebugMenuOverlay
{
public const int MaxLines = 32;
public const int MaxColumns = 76;
private const float LineHeight = 13;
private const float CharacterAdvance = 7.5f;
private const int AtlasWidth = 128, AtlasHeight = 64;
private readonly int firstVertex;
private readonly uint firstIndex;
private readonly int textureId;
public Mesh Mesh { get; }
public DebugMenuOverlay(Mesh combined)
{
ArgumentNullException.ThrowIfNull(combined);
firstVertex = combined.Vertices.Length;
firstIndex = (uint)combined.Indices.Length;
var vertices = combined.Vertices.ToList();
var indices = combined.Indices.ToList();
var draws = combined.Draws.ToList();
if (!combined.HasExplicitDraws && draws.Count == 0 && indices.Count > 0)
draws.Add(new(0, (uint)indices.Count, -1));
var textures = new Dictionary<int, PngImage>(combined.Textures);
textureId = int.MinValue;
while (textures.ContainsKey(textureId)) textureId++;
textures.Add(textureId, CreateFont());
// Two solid quads precede a fixed range for each text row. Keeping the
// ranges fixed means the Vulkan vertex/index buffers never need resizing.
AddRange(1);
AddRange(1);
for (var row = 0; row < MaxLines; row++) AddRange(MaxColumns);
Mesh = new(vertices.ToArray(), indices.ToArray())
{
Draws = draws.ToArray(), HasExplicitDraws = true,
Textures = textures, CollisionPolygons = combined.CollisionPolygons
};
void AddRange(int quads)
{
var start = (uint)indices.Count;
for (var i = 0; i < quads; i++)
{
var vertex = (uint)vertices.Count;
for (var corner = 0; corner < 4; corner++) vertices.Add(new(Vector3.Zero, Vector2.Zero, Vector3.One));
indices.AddRange([vertex, vertex + 1, vertex + 2, vertex, vertex + 2, vertex + 3]);
}
draws.Add(new(start, (uint)quads * 6, textureId) { ScreenSpace = true, Visible = false });
}
}
/// <summary>
/// Empty lines hide all overlay draws. Accepts at most32 lines; individual
/// lines clip at76 characters. selectedLine is -1 or an index into lines.
/// Lowercase renders as uppercase, unsupported characters as question marks.
/// Layout uses the same640x480 reference viewport as the original HUD.
/// </summary>
public void Update(Mesh destination, IReadOnlyList<string> lines, int selectedLine = -1)
{
ArgumentNullException.ThrowIfNull(destination);
ArgumentNullException.ThrowIfNull(lines);
if (lines.Count > MaxLines) throw new ArgumentOutOfRangeException(nameof(lines));
if (selectedLine < -1 || selectedLine >= lines.Count) throw new ArgumentOutOfRangeException(nameof(selectedLine));
if (destination.Vertices.Length < Mesh.Vertices.Length || destination.Indices.Length < Mesh.Indices.Length)
throw new ArgumentException("Destination omits debug overlay ranges", nameof(destination));
var background = FindDraw(firstIndex, 6);
var highlight = FindDraw(firstIndex + 6, 6);
background.Visible = lines.Count > 0;
highlight.Visible = selectedLine >= 0;
var solid = new Vector2(127.5f / AtlasWidth, 63.5f / AtlasHeight);
Quad(firstVertex, 18, 18, 604, Math.Clamp(lines.Count * LineHeight + 24, 80, 444), solid, solid, new(.025f, .04f, .07f));
Quad(firstVertex + 4, 25, 27 + selectedLine * LineHeight, 590, LineHeight,
solid, solid, new(.07f, .24f, .33f));
for (var row = 0; row < MaxLines; row++)
{
var draw = FindDraw(firstIndex + 12 + (uint)(row * MaxColumns * 6), MaxColumns * 6);
draw.Visible = row < lines.Count;
if (!draw.Visible) continue;
var line = lines[row] ?? "";
var color = row == 0 ? new Vector3(1, .83f, .35f) : new(.85f, .93f, .98f);
for (var column = 0; column < MaxColumns; column++)
{
var character = column < line.Length ? char.ToUpperInvariant(line[column]) : ' ';
if (character is < ' ' or > '~') character = '?';
var cell = character - ' ';
var x = cell % 16 * 8 + 1;
var y = cell / 16 * 8;
Quad(firstVertex + 8 + (row * MaxColumns + column) * 4,
30 + column * CharacterAdvance, 30 + row * LineHeight, 7, 9.8f,
new((float)x / AtlasWidth, (float)y / AtlasHeight),
new((x + 5f) / AtlasWidth, (y + 7f) / AtlasHeight), color);
}
}
MeshDraw FindDraw(uint start, uint count) => destination.Draws.FirstOrDefault(d =>
d.FirstIndex == start && d.IndexCount == count && d.TextureId == textureId && d.ScreenSpace)
?? throw new ArgumentException("Destination omits debug overlay draws", nameof(destination));
void Quad(int start, float x, float y, float width, float height, Vector2 minUv, Vector2 maxUv, Vector3 color)
{
destination.Vertices[start] = new(Position(x, y), minUv, color);
destination.Vertices[start + 1] = new(Position(x + width, y), new(maxUv.X, minUv.Y), color);
destination.Vertices[start + 2] = new(Position(x + width, y + height), maxUv, color);
destination.Vertices[start + 3] = new(Position(x, y + height), new(minUv.X, maxUv.Y), color);
}
}
private static Vector3 Position(float x, float y) => new(2 * x / 640 - 1, 2 * y / 480 - 1, 0);
private static PngImage CreateFont()
{
// Seven rows of five bits per glyph. This intentionally simple developer
// font is generated from code, not a claim about the original font layout.
var glyphs = new Dictionary<char, string>
{
['A']="0E11111F111111", ['B']="1E11111E11111E", ['C']="0F10101010100F",
['D']="1E11111111111E", ['E']="1F10101E10101F", ['F']="1F10101E101010",
['G']="0F10101711110F", ['H']="1111111F111111", ['I']="1F04040404041F",
['J']="0101010111110E", ['K']="11121418141211", ['L']="1010101010101F",
['M']="111B1515111111", ['N']="11191513111111", ['O']="0E11111111110E",
['P']="1E11111E101010", ['Q']="0E11111115120D", ['R']="1E11111E141211",
['S']="0F10100E01011E", ['T']="1F040404040404", ['U']="1111111111110E",
['V']="11111111110A04", ['W']="11111115151B11", ['X']="11110A040A1111",
['Y']="11110A04040404", ['Z']="1F01020408101F",
['0']="0E11131519110E", ['1']="040C040404040E", ['2']="0E11010204081F",
['3']="1E01010E01011E", ['4']="02060A121F0202", ['5']="1F10101E01011E",
['6']="0E10101E11110E", ['7']="1F010204080808", ['8']="0E11110E11110E",
['9']="0E11110F01010E", [' ']="00000000000000", ['.']="00000000000C0C",
[',']="00000000000C08", [':']="000C0C000C0C00", [';']="000C0C000C0C08",
['-']="0000001F000000", ['_']="0000000000001F", ['+']="0004041F040400",
['=']="00001F001F0000", ['/']="01010204081010", ['\\']="10100804020101",
['(']="02040808080402", [')']="08040202020408", ['[']="0E08080808080E",
[']']="0E02020202020E", ['<']="01020408040201", ['>']="10080402040810",
['!']="04040404040004", ['?']="0E110102040004", ['#']="0A0A1F0A1F0A0A",
['%']="19190204081313", ['*']="00150E1F0E1500", ['|']="04040404040404",
['\'']="04040800000000", ['"']="0A0A1400000000", ['@']="0E11171517100E",
['$']="040F140E051E04", ['&']="0C12140C15120D", ['^']="040A1100000000",
['~']="00000D12000000", ['{']="02040408040402", ['}']="08040402040408",
['`']="08040200000000"
};
var pixels = new byte[AtlasWidth * AtlasHeight * 4];
for (var i = 0; i < pixels.Length; i += 4) pixels[i] = pixels[i + 1] = pixels[i + 2] = 255;
for (var character = ' '; character <= '~'; character++)
{
var rows = Convert.FromHexString(glyphs.GetValueOrDefault(char.ToUpperInvariant(character), glyphs['?']));
var cell = character - ' ';
for (var y = 0; y < 7; y++)
for (var x = 0; x < 5; x++)
if ((rows[y] & (1 << (4 - x))) != 0)
pixels[((cell / 16 * 8 + y) * AtlasWidth + cell % 16 * 8 + 1 + x) * 4 + 3] = 255;
}
pixels[(AtlasWidth * AtlasHeight - 1) * 4 + 3] = 255;
return new(AtlasWidth, AtlasHeight, pixels);
}
}