Compare commits

..
3 changed files with 38 additions and 5 deletions

No files matched your search

@@ -15,7 +15,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.runtime.withFrameMillis
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
@@ -99,7 +99,7 @@ private fun rememberMarkClock(avoid: Rect?): MarkClock {
var vx = speed * 0.8f
var vy = -speed * 0.6f
while (true) {
if (clock.page == Size.Zero) { withFrameMillis { }; continue }
if (clock.page == Size.Zero) { withFrameNanos { }; continue }
val f = MarkFrame(clock.page, Offset.Zero)
if (clock.at == Offset.Unspecified) clock.at = Offset(clock.page.width * 0.64f, clock.page.height * 0.42f)
if (Motion.scale == 0f) {
@@ -108,7 +108,10 @@ private fun rememberMarkClock(avoid: Rect?): MarkClock {
clock.wall = 0f
return@LaunchedEffect
}
withFrameMillis { now ->
withFrameNanos { nanos ->
val settled = clock.intro >= END && clock.ball >= IMPACT + REVEAL_MS && !clock.poked
if (settled && last >= 0 && !IdleFrames.due(nanos)) return@withFrameNanos
val now = nanos / 1_000_000L
val dt = if (last < 0) 0f else min(50f, (now - last) / Motion.scale)
last = now
clock.intro += dt
@@ -82,7 +82,18 @@ import kotlinx.coroutines.withContext
private const val WALL_MIN_GAMES = 4
private const val WALL_DISTINCT = 40
private const val DRIFT_STEP_NS = 15_000_000L
private const val IDLE_FRAME_NS = 30_000_000L
internal object IdleFrames {
private var shown = 0L
fun due(nanos: Long): Boolean {
if (nanos == shown) return true
if (nanos - shown < IDLE_FRAME_NS) return false
shown = nanos
return true
}
}
@Composable
internal fun SteamHome(s: FrontEndState, a: FrontEndActions, modifier: Modifier) {
@@ -281,7 +292,7 @@ internal fun TiltedWall(
var shown = 0L
while (true) withFrameNanos { now ->
if (shown == 0L) shown = now
else if (now - shown >= DRIFT_STEP_NS) {
else if (IdleFrames.due(now)) {
drift.floatValue = ((drift.floatValue + (now - shown) / (period.value * 1_000_000.0 * Motion.scale)) % 1.0).toFloat()
shown = now
}
@@ -0,0 +1,19 @@
package com.droiddeck.launcher.ui
import org.junit.Assert.assertEquals
import org.junit.Test
class IdleFramesTest {
@Test
fun idleLoopsDrawThirtyFramesASecondAndShareEachOne() {
val start = 5_000_000_000L
val frame = 8_333_333L
val drawn = (0 until 120).count { IdleFrames.due(start + it * frame) }
assertEquals(30, drawn)
val later = start + 1_000_000_000L
assertEquals(listOf(true, true, true), List(3) { IdleFrames.due(later) })
assertEquals(listOf(false, false), List(2) { IdleFrames.due(later + frame) })
assertEquals(listOf(true, true), List(2) { IdleFrames.due(later + 4 * frame) })
}
}