diff --git a/engines/toon/toon.cpp b/engines/toon/toon.cpp index 44c747c..30c4239 100644 --- a/engines/toon/toon.cpp +++ b/engines/toon/toon.cpp @@ -464,24 +464,42 @@ void ToonEngine::doMagnifierEffect() { 11, 11, 11, 11, 12 }; + // clip the range so that it doesn't go outside the screen + int32 minY = -12, maxY = 12, minX = -12, maxX = 12; + if (minY + posY < 0) + minY = -posY; + if (maxY + posY >= surface.h) + maxY = surface.h - posY - 1; + if (minX + posX < 0) + minX = -posX; + if (maxX + posX >= surface.w) + maxX = surface.w - posX - 1; + byte tempBuffer[25 * 25]; - for (int32 y = -12; y <= 12; y++) { - for (int32 x = -12; x <= 12; x++) { + for (int32 y = minY; y <= maxY; y++) { + for (int32 x = minX; x <= maxX; x++) { int32 destPitch = surface.pitch; uint8 *curRow = (uint8 *)surface.pixels + (posY + y) * destPitch + (posX + x); tempBuffer[(y + 12) * 25 + x + 12] = *curRow; } } - for (int32 y = -12; y <= 12; y++) { - for (int32 x = -12; x <= 12; x++) { + for (int32 y = minY; y <= maxY; y++) { + for (int32 x = minX; x <= maxX; x++) { int32 dist = y * y + x * x; if (dist > 144) continue; int32 destPitch = surface.pitch; uint8 *curRow = (uint8 *)surface.pixels + (posY + y) * destPitch + (posX + x); + + // force the magnification source to only use pixels which are present int32 lerp = (512 + intSqrt[dist] * 256 / 12); - *curRow = tempBuffer[(y * lerp / 1024 + 12) * 25 + x * lerp / 1024 + 12]; + int32 srcX = (x * lerp / 1024); + srcX = MAX(MIN(srcX, maxX), minX); + int32 srcY = (y * lerp / 1024); + srcY = MAX(MIN(srcY, maxY), minY); + + *curRow = tempBuffer[(srcY + 12) * 25 + srcX + 12]; } } }