Skip to content

Commit

Permalink
Use FastDuration internally and provide overloads to avoid allocation…
Browse files Browse the repository at this point in the history
…s on JS (#2239)
  • Loading branch information
soywiz authored Jun 17, 2024
1 parent 7888240 commit 3e48c4f
Show file tree
Hide file tree
Showing 63 changed files with 628 additions and 345 deletions.
5 changes: 3 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ kotlinx-coroutines = "1.9.0-RC"
kotlinx-serialization = "1.7.0"
kotlinx-atomicfu = "0.24.0"

korlibs = "6.0.0-alpha2"
korlibs = "6.0.0-alpha3"
#korlibs = "999.0.0.999"

kotlinx-benchmark = "0.4.7"
dokka = "1.9.10"
Expand All @@ -33,7 +34,7 @@ korlibs-image = { module = "com.soywiz:korlibs-image", version.ref = "korlibs" }
korlibs-inject = { module = "com.soywiz:korlibs-inject", version.ref = "korlibs" }
korlibs-template = { module = "com.soywiz:korlibs-template", version.ref = "korlibs" }
korlibs-time = { module = "com.soywiz:korlibs-time", version.ref = "korlibs" }
korlibs-serialization-yaml = { module = "com.soywiz:korlibs-serialization-yaml", version.ref = "korlibs" }
korlibs-serialization = { module = "com.soywiz:korlibs-serialization", version.ref = "korlibs" }
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" }
jackson-module-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson" }
junit = { module = "junit:junit", version.ref = "junit" }
Expand Down
2 changes: 1 addition & 1 deletion korge-gradle-plugin-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
//implementation(localGroovy())
//implementation("org.eclipse.jgit:org.eclipse.jgit:6.3.0.202209071007-r")
implementation(libs.jgit)
implementation(libs.korlibs.serialization.yaml)
implementation(libs.korlibs.serialization)
testImplementation(libs.bundles.kotlin.test)
}

Expand Down
2 changes: 1 addition & 1 deletion korge-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ group = RootKorlibsPlugin.KORGE_GRADLE_PLUGIN_GROUP

dependencies {
implementation(project(":korge-gradle-plugin-common"))
implementation(libs.korlibs.serialization.yaml)
implementation(libs.korlibs.serialization)
}

gradlePlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TestableExecSpec : ExecSpec {

data class TestableExecResult(val stdout: String, val stderr: String = "", val exitCode: Int = 0) : ExecResult {
override fun getExitValue(): Int = exitCode
override fun assertNormalExitValue(): ExecResult = this.apply { assert(exitValue == 0) }
override fun assertNormalExitValue(): ExecResult = this.apply { check(exitValue == 0) }
override fun rethrowFailure(): ExecResult = this.apply { assertNormalExitValue() }
}

Expand Down
4 changes: 2 additions & 2 deletions korge-sandbox/src/samples/MainBVH.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MainBVH : Scene() {
rects += view
bvh.insertOrUpdate(view.getBounds(this), view)
}
addUpdater {
addFastUpdater {
for (n in rects.size - 100 until rects.size) {
val view = rects[n]
if (view.x < 0) {
Expand Down Expand Up @@ -75,7 +75,7 @@ class MainBVH : Scene() {
}
updateRay()

addUpdater {
addFastUpdater {
//println("moved")
val mousePos = localMousePos(views)
val angle = Point.angleFull(center, mousePos)
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainBezier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MainBezier : Scene() {
}
val bez = Bez()

addUpdater {
addFastUpdater {
shape.updateShape {
//val curve = Bezier.Quad(bez.p1, bez.p2, bez.p3)
val curve = Bezier(bez.p1, bez.p2, bez.p3, bez.p4)
Expand Down
4 changes: 2 additions & 2 deletions korge-sandbox/src/samples/MainBlur.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class MainBlur : Scene() {
val initialBlur = 6.0
var filterScale = 1.0
fun <T : View> T.bindScale(): T {
addUpdater { this.filterScale = filterScale }
addFastUpdater { this.filterScale = filterScale }
return this
}

val blur1 = BlurFilter(initialBlur)
val blur2 = BlurFilter(initialBlur)
val radiusProps = fastArrayListOf<KMutableProperty0<Double>>()

addUpdater {
addFastUpdater {
//blur2.radius = blur1.radius
blur2.radius = blur1.radius / 2f
for (prop in radiusProps) prop.set(blur1.radius)
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainCircleColor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MainCircleColor : Scene() {
shape.stroke = Colors.RED
shape.fill = Colors.GREEN
shape.strokeThickness = 16.0
addUpdater { shape.radius += 1f }
addFastUpdater { shape.radius += 1f }
}
}
}
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainCircles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MainCircles : Scene() {
collisionViews = fastArrayListOf<View>(rect1, rect1b, rect2)
println(rect1.hitShape2d)
println(rect2.hitShape2d)
addUpdater { dt ->
addFastUpdater { dt ->
val dx = input.keys.getDeltaAxis(Key.LEFT, Key.RIGHT)
val dy = input.keys.getDeltaAxis(Key.UP, Key.DOWN)
//if (dx != 0.0 || dy != 0.0) {
Expand Down
9 changes: 2 additions & 7 deletions korge-sandbox/src/samples/MainColorPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ package samples

import korlibs.korge.input.mouse
import korlibs.korge.scene.Scene
import korlibs.korge.view.SContainer
import korlibs.korge.view.addUpdater
import korlibs.korge.view.image
import korlibs.korge.view.scale
import korlibs.korge.view.unsafeRenderToBitmapSync
import korlibs.korge.view.xy
import korlibs.image.bitmap.Bitmaps
import korlibs.image.bitmap.slice
import korlibs.image.format.readBitmap
import korlibs.io.file.std.resourcesVfs
import korlibs.korge.view.*
import korlibs.math.geom.*

class MainColorPicker : Scene() {
Expand All @@ -27,7 +22,7 @@ class MainColorPicker : Scene() {
invalidateRender()
}
}
addUpdater {
addFastUpdater {
}
}
}
4 changes: 2 additions & 2 deletions korge-sandbox/src/samples/MainCustomSolidRectShader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class MainCustomSolidRectShader : Scene() {
override suspend fun SContainer.sceneMain() {
val solidRect = solidRect(200, 200, Colors.RED).xy(100, 100)

var time = 0.seconds
var time = 0.fastSeconds
solidRect.updateProgramUniforms = {
it[TimeUB].push {
it[timeUniform] = time.seconds.toFloat()
}
}
addUpdater {
addFastUpdater {
time += it
invalidateRender()
}
Expand Down
4 changes: 2 additions & 2 deletions korge-sandbox/src/samples/MainFlag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class MainFlag : ScaledScene(592, 592) {
}

// Propagates the wave over time
addUpdater { dt: Duration ->
addFastUpdater { dt ->
//println("MainFlag.addUpdater: dt=$dt")
flagFilter.time = flagFilter.time.plus(dt)
flagFilter.fastTime = flagFilter.time.plus(dt)
invalidateRender()
}

Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainHelloWorld.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import korlibs.korge.view.position
import korlibs.korge.view.scale
import korlibs.image.bitmap.*
import korlibs.math.geom.degrees
import korlibs.math.interpolation.Easing
import korlibs.math.interpolation.*

// @TODO: We could autogenerate this via gradle
private val korlibs.io.resources.ResourcesContainer.korge_png by resourceBitmap("korge.png")
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainMasks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MainMasks : Scene() {
.backdropFilters(ColorMatrixFilter(ColorMatrixFilter.GRAYSCALE_MATRIX))
.backdropFilters(BlurFilter())
.backdropFilters(WaveFilter(crestDistance = Vector2D(50.0, 25.0), time = 0.seconds).also { filter ->
addUpdater { filter.time += it }
addFastUpdater { filter.fastTime += it }
})

/*
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainSDF.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ open class CircleSDFView(size: Size = Size(100f, 100f)) : ShadedView(PROGRAM, si
var time = 0.0f

init {
addUpdater {
addFastUpdater {
time += it.seconds.toFloat()
invalidateRender()
}
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainShape2d.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class MainShape2dScene : Scene() {
//}


addUpdater {
addFastUpdater {
update()
}
updateShape()
Expand Down
8 changes: 4 additions & 4 deletions korge-sandbox/src/samples/MainSpriteAnim.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@ class MainSpriteAnim : ScaledScene(512, 512) {
addChild(player1)
addChild(player2)

addUpdater { time ->
val scale = 16.milliseconds / time
addFastUpdater { time ->
val scale = 16.fastMilliseconds / time
val disp = 2 * scale
val keys = views.input.keys

player1.handleKeys(keys, disp.toFloat())
player2.handleKeys(keys, disp.toFloat())

if (keys[Key.L]) { player1.playAnimationLooped(spriteAnimationDown, 100.milliseconds) }
if (keys[Key.T]) { player1.playAnimation(spriteAnimation = spriteAnimationDown, times = 3, spriteDisplayTime = 200.milliseconds) }
if (keys[Key.L]) { player1.playAnimationLooped(spriteAnimationDown, 100.fastMilliseconds) }
if (keys[Key.T]) { player1.playAnimation(spriteAnimation = spriteAnimationDown, times = 3, spriteDisplayTime = 200.fastMilliseconds) }
if (keys[Key.C]) { player1.playAnimationForDuration(1.seconds, spriteAnimationDown); player1.y -= 2 }
if (keys[Key.ESCAPE]) { player1.stopAnimation() }
}
Expand Down
4 changes: 2 additions & 2 deletions korge-sandbox/src/samples/MainSprites10k.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class MainSprites10k : Scene() {
val random = Random(0)
val randoms = FloatArray(greenSprites.size) { random.nextDouble(0.5, 1.1).toFloat() }

addUpdater {
val scale = (if (it == 0.0.milliseconds) 0f else (it / 16.666666.milliseconds).toFloat())
addFastUpdater {
val scale = (if (it == 0.0.fastMilliseconds) 0f else (it / 16.666666.fastMilliseconds).toFloat())

greenSprites.forEachIndexed { index, sprite ->
sprite.walkDirection(index % greenAnimations.size, scale * randoms[index])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MainStressMatrixMultiplication : Scene() {
val text = text("result")
var N = 1_000
var step = 0
addUpdater {
addFastUpdater {
var sum = 0.0
for (n in 0 until N) {
val mat = this.localMatrix * this@sceneMain.localMatrix * this@MainStressMatrixMultiplication.stage.localMatrix * Matrix().scaled(n.toFloat()).translated(x.toFloat() * 10f, y.toFloat() * -20f)
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainStrokesExperiment3.kt
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class MainStrokesExperiment : Scene() {
val container = container {
}
val offsetInfo = OffsetInfo()
addUpdater {
addFastUpdater {
container.removeChildren()
container.addChild(generateDashes(offsetInfo.offset))
}
Expand Down
4 changes: 2 additions & 2 deletions korge-sandbox/src/samples/MainSvgAnimation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class MainSvgAnimation : Scene() {
println("GPU SHAPE: $it")
}

addUpdater { dt ->
svg.updateStyles(dt)
addFastUpdater { dt ->
svg.updateStyles(dt.toDuration())
shape.updateShape {
buildGraphics()
}
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainTextMetrics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class MainTextMetrics : Scene() {
}
}

addUpdater {
addFastUpdater {
updateBounds()
}
updateBounds()
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainTiledBackground.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MainTiledBackground : Scene() {
val tilemap = tileMap(IntArray2(2, 2, intArrayOf(0, 1, 1, 0)), repeatX = TileMapRepeat.REPEAT, repeatY = TileMapRepeat.REPEAT, tileset = tileset)
tilemap.x += 300
tilemap.y += 300
addUpdater {
addFastUpdater {
//tilemap.rotation += 1.degrees
tilemap.x += 1
tilemap.y += 0.25
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainTilemapTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class MainTilemapTest : Scene() {
var camPos: Point = Point.ZERO
}

addUpdater {
addFastUpdater {
val mouseButtons = input.mouseButtons
val isDown = (mouseButtons and 5) != 0
if (isDown) {
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainTweenPoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MainTweenPoint : Scene() {
}
}

addUpdater {
addFastUpdater {
println("views.ag.getStats: ${views.ag.getStats()}")
}
}
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/MainVectorNinePatch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MainVectorNinePatch : Scene() {
// NinePatchSlices(77.0 until (300.0 - 77.0)),
//))

addUpdater {
addFastUpdater {
view.size(mousePos.x, mousePos.y)
}
}
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/samples/asteroids/Asteroid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Asteroid(
scaleAvg = asteroidSize.toDouble() / 3.0
name = "asteroid"
speed = 0.6
addUpdater { time ->
addFastUpdater { time ->
val scale = time / 16.0.milliseconds
val dx = angle.cosine * scale
val dy = angle.sine * scale
Expand Down
8 changes: 4 additions & 4 deletions korge-sandbox/src/samples/asteroids/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class Game(val scene: MainAsteroids) {
spawnRandomAsteroids(ship.image)
}

private val stageUpdater = sceneView.addUpdater { time ->
private val stageUpdater = sceneView.addFastUpdater { time ->
if (!gameOver) {
val scale = time / 16.0.milliseconds
val scale = time / 16.0.fastMilliseconds

if (ship.bulletReload > 0) ship.bulletReload -= scale

Expand Down Expand Up @@ -79,8 +79,8 @@ class Game(val scene: MainAsteroids) {
}
}

bullet.addUpdater {
val scale = it / 16.milliseconds
bullet.addFastUpdater {
val scale = it / 16.fastMilliseconds
bullet.advance(+3.0 * scale)
// If the bullet flies off the screen, discard it
if (bullet.x < -BULLET_SIZE || bullet.y < -BULLET_SIZE || bullet.x > WIDTH + BULLET_SIZE || bullet.y > HEIGHT + BULLET_SIZE) {
Expand Down
Loading

0 comments on commit 3e48c4f

Please sign in to comment.