new item type, "Dynamic Item"; working text terminal

Former-commit-id: 81e6d836f5f1e6490027d38146a32d404cf9ce3e
Former-commit-id: af6557340f9cd0ea0b67eb7a8825aeffe75f9d82
This commit is contained in:
Song Minjae
2016-09-10 16:45:04 +09:00
parent 9b9b65efba
commit d8b70887a9
69 changed files with 1310 additions and 131 deletions

View File

@@ -0,0 +1,86 @@
package net.torvald.aa
import net.torvald.terrarum.gameworld.toUint
import org.newdawn.slick.*
/**
* Created by minjaesong on 16-08-10.
*/
class AAFrame @Throws(SlickException::class)
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.
* Lower 8 bits: CP437
*/
internal val frameBuffer: CharArray
val sizeof = 2 * width * height // magic number 2: indicator that we're using char
init {
frameBuffer = CharArray(width * height)
}
fun drawBuffer(x: Int, y: Int, c: Char, colourKey: Int) {
if (y * width + x >= frameBuffer.size)
throw ArrayIndexOutOfBoundsException("x: $x, y; $y")
frameBuffer[y * width + x] = ((c.toInt().and(0xFF)) + colourKey.shl(8)).toChar()
}
fun drawBuffer(x: Int, y: Int, raw: Char): Boolean =
if (checkOOB(x, y))
false
else {
frameBuffer[y * width + x] = raw
true
}
fun drawFromBytes(other: ByteArray) {
for (i in 0..other.size - 1 step 2) {
val char = (other[i].toUint().shl(8) + other[i + 1].toUint()).toChar()
frameBuffer[i.ushr(1)] = char
}
}
fun getBackgroundColour(x: Int, y: Int): Int {
return frameBuffer[y * width + x].toInt().ushr(12) and 0xF
}
fun getForegroundColour(x: Int, y: Int): Int {
return frameBuffer[y * width + x].toInt().ushr(8) and 0xF
}
fun getChar(x: Int, y: Int): Char {
return (frameBuffer[y * width + x].toInt() and 0xFF).toChar()
}
fun getRaw(x: Int, y: Int): Char? =
if (checkOOB(x, y))
null
else
frameBuffer[y * width + x]
fun clear(background: Int = 0) {
for (y in 0..height - 1) {
for (x in 0..width - 1) {
drawBuffer(x, y, 0.toChar(), background.shl(4))
}
}
}
fun drawFromOther(other: AAFrame) {
//this.framebuffer = other.getFrameBuffer();
for (y in 0..height - 1) {
for (x in 0..width - 1) {
frameBuffer[y * width + x] = other.getRaw(x, y)!!
}
}
}
private fun checkOOB(x: Int, y: Int) = (x < 0 || y < 0 || x >= width || y >= height)
fun getColourKey(x: Int, y: Int): Int = frameBuffer[y * width + x].toInt().ushr(8).and(0xFF)
}

View File

@@ -0,0 +1,102 @@
package net.torvald.aa
import net.torvald.terrarum.virtualcomputers.terminal.Terminal
import org.newdawn.slick.Color
import org.newdawn.slick.Font
import org.newdawn.slick.Image
import org.newdawn.slick.SpriteSheet
import java.util.*
/**
* Based on multisheet slick spritesheef font (net.torvald.imagefont.GameFontBase) of my game project.
* Again, based on my Ba-AA project (github.com/minjaesong/ba-aa)
*
* Created by minjaesong on 16-08-12.
* Adopted by minjaesong on 16-09-07.
*/
class ColouredFastFont(val vt: Terminal, fontRef: String, val fontW: Int, val fontH: Int) : Font {
val colouredSheet = ArrayList<SpriteSheet>() // index zero: dark grey
private var sheetW = 0
private var sheetH = 0
private lateinit var sheetImageBuffer: Image
init {
val getSizeImg = Image(fontRef)
sheetW = getSizeImg.width
sheetH = getSizeImg.height
getSizeImg.destroy()
sheetImageBuffer = Image(sheetW, sheetH)
for (i in 0..vt.coloursCount - 1) {
val sheet = SpriteSheet("$fontRef.$i.png", fontW, fontH)
colouredSheet.add(sheet)
//sheetImageBuffer.graphics.clear()
}
sheetImageBuffer.destroy()
}
private fun getIndexX(ch: Char) = ch.toInt() % (sheetW / fontW)
private fun getIndexY(ch: Char) = ch.toInt() / (sheetW / fontW)
override fun getHeight(p0: String): Int = fontH
override fun getWidth(p0: String): Int {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getLineHeight(): Int = fontH
override fun drawString(p0: Float, p1: Float, p2: String) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun drawString(p0: Float, p1: Float, p2: String, p3: Color) {
//search colour
var colourIndex = -1
for (i in 0..vt.coloursCount - 1) {
if (vt.getColor(i) == p3) {
colourIndex = i
break
}
}
if (colourIndex >= 0) {
colouredSheet[colourIndex].startUse()
for (i in 0..p2.length - 1) {
val ch = p2[i]
colouredSheet[colourIndex].renderInUse(
p0.floorInt() + (i * fontW),
p1.floorInt(),
getIndexX(ch),
getIndexY(ch)
)
}
colouredSheet[colourIndex].endUse()
}
else {
//System.err.println("[ColouredFastFont] unmatched colour! $p3")
}
}
override fun drawString(p0: Float, p1: Float, p2: String, p3: Color, p4: Int, p5: Int) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
fun Float.floorInt() = this.toInt()
}