Skip to content

Commit

Permalink
Merge pull request #451 from Reco1I/optimze-texture-font
Browse files Browse the repository at this point in the history
Introduce texture font and optimize texture-based scoring counters
  • Loading branch information
Rian8337 authored Nov 15, 2024
2 parents ec56cf8 + c674052 commit cdec2aa
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 84 deletions.
15 changes: 13 additions & 2 deletions src/com/reco1l/andengine/container/Container.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,20 @@ open class Container : ExtendedEntity() {

val child = mChildren.getOrNull(i) ?: continue

var offsetX = child.x
var offsetY = child.y

if (child is ExtendedEntity) {
offsetX += child.originOffsetX
offsetY += child.originOffsetY
}

offsetX = max(0f, offsetX)
offsetY = max(0f, offsetY)

if (child is IShape) {
contentWidth = max(contentWidth, child.x + child.width)
contentHeight = max(contentHeight, child.y + child.height)
contentWidth = max(contentWidth, offsetX + child.width)
contentHeight = max(contentHeight, offsetY + child.height)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/com/reco1l/andengine/shape/Box.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ open class Box : ExtendedEntity(vertexBuffer = BoxVertexBuffer()) {
put(6, width)
put(7, height)
}

setHardwareBufferNeedsUpdate()
}

}
Expand Down Expand Up @@ -222,6 +224,8 @@ open class RoundedBox(segmentsPerArc: Int = 10) : ExtendedEntity(RoundedBoxVerte

// Bottom left corner (90° to 180°)
addArc(cornerRadius, height - cornerRadius, 90f, 180f)

setHardwareBufferNeedsUpdate()
}


Expand Down
120 changes: 120 additions & 0 deletions src/com/reco1l/andengine/text/TextureFont.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.reco1l.andengine.text

import com.reco1l.andengine.*
import com.reco1l.andengine.shape.*
import org.anddev.andengine.engine.camera.*
import org.anddev.andengine.opengl.texture.region.*
import org.anddev.andengine.opengl.util.GLHelper
import javax.microedition.khronos.opengles.*
import kotlin.math.*

open class TextureFont(private val characters: MutableMap<Char, TextureRegion>) : Box() {


override var autoSizeAxes = Axes.Both


/**
* The spacing between glyphs.
*/
var spacing = 0f
set(value) {
if (field != value) {
field = value
isTextDirty = true
}
}

/**
* The text to display.
*/
var text = ""
set(value) {
if (field != value) {
field = value
isTextDirty = true
}
}


private val textureRegions = mutableListOf<TextureRegion>()


private var isTextDirty = true


override fun onManagedDraw(pGL: GL10, pCamera: Camera) {

if (isTextDirty) {
onUpdateText()
}

super.onManagedDraw(pGL, pCamera)
}


private fun onUpdateText() {

isTextDirty = false

contentWidth = 0f
contentHeight = 0f

textureRegions.clear()
for (i in text.indices) {

val textureRegion = characters[text[i]] ?: continue

textureRegions.add(textureRegion)

contentWidth += textureRegion.width + spacing
contentHeight = max(contentHeight, textureRegion.height.toFloat())
}

contentWidth -= spacing
onContentSizeMeasured()
}

override fun onInitDraw(pGL: GL10) {
super.onInitDraw(pGL)

GLHelper.enableTextures(pGL)
GLHelper.enableTexCoordArray(pGL)
}

override fun doDraw(gl: GL10, camera: Camera) {
onInitDraw(gl)

var offsetX = 0f

for (i in textureRegions.indices) {

val texture = textureRegions[i]
val textureWidth = texture.width.toFloat()
val textureHeight = texture.height.toFloat()

gl.glPushMatrix()
gl.glTranslatef(offsetX, 0f, 0f)

vertexBuffer.update(textureWidth, textureHeight)
texture.onApply(gl)

onApplyVertices(gl)
drawVertices(gl, camera)
gl.glPopMatrix()

offsetX += textureWidth + spacing
}
}


override fun getVertexBuffer(): BoxVertexBuffer {
return super.getVertexBuffer() as BoxVertexBuffer
}


override fun updateVertexBuffer() = Unit

override fun onUpdateVertexBuffer() = Unit

}
7 changes: 3 additions & 4 deletions src/com/reco1l/osu/playfield/GameplayHUD.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ class GameplayHUD(private val stat: StatisticV2, private val game: GameScene, pr
}

