Skip to content

Commit

Permalink
Replace reference tests with screenshot tests and do some fixes and i…
Browse files Browse the repository at this point in the history
…mprovements
  • Loading branch information
soywiz committed Feb 12, 2023
1 parent ed46111 commit 0ebef8a
Show file tree
Hide file tree
Showing 84 changed files with 481 additions and 4,110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,14 @@ object RootKorlibsPlugin {
if (!JvmAddOpens.beforeJava9) jvmArgs(*JvmAddOpens.createAddOpensTypedArray())
if (headlessTests) systemProperty("java.awt.headless", "true")
}
val jvmTestInteractive = tasks.createThis<Test>("jvmTestInteractive") {
group = "verification"
environment("INTERACTIVE_SCREENSHOT", "true")
testClassesDirs = jvmTest.testClassesDirs
classpath = jvmTest.classpath
bootstrapClasspath = jvmTest.bootstrapClasspath
if (!JvmAddOpens.beforeJava9) jvmArgs(*JvmAddOpens.createAddOpensTypedArray())
}
if (!JvmAddOpens.beforeJava9) jvmTest.jvmArgs(*JvmAddOpens.createAddOpensTypedArray())
if (headlessTests) jvmTest.systemProperty("java.awt.headless", "true")
}
Expand Down
63 changes: 63 additions & 0 deletions korge-sandbox/src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@

import com.soywiz.korge.*
import com.soywiz.korge.particle.*
import com.soywiz.korge.scene.*
import com.soywiz.korge.time.*
import com.soywiz.korge.ui.*
import com.soywiz.korge.view.*
import com.soywiz.korim.color.*
import com.soywiz.korio.async.*
import com.soywiz.korio.file.std.*
import com.soywiz.korio.lang.*
import samples.*
import samples.asteroids.*
import samples.connect4.*
import samples.minesweeper.*
import samples.pong.*
import kotlin.random.*

val DEFAULT_KORGE_BG_COLOR = Colors.DARKCYAN.mix(Colors.BLACK, 0.8)

Expand All @@ -26,6 +30,65 @@ suspend fun main() = Korge(
//forceRenderEveryFrame = true
//debugAg = true,
) {


val particleEmitter = SingleFileMemoryVfs("""
<particleEmitterConfig>
<texture name="texture.png"/>
<sourcePosition x="300.00" y="300.00"/>
<sourcePositionVariance x="0.00" y="0.00"/>
<speed value="100.00"/>
<speedVariance value="30.00"/>
<particleLifeSpan value="2.0000"/>
<particleLifespanVariance value="1.9000"/>
<angle value="270.00"/>
<angleVariance value="2.00"/>
<gravity x="0.00" y="0.00"/>
<radialAcceleration value="0.00"/>
<tangentialAcceleration value="0.00"/>
<radialAccelVariance value="0.00"/>
<tangentialAccelVariance value="0.00"/>
<startColor red="1.00" green="0.31" blue="0.00" alpha="0.62"/>
<startColorVariance red="0.00" green="0.00" blue="0.00" alpha="0.00"/>
<finishColor red="1.00" green="0.31" blue="0.00" alpha="0.00"/>
<finishColorVariance red="0.00" green="0.00" blue="0.00" alpha="0.00"/>
<maxParticles value="500"/>
<startParticleSize value="70.00"/>
<startParticleSizeVariance value="49.53"/>
<finishParticleSize value="10.00"/>
<FinishParticleSizeVariance value="5.00"/>
<duration value="-1.00"/>
<emitterType value="0"/>
<maxRadius value="100.00"/>
<maxRadiusVariance value="0.00"/>
<minRadius value="0.00"/>
<minRadiusVariance value="0.00"/>
<rotatePerSecond value="0.00"/>
<rotatePerSecondVariance value="0.00"/>
<blendFuncSource value="770"/>
<blendFuncDestination value="1"/>
<rotationStart value="0.00"/>
<rotationStartVariance value="0.00"/>
<rotationEnd value="0.00"/>
<rotationEndVariance value="0.00"/>
</particleEmitterConfig>
""".trimIndent()).noSuspend { readParticleEmitter() }

val emitter = particleEmitter(particleEmitter, random = Random(0L)
) {
xy(0, 0)
}

speed = 0.0
return@Korge

for (n in 0 until 20) {
delayFrame()
emitter.xy(120 + n * 5, 100)
}


return@Korge
//sceneContainer(views).changeTo({ MainGifAnimation() }); return@Korge
//sceneContainer(views).changeTo({ MainStressButtons() }); return@Korge
//sceneContainer(views).changeTo({ MainTransitionFilter() }); return@Korge
Expand Down
8 changes: 5 additions & 3 deletions korge/src/commonMain/kotlin/com/soywiz/korge/Korge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ object Korge {
batchMaxQuads: Int = BatchBuilder2D.DEFAULT_BATCH_QUADS,
multithreaded: Boolean? = null,
forceRenderEveryFrame: Boolean = true,
stageBuilder: (Views) -> Stage = { Stage(it) },
entry: suspend Stage.() -> Unit
) {
RegisteredImageFormats.register(imageFormats)
Expand Down Expand Up @@ -218,7 +219,8 @@ object Korge {
gameWindow = gameWindow,
gameId = gameId,
settingsFolder = settingsFolder,
batchMaxQuads = batchMaxQuads
batchMaxQuads = batchMaxQuads,
stageBuilder = stageBuilder
).also {
it.init()
}
Expand Down Expand Up @@ -526,6 +528,7 @@ object Korge {
val firstRenderDeferred = CompletableDeferred<Unit>()

fun renderBlock(event: RenderEvent) {
//println("renderBlock: $event")
try {
views.frameUpdateAndRender(
fixedSizeStep = fixedSizeStep,
Expand All @@ -549,10 +552,9 @@ object Korge {
}

views.gameWindow.onRenderEvent { event ->
//println("RenderEvent: $it")
//println("RenderEvent: $event")
if (!event.render) {
renderBlock(event)

} else {
views.renderContext.doRender {
if (!renderShown) {
Expand Down
37 changes: 32 additions & 5 deletions korge/src/commonMain/kotlin/com/soywiz/korge/KorgeHeadless.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,43 @@ import com.soywiz.korim.color.*
import com.soywiz.korim.format.*
import com.soywiz.korinject.*
import com.soywiz.korma.geom.*
import kotlinx.coroutines.*

object KorgeHeadless {
class HeadlessGameWindowCoroutineDispatcher(val gameWindow: HeadlessGameWindow) : GameWindowCoroutineDispatcher() {
//init {
// frameRenderLoop()
//}
//
//fun frameRenderLoop() {
// this.invokeOnTimeout(16L, Runnable {
// //println("frameRenderLoop")
// gameWindow.frameRender()
// frameRenderLoop()
// }, gameWindow.coroutineDispatcher)
//}

override fun executePending(availableTime: TimeSpan) {
//println("HeadlessGameWindowCoroutineDispatcher.executePending: timedTasks=${_timedTasks.size}, tasks=${_tasks.size}")
super.executePending(availableTime)
}
}

class HeadlessGameWindow(
override val width: Int = 640,
override val height: Int = 480,
val draw: Boolean = false,
override val ag: AG = AGDummy(width, height),
exitProcessOnClose: Boolean = false
exitProcessOnClose: Boolean = false,
override val devicePixelRatio: Double = 1.0,
) : GameWindow() {
init {
this.exitProcessOnClose = exitProcessOnClose
}

override val coroutineDispatcher: GameWindowCoroutineDispatcher = HeadlessGameWindowCoroutineDispatcher(this)


//override val ag: AG = if (draw) AGSoftware(width, height) else DummyAG(width, height)
//override val ag: AG = AGDummy(width, height)
}
Expand Down Expand Up @@ -54,17 +79,19 @@ object KorgeHeadless {
debugAg: Boolean = false,
draw: Boolean = false,
ag: AG = AGDummy(width, height),
entry: suspend Stage.() -> Unit
devicePixelRatio: Double = 1.0,
stageBuilder: (Views) -> Stage = { Stage(it) },
entry: suspend Stage.() -> Unit,
): HeadlessGameWindow {
val gameWindow = HeadlessGameWindow(width, height, draw = draw, ag = ag)
val gameWindow = HeadlessGameWindow(width, height, draw = draw, ag = ag, devicePixelRatio = devicePixelRatio)
gameWindow.exitProcessOnClose = false
Korge(
title, width, height, virtualWidth, virtualHeight, icon, iconPath, /*iconDrawable,*/ imageFormats, quality,
targetFps, scaleAnchor, scaleMode, clipBorders, bgcolor, debug, debugFontExtraScale, debugFontColor,
fullscreen, args, gameWindow, timeProvider, injector,
blocking = blocking, debugAg = debugAg, entry = {
blocking = blocking, debugAg = debugAg, stageBuilder = stageBuilder, entry = {
entry()
}
}, forceRenderEveryFrame = true
)
return gameWindow
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,10 +875,13 @@ class BatchBuilder2D constructor(
private val batches = fastArrayListOf<AGBatch>()

private var lastIndexPos = 0
var batchCount = 0
var fullBatchCount = 0

fun createBatchIfRequired() {
if (lastIndexPos == indexPos) return
updateStandardUniforms()
batchCount++

//println("BATCH: currentBuffers.vertexData=${currentBuffers.vertexData}")
batches += AGBatch(
Expand Down Expand Up @@ -930,6 +933,7 @@ class BatchBuilder2D constructor(
ag.draw(AGMultiBatch(batches.toList()))
batches.clear()
beforeFlush(this)
fullBatchCount++

buffersListToReturn += currentBuffers
currentBuffers = buffersList.alloc()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import com.soywiz.kds.IntMap
import com.soywiz.kds.toIntMap
import com.soywiz.korim.bitmap.*
import com.soywiz.korim.font.BitmapFont
import com.soywiz.korim.format.PNG
import com.soywiz.korim.format.readBitmap
import com.soywiz.korim.format.*
import com.soywiz.korio.stream.openAsync
import com.soywiz.krypto.encoding.fromBase64
import kotlin.native.concurrent.ThreadLocal
Expand Down Expand Up @@ -41,7 +40,10 @@ fun debugBmpFont(tex: BmpSlice): BitmapFont {
}.toIntMap(), IntMap())
}

suspend fun debugBmpFont(): BitmapFont = debugBmpFont(DEBUG_FONT_BYTES.openAsync().readBitmap().slice())
suspend fun debugBmpFont(): BitmapFont {
//debugBmpFontSync
return debugBmpFont(DEBUG_FONT_BYTES.openAsync().readBitmap(ImageDecodingProps(preferKotlinDecoder = true)).slice())
}

@Deprecated("Use debugBmpFont() instead")
val debugBmpFontSync: BitmapFont get() {
Expand Down
2 changes: 1 addition & 1 deletion korge/src/commonMain/kotlin/com/soywiz/korge/view/Stage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import kotlinx.coroutines.*
* Singleton root [View] and [Container] that contains a reference to the [Views] singleton and doesn't have any parent.
*/
@RootViewDslMarker
class Stage(override val views: Views) : FixedSizeContainer()
open class Stage internal constructor(override val views: Views) : FixedSizeContainer()
, View.Reference
, CoroutineScope by views
, EventDispatcher by EventDispatcher.Mixin()
Expand Down
5 changes: 3 additions & 2 deletions korge/src/commonMain/kotlin/com/soywiz/korge/view/Views.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Views constructor(
val settingsFolder: String? = null,
val batchMaxQuads: Int = BatchBuilder2D.DEFAULT_BATCH_QUADS,
val bp: BoundsProvider = BoundsProvider.Base(),
val stageBuilder: (Views) -> Stage = { Stage(it) }
) :
Extra by Extra.Mixin(),
EventDispatcher by EventDispatcher.Mixin(),
Expand Down Expand Up @@ -198,7 +199,7 @@ class Views constructor(
private val resizedEvent = ReshapeEvent(0, 0)

/** Reference to the root node [Stage] */
val stage: Stage = Stage(this)
val stage: Stage = stageBuilder(this)

/** Reference to the root node [Stage] (alias) */
val root = stage
Expand Down Expand Up @@ -531,7 +532,7 @@ class ViewsLog constructor(
suspend fun init() {
if (!initialized) {
initialized = true
RegisteredImageFormats.register(PNG) // This might be required for Node.JS debug bitmap font in tests
RegisteredImageFormats.register(QOI, PNG) // This might be required for Node.JS debug bitmap font in tests
views.init()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class KorgeHeadlessTest {
}

while (true) {
println("STEP")
//println("STEP")
image.tween(image::rotation[minDegrees], time = 0.5.seconds, easing = Easing.EASE_IN_OUT)
image.tween(image::rotation[maxDegrees], time = 0.5.seconds, easing = Easing.EASE_IN_OUT)
views.gameWindow.close() // We close the window, finalizing the test here
Expand Down
Loading

0 comments on commit 0ebef8a

Please sign in to comment.