Hi @hasanchik I tried to optimize this as below, try it pls
I tested on my Meobit(same mcu with your Xtron Pro). Total time from 5.3s down to about 0.3~0.4s(with pause(0)).
What I changed:
- As each step(threshold) increase only 1 pixel in 4x4 dither sqr, I saved them into 2 arraies in order.
- Send prev updated image(bg2) into next dither() calling, so only call setPixel() on dots that really need to update.
It will much more faster if you remove basic.pause(0), almostly can’t see the changing progress, even on device.
Bad side, I didn’t read details for the logic about offx and offy, so just leave them along. Hope it’s not very complicate to add them back for you.
let ditherStepX=[0,2,0,2,1,1,3,3,0,2,0,2,1,3,1,3]
let ditherStepY=[0,2,2,0,1,3,3,1,1,3,3,1,0,2,2,0]
function dither(imgFrom: Image, threshold: number, color: number = 0, imgTo: Image = null, offx: number = 0, offy: number = 0) {
let og = imgFrom
for (let y = 0; y < imgFrom.height; y+=4) {
for (let x = 0; x < imgFrom.width; x+=4) {
if (imgTo) {
og.setPixel(x + ditherStepX[threshold], y + ditherStepY[threshold], imgTo.getPixel(x + ditherStepX[threshold], y + ditherStepY[threshold]))
}else
og.setPixel(x + ditherStepX[threshold], y + ditherStepY[threshold], color)
}
}
return og
}
let bgFrom = sprites.background.lilypads
let bgTo = sprites.background.lunarnewyear
let bgDither:Image
doDither()
function doDither(delay:number=0){
bgDither=bgFrom.clone()
let msBegin = control.millis()
for (let index = 0; index <= 15; index++) {
bgDither = dither(bgDither, index, 1, bgTo, 4, 4)
scene.setBackgroundImage(bgDither)
basic.pause(delay)
}
info.setScore((control.millis() - msBegin))
}