posprintf¶
posprintf - a condensed version of sprintf for Thumb, esp. GBA
https://www.danposluns.com/gbadev/posprintf
By Dan Posluns, released into the public domain.
Warning
This routine is unsafe, make sure the destination buffer is big enough to hold the output string!
Also, note that it functions slightly differently from sprintf in the C standard library.
Example:
import natu/[video, irq, bios, tte, posprintf]
let friends = 12
let maxFriends = 50
# write formatted text into a buffer
var buffer: array[100, char]
posprintf(addr buffer, "You found %d out of %d friends.", friends, maxFriends)
# draw the text on background 0
tte.initChr4c(bgnr = 0, initBgCnt(cbb = 0, sbb = 31))
tte.write(addr buffer)
# show background 0
dispcnt = initDispCnt(bg0 = true)
# wait forever:
irq.enable(iiVBlank)
while true:
VBlankIntrWait()
- proc posprintf(dest: cstring, src: cstring)¶
srcmust be a valid zero-terminated C string.destmust point to a sufficiently large block of memory to contain the result string.
The following format specifiers are valid:
%%print a
%symbol%sprint a string; parameter must point to a valid zero-terminated C string
%dprint a 16-bit (short) integer; must be within
-65,535 .. 65,535%lprint a 29-bit integer; approximate range
-500,000,000 .. 500,000,000%xprint a hexadecimal number (lowercase digits)
%Xprint a hexadecimal number (uppercase digits)
The specifiers
%d,%l,%xand%Xmay be modified as follows:- Digits 1 through 9 indicate number of leading spaces to print, eg.
%5dwould print the number123as" 123"%5dwould print the number123456as"123456"(no leading spaces) - When above digit is prefixed with 0, leading zeros are printed instead of spaces
%05dwould print the number123as"00123"%04dwould print the number12345as"12345"(no leading zeros) - Negative sign consumes a leading space, eg.
%05dwould print the number-123as"-0123"(Hexadecimal numbers are considered unsigned)