fixed wrong scroll up behaviour

Former-commit-id: 07837a20ae008e1072497e25b25709a6cbe97df1
Former-commit-id: 93db3957e5e9d745c78a864eb2cddd5a80a94320
This commit is contained in:
Song Minjae
2016-10-04 01:36:45 +09:00
parent cd2bcaf5cc
commit a4a9c037a6
2 changed files with 33 additions and 12 deletions

View File

@@ -12,8 +12,10 @@ constructor(var width: Int, var height: Int) {
/** /**
* 0000_0000_00000000 * 0000_0000_00000000
* Upper bits: Background colour 0 black 1 dark grey 2 grey 3 white * Upper bits: Background colour
* Middle bits: Foreground colour ditto. *
* Middle bits: Foreground colour
*
* Lower 8 bits: CP437 * Lower 8 bits: CP437
*/ */
internal val frameBuffer: CharArray 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) { 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") throw ArrayIndexOutOfBoundsException("x: $x, y; $y")
frameBuffer[y * width + x] = ((c.toInt().and(0xFF)) + colourKey.shl(8)).toChar() frameBuffer[y * width + x] = ((c.toInt().and(0xFF)) + colourKey.shl(8)).toChar()
} }

View File

@@ -5,6 +5,7 @@ import net.torvald.aa.ColouredFastFont
import net.torvald.terrarum.blendNormal import net.torvald.terrarum.blendNormal
import net.torvald.terrarum.blendMul import net.torvald.terrarum.blendMul
import net.torvald.terrarum.blendScreen import net.torvald.terrarum.blendScreen
import net.torvald.terrarum.gameactors.abs
import net.torvald.terrarum.virtualcomputer.computer.BaseTerrarumComputer import net.torvald.terrarum.virtualcomputer.computer.BaseTerrarumComputer
import org.lwjgl.BufferUtils import org.lwjgl.BufferUtils
import org.lwjgl.openal.AL import org.lwjgl.openal.AL
@@ -205,13 +206,15 @@ open class SimpleTextTerminal(
/** Emits a bufferChar. Does not move cursor /** Emits a bufferChar. Does not move cursor
* It is also not affected by the control sequences; just print them out as symbol */ * It is also not affected by the control sequences; just print them out as symbol */
override fun emitChar(bufferChar: Int, x: Int, y: Int) { 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 /** Emits a char. Does not move cursor
* It is also not affected by the control sequences; just print them out as symbol */ * It is also not affected by the control sequences; just print them out as symbol */
override fun emitChar(c: Char, x: Int, y: Int) { 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. */ /** Prints a char and move cursor accordingly. */
@@ -292,15 +295,31 @@ open class SimpleTextTerminal(
} }
override fun scroll(amount: Int) { 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 if (amount == 0) return
for (i in offset..screenBuffer.sizeof.ushr(1) - 1) {
screenBuffer.frameBuffer[i - offset] = screenBuffer.frameBuffer[i] 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) { else {
cursorY -= 1 for (i in bsize - 1 downTo 0) {
clearLine() 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
} }
} }