Skip to content

Commit

Permalink
Revert "Fix blurry DefaultTtfFontAsBitmap text in CachedContainer whe…
Browse files Browse the repository at this point in the history
…n virtual size < window size (#1760)"

This reverts commit 924b1cd.
  • Loading branch information
soywiz committed Jul 17, 2023
1 parent 924b1cd commit 2543f7b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 58 deletions.
39 changes: 1 addition & 38 deletions korge-sandbox/src/commonMain/kotlin/samples/MainCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,19 @@ import korlibs.korge.time.*
import korlibs.korge.ui.*
import korlibs.korge.view.*
import korlibs.image.color.*
import korlibs.image.font.*
import korlibs.image.text.*
import korlibs.korge.*
import korlibs.math.geom.*
import korlibs.math.interpolation.*
import korlibs.math.random.*
import korlibs.memory.*
import kotlin.random.*

//class MainCache : ScaledScene(512, 512) {
class MainCache : Scene() {
override suspend fun SContainer.sceneInit() {
setVirtualSize(Korge.DEFAULT_WINDOW_SIZE / 2)
}

override suspend fun SContainer.sceneMain() {
//val cached = CachedContainer().addTo(this)
//val cached = container { }
val cached = cachedContainer { }
val random = Random(0L)
for (n in 0 until 100_000) {
cached.solidRect(1, 1, random[Colors.RED, Colors.BLUE]).xy((n % 300), (n / 300))
cached.solidRect(2, 2, random[Colors.RED, Colors.BLUE]).xy(2 * (n % 300), 2 * (n / 300))
}
uiHorizontalStack {
uiButton("Cached").clicked {
Expand All @@ -44,37 +35,9 @@ class MainCache : Scene() {
println(cached.getChildAt(50000)._invalidateNotifier)
}

uiHorizontalStack {
this.position(315, 0)

uiScrollable(size = Size(100, 100)) {
it.sampleTextBlock(RichTextData("Expensive Scaling: Default", font = DefaultTtfFontAsBitmap))
}

uiScrollable(size = Size(100, 100)) {
it.expensiveScaling = false
it.sampleTextBlock(RichTextData("Cheap Scaling: Opt-in", font = DefaultTtfFontAsBitmap))
}
}

//timeout(1.seconds) {
// rect.color = Colors.BLUE
//}
}

override suspend fun sceneAfterDestroy() {
super.sceneAfterDestroy()
setVirtualSize(Korge.DEFAULT_WINDOW_SIZE)
}

private fun setVirtualSize(size: Size) {
views.setVirtualSize(size.width.toIntCeil(), size.height.toIntCeil())
}

private fun Container.sampleTextBlock(richTextData: RichTextData): TextBlock {
return textBlock(align = TextAlignment.MIDDLE_CENTER, size = Size(100, 100)) {
text = richTextData
fill = Colors.WHITE
}
}
}
26 changes: 6 additions & 20 deletions korge/src/commonMain/kotlin/korlibs/korge/view/CachedContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ open class FixedSizeCachedContainer(

open class CachedContainer(
@property:ViewProperty
var cache: Boolean = true,
@property:ViewProperty
var expensiveScaling: Boolean? = null,
var cache: Boolean = true
) : Container(), InvalidateNotifier {
//@ViewProperty
//var cache: Boolean = cache
Expand All @@ -64,7 +62,6 @@ open class CachedContainer(
private var dirty = true
private var scaledCache = -1f
private var lbounds = Rectangle()
private var windowLocalRatio: Scale = Scale(1)

override fun invalidateRender() {
super.invalidateRender()
Expand All @@ -84,36 +81,25 @@ open class CachedContainer(
val cache = _cacheTex!!
ctx.refGcCloseable(cache)

val renderScale: Float = when (ctx.quality) {
val renderScale: Float = when (ctx.views?.gameWindow?.quality) {
GameWindow.Quality.PERFORMANCE -> 1f
else -> ctx.devicePixelRatio
}
//val renderScale = 1.0

val doExpensiveScaling = expensiveScaling ?: when(ctx.quality) {
GameWindow.Quality.PERFORMANCE -> false
else -> true
}

if (dirty || scaledCache != renderScale) {
scaledCache = renderScale
lbounds = getLocalBounds(includeFilters = false)
windowLocalRatio = if (doExpensiveScaling) {
windowBounds.size / lbounds.size
} else {
Scale(1)
}

dirty = false
val texWidth = (lbounds.width * renderScale * windowLocalRatio.scaleX).toInt().coerceAtLeast(1)
val texHeight = (lbounds.height * renderScale * windowLocalRatio.scaleY).toInt().coerceAtLeast(1)
val texWidth = (lbounds.width * renderScale).toInt().coerceAtLeast(1)
val texHeight = (lbounds.height * renderScale).toInt().coerceAtLeast(1)
cache.resize(texWidth, texHeight)
ctx.flush()
ctx.renderToFrameBuffer(cache.rb) {
//ctx.ag.clear(Colors.TRANSPARENT, clearColor = true)
ctx.setViewMatrixTemp(globalMatrixInv
.translated(-lbounds.x, -lbounds.y)
.scaled(renderScale * windowLocalRatio.scaleX, renderScale * windowLocalRatio.scaleY)
.scaled(renderScale)
) {
super.renderInternal(ctx)
}
Expand All @@ -125,7 +111,7 @@ open class CachedContainer(
cache.tex,
m = globalMatrix
.pretranslated(lbounds.x, lbounds.y)
.prescaled(1.0 / renderScale / windowLocalRatio.scaleX, 1.0 / renderScale / windowLocalRatio.scaleY)
.prescaled(1.0 / renderScale)
,
colorMul = renderColorMul,
blendMode = blendMode,
Expand Down

0 comments on commit 2543f7b

Please sign in to comment.