From 8ae07c8d20af3bd2263949eb5e80e36967d59edd Mon Sep 17 00:00:00 2001 From: Song Minjae Date: Sat, 14 Jan 2017 16:00:07 +0900 Subject: [PATCH] working tapestry decoder and fixture object Former-commit-id: dd61b61e6061cfb3dc2ad9d9f65fd1b272475718 Former-commit-id: 7acc72a0e59203c0ea56c355eadaeed1474e3ac1 --- assets/theworld | Bin 0 -> 2074 bytes .../torvald/terrarum/console/CommandDict.kt | 3 +- .../torvald/terrarum/console/SpawnTapestry.kt | 25 ++++ .../terrarum/gameactors/DecodeTapestry.kt | 116 +++++++++++++++++- .../terrarum/gameactors/TapestryObject.kt | 14 ++- .../colourmap/64_from_master_final.act | Bin 772 -> 772 bytes 6 files changed, 151 insertions(+), 7 deletions(-) create mode 100644 assets/theworld create mode 100644 src/net/torvald/terrarum/console/SpawnTapestry.kt diff --git a/assets/theworld b/assets/theworld new file mode 100644 index 0000000000000000000000000000000000000000..b899ed5f7e2735f13a02678e079b8114186e90c8 GIT binary patch literal 2074 zcmcJPF^k+V5QRmWY$N0@NQLBX*+yJxgISUy`2`E90|}%E99)s#pZ8`ovTnUeb7TxB z+0ysk%t*Jl&!4`$bME8q=a2d8^UJUA?)Loh`y0mdhqpa%#LxdDVvK*4J*+I}!vj9< zU*ou*#ue@v7u!7gL4eb~Cnc<<)ZZbn``9c-F9N{eCSgEHjAA=Ez7Fh8F)1&5n*csy zwiI8l)tA@I@CIv8nm-Z6L{kLfKY8x+6lkc8ltHj^@2emDIK8w-cYEYcfs$)RuGcH( z%m<`rUwpLvZM0W9faGQ*|B2Xd0yd*pArZF7NUviW@vAyK(e?=0tG6hj)EhB!h z7cQW9RVd9r1jTb@W=s(q@Qg)VG)hK}$XpxP{7YcKALa1uw`~)VZEMs%z!E9;18DWX zmq05aScq%j*6yVFBWDV*1pI0J2haNH->wZ5aBGh|OO|^pZHN%C{sX5!|0VV8F*%w4 zdeMh1hX9L^vr6s!TLT46;!K@WF|Oh3syKTH0kjG=?y)SJR3-u^MCqJOqtXJUIoP;X{OnlPN6@}TXA1eWocxTC`klM$Km1`c H?f!oNC9f`S literal 0 HcmV?d00001 diff --git a/src/net/torvald/terrarum/console/CommandDict.kt b/src/net/torvald/terrarum/console/CommandDict.kt index 4fb115278..972743440 100644 --- a/src/net/torvald/terrarum/console/CommandDict.kt +++ b/src/net/torvald/terrarum/console/CommandDict.kt @@ -50,7 +50,8 @@ object CommandDict { Pair("testgetlight", TestGetLight), Pair("spawnball", SpawnPhysTestBall), Pair("spawntorch", SpawnTikiTorch), - Pair("musictest", MusicTest) + Pair("musictest", MusicTest), + Pair("spawntapestry", SpawnTapestry) ) operator fun get(commandName: String): ConsoleCommand { diff --git a/src/net/torvald/terrarum/console/SpawnTapestry.kt b/src/net/torvald/terrarum/console/SpawnTapestry.kt new file mode 100644 index 000000000..7b9a0c7e0 --- /dev/null +++ b/src/net/torvald/terrarum/console/SpawnTapestry.kt @@ -0,0 +1,25 @@ +package net.torvald.terrarum.console + +import net.torvald.terrarum.Terrarum +import net.torvald.terrarum.gameactors.DecodeTapestry +import java.io.File +import java.io.FileInputStream + +/** + * Created by SKYHi14 on 2017-01-14. + */ +object SpawnTapestry : ConsoleCommand { + override fun execute(args: Array) { + if (args.size < 2) { + printUsage() + return + } + + val tapestry = DecodeTapestry(File(args[1])) + Terrarum.ingame.addActor(tapestry) + } + + override fun printUsage() { + println("Usage: spawntapestry ") + } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/gameactors/DecodeTapestry.kt b/src/net/torvald/terrarum/gameactors/DecodeTapestry.kt index e0a2b6970..04dd0dc85 100644 --- a/src/net/torvald/terrarum/gameactors/DecodeTapestry.kt +++ b/src/net/torvald/terrarum/gameactors/DecodeTapestry.kt @@ -1,11 +1,20 @@ package net.torvald.terrarum.gameactors +import com.jme3.math.FastMath +import net.torvald.terrarum.gameworld.toUint import org.newdawn.slick.Color +import org.newdawn.slick.Image +import org.newdawn.slick.ImageBuffer +import org.newdawn.slick.opengl.ImageData +import org.newdawn.slick.opengl.TextureImpl import java.io.File +import java.nio.ByteBuffer +import java.nio.charset.Charset +import java.util.* object DecodeTapestry { - val colourIndices = arrayListOf( + val colourIndices64 = arrayOf( 0x333.fourBitCol(), 0x600.fourBitCol(), 0xA36.fourBitCol(), @@ -72,13 +81,116 @@ object DecodeTapestry { 0xEBA.fourBitCol() ) + val colourIndices16 = arrayOf( + 0x000.fourBitCol(), + 0xfff.fourBitCol(), + 0x666.fourBitCol(), + 0xccc.fourBitCol(), + 0xfe0.fourBitCol(), + 0xe60.fourBitCol(), + 0xe00.fourBitCol(), + 0xe2a.fourBitCol(), + 0x427.fourBitCol(), + 0x32f.fourBitCol(), + 0x4af.fourBitCol(), + 0x5f0.fourBitCol(), + 0x390.fourBitCol(), + 0x353.fourBitCol(), + 0x533.fourBitCol(), + 0xa63.fourBitCol() + ) + private fun Int.fourBitCol() = Color( this.and(0xF00).shl(12) + this.and(0xF00).shl(8) + this.and(0x0F0).shl(8) + this.and(0x0F0).shl(4) + this.and(0x00F).shl(4) + this.and(0x00F) ) - operator fun invoke(file: File) { + val MAGIC = "TEAF".toByteArray(charset = Charset.forName("US-ASCII")) + val FORMAT_16 = 1 + val FORMAT_64 = 2 + operator fun invoke(fileObj: File): TapestryObject { + fun magicMismatch(magic: ByteArray): Boolean { + MAGIC.forEachIndexed { i, byte -> + if (byte != magic[i]) + return true + } + return false + } + + val file = fileObj.readBytes() + + val magic = file.copyOfRange(0, 4) + + if (magicMismatch(magic)) + throw RuntimeException("Invalid file -- type mismatch: expected header " + + "${MAGIC[0]} ${MAGIC[1]} ${MAGIC[2]} ${MAGIC[3]}; got " + + "${magic[0]} ${magic[1]} ${magic[2]} ${magic[3]}") + + val colourModel = file[4].toUint() + + if (colourModel != FORMAT_16 && colourModel != FORMAT_64) + throw RuntimeException("Invalid colour model: $colourModel") + + val width = file[6].toUint().shl(8) + file[7].toUint() + + val artNameBytes = ArrayList() + val authorNameBytes = ArrayList() + + var readCounter = 8 + + while (file[readCounter] != 0x00.toByte()) { + artNameBytes.add(file[readCounter]) + readCounter++ + } + + readCounter++ // jump over null terminator + + while (file[readCounter] != 0x00.toByte()) { + authorNameBytes.add(file[readCounter]) + readCounter++ + } + + readCounter++ // jump over null terminator + + + + val artName = kotlin.text.String(artNameBytes.toByteArray(), charset = Charset.forName("UTF-8")) + val authorName = kotlin.text.String(authorNameBytes.toByteArray(), charset = Charset.forName("UTF-8")) + + val imageDataSize = file.size - readCounter + val height = imageDataSize / width + val outImageData = ImageBuffer(width, height) + val counterOffset = readCounter + while (readCounter < file.size) { + val ofs = readCounter - counterOffset + val palIndex = file[readCounter].toUint() + + if (colourModel == FORMAT_16) { + outImageData.setRGBA( + ofs % width, + ofs / width, + colourIndices16[palIndex].redByte, + colourIndices16[palIndex].greenByte, + colourIndices16[palIndex].blueByte, + 255 + ) + } + else { + outImageData.setRGBA( + ofs % width, + ofs / width, + colourIndices64[palIndex].redByte, + colourIndices64[palIndex].greenByte, + colourIndices64[palIndex].blueByte, + 255 + ) + } + + readCounter++ + } + + return TapestryObject(outImageData.image.getScaledCopy(2f), artName, authorName) } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/gameactors/TapestryObject.kt b/src/net/torvald/terrarum/gameactors/TapestryObject.kt index 8317efe14..feb8bb3af 100644 --- a/src/net/torvald/terrarum/gameactors/TapestryObject.kt +++ b/src/net/torvald/terrarum/gameactors/TapestryObject.kt @@ -1,17 +1,23 @@ package net.torvald.terrarum.gameactors -import org.newdawn.slick.Color +import net.torvald.terrarum.Terrarum +import net.torvald.terrarum.gamecontroller.mouseX +import net.torvald.terrarum.gamecontroller.mouseY import org.newdawn.slick.GameContainer import org.newdawn.slick.Graphics -import java.io.File +import org.newdawn.slick.Image /** * Created by SKYHi14 on 2017-01-07. */ -class TapestryObject(imageDataPath: String) : FixtureBase() { +class TapestryObject(val image: Image, val artName: String, val artAuthor: String) : FixtureBase() { init { - + makeNewSprite(image.width, image.height) + setHitboxDimension(image.width, image.height, 0, 0) + sprite!!.setSpriteImage(image) + isNoSubjectToGrav = true + setPosition(Terrarum.appgc.mouseX, Terrarum.appgc.mouseY) } override fun update(gc: GameContainer, delta: Int) { diff --git a/work_files/graphics/colourmap/64_from_master_final.act b/work_files/graphics/colourmap/64_from_master_final.act index 14451f449b11ab97a37381ec5700e33c73a1e62b..96b59c18ecd279d3504b97944b0fbd3b91987369 100644 GIT binary patch literal 772 zcmeHDNf87v2t!LPU|=6uz(Oow0SmQ&1uWD;Enp#A5BuFrUO8qmQB{bP0*cVK6=p{? zE!v*uYOVn-XxI`F03|Z3?&4l_OI=jZGj)qokk+*ZlK!f?_nwzW4Vac#1ZYL?N=`7J q&5EnLR;lrINmi7j#7)8BoK6VkD=8f5yep~+dKk&l?AMy(+(`P^c literal 772 zcmeH@F%bko2n5lx7BKJu7O*fDuz-cNfCVh9g|&c%`FjnYUT0m^QI}#+yfdrP5Y~}f(-ajH%_v&5J$mVx1~i!*?+T{6Hrtx+RY{(6_TB(gRbJi$e;)XdUjP+nKmY&$