NuGet
How to use a NuGet package in a BitMagic project. The example uses StbImageSharp to load an image while the program is built, converts it to the X16 palette, then displays it.
How to run it
Open the folder in VSCode and press F5. The build converts assets/m65.png and writes it to the SD card, and the program loads it into VRAM as a 320x240 bitmap. You’ll need an internet connection the first time, so BitMagic can download the package.
Referencing a NuGet package
The first line of src/main.bmasm references the package:
nuget StbImageSharp, 2.30.15;
This downloads version 2.30.15 of StbImageSharp from NuGet and puts its DLLs in the bin folder with the rest of the build output. It also loads the assembly, so there’s nothing else to reference. Leave out the version number to use the latest one.
The reference doesn’t have to be on the first line, but the top of the file is a good place for it. Your code will still need using statements:
using StbImageSharp;
Converting the image
These lines near the start of the file load the image, convert it to a 320x240 image in the X16’s default palette and write the result as a binary file:
var image = ImageResult.FromMemory(File.ReadAllBytes(@"..\assets\m65.png"), ColorComponents.RedGreenBlue);
File.WriteAllBytes(@$"..\sdcard\{filename}", GetX16Image(image, 320, 240, colours));
StbImageSharp decodes the PNG into an array of RGB bytes. The default palette is the int array colours. GetX16Image scales the image down by averaging blocks of pixels, then picks the closest palette entry for each one, using Floyd-Steinberg dithering to spread the error to the neighbouring pixels. That gives one byte per pixel.
project.json adds the sdcard folder to the emulator’s SD card, so the program can load the file.
Displaying it
The rest of src/main.bmasm sets layer 0 to an 8bpp bitmap at $00000 in VRAM, then uses the kernal’s MACPTR to load the file straight into VRAM through DATA0.