optimize charfade example, added textspotlight example

This commit is contained in:
Irmen de Jong
2025-10-05 19:09:39 +02:00
parent ec9475c308
commit 37535f2913
4 changed files with 187 additions and 64 deletions
+1
View File
@@ -177,6 +177,7 @@ class TestCompilerOnExamplesCx16: FunSpec({
"test_gfx_lores",
"test_gfx_hires",
"testmonogfx",
"textspotlight",
),
listOf(false, true)
)
+3
View File
@@ -1,6 +1,9 @@
TODO
====
check the accuracy of the various disc routines (filled circles), they seem less accurate than the circle routine (virt monogfx)
LONG TYPE
---------
- implement the other comparison operators (<,>,<=,>=) on longs
+66 -64
View File
@@ -10,22 +10,20 @@
main {
^^uword palette = memory("palette", 256*2, 0)
^^ubyte closest = memory("closest", 4096, 0)
ubyte[256] fadeLUT ; for each of the 256 colors in the palette, give the color index of the closest 1 step darker color.
const ubyte WIDTH = 40
const ubyte HEIGHT = 30
sub start() {
fill_default_palette()
txt.print("\nprecalc color fade table, patience plz ")
precalc_fade_table()
cx16.set_screen_mode(128)
cx16.GRAPH_set_colors(0,0,0)
cx16.GRAPH_clear()
txt.t256c(true)
txt.t256c(true) ; to allow characters to use all 256 colors in the palette instead of just the first 16
ubyte x,y
for y in 0 to HEIGHT-1 {
for x in 0 to WIDTH-1 {
@@ -33,18 +31,15 @@ main {
}
}
sys.wait(60)
sys.wait(30)
repeat {
repeat 4 sys.waitvsync()
for y in 0 to HEIGHT-1 {
for x in 0 to WIDTH-1 {
ubyte @zp currentcolor = txt.getclr(x,y)
cx16.r0L = closest[darken(palette[currentcolor])]
if cx16.r0L>0 and cx16.r0L == currentcolor
cx16.r0L = 15 ; to avoid stuck colors, make it gray, this will be able to fade to black because the pallette contains all shades of gray.
txt.setclr(x,y, cx16.r0L)
; this can be done much faster using Vera's auto increment mode, but for the sake of clarity, we'll do it like this
txt.setclr(x,y, fadeLUT[txt.getclr(x,y)])
}
txt.setcc2(math.rnd() % WIDTH, math.rnd() % HEIGHT, math.rnd(), math.rnd()) ; add new chars
@@ -52,62 +47,69 @@ main {
}
}
sub precalc_fade_table() {
uword colorindex
ubyte r,g,b
alias color = cx16.r2
for r in 0 to 15 {
txt.print_ub(16-r)
txt.spc()
for g in 0 to 15 {
for b in 0 to 15 {
color = mkword(r, g<<4 | b)
closest[colorindex] = find_closest(color)
colorindex++
sub precalc_fade_table() {
txt.print("\nprecalc color fade table, patience plz")
fill_default_palette()
ubyte index
for index in 0 to 255 {
fadeLUT[index] = find_darker(index)
}
sub fill_default_palette() {
ubyte pal_bank
uword pal_addr
pal_bank, pal_addr = cx16.get_default_palette() ; needs ROM 49+
cx16.push_rombank(pal_bank)
sys.memcopy(pal_addr, palette, 256*2)
cx16.pop_rombank()
}
sub find_darker(ubyte color_index) -> ubyte {
uword darker_rgb = darken(palette[index])
if darker_rgb == 0
return 0
ubyte closest, second
closest, second = find_2_closest_colorindexes(darker_rgb)
if closest == color_index
return second ; to avoid stuck colors
return closest
}
sub darken(uword color @R0) -> uword {
if cx16.r0H!=0
cx16.r0H--
if cx16.r0L & $0f != 0
cx16.r0L--
if cx16.r0L & $f0 != 0
cx16.r0L -= $10
return cx16.r0
}
sub find_2_closest_colorindexes(uword rgb @R0) -> ubyte, ubyte {
ubyte distance = 255
ubyte current
ubyte second = 15
alias index = cx16.r1L
for index in 0 to 255 {
uword @zp pc = palette[index]
ubyte @zp d2 = abs(msb(pc) as byte - msb(rgb) as byte) ; RED distance
d2 += abs((lsb(pc) & $f0) as byte - (lsb(rgb) & $f0) as byte) >> 4 ; GREEN distance
d2 += abs((lsb(pc) & $0f) as byte - (lsb(rgb) & $0f) as byte) ; BLUE distance
if d2==0
return index, second
if d2 < distance {
distance = d2
second = current
current = index
}
}
return current, second
}
}
sub darken(uword color @R0) -> uword {
if cx16.r0H!=0
cx16.r0H--
if cx16.r0L & $0f != 0
cx16.r0L--
if cx16.r0L & $f0 != 0
cx16.r0L -= $10
return cx16.r0
}
sub find_closest(uword rgb @R0) -> ubyte {
ubyte distance = 255
ubyte current = 0
alias index = cx16.r1L
for index in 0 to 255 {
uword @zp pc = palette[index]
ubyte @zp d2 = abs(msb(pc) as byte - msb(rgb) as byte) ; RED distance
d2 += abs((lsb(pc) & $f0) as byte - (lsb(rgb) & $f0) as byte) >> 4 ; GREEN distance
d2 += abs((lsb(pc) & $0f) as byte - (lsb(rgb) & $0f) as byte) ; BLUE distance
if d2==0
return index
if d2 < distance {
distance = d2
current = index
}
}
return current
}
sub fill_default_palette() {
ubyte pal_bank
uword pal_addr
pal_bank, pal_addr = cx16.get_default_palette() ; needs ROM 49+
cx16.push_rombank(pal_bank)
sys.memcopy(pal_addr, palette, 256*2)
cx16.pop_rombank()
}
}
+117
View File
@@ -0,0 +1,117 @@
%import textio
main {
sub start() {
lorumipsum()
txt.t256c(true)
ubyte x=5
ubyte y=8
byte dx=1
byte dy=1
repeat {
colorize(x,y)
sys.waitvsync()
sys.waitvsync()
sys.waitvsync()
black(x,y)
x += dx as ubyte
if_neg {
x = 1
dx = 1
} else if x==txt.DEFAULT_WIDTH-30 {
x = txt.DEFAULT_WIDTH-31
dx = -1
}
y += dy as ubyte
if_neg {
y = 1
dy = 1
} else if y==txt.DEFAULT_HEIGHT-30 {
y = txt.DEFAULT_HEIGHT-31
dy = -1
}
}
}
sub black(ubyte xx, ubyte yy) {
uword vera_offset = (txt.VERA_TEXTMATRIX & $ffff) + xx*2 + 1 + yy*256
repeat 31 {
cx16.vaddr_autoincr(msw(txt.VERA_TEXTMATRIX), vera_offset, 0, 2)
unroll 31 cx16.VERA_DATA0 = 0
vera_offset += 256
}
}
sub colorize(ubyte xx, ubyte yy) {
^^ubyte colorptr = &colors
uword vera_offset = (txt.VERA_TEXTMATRIX & $ffff) + xx*2 + 1 + yy*256
repeat 31 {
cx16.vaddr_autoincr(msw(txt.VERA_TEXTMATRIX), vera_offset, 0, 2)
repeat 31 {
cx16.VERA_DATA0 = @(colorptr)
colorptr ++
}
vera_offset += 256
}
}
sub lorumipsum() {
txt.lowercase()
txt.color(0)
repeat 3 {
txt.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas ultricies ex massa, sit amet aliquet risus aliquet vitae. ")
txt.print("Sed sit amet turpis et augue ultrices rhoncus at vel metus. Nunc malesuada felis in libero sodales pulvinar. Donec rutrum est sed luctus sodales. ")
txt.print("Sed egestas faucibus purus, fermentum porttitor arcu cursus ac. Vivamus fermentum et justo eget cursus. Pellentesque accumsan ultrices placerat. ")
txt.print("Fusce dapibus ut orci a posuere. Mauris tristique eget orci ut feugiat. Suspendisse eget leo semper, condimentum nisl a, tempus turpis. ")
txt.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ultrices, tellus ac bibendum vulputate, enim nulla bibendum massa, ")
txt.print("ultrices lobortis quam enim eget sem. Cras in metus elit.\n\n")
txt.print("Quisque convallis leo metus, at luctus elit pretium mollis. Aliquam quis tellus est. Vestibulum rutrum lorem vel luctus posuere. ")
txt.print("Aliquam finibus eros sit amet eleifend sodales. Phasellus at viverra nisi. Suspendisse potenti. Sed sem quam, rhoncus dictum urna eu, malesuada pretium libero. ")
txt.print("Integer quis odio nulla. Fusce luctus vulputate quam id tempor. Etiam eget nisl leo. Morbi fermentum ullamcorper tellus id euismod.\n\n")
txt.print("Nam scelerisque ante ut pharetra pretium. In vitae vehicula sem, eget consequat dolor. Vestibulum mollis libero diam, id molestie est ullamcorper sed. ")
txt.print("Aenean gravida tortor a orci rutrum sodales. Cras mollis iaculis ipsum id sollicitudin. Mauris at elit vitae odio tempor efficitur. ")
txt.print("Curabitur convallis turpis vitae diam elementum vestibulum.\n\n")
}
}
colors:
; 31 x 31 cell colors. This is too large for a native prog8 array so we use an inline assembly block
%asm {{
.byte $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $10, $10, $10, $10, $10, $10, $10, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
.byte $00, $00, $00, $00, $00, $00, $00, $00, $00, $10, $10, $10, $11, $11, $11, $11, $11, $11, $11, $10, $10, $10, $00, $00, $00, $00, $00, $00, $00, $00, $00
.byte $00, $00, $00, $00, $00, $00, $00, $10, $10, $11, $11, $11, $12, $12, $12, $12, $12, $12, $12, $11, $11, $11, $10, $10, $00, $00, $00, $00, $00, $00, $00
.byte $00, $00, $00, $00, $00, $00, $10, $10, $11, $12, $12, $12, $13, $13, $13, $13, $13, $13, $13, $12, $12, $12, $11, $10, $10, $00, $00, $00, $00, $00, $00
.byte $00, $00, $00, $00, $00, $10, $11, $11, $12, $12, $13, $13, $14, $14, $14, $14, $14, $14, $14, $13, $13, $12, $12, $11, $11, $10, $00, $00, $00, $00, $00
.byte $00, $00, $00, $00, $10, $11, $11, $12, $13, $13, $14, $14, $15, $15, $15, $15, $15, $15, $15, $14, $14, $13, $13, $12, $11, $11, $10, $00, $00, $00, $00
.byte $00, $00, $00, $10, $11, $11, $12, $13, $13, $14, $15, $15, $15, $16, $16, $16, $16, $16, $15, $15, $15, $14, $13, $13, $12, $11, $11, $10, $00, $00, $00
.byte $00, $00, $10, $10, $11, $12, $13, $14, $14, $15, $15, $16, $16, $17, $17, $17, $17, $17, $16, $16, $15, $15, $14, $14, $13, $12, $11, $10, $10, $00, $00
.byte $00, $00, $10, $11, $12, $13, $13, $14, $15, $16, $16, $17, $17, $18, $18, $18, $18, $18, $17, $17, $16, $16, $15, $14, $13, $13, $12, $11, $10, $00, $00
.byte $00, $10, $11, $12, $12, $13, $14, $15, $16, $16, $17, $18, $18, $19, $19, $19, $19, $19, $18, $18, $17, $16, $16, $15, $14, $13, $12, $12, $11, $10, $00
.byte $00, $10, $11, $12, $13, $14, $15, $15, $16, $17, $18, $18, $19, $1A, $1A, $1A, $1A, $1A, $19, $18, $18, $17, $16, $15, $15, $14, $13, $12, $11, $10, $00
.byte $00, $10, $11, $12, $13, $14, $15, $16, $17, $18, $18, $19, $1A, $1A, $1B, $1B, $1B, $1A, $1A, $19, $18, $18, $17, $16, $15, $14, $13, $12, $11, $10, $00
.byte $10, $11, $12, $13, $14, $15, $15, $16, $17, $18, $19, $1A, $1B, $1B, $1C, $1C, $1C, $1B, $1B, $1A, $19, $18, $17, $16, $15, $15, $14, $13, $12, $11, $10
.byte $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $1A, $1A, $1B, $1C, $1D, $1D, $1D, $1C, $1B, $1A, $1A, $19, $18, $17, $16, $15, $14, $13, $12, $11, $10
.byte $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $1A, $1B, $1C, $1D, $1D, $1E, $1D, $1D, $1C, $1B, $1A, $19, $18, $17, $16, $15, $14, $13, $12, $11, $10
.byte $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $1A, $1B, $1C, $1D, $1E, $01, $1E, $1D, $1C, $1B, $1A, $19, $18, $17, $16, $15, $14, $13, $12, $11, $10
.byte $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $1A, $1B, $1C, $1D, $1D, $1E, $1D, $1D, $1C, $1B, $1A, $19, $18, $17, $16, $15, $14, $13, $12, $11, $10
.byte $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $1A, $1A, $1B, $1C, $1D, $1D, $1D, $1C, $1B, $1A, $1A, $19, $18, $17, $16, $15, $14, $13, $12, $11, $10
.byte $10, $11, $12, $13, $14, $15, $15, $16, $17, $18, $19, $1A, $1B, $1B, $1C, $1C, $1C, $1B, $1B, $1A, $19, $18, $17, $16, $15, $15, $14, $13, $12, $11, $10
.byte $00, $10, $11, $12, $13, $14, $15, $16, $17, $18, $18, $19, $1A, $1A, $1B, $1B, $1B, $1A, $1A, $19, $18, $18, $17, $16, $15, $14, $13, $12, $11, $10, $00
.byte $00, $10, $11, $12, $13, $14, $15, $15, $16, $17, $18, $18, $19, $1A, $1A, $1A, $1A, $1A, $19, $18, $18, $17, $16, $15, $15, $14, $13, $12, $11, $10, $00
.byte $00, $10, $11, $12, $12, $13, $14, $15, $16, $16, $17, $18, $18, $19, $19, $19, $19, $19, $18, $18, $17, $16, $16, $15, $14, $13, $12, $12, $11, $10, $00
.byte $00, $00, $10, $11, $12, $13, $13, $14, $15, $16, $16, $17, $17, $18, $18, $18, $18, $18, $17, $17, $16, $16, $15, $14, $13, $13, $12, $11, $10, $00, $00
.byte $00, $00, $10, $10, $11, $12, $13, $14, $14, $15, $15, $16, $16, $17, $17, $17, $17, $17, $16, $16, $15, $15, $14, $14, $13, $12, $11, $10, $10, $00, $00
.byte $00, $00, $00, $10, $11, $11, $12, $13, $13, $14, $15, $15, $15, $16, $16, $16, $16, $16, $15, $15, $15, $14, $13, $13, $12, $11, $11, $10, $00, $00, $00
.byte $00, $00, $00, $00, $10, $11, $11, $12, $13, $13, $14, $14, $15, $15, $15, $15, $15, $15, $15, $14, $14, $13, $13, $12, $11, $11, $10, $00, $00, $00, $00
.byte $00, $00, $00, $00, $00, $10, $11, $11, $12, $12, $13, $13, $14, $14, $14, $14, $14, $14, $14, $13, $13, $12, $12, $11, $11, $10, $00, $00, $00, $00, $00
.byte $00, $00, $00, $00, $00, $00, $10, $10, $11, $12, $12, $12, $13, $13, $13, $13, $13, $13, $13, $12, $12, $12, $11, $10, $10, $00, $00, $00, $00, $00, $00
.byte $00, $00, $00, $00, $00, $00, $00, $10, $10, $11, $11, $11, $12, $12, $12, $12, $12, $12, $12, $11, $11, $11, $10, $10, $00, $00, $00, $00, $00, $00, $00
.byte $00, $00, $00, $00, $00, $00, $00, $00, $00, $10, $10, $10, $11, $11, $11, $11, $11, $11, $11, $10, $10, $10, $00, $00, $00, $00, $00, $00, $00, $00, $00
.byte $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $10, $10, $10, $10, $10, $10, $10, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
}}
}