Skip to content

Commit

Permalink
Fixed Surface.wasmJs.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Laxystem committed Jan 22, 2025
1 parent c0b95c7 commit 0ec8e8d
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion toolkit/src/wasmJsMain/kotlin/Surface.wasmJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import org.w3c.dom.HTMLCanvasElement
import quest.laxla.spock.ExperimentalSpockApi
import quest.laxla.spock.RawSpockApi
import quest.laxla.spock.math.UIntSpace
import quest.laxla.spock.math.Vector2ui
import quest.laxla.spock.math.vectorOf
import quest.laxla.spock.windowing.Window

private val SupportedTextureFormats = persistentSetOf(
TextureFormat.BGRA8Unorm,
Expand All @@ -26,9 +28,24 @@ private val SupportedAlphaModes = persistentSetOf(

private val scope = CoroutineScope(Dispatchers.Default)

private class CanvasSurfaceWindow(val surface: Surface) : Window { // todo: improve upon this cursedness
override val shouldClose: Boolean
get() = false

@OptIn(RawSpockApi::class)
@ExperimentalSpockApi
override val size: Deferred<Vector2ui>
get() = scope.async { UIntSpace.vectorOf(surface.raw.width, surface.raw.height) }

override suspend fun close() {
// no-op on WasmJS
}

}

@OptIn(RawSpockApi::class)
public actual class Surface(@RawSpockApi public val raw: CanvasSurface) : AutoCloseable {
public actual val size: Deferred<Vector2ui> = scope.async { UIntSpace.vectorOf(raw.width, raw.height) }
public actual val window: Window = CanvasSurfaceWindow(this)

public actual val preferredTextureFormat: TextureFormat? = raw.preferredCanvasFormat

Expand Down Expand Up @@ -59,3 +76,4 @@ public actual class Surface(@RawSpockApi public val raw: CanvasSurface) : AutoCl
raw.close()
}
}

0 comments on commit 0ec8e8d

Please sign in to comment.