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
40
samples/Hello/HelloStb.cs
Normal file
40
samples/Hello/HelloStb.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* HelloStb
|
||||
*
|
||||
* Writes a generated gradient to a PNG with stb_image_write, reads it back with stb_image and
|
||||
* prints a pixel.
|
||||
*/
|
||||
|
||||
#:package Ahwoo.Brutal.Stb@2026.10.2
|
||||
#:package Ahwoo.Brutal.Numerics@2026.10.1
|
||||
|
||||
// RIAN: Warning this library is handy for PNG, but it's pretty slow at runtime.
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
using Brutal.Numerics;
|
||||
using Brutal.StbApi;
|
||||
|
||||
const int WIDTH = 64;
|
||||
const int HEIGHT = 64;
|
||||
|
||||
Run();
|
||||
|
||||
static void Run()
|
||||
{
|
||||
// Generate an RGB gradient.
|
||||
var pixels = new byte3[WIDTH * HEIGHT];
|
||||
for (var y = 0; y < HEIGHT; y++)
|
||||
{
|
||||
for (var x = 0; x < WIDTH; x++)
|
||||
pixels[y * WIDTH + x] = new byte3((byte)(x * 4), (byte)(y * 4), 128);
|
||||
}
|
||||
|
||||
var path = Path.Combine(Path.GetTempPath(), "HelloStb.png");
|
||||
Stb.WritePng(path, WIDTH, HEIGHT, 3, MemoryMarshal.AsBytes(pixels.AsSpan()));
|
||||
Console.WriteLine($"Wrote {path}");
|
||||
|
||||
// Freed when Run returns.
|
||||
using var image = Stb.Load(path);
|
||||
Console.WriteLine($"Loaded {image.Width}x{image.Height}, {image.Channels} channels");
|
||||
Console.WriteLine($"Pixel (63, 0) = {image.AsSpan<byte3>()[63]}");
|
||||
}
|
||||
Loading…
Reference in a new issue