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
57
samples/Hello/HelloKtx.cs
Normal file
57
samples/Hello/HelloKtx.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* HelloKtx
|
||||
*
|
||||
* Creates a KTX2 texture, fills it with a checkerboard, writes it to disk and loads it back.
|
||||
*/
|
||||
|
||||
#:package Ahwoo.Brutal.Ktx@2026.10.1
|
||||
|
||||
using Brutal.KtxApi;
|
||||
using Brutal.KtxApi.Abstractions;
|
||||
|
||||
const int SIZE = 64;
|
||||
|
||||
Run();
|
||||
|
||||
static void Run()
|
||||
{
|
||||
var createInfo = new KtxTextureCreateInfo
|
||||
{
|
||||
VkFormat = (uint)VkFormat.R8G8B8A8Unorm,
|
||||
BaseWidth = SIZE,
|
||||
BaseHeight = SIZE,
|
||||
BaseDepth = 1,
|
||||
NumDimensions = 2,
|
||||
NumLevels = 1,
|
||||
NumLayers = 1,
|
||||
NumFaces = 1,
|
||||
};
|
||||
|
||||
KtxTexture2.Create(ref createInfo, KtxTextureCreateStorageEnum.TextureCreateAllocStorage, out var texture);
|
||||
|
||||
var path = Path.Combine(Path.GetTempPath(), "HelloKtx.ktx2");
|
||||
|
||||
// The texture is only needed until it is written, so it is disposed at the end of this block.
|
||||
using (texture)
|
||||
{
|
||||
// Fill mip level 0 with a checkerboard.
|
||||
var pixels = new byte[SIZE * SIZE * 4];
|
||||
for (var y = 0; y < SIZE; y++)
|
||||
for (var x = 0; x < SIZE; x++)
|
||||
pixels.AsSpan((y * SIZE + x) * 4, 4).Fill((x / 8 + y / 8) % 2 == 0 ? (byte)255 : (byte)0);
|
||||
|
||||
texture.SetImageFromMemory(0, 0, 0, pixels);
|
||||
texture.WriteToNamedFile(path);
|
||||
}
|
||||
|
||||
Console.WriteLine($"Wrote {path}");
|
||||
|
||||
KtxTexture2.CreateFromMemory(File.ReadAllBytes(path), KtxTextureCreateFlagBits.LoadImageDataBit, out var loaded);
|
||||
|
||||
using (loaded)
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"Loaded {loaded.BaseWidth}x{loaded.BaseHeight} {loaded.GetVkFormat()}, {loaded.NumLevels} level(s), {loaded.DataSize} bytes"
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue