print book without GPU

This commit is contained in:
minjaesong
2024-05-16 01:35:58 +09:00
parent 1aecfc781f
commit 7320a14a4d
8 changed files with 383 additions and 141 deletions

View File

@@ -1,11 +1,14 @@
package net.torvald.terrarum.imagefont
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Pixmap
import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.GlyphLayout
import net.torvald.terrarum.roundToFloat
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import kotlin.math.roundToInt
/**
* Created by minjaesong on 2016-04-15.
@@ -16,13 +19,18 @@ object TinyAlphNum : BitmapFont() {
internal const val H = 13
internal val fontSheet = TextureRegionPack("./assets/graphics/fonts/7x13_Tamzen7x14b.tga", W+1, H+1)
internal val fontPixmap = Pixmap(Gdx.files.internal("./assets/graphics/fonts/7x13_Tamzen7x14b.tga"))
init {
setOwnsTexture(true)
setUseIntegerPositions(true)
}
override fun dispose() {
fontSheet.dispose()
fontPixmap.dispose()
}
fun getWidth(str: String): Int {
var l = 0
for (char in str) {
@@ -33,7 +41,8 @@ object TinyAlphNum : BitmapFont() {
return W * l
}
lateinit var colMain: Color
private var colMain = Color.WHITE
private var colMainInt = -1
override fun draw(batch: Batch, text: CharSequence, x: Float, y: Float): GlyphLayout? {
val originalColour = batch.color.cpy()
@@ -65,6 +74,79 @@ object TinyAlphNum : BitmapFont() {
return null
}
fun drawToPixmap(pixmap: Pixmap, text: String, x: Int, y: Int) {
var charsPrinted = 0
text.forEachIndexed { index, c ->
if (isColourCodeHigh(c)) {
val cchigh = c
val cclow = text[index + 1]
val colour = getColour(cchigh, cclow)
colMainInt = colour.toRGBA8888()
}
else if (c in 0.toChar()..255.toChar()) {
val srcX = (c.code % 16) * (W+1)
val srcY = (c.code / 16) * (H+1)
val destX = x + charsPrinted * W
val destY = y
pixmap.drawPixmap(fontPixmap, srcX, srcY, W+1, H+1, destX, destY, colMainInt)
charsPrinted += 1
}
}
}
/***
* @param col RGBA8888 representation
*/
private fun Pixmap.drawPixmap(pixmap: Pixmap, srcX: Int, srcY: Int, srcW: Int, srcH: Int, destX: Int, destY: Int, col: Int) {
for (y in srcY until srcY + srcH) {
for (x in srcX until srcX + srcW) {
val pixel = pixmap.getPixel(x, y)
val newPixel = pixel colorTimes col
this.drawPixel(destX + x - srcX, destY + y - srcY, newPixel)
}
}
}
private fun Color.toRGBA8888() =
(this.r * 255f).toInt().shl(24) or
(this.g * 255f).toInt().shl(16) or
(this.b * 255f).toInt().shl(8) or
(this.a * 255f).toInt()
/**
* RGBA8888 representation
*/
private fun Int.forceOpaque() = this.and(0xFFFFFF00.toInt()) or 0xFF
private infix fun Int.colorTimes(other: Int): Int {
val thisBytes = IntArray(4) { this.ushr(it * 8).and(255) }
val otherBytes = IntArray(4) { other.ushr(it * 8).and(255) }
return (thisBytes[0] times256 otherBytes[0]) or
(thisBytes[1] times256 otherBytes[1]).shl(8) or
(thisBytes[2] times256 otherBytes[2]).shl(16) or
(thisBytes[3] times256 otherBytes[3]).shl(24)
}
private infix fun Int.times256(other: Int) = multTable255[this][other]
private val multTable255 = Array(256) { left ->
IntArray(256) { right ->
(255f * (left / 255f).times(right / 255f)).roundToInt()
}
}
override fun getLineHeight() = H.toFloat()
override fun getCapHeight() = getLineHeight()
override fun getXHeight() = getLineHeight()

View File

