examples: Add example of using rich text from NitroFS

This commit is contained in:
Antonio Niño Díaz 2024-03-02 13:13:09 +00:00
parent 50e14b2c21
commit 9fd31b81ad
10 changed files with 237 additions and 0 deletions

View File

@ -0,0 +1,8 @@
__pycache__/
graph.png
*.nds
*.sav
build/
.ninja_deps
.ninja_log
*.ninja

View File

@ -0,0 +1,31 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: CC0-1.0
#
# SPDX-FileContributor: Antonio Niño Díaz, 2024
from architectds import *
nitrofs = NitroFS()
nitrofs.add_grit(['fonts'], out_dir='fonts')
nitrofs.add_bmfont_fnt(['fonts'], out_dir='fonts')
nitrofs.generate_image()
arm9 = Arm9Binary(
sourcedirs=['source'],
libs=['NE', 'dsf', 'nds9'],
libdirs=[
'${BLOCKSDS}/libs/libnds',
'${BLOCKSDSEXT}/libdsf',
'${BLOCKSDSEXT}/nitro-engine'
]
)
arm9.generate_elf()
nds = NdsRom(
binaries=[arm9, nitrofs],
game_title='DSF: NE: BMFont for NDS',
)
nds.generate_nds()
nds.run_command_line_arguments()

View File

@ -0,0 +1,59 @@
# AngelCode Bitmap Font Generator configuration file
fileVersion=1
# font settings
fontName=DejaVu Sans
fontFile=
charSet=0
fontSize=32
aa=1
scaleH=100
useSmoothing=1
isBold=0
isItalic=0
useUnicode=1
disableBoxChars=1
outputInvalidCharGlyph=0
dontIncludeKerningPairs=0
useHinting=1
renderFromOutline=0
useClearType=1
autoFitNumPages=0
autoFitFontSizeMin=0
autoFitFontSizeMax=0
# character alignment
paddingDown=0
paddingUp=0
paddingRight=0
paddingLeft=0
spacingHoriz=1
spacingVert=1
useFixedHeight=0
forceZero=0
widthPaddingFactor=0.00
# output file
outWidth=256
outHeight=256
outBitDepth=8
fontDescFormat=2
fourChnlPacked=0
textureFormat=png
textureCompression=0
alphaChnl=1
redChnl=0
greenChnl=0
blueChnl=0
invA=0
invR=0
invG=0
invB=0
# outline
outlineThickness=0
# selected chars
chars=32-126,160-255,65529-65533
# imported icon images

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -0,0 +1,2 @@
# 16-color texture, set black as transparent color
-gx -gb -gB4 -gT000000

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -0,0 +1,2 @@
# 256-color texture, set black as transparent color
-gx -gb -gB8 -gT000000

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -0,0 +1,21 @@
# Rich text font from NitroFS
This example shows how to load BMFont fonts from Nitro FS. Check the official
[website](https://www.angelcode.com/products/bmfont/) to see how to generate
them. Remember that the output format must be binary for the font metadata, and
PNG for the images.
The build system used is [ArchitectDS](https://github.com/AntonioND/architectds)
because of the flexibility it gives the user.
In order to build this example, run:
```bash
python3 build.py
```
To clean the build results, run:
```bash
python3 build.py --clean
```

View File

@ -0,0 +1,114 @@
// SPDX-License-Identifier: CC0-1.0
//
// SPDX-FileContributor: Antonio Niño Díaz, 2024
#include <stdio.h>
#include <filesystem.h>
#include <nds.h>
#include <NEMain.h>
NE_Sprite *TextSprite;
void Draw3DScene(void)
{
NE_ClearColorSet(RGB15(0, 7, 7), 31, 63);
NE_2DViewInit();
NE_RichTextRender3D(3, "VAWATa\ntajl", 0, 0);
NE_RichTextRender3DAlpha(2, "Text with alpha", 10, 80,
POLY_ALPHA(20) | POLY_CULL_BACK, 30);
}
void Draw3DScene2(void)
{
NE_ClearColorSet(RGB15(7, 0, 7), 31, 63);
NE_2DViewInit();
NE_SpriteSetPos(TextSprite, 16, 32);
NE_SpriteDraw(TextSprite);
}
__attribute__((noreturn)) void WaitLoop(void)
{
printf("Press START to exit");
while (1)
{
swiWaitForVBlank();
scanKeys();
if (keysHeld() & KEY_START)
exit(0);
}
}
int main(void)
{
irqEnable(IRQ_HBLANK);
irqSet(IRQ_VBLANK, NE_VBLFunc);
irqSet(IRQ_HBLANK, NE_HBLFunc);
// Init 3D mode
NE_InitDual3D();
NE_InitConsole();
if (!nitroFSInit(NULL))
{
printf("nitroFSInit failed.\n");
WaitLoop();
}
// Load a 256-color font to be used for rendering text as quads
NE_RichTextInit(2);
NE_RichTextMetadataLoadFAT(2, "fonts/font.fnt");
NE_RichTextMaterialLoadGRF(2, "fonts/font_16_png.grf");
// Load a 16-color font to be used for rendering text as quads
NE_RichTextInit(3);
NE_RichTextMetadataLoadFAT(3, "fonts/font.fnt");
NE_RichTextMaterialLoadGRF(3, "fonts/font_256_png.grf");
// Load a 16-color font to be used for rendering text to textures.
NE_RichTextInit(5);
NE_RichTextMetadataLoadFAT(5, "fonts/font.fnt");
NE_RichTextBitmapLoadGRF(5, "fonts/font_16_png.grf");
// Render text to a texture using the last font we've loaded
// We don't care about the palette, passing NULL will mark the palette
// to be autodeleted when the material is deleted.
NE_Material *Material = NULL;
NE_RichTextRenderMaterial(5,
"Sample: AWAV.\nÿ_ßðñÑü(o´Áá)|\nInvalid char: ŋ",
&Material, NULL);
// Create a sprite to be used to render the texture we've rendered
TextSprite = NE_SpriteCreate();
NE_SpriteSetMaterial(TextSprite, Material);
while (1)
{
NE_WaitForVBL(0);
NE_ProcessDual(Draw3DScene, Draw3DScene2);
scanKeys();
if (keysHeld() & KEY_START)
break;
}
NE_SpriteDelete(TextSprite);
NE_MaterialDelete(Material);
NE_RichTextEnd(2);
NE_RichTextEnd(3);
NE_RichTextEnd(5);
return 0;
}