scoreCounter.setScore(0)
scoreCounter.onUpdateText()

accuracyCounter.y += scoreCounter.y + scoreCounter.height
accuracyCounter.onUpdateText()
accuracyCounter.setAccuracy(100f)

} else {
healthBar = null
Expand All @@ -75,7 +72,9 @@ class GameplayHUD(private val stat: StatisticV2, private val game: GameScene, pr
if (withStatistics) {
comboCounter!!.setCombo(stat.combo)
scoreCounter!!.setScore(stat.totalScoreWithMultiplier)

accuracyCounter!!.setAccuracy(stat.accuracy)
accuracyCounter.y = 9f + scoreCounter.y + scoreCounter.height

if (Config.getProgressIndicatorType() == PIE) {
pieSongProgress!!.x = accuracyCounter.x - accuracyCounter.widthScaled - 18f
Expand Down
91 changes: 13 additions & 78 deletions src/com/reco1l/osu/playfield/SpriteFont.kt
Original file line number Diff line number Diff line change
@@ -1,90 +1,25 @@
package com.reco1l.osu.playfield

import com.reco1l.andengine.container.*
import com.reco1l.andengine.sprite.*
import com.reco1l.andengine.text.*
import org.anddev.andengine.opengl.texture.region.*
import ru.nsu.ccfit.zuev.osu.*
import ru.nsu.ccfit.zuev.skins.*


open class SpriteFont(private val texturePrefix: StringSkinData) : LinearContainer() {
open class SpriteFont(private val texturePrefix: StringSkinData) : TextureFont(mutableMapOf<Char, TextureRegion>().also {

/**
* The text to display.
*/
var text = ""
set(value) {
if (field != value) {
field = value
isTextDirty = true
}
}


/**
* The characters to display.
*/
val characters = mutableMapOf<Char, TextureRegion>().also {

fun addChar(char: Char, textureName: String) {
it[char] = ResourceManager.getInstance().getTextureWithPrefix(texturePrefix, textureName)
}

for (i in 0..9) {
addChar('0' + i, i.toString())
}

addChar('.', "comma")
addChar('%', "percent")
addChar('x', "x")
addChar('d', "d")
addChar('p', "p")

}.toMap()


private var isTextDirty = true


/**
* Called when the text needs to be updated.
*
* This method is called automatically when the text is changed on the update thread.
* You can also call it manually if needed to force an update.
*/
fun onUpdateText() {
isTextDirty = false

if (text.length > childCount) {
for (i in childCount until text.length) {
attachChild(ExtendedSprite())
}
} else {
for (i in childCount - 1 downTo text.length) {
detachChild(getChild(i))
}
}

for (i in text.indices) {

val char = text[i]
val sprite = getChild(i) as? ExtendedSprite ?: continue

sprite.textureRegion = characters[char]
}

onMeasureContentSize()
fun addChar(char: Char, textureName: String) {
it[char] = ResourceManager.getInstance().getTextureWithPrefix(texturePrefix, textureName)
}


override fun onManagedUpdate(pSecondsElapsed: Float) {

if (isTextDirty) {
isTextDirty = false
onUpdateText()
}

super.onManagedUpdate(pSecondsElapsed)
for (i in 0..9) {
addChar('0' + i, i.toString())
}

}
addChar('.', "comma")
addChar('%', "percent")
addChar('x', "x")
addChar('d', "d")
addChar('p', "p")

})

0 comments on commit cdec2aa

Please sign in to comment.