diff --git a/src/net/torvald/aa/AAFrame.kt b/src/net/torvald/aa/AAFrame.kt index 64101da44..067828717 100644 --- a/src/net/torvald/aa/AAFrame.kt +++ b/src/net/torvald/aa/AAFrame.kt @@ -12,8 +12,10 @@ constructor(var width: Int, var height: Int) { /** * 0000_0000_00000000 - * Upper bits: Background colour 0 black 1 dark grey 2 grey 3 white - * Middle bits: Foreground colour ditto. + * Upper bits: Background colour + * + * Middle bits: Foreground colour + * * Lower 8 bits: CP437 */ internal val frameBuffer: CharArray @@ -25,7 +27,7 @@ constructor(var width: Int, var height: Int) { } fun drawBuffer(x: Int, y: Int, c: Char, colourKey: Int) { - if (y * width + x >= frameBuffer.size) + if (y * width + x >= frameBuffer.size || y * width + x < 0) throw ArrayIndexOutOfBoundsException("x: $x, y; $y") frameBuffer[y * width + x] = ((c.toInt().and(0xFF)) + colourKey.shl(8)).toChar() } diff --git a/src/net/torvald/terrarum/virtualcomputer/terminal/SimpleTextTerminal.kt b/src/net/torvald/terrarum/virtualcomputer/terminal/SimpleTextTerminal.kt index 857f95799..bb10a6460 100644 --- a/src/net/torvald/terrarum/virtualcomputer/terminal/SimpleTextTerminal.kt +++ b/src/net/torvald/terrarum/virtualcomputer/terminal/SimpleTextTerminal.kt @@ -5,6 +5,7 @@ import net.torvald.aa.ColouredFastFont import net.torvald.terrarum.blendNormal import net.torvald.terrarum.blendMul import net.torvald.terrarum.blendScreen +import net.torvald.terrarum.gameactors.abs import net.torvald.terrarum.virtualcomputer.computer.BaseTerrarumComputer import org.lwjgl.BufferUtils import org.lwjgl.openal.AL @@ -205,13 +206,15 @@ open class SimpleTextTerminal( /** Emits a bufferChar. Does not move cursor * It is also not affected by the control sequences; just print them out as symbol */ override fun emitChar(bufferChar: Int, x: Int, y: Int) { - screenBuffer.drawBuffer(x, y, bufferChar.toChar()) + try { screenBuffer.drawBuffer(x, y, bufferChar.toChar()) } + catch (e: ArrayIndexOutOfBoundsException) { e.printStackTrace() } } /** Emits a char. Does not move cursor * It is also not affected by the control sequences; just print them out as symbol */ override fun emitChar(c: Char, x: Int, y: Int) { - screenBuffer.drawBuffer(x, y, c.toInt().and(0xFF).toChar(), colourKey) + try { screenBuffer.drawBuffer(x, y, c.toInt().and(0xFF).toChar(), colourKey) } + catch (e: ArrayIndexOutOfBoundsException) { e.printStackTrace() } } /** Prints a char and move cursor accordingly. */ @@ -292,15 +295,31 @@ open class SimpleTextTerminal( } override fun scroll(amount: Int) { - if (amount < 0) throw IllegalArgumentException("cannot scroll up") + val offset = amount.times(width).abs() + val bsize = screenBuffer.sizeof.ushr(1) - val offset = amount * width - for (i in offset..screenBuffer.sizeof.ushr(1) - 1) { - screenBuffer.frameBuffer[i - offset] = screenBuffer.frameBuffer[i] + if (amount == 0) return + + if (amount > 0) { + for (i in 0..bsize - 1) { + if (i < Math.min(bsize, bsize - offset - 1)) + // displace contents in buffer + screenBuffer.frameBuffer[i] = screenBuffer.frameBuffer[i + offset] + else + // clean up garbages + screenBuffer.frameBuffer[i] = 0.toChar() + } + cursorY -= amount } - for (c in 1..amount) { - cursorY -= 1 - clearLine() + else { + for (i in bsize - 1 downTo 0) { + if (i > Math.max(offset - 1, 0)) + // displace contents in buffer + screenBuffer.frameBuffer[i] = screenBuffer.frameBuffer[i - offset] + else + screenBuffer.frameBuffer[i] = 0.toChar() + } + cursorY += amount } }