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:
Rian Drake 2026-10-27 09:10:25 +13:00 • committed by GitHub
commit 7a3b72d843
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,54 @@
/*
* HelloTexture
*
* Prints texture format descriptors, fills a GenericTexture with a gradient, saves it as .png
* and .ktx2, then loads both back with TextureLoader.
*/
#:package Ahwoo.Brutal.Texture@2026.10.2
#:package Ahwoo.Brutal.Texture.Abstractions@2026.10.2
#:package Ahwoo.Brutal.Core@2026.10.1
#:package Ahwoo.Brutal.Numerics@2026.10.1
using System.Runtime.InteropServices;
using Brutal.Numerics;
using Brutal.TextureApi;
using Brutal.TextureApi.Abstractions;
Run();
static void Run()
{
// Format descriptors describe how texels are laid out in memory.
foreach (var format in new[] { TextureFormat.R8G8B8A8_UNorm, TextureFormat.BC7_UNorm_Block })
{
var descriptor = format.Descriptor();
Console.WriteLine(
$"{format}: {descriptor.BlockSizeInBytes} per {descriptor.BlockWidth}x{descriptor.BlockHeight} block, compressed: {descriptor.IsBlockCompressed}"
);
}
var pngPath = Path.Combine(Path.GetTempPath(), "HelloTexture.png");
var ktxPath = Path.Combine(Path.GetTempPath(), "HelloTexture.ktx2");
// GenericTexture is a plain in-memory texture you can fill and pass to any writer or uploader.
// Textures aren't IDisposable, so each one is destroyed or unloaded by hand.
var texture = GenericTexture.Defaults.RGBA8UNorm(new int2(64, 64));
var texels = MemoryMarshal.Cast<byte, byte4>(texture.Data);
for (var i = 0; i < texels.Length; i++)
texels[i] = new byte4((byte)(i % 64 * 4), (byte)(i / 64 * 4), 128, 255);
TextureWriter.SaveToFile(texture, pngPath);
TextureWriter.SaveToFile(texture, ktxPath);
texture.Destroy();
// TextureLoader picks the right backend (stb, KTX or GLI) from the file extension.
foreach (var path in new[] { pngPath, ktxPath })
{
var loaded = TextureLoader.LoadFromFile(path);
Console.WriteLine(
$"{Path.GetFileName(path)}: {loaded.GetType().Name}, {loaded.Format}, {loaded.Extent.X}x{loaded.Extent.Y}, {loaded.Data.Length} bytes"
);
TextureLoader.Unload(loaded);
}
}