@@ -13,7 +13,9 @@ import com.badlogic.gdx.graphics.glutils.ShaderProgram
import net.torvald.btex.BTeXParser
import net.torvald.terrarum.*
import net.torvald.terrarum.btex.BTeXDocument
import net.torvald.terrarum.imagefont.TinyAlphNum
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.ui.Toolkit
import net.torvald.unicode.EMDASH
import java.io.File
import kotlin.system.measureTimeMillis
@@ -44,7 +46,10 @@ class BTeXTest : ApplicationAdapter() {
)
override fun create() {
Lang.invoke()
Lang
TinyAlphNum
Toolkit
BTeXParser.BTeXHandler.preloadFonts()
batch = FlippingSpriteBatch(1000)
camera = OrthographicCamera(1280f, 720f)
@@ -57,26 +62,27 @@ class BTeXTest : ApplicationAdapter() {
val isBookFinalised = filePath.endsWith(".btxbook")
if (!isBookFinalised) {
measureTimeMillis {
val f = BTeXParser.invoke(Gdx.files.internal("./assets/mods/basegame/books/$filePath"), varMap)
document = f.first
documentHandler = f.second
}.also {
println("Time spent on typesetting [ms]: $it")
}
Thread {
measureTimeMillis {
val f = BTeXParser.invoke(Gdx.files.internal("./assets/mods/basegame/books/$filePath"), varMap)
document = f.first
documentHandler = f.second
}.also {
println("Time spent on typesetting [ms]: $it")
}
/*measureTimeMillis {
document.finalise()
documentHandler.dispose()
}.also {
println("Time spent on finalising [ms]: $it")
}
measureTimeMillis {
document.finalise()
}.also {
println("Time spent on finalising [ms]: $it")
}
measureTimeMillis {
/*measureTimeMillis {
document.serialise(File("./assets/mods/basegame/books/${filePath.replace(".xml", ".btxbook")}"))
}.also {
println("Time spent on serialisation [ms]: $it")
}*/
}.also {
println("Time spent on serialisation [ms]: $it")
}*/
}.start()
}
else {
measureTimeMillis {
@@ -96,28 +102,35 @@ class BTeXTest : ApplicationAdapter() {
gdxClearAndEnableBlend(.063f, .070f, .086f, 1f)
val drawX = (1280 - (pageGap + document.pageDimensionWidth*2)) / 2
val drawY = 24
if (::document.isInitialized) {
if (document.isFinalised) {
val drawX = (1280 - (pageGap + document.pageDimensionWidth * 2)) / 2
val drawY = 24
batch.inUse {
batch.color = Color.WHITE
batch.draw(bg, 0f, 0f)
batch.inUse {
batch.color = Color.WHITE
batch.draw(bg, 0f, 0f)
if (scroll - 1 in document.pageIndices)
document.render(0f, batch, scroll - 1, drawX, drawY)
if (scroll in document.pageIndices)
document.render(0f, batch, scroll, drawX + (6 + document.pageDimensionWidth), drawY)
if (scroll - 1 in document.pageIndices)
document.render(0f, batch, scroll - 1, drawX, drawY)
if (scroll in document.pageIndices)
document.render(0f, batch, scroll, drawX + (6 + document.pageDimensionWidth), drawY)
}
if (Gdx.input.isKeyJustPressed(Input.Keys.LEFT))
scroll = (scroll - 2).coerceAtLeast(0)
else if (Gdx.input.isKeyJustPressed(Input.Keys.RIGHT))
scroll =
(scroll + 2).coerceAtMost(
document.pageIndices.endInclusive.toFloat().div(2f).ceilToInt().times(2)
)
else if (Gdx.input.isKeyJustPressed(Input.Keys.PAGE_UP))
scroll = 0
else if (Gdx.input.isKeyJustPressed(Input.Keys.PAGE_DOWN))
scroll = document.pageIndices.endInclusive.toFloat().div(2f).ceilToInt().times(2)
}
}
if (Gdx.input.isKeyJustPressed(Input.Keys.LEFT))
scroll = (scroll - 2).coerceAtLeast(0)
else if (Gdx.input.isKeyJustPressed(Input.Keys.RIGHT))
scroll = (scroll + 2).coerceAtMost(document.pageIndices.endInclusive.toFloat().div(2f).ceilToInt().times(2))
else if (Gdx.input.isKeyJustPressed(Input.Keys.PAGE_UP))
scroll = 0
else if (Gdx.input.isKeyJustPressed(Input.Keys.PAGE_DOWN))
scroll = document.pageIndices.endInclusive.toFloat().div(2f).ceilToInt().times(2)
}

View File

@@ -188,6 +188,18 @@ object Toolkit : Disposable {
}
// draws highly simplified box border
fun drawBoxBorderToPixmap(pixmap: Pixmap, x: Int, y: Int, w: Int, h: Int) {
// top edge
pixmap.fillRectangle(x, y - 1, w, 1)
// bottom edge
pixmap.fillRectangle(x, y + h, w, 1)
// left edge
pixmap.fillRectangle(x - 1, y, 1, h)
// right edge
pixmap.fillRectangle(x + w, y, 1, h)
}
private lateinit var blurtex0: Texture
private lateinit var blurtex1: Texture
private lateinit var blurtex2: Texture