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
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/HelloSdlTtf.cs
Normal file
77
samples/Hello/HelloSdlTtf.cs
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* HelloSdlTtf
|
||||
*
|
||||
* Opens a font with Sdl.Ttf, renders a line of text to an SDL surface, prints its pixel size and
|
||||
* saves it as a BMP. Runs headless (no window).
|
||||
*
|
||||
* Pass a font, or it tries a common system font for your OS:
|
||||
*
|
||||
* dotnet run Hello/HelloSdlTtf.cs -- path/to/font.ttf
|
||||
*/
|
||||
|
||||
#:property AllowUnsafeBlocks=true
|
||||
#:package Ahwoo.Brutal.Sdl@2026.10.2
|
||||
#:package Ahwoo.Brutal.Sdl.Ttf@2026.10.2
|
||||
|
||||
using Brutal.SdlApi;
|
||||
using Brutal.SdlApi.Ttf;
|
||||
|
||||
// yuck, sorry
|
||||
string[] systemFonts =
|
||||
[
|
||||
"/System/Library/Fonts/Supplemental/Arial.ttf", // macOS
|
||||
@"C:\Windows\Fonts\arial.ttf", // Windows
|
||||
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", // Debian, Ubuntu
|
||||
"/usr/share/fonts/TTF/DejaVuSans.ttf", // Arch
|
||||
"/usr/share/fonts/dejavu-sans-fonts/DejaVuSans.ttf", // Fedora
|
||||
];
|
||||
|
||||
var fontPath = args.Length > 0 ? args[0] : systemFonts.FirstOrDefault(File.Exists);
|
||||
if (fontPath is null)
|
||||
{
|
||||
Console.WriteLine("No system font found. Usage: dotnet run Hello/HelloSdlTtf.cs -- path/to/font.ttf");
|
||||
return;
|
||||
}
|
||||
|
||||
SdlTtf.Init();
|
||||
try
|
||||
{
|
||||
Run(fontPath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
SdlTtf.Quit();
|
||||
}
|
||||
|
||||
static void Run(string fontPath)
|
||||
{
|
||||
// Closed when Run returns, before Sdl.Ttf quits.
|
||||
using var font = SdlTtf.OpenFont(fontPath, 32f);
|
||||
Console.WriteLine($"Font: {SdlTtf.GetFontFamilyName(font)} {SdlTtf.GetFontStyleName(font)} ({fontPath})");
|
||||
|
||||
var bmpPath = Path.Combine(Path.GetTempPath(), "HelloSdlTtf.bmp");
|
||||
unsafe
|
||||
{
|
||||
// Blended renders anti-aliased text onto a transparent surface; length 0 means null-terminated.
|
||||
var surface = SdlTtf.RenderTextBlended(
|
||||
font,
|
||||
"Hello, Sdl.Ttf!",
|
||||
0,
|
||||
new SdlColor
|
||||
{
|
||||
r = 255,
|
||||
g = 255,
|
||||
b = 255,
|
||||
a = 255,
|
||||
}
|
||||
);
|
||||
|
||||
Console.WriteLine($"Rendered text: {surface->w}x{surface->h} pixels");
|
||||
|
||||
Sdl.PInvoke.SaveBMP(surface, bmpPath);
|
||||
Console.WriteLine($"Saved {bmpPath}");
|
||||
|
||||
// The surface is a raw pointer rather than an IDisposable handle, so destroy it by hand.
|
||||
Sdl.PInvoke.DestroySurface(surface);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue