-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #451 from Reco1I/optimze-texture-font
Introduce texture font and optimize texture-based scoring counters
- Loading branch information
Showing
5 changed files
with
153 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
}) |