the multiview emulator thingamajig

This commit is contained in:
minjaesong
2022-10-23 19:40:23 +09:00
parent 5222d9c962
commit 477156e1bd
8 changed files with 602 additions and 3 deletions

View File

@@ -0,0 +1,52 @@
package net.torvald.terrarum
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
/**
* Don't flip the assets! Flip the draw command instead!
*
* Created by minjaesong on 2021-12-13.
*/
class FlippingSpriteBatch : SpriteBatch() {
/**
* This function draws the flipped version of the image by giving flipped uv-coord to the SpriteBatch
*/
override fun draw(texture: Texture, x: Float, y: Float, width: Float, height: Float) =
draw(texture, x, y, width, height, 0f, 0f, 1f, 1f)
override fun draw(texture: Texture, x: Float, y: Float) =
draw(texture, x, y, texture.width.toFloat(), texture.height.toFloat(), 0f, 0f, 1f, 1f)
fun drawFlipped(texture: Texture, x: Float, y: Float, width: Float, height: Float) =
draw(texture, x, y, width, height, 0f, 1f, 1f, 0f)
fun drawFlipped(texture: Texture, x: Float, y: Float) =
draw(texture, x, y, texture.width.toFloat(), texture.height.toFloat(), 0f, 1f, 1f, 0f)
/**
* This function does obey the flipping set to the TextureRegion and try to draw flipped version of it,
* without touching the flipping setting of the given region.
*/
override fun draw(region: TextureRegion, x: Float, y: Float, width: Float, height: Float) =
draw(region.texture, x, y, width, height, region.u, region.v, region.u2, region.v2)
override fun draw(region: TextureRegion, x: Float, y: Float) =
draw(region.texture, x, y, region.regionWidth.toFloat(), region.regionHeight.toFloat(), region.u, region.v, region.u2, region.v2)
fun drawFlipped(region: TextureRegion, x: Float, y: Float, width: Float, height: Float) =
draw(region.texture, x, y, width, height, region.u, region.v2, region.u2, region.v)
fun drawFlipped(region: TextureRegion, x: Float, y: Float) =
draw(region.texture, x, y, region.regionWidth.toFloat(), region.regionHeight.toFloat(), region.u, region.v2, region.u2, region.v)
/**
* NOTE TO SELF:
*
* It seems that original SpriteBatch Y-flips when it's drawing a texture, but NOT when it's drawing a textureregion
*
* (textureregion's default uv-coord is (0,0,1,1)
*/
}

View File

@@ -0,0 +1,106 @@
package net.torvald.terrarum.imagefont
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.GlyphLayout
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import kotlin.math.roundToInt
/**
* Created by minjaesong on 2016-04-15.
*/
object TinyAlphNum : BitmapFont() {
internal val W = 7
internal val H = 13
internal val fontSheet = TextureRegionPack(Gdx.files.internal("net/torvald/terrarum/imagefont/font.tga"), W, H)
init {
setOwnsTexture(true)
setUseIntegerPositions(true)
}
fun getWidth(str: String): Int {
var l = 0
for (char in str) {
if (!isColourCodeHigh(char) && !isColourCodeLow(char)) {
l += 1
}
}
return W * l
}
lateinit var colMain: Color
lateinit var colShadow: Color
override fun draw(batch: Batch, text: CharSequence, x: Float, y: Float): GlyphLayout? {
val originalColour = batch.color.cpy()
colMain = batch.color.cpy()
colShadow = colMain.cpy().mul(0.5f, 0.5f, 0.5f, 1f)
val x = x.roundToInt().toFloat()
val y = y.roundToInt().toFloat()
var charsPrinted = 0
text.forEachIndexed { index, c ->
if (isColourCodeHigh(c)) {
val cchigh = c
val cclow = text[index + 1]
val colour = getColour(cchigh, cclow)
colMain = colour
colShadow = colMain.cpy().mul(0.5f, 0.5f, 0.5f, 1f)
}
else if (c in 0.toChar()..255.toChar()) {
batch.color = colShadow
batch.draw(fontSheet.get(c.toInt() % 16, c.toInt() / 16), x + charsPrinted * W + 1, y)
batch.draw(fontSheet.get(c.toInt() % 16, c.toInt() / 16), x + charsPrinted * W, y + 1)
batch.draw(fontSheet.get(c.toInt() % 16, c.toInt() / 16), x + charsPrinted * W + 1, y + 1)
batch.color = colMain
batch.draw(fontSheet.get(c.toInt() % 16, c.toInt() / 16), x + charsPrinted * W, y)
charsPrinted += 1
}
}
batch.color = originalColour
return null
}
override fun getLineHeight() = H.toFloat()
override fun getCapHeight() = getLineHeight()
override fun getXHeight() = getLineHeight()
private fun isColourCodeHigh(c: Char) = c.toInt() in 0b110110_1111000000..0b110110_1111111111
private fun isColourCodeLow(c: Char) = c.toInt() in 0b110111_0000000000..0b110111_1111111111
private fun getColour(charHigh: Char, charLow: Char): Color { // input: 0x10ARGB, out: RGBA8888
val codePoint = Character.toCodePoint(charHigh, charLow)
if (colourBuffer.containsKey(codePoint))
return colourBuffer[codePoint]!!
val a = codePoint.and(0xF000).ushr(12)
val r = codePoint.and(0x0F00).ushr(8)
val g = codePoint.and(0x00F0).ushr(4)
val b = codePoint.and(0x000F)
val col = Color(r.shl(28) or r.shl(24) or g.shl(20) or g.shl(16) or b.shl(12) or b.shl(8) or a.shl(4) or a)
colourBuffer[codePoint] = col
return col
}
private val colourBuffer = HashMap<Int, Color>()
}

Binary file not shown.