mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 12:38:24 +01:00
samples: add Hello/Demo samples for most BRUTAL packages
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
a25cdf80b2
commit
b0c9fd9cdc
1 changed files with 4599 additions and 1 deletions
77
samples/Hello/HelloImPlot.cs
Normal file
77
samples/Hello/HelloImPlot.cs
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* HelloImPlot
|
||||
*
|
||||
* Plots a sine wave with ImPlot for one headless Dear ImGui frame and prints how much geometry
|
||||
* it produced. See Demo/DemoImGui.cs for an ImPlot plot on screen.
|
||||
*/
|
||||
|
||||
#:package Ahwoo.Brutal.ImGui@2026.10.2
|
||||
#:package Ahwoo.Brutal.ImPlot@2026.10.2
|
||||
#:package Ahwoo.Brutal.Core@2026.10.1
|
||||
#:package Ahwoo.Brutal.Numerics@2026.10.1
|
||||
|
||||
using Brutal.ImGuiApi;
|
||||
using Brutal.ImPlotApi;
|
||||
using Brutal.Numerics;
|
||||
|
||||
var context = ImGui.CreateContext();
|
||||
|
||||
// ImPlot keeps its own context alongside ImGui's.
|
||||
var plotContext = ImPlot.CreateContext();
|
||||
ImPlot.SetImGuiContext(context);
|
||||
|
||||
try
|
||||
{
|
||||
Run();
|
||||
}
|
||||
finally
|
||||
{
|
||||
ImPlot.DestroyContext(plotContext);
|
||||
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;
|
||||
|
||||
var xs = new float[200];
|
||||
var ys = new float[200];
|
||||
for (var i = 0; i < xs.Length; i++)
|
||||
{
|
||||
xs[i] = i * 0.05f;
|
||||
ys[i] = MathF.Sin(xs[i]);
|
||||
}
|
||||
|
||||
ImGui.NewFrame();
|
||||
|
||||
ImGui.SetNextWindowSize(new float2(500, 400));
|
||||
ImGui.Begin("Hello ImPlot"u8);
|
||||
if (ImPlot.BeginPlot("Sine Wave"u8, new float2(-1, -1))) // Fill the window.
|
||||
{
|
||||
// Auto-fit only kicks in on the next frame, so give this single frame its limits up front.
|
||||
ImPlot.SetupAxesLimits(0, 10, -1.1, 1.1);
|
||||
ImPlot.PlotLine("sin(x)"u8, xs, ys);
|
||||
ImPlot.EndPlot();
|
||||
}
|
||||
ImGui.End();
|
||||
|
||||
ImGui.Render();
|
||||
|
||||
// A renderer would draw these; here we just count them.
|
||||
var drawData = ImGui.GetDrawData();
|
||||
var commands = 0;
|
||||
for (var i = 0; i < drawData.CmdListsCount; i++)
|
||||
commands += drawData.CmdLists[i].CmdBuffer.Count;
|
||||
|
||||
Console.WriteLine($"Plotted {xs.Length} points");
|
||||
Console.WriteLine(
|
||||
$"Draw data: {drawData.CmdListsCount} lists, {drawData.TotalVtxCount} vertices, {drawData.TotalIdxCount} indices, {commands} commands"
|
||||
);
|
||||
}
|
||||
Loading…
Reference in a new issue