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
54
samples/Hello/HelloTexture.cs
Normal file
54
samples/Hello/HelloTexture.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue