mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 20:48:24 +01:00
samples: add Hello/Demo samples for most BRUTAL packages (#19)
Add samples/, a collection of single-file .NET 10 apps run with `dotnet run <file>.cs`. Hello/ has one minimal sample per package that has a public API, many of them headless; Demo/ has fuller programs that combine packages into a more interesting application. samples/README.md indexes every sample and covers the layout, error handling and per-OS requirements.
This commit is contained in:
parent
4ac91e105e
commit
7a3b72d843
1 changed files with 4599 additions and 1 deletions
85
samples/Hello/HelloImGuiAbstractions.cs
Normal file
85
samples/Hello/HelloImGuiAbstractions.cs
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* HelloImGuiAbstractions
|
||||
*
|
||||
* The in-game ConsoleWindow and its TerminalInterface: registers [TerminalAction] commands, runs
|
||||
* a few command lines with typed arguments, then draws the console headless.
|
||||
* See Hello/HelloImGui.cs for Dear ImGui on screen.
|
||||
*/
|
||||
|
||||
#:package Ahwoo.Brutal.ImGui.Abstractions@2026.10.2
|
||||
#:package Ahwoo.Brutal.Core@2026.10.1
|
||||
#:package Ahwoo.Brutal.ImGui@2026.10.2
|
||||
#:package Ahwoo.Brutal.Numerics@2026.10.1
|
||||
|
||||
// RIAN: This sample primarly showcases the console interface that all RW projects use.
|
||||
|
||||
using Brutal.ImGuiApi;
|
||||
using Brutal.ImGuiApi.Abstractions;
|
||||
using Brutal.Numerics;
|
||||
|
||||
var context = ImGui.CreateContext();
|
||||
|
||||
try
|
||||
{
|
||||
Run();
|
||||
}
|
||||
finally
|
||||
{
|
||||
ImGui.DestroyContext(context);
|
||||
}
|
||||
|
||||
static void Run()
|
||||
{
|
||||
var io = ImGui.GetIO();
|
||||
io.IniFilename = null; // Don't write imgui.ini.
|
||||
io.DisplaySize = new float2(800, 600); // No window, so tell ImGui how big the screen is.
|
||||
io.DeltaTime = 1f / 60f;
|
||||
|
||||
// Claim a renderer that handles texture requests so ImGui builds the font atlas during the frame.
|
||||
// Nothing uploads it here, which is fine for a headless frame.
|
||||
io.BackendFlags |= ImGuiBackendFlags.RendererHasTextures;
|
||||
|
||||
// Disposed when Run returns, before the ImGui context is destroyed.
|
||||
using var console = new ConsoleWindow();
|
||||
console.DisableMessageFading();
|
||||
|
||||
// Commands are plain methods; the terminal parses arguments into their parameter types.
|
||||
console.Terminal.RegisterCommand(Hello);
|
||||
console.Terminal.RegisterCommand(Add);
|
||||
console.Terminal.OnOutput += (message, type) => Say($"[{type}] {message}"); // Parse errors land here.
|
||||
|
||||
// What a player would type into the console; ';' separates commands on one line.
|
||||
string[] lines = ["hello World", "add 2 3; add 40", "add two 3"];
|
||||
foreach (var line in lines)
|
||||
{
|
||||
Console.WriteLine($"> {line}");
|
||||
console.Submit(line);
|
||||
}
|
||||
|
||||
// Draw the open console as a game would each frame; it sizes itself to its log, which takes a
|
||||
// frame to measure, so draw two.
|
||||
console.Show();
|
||||
for (var frame = 0; frame < 2; frame++)
|
||||
{
|
||||
ImGui.NewFrame();
|
||||
var viewport = ImGui.GetMainViewport();
|
||||
console.Draw(viewport.Pos, viewport.Size, io.DeltaTime);
|
||||
ImGui.Render();
|
||||
}
|
||||
|
||||
var drawData = ImGui.GetDrawData();
|
||||
Console.WriteLine($"Console drawn: {drawData.TotalVtxCount} vertices, {drawData.TotalIdxCount} indices");
|
||||
|
||||
// Print to the in-game console, and echo to stdout since there's no window to see it in.
|
||||
void Say(string message)
|
||||
{
|
||||
console.Print(message);
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
|
||||
[TerminalAction("hello", "Prints a greeting")]
|
||||
void Hello(string name) => Say($"Hello, {name}!");
|
||||
|
||||
[TerminalAction("add", "Adds two integers")]
|
||||
void Add(int a, int b = 2) => Say($"{a} + {b} = {a + b}");
|
||||
}
|
||||
Loading…
Reference in a new issue