REM Render the complete karaoke frame off screen at the actual output resolution.
REM Raster scenery/score is scaled once; queued outline text is painted afterwards
REM inside that same high-resolution sprite. The Wimp sees one finished blit.
DEF PROCkarahiresensure
LOCAL w%,h%,bytes%,total%,area%,base%,flags%,need%,i%,sx%,sy%,handle%,name$
khires%=FALSE
IF kfont%(1)=0 THEN ENDPROC
w%=renderw% DIV pixelx%:h%=renderh% DIV pixely%
IF w%<=600 AND h%<=390 THEN ENDPROC
IF w%=khfailedw% AND h%=khfailedh% THEN ENDPROC
IF khareaid%>=0 AND (w%<>khw% OR h%<>khh%) THEN PROCkarahiresfree
IF khareaid%<0 THEN
  bytes%=60+w%*h%*4:total%=(bytes%+65536+4095) AND -4096
  SYS "XOS_DynamicArea",0,-1,total%,-1,0,total%,0,-1,"Xmas26 Karaoke" TO ,area%,,base%;flags%
  IF flags% AND 1 THEN khfailedw%=w%:khfailedh%=h%:ENDPROC
  khareaid%=area%:kharea%=base%:khw%=w%:khh%=h%:khvalid%=FALSE
  !kharea%=bytes%:kharea%!4=0:kharea%!8=16:kharea%!12=16
  SYS "OS_SpriteOp",&10F,kharea%,"karaoke",0,w%,h%,&301680B5
  SYS "OS_SpriteOp",&118,kharea%,"karaoke" TO ,,khptr%
  SYS "OS_SpriteOp",&23E,kharea%,khptr% TO ,,,need%
  IF need%>65536 THEN PROCkarahiresfree:khfailedw%=w%:khfailedh%=h%:ENDPROC
  khsave%=kharea%+bytes%
  FOR i%=1 TO 3
    sx%=INT(76.8*(i%+1)*w%/600+.5):sy%=INT(76.8*(i%+1)*h%/390+.5)
    name$="Homerton.Medium":IF i%>1 THEN name$="Homerton.Bold"
    SYS "XFont_FindFont",,name$,sx%,sy%,90,90 TO handle%;flags%
    IF flags% AND 1 THEN PROCkarahiresfree:khfailedw%=w%:khfailedh%=h%:ENDPROC
    khfont%(i%)=handle%
  NEXT
  khmodevalid%=FALSE
ENDIF
IF khmodevalid%=FALSE THEN
  SYS "ColourTrans_GenerateTable",kharea%,khptr%,-1,-1,0,17 TO ,,,,need%
  IF need%>1024 THEN PROCkarahiresfree:ENDPROC
  khtable%=0
  IF need%>0 THEN
    SYS "ColourTrans_GenerateTable",kharea%,khptr%,-1,-1,khtrans%,17
    khtable%=khtrans%
  ENDIF
  khmodevalid%=TRUE
ENDIF
!khscale%=renderw%:khscale%!4=renderh%
khscale%!8=khw%*pixelx%:khscale%!12=khh%*pixely%
khires%=TRUE
ENDPROC
:
DEF PROCkarahiresfree
LOCAL i%
IF khready%=FALSE THEN ENDPROC
FOR i%=1 TO 3
  IF khfont%(i%)<>0 THEN SYS "XFont_LoseFont",khfont%(i%):khfont%(i%)=0
NEXT
IF khareaid%>=0 THEN SYS "XOS_DynamicArea",1,khareaid%
khareaid%=-1:kharea%=0:khptr%=0:khires%=FALSE:khmodevalid%=FALSE
krecord%=FALSE:khfailedw%=0:khfailedh%=0:khvalid%=FALSE
ENDPROC
:
DEF PROCkaraqueue(text$,x,y%,factor%,foreground%,background%)
IF kqcount%>=128 THEN ERROR 1,"Karaoke text queue full"
kqtext$(kqcount%)=text$:kqx(kqcount%)=x:kqy%(kqcount%)=y%:kqfont%(kqcount%)=factor%
kqfore%(kqcount%)=foreground%:kqback%(kqcount%)=background%
kqtop%(kqcount%)=kcliptop%:kqbottom%(kqcount%)=kclipbottom%
kqcount%+=1
ENDPROC
:
DEF PROCkarahirescompose
LOCAL i%,factor%,sx%,sy%,foreground%,background%,text$,partial%,oldx,newx,bar%
partial%=FNkaracached
!khsave%=0
SYS "OS_SpriteOp",&23C,kharea%,khptr%,khsave% TO bufferold0%,bufferold1%,bufferold2%,bufferold3%
bufferbusy%=TRUE:VDU 26,4
!khcompose%=khw%:khcompose%!4=khh%:khcompose%!8=600:khcompose%!12=390
IF partial% THEN
  REM Queued native text and sounding notes are unchanged. Restore only the
  REM moving bar and the old/new score cursor from the fresh base frame.
  IF lyricrecord%>=0 THEN
    bar%=90+INT(34*(1-kscroll%/35)^2)
    PROCkarapatch(0,bar%,600,bar%+4)
  ENDIF
  oldx=94+(khclock% MOD 800)*.6:newx=94+(displayclock% MOD 800)*.6
  PROCkarapatch(INT(oldx)-2,213,INT(oldx)+3,366)
  IF INT(newx)<>INT(oldx) THEN PROCkarapatch(INT(newx)-2,213,INT(newx)+3,366)
  khclock%=displayclock%:PROCbufferrestore:ENDPROC
