diff --git a/.idea/libraries/com_badlogicgames_gdx_gdx_1_10_0.xml b/.idea/libraries/badlogicgames_gdx.xml similarity index 79% rename from .idea/libraries/com_badlogicgames_gdx_gdx_1_10_0.xml rename to .idea/libraries/badlogicgames_gdx.xml index 07d1917..53c83a1 100644 --- a/.idea/libraries/com_badlogicgames_gdx_gdx_1_10_0.xml +++ b/.idea/libraries/badlogicgames_gdx.xml @@ -1,5 +1,5 @@ - + diff --git a/.idea/libraries/badlogicgames_gdx_backend_lwjgl3.xml b/.idea/libraries/badlogicgames_gdx_backend_lwjgl3.xml new file mode 100644 index 0000000..f31d8e6 --- /dev/null +++ b/.idea/libraries/badlogicgames_gdx_backend_lwjgl3.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/BuildJAR_TerrarumSansBitmap.iml b/BuildJAR_TerrarumSansBitmap.iml index a395c44..482ad08 100755 --- a/BuildJAR_TerrarumSansBitmap.iml +++ b/BuildJAR_TerrarumSansBitmap.iml @@ -7,7 +7,7 @@ - + \ No newline at end of file diff --git a/FontTestGDX/FontTestGDX.iml b/FontTestGDX/FontTestGDX.iml index de9052b..c05f7d0 100755 --- a/FontTestGDX/FontTestGDX.iml +++ b/FontTestGDX/FontTestGDX.iml @@ -9,5 +9,7 @@ + + \ No newline at end of file diff --git a/FontTestGDX/src/FontTestGDX.kt b/FontTestGDX/src/FontTestGDX.kt index b64ed72..3b0d6e8 100755 --- a/FontTestGDX/src/FontTestGDX.kt +++ b/FontTestGDX/src/FontTestGDX.kt @@ -1,6 +1,6 @@ import com.badlogic.gdx.* -import com.badlogic.gdx.backends.lwjgl.LwjglApplication -import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration +import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application +import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration import com.badlogic.gdx.graphics.* import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.glutils.FrameBuffer @@ -109,7 +109,7 @@ class FontTestGDX : Game() { batch.begin() batch.color = Color.WHITE - batch.draw(tex, 0f, (TEXH.toFloat()/appConfig.height)*TEXH - scrollOffsetY, TEXW.toFloat(), -(TEXH.toFloat() / appConfig.height) * TEXH.toFloat()) + batch.draw(tex, 0f, (TEXH.toFloat()/HEIGHT)*TEXH - scrollOffsetY, TEXW.toFloat(), -(TEXH.toFloat() / HEIGHT) * TEXH.toFloat()) batch.end() @@ -129,19 +129,19 @@ class FontTestGDX : Game() { } fun scrollAdd(x: Int = 1) { - scrollOffsetY -= (TEXH.toFloat() / appConfig.height) * 20f * x + scrollOffsetY -= (TEXH.toFloat() / HEIGHT) * 20f * x } fun scrollSub(x: Int = 1) { - scrollOffsetY += (TEXH.toFloat() / appConfig.height) * 20f * x + scrollOffsetY += (TEXH.toFloat() / HEIGHT) * 20f * x } class Navigator(val main: FontTestGDX) : InputAdapter() { - override fun scrolled(amount: Int): Boolean { - if (amount >= 0) - main.scrollSub(amount) + override fun scrolled(amountX: Float, amountY: Float): Boolean { + if (amountY >= 0) + main.scrollSub(amountY.toInt()) else - main.scrollAdd(-amount) + main.scrollAdd(-amountY.toInt()) return true } @@ -157,17 +157,19 @@ class FontTestGDX : Game() { } } -lateinit var appConfig: LwjglApplicationConfiguration +lateinit var appConfig: Lwjgl3ApplicationConfiguration const val TEXW = 874 const val TEXH = 2400 -fun main(args: Array) { - appConfig = LwjglApplicationConfiguration() - appConfig.vSyncEnabled = false - appConfig.resizable = false//true; - appConfig.width = TEXW - appConfig.height = 768 - appConfig.title = "Terrarum Sans Bitmap Test (GDX)" +const val WIDTH = TEXW +const val HEIGHT = 768 - LwjglApplication(FontTestGDX(), appConfig) +fun main(args: Array) { + appConfig = Lwjgl3ApplicationConfiguration() + appConfig.useVsync(false) + appConfig.setResizable(false) + appConfig.setWindowedMode(WIDTH, HEIGHT) + appConfig.setTitle("Terrarum Sans Bitmap Test") + + Lwjgl3Application(FontTestGDX(), appConfig) } diff --git a/src/net/torvald/terrarumsansbitmap/gdx/GameFontBase.kt b/src/net/torvald/terrarumsansbitmap/gdx/GameFontBase.kt index e24f4af..0f10a1c 100755 --- a/src/net/torvald/terrarumsansbitmap/gdx/GameFontBase.kt +++ b/src/net/torvald/terrarumsansbitmap/gdx/GameFontBase.kt @@ -37,6 +37,7 @@ import java.security.MessageDigest import java.util.* import java.util.zip.CRC32 import java.util.zip.GZIPInputStream +import kotlin.collections.HashMap import kotlin.math.roundToInt import kotlin.math.sign @@ -117,8 +118,11 @@ class GameFontBase( //private val textCache = HashMap() private data class TextCacheObj(var age: Int, val hash: Long, val glyphLayout: ShittyGlyphLayout?): Comparable { +// var disposed = false; private set + fun dispose() { glyphLayout?.linotype?.dispose() +// disposed = true } override fun compareTo(other: TextCacheObj): Int { @@ -127,80 +131,41 @@ class GameFontBase( } private var textCacheCap = 0 - private val textCache = Array(textCacheSize) { TextCacheObj(-1, -1L, null) } + private val textCache = TreeMap() /** * Insertion sorts the last element fo the textCache */ private fun addToCache(text: CodepointSequence, linotype: Texture, width: Int) { - // Caching rules: - // 1. always accept new element. - // 2. often-called element have higher chance of survival (see: getCache(long)) - + val cacheObj = TextCacheObj(0, text.getHash(), ShittyGlyphLayout(text, linotype, width)) if (textCacheCap < textCacheSize) { - textCache[textCacheCap] = TextCacheObj(0, text.getHash(), ShittyGlyphLayout(text, linotype, width)) + textCache[cacheObj.hash] = cacheObj textCacheCap += 1 - - // make everybody age - textCache.forEach { - it.age += 1 - } } else { - // search for an oldest element - var oldestElemIndex = 0 - var ageOfOldest = textCacheCap - textCache.forEachIndexed { index, it -> - // make everybody age - textCache[index].age += 1 + // randomly eliminate one + textCache.remove(textCache.keys.random())!!.dispose() - // mark oldest - if (it.age > ageOfOldest) { - oldestElemIndex = index - } - } - - - // dispose of the oldest one before overwriting - textCache[oldestElemIndex].dispose() - - // overwrite oldest one - textCache[oldestElemIndex] = TextCacheObj(0, text.getHash(), ShittyGlyphLayout(text, linotype, width)) + // add new one + textCache[cacheObj.hash] = cacheObj } - - // sort the list - textCache.sortBy { it.hash } } private fun getCache(hash: Long): TextCacheObj? { - var low = 0 - var high = textCacheCap - 1 - var key = -1 + val cache = textCache[hash] - - while (low <= high) { - val mid = (low + high).ushr(1) // safe from overflows - - val midVal = textCache[mid] - - if (hash > midVal.hash) - low = mid + 1 - else if (hash < midVal.hash) - high = mid - 1 - else { - key = mid - break // the condition is not enough to break the loop - } - } - - if (key < 0) + if (cache == null) return null - - // decrement age count (see: addToCache(CodepointSequence, Pixmap, Int)) - if (textCache[key].age > 0) textCache[key].age -= 1 - - return textCache[key] +// else if (cache.disposed) { +// textCache.remove(hash) +// return null +// } + else { + // decrement age count (see: addToCache(CodepointSequence, Pixmap, Int)) + if (cache.age > 0) cache.age -= 1 + return cache + } } @@ -559,7 +524,7 @@ class GameFontBase( override fun dispose() { super.dispose() - textCache.forEach { it.dispose() } + textCache.values.forEach { it.dispose() } sheets.forEach { it.dispose() } } @@ -1307,18 +1272,6 @@ class GameFontBase( - private fun CharSequence.sha256(): ByteArray { - val digest = MessageDigest.getInstance("SHA-256") - this.forEach { - val it = it.toInt() - val b1 = it.shl(8).and(255).toByte() - val b2 = it.and(255).toByte() - digest.update(b1) - digest.update(b2) - } - - return digest.digest() - } private fun CharSequence.crc32(): Int { val crc = CRC32() this.forEach {