ENDIF
SYS "OS_SpriteOp",&234,bufferarea%,bufferptr%,0,0,32,khcompose%,0
IF kqcount%=0 THEN PROCkararemember:PROCbufferrestore:ENDPROC
FOR i%=0 TO kqcount%-1
  VDU 24,0;INT((390-kqbottom%(i%))*khh%/390)*2;(khw%-1)*2;INT((390-kqtop%(i%))*khh%/390-1)*2;
  factor%=kqfont%(i%):foreground%=kqfore%(i%):background%=kqback%(i%)
  SYS "ColourTrans_SetFontColours",khfont%(factor%),background%,foreground%,14
  sx%=INT(kqx(i%)*khw%/600*800+.5)
  sy%=khh%*800-INT((kqy%(i%)+8*factor%)*khh%/390*800+.5)
  text$=kqtext$(i%)
  SYS "Font_Paint",khfont%(factor%),text$,&100,sx%,sy%
NEXT
PROCkararemember:PROCbufferrestore
ENDPROC

:
DEF FNkaracached
LOCAL i%
IF khvalid%=FALSE THEN =FALSE
IF kqcount%<>khcount% OR displaysong%<>khsong% OR musicmode%<>khmode% THEN =FALSE
IF displayclock%<khclock% OR displayclock% DIV 800<>khclock% DIV 800 THEN =FALSE
IF musicmode%=5 THEN
  IF trackerrow%<>khrow% THEN =FALSE
ELSE
  FOR i%=0 TO 2:IF current%(i%)<>khnote%(i%) THEN =FALSE
  NEXT
ENDIF
IF kqcount%=0 THEN =FALSE
FOR i%=0 TO kqcount%-1
  IF kqtext$(i%)<>khtext$(i%) OR kqx(i%)<>khx(i%) OR kqy%(i%)<>khy%(i%) THEN =FALSE
  IF kqfont%(i%)<>khfactor%(i%) OR kqfore%(i%)<>khfore%(i%) OR kqback%(i%)<>khback%(i%) THEN =FALSE
  IF kqtop%(i%)<>khtop%(i%) OR kqbottom%(i%)<>khbottom%(i%) THEN =FALSE
NEXT
=TRUE
:
DEF PROCkararemember
LOCAL i%
khcount%=kqcount%:khsong%=displaysong%:khmode%=musicmode%:khclock%=displayclock%:khrow%=trackerrow%
FOR i%=0 TO 2:khnote%(i%)=current%(i%):NEXT
IF kqcount%>0 THEN
  FOR i%=0 TO kqcount%-1
    khtext$(i%)=kqtext$(i%):khx(i%)=kqx(i%):khy%(i%)=kqy%(i%):khfactor%(i%)=kqfont%(i%)
    khfore%(i%)=kqfore%(i%):khback%(i%)=kqback%(i%):khtop%(i%)=kqtop%(i%):khbottom%(i%)=kqbottom%(i%)
  NEXT
ENDIF
khvalid%=TRUE
ENDPROC
:
DEF PROCkarapatch(x0%,y0%,x1%,y1%)
LOCAL l%,b%,r%,t%
l%=INT(x0%*khw%/600):r%=INT((x1%*khw%+599)/600)-1
b%=INT((390-y1%)*khh%/390):t%=INT(((390-y0%)*khh%+389)/390)-1
VDU 24,l%*2;b%*2;r%*2;t%*2;
SYS "OS_SpriteOp",&234,bufferarea%,bufferptr%,0,0,32,khcompose%,0
ENDPROC
