-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WASM Browser support for all the libraries including korgw and korge …
…(DISABLED) (#1626)
- Loading branch information
Showing
29 changed files
with
1,444 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
kmem/src/wasmMain/kotlin/korlibs/memory/internal/wasmInternal.kt
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
kmem/src/wasmMain/kotlin/korlibs/memory/wasm/ArrayConversionExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package korlibs.memory.wasm | ||
|
||
import org.khronos.webgl.* | ||
|
||
internal fun ArrayBuffer.toByteArray(): ByteArray = Int8Array(this).toByteArray() | ||
internal fun Uint8Array.toByteArray(): ByteArray { | ||
return Int8Array(this.buffer).toByteArray() | ||
} | ||
internal fun Int8Array.toByteArray(): ByteArray { | ||
val out = ByteArray(this.length) | ||
for (n in out.indices) out[n] = this[n] | ||
return out | ||
} | ||
internal fun Int16Array.toShortArray(): ShortArray { | ||
val out = ShortArray(this.length) | ||
for (n in out.indices) out[n] = this[n] | ||
return out | ||
} | ||
internal fun Int32Array.toIntArray(): IntArray { | ||
val out = IntArray(this.length) | ||
for (n in out.indices) out[n] = this[n] | ||
return out | ||
} | ||
internal fun Float32Array.toFloatArray(): FloatArray { | ||
val out = FloatArray(this.length) | ||
for (n in out.indices) out[n] = this[n] | ||
return out | ||
} | ||
internal fun Float64Array.toDoubleArray(): DoubleArray { | ||
val out = DoubleArray(this.length) | ||
for (n in out.indices) out[n] = this[n] | ||
return out | ||
} | ||
|
||
internal fun ByteArray.toInt8Array(): Int8Array { | ||
//val tout = this.asDynamic() | ||
//if (tout is Int8Array) { | ||
// return tout.unsafeCast<Int8Array>() | ||
//} else { | ||
val out = Int8Array(this.size) | ||
for (n in 0 until out.length) out[n] = this[n] | ||
return out | ||
//} | ||
} | ||
|
||
internal fun ByteArray.toUint8Array(): Uint8Array { | ||
return Uint8Array(toInt8Array().buffer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package korlibs.korge | ||
|
||
import korlibs.audio.sound.* | ||
import korlibs.korge.view.* | ||
|
||
internal actual fun completeViews(views: Views) { | ||
// Already performed on Korge start | ||
//HtmlSimpleSound.unlock // Tries to unlock audio as soon as possible | ||
} |
4 changes: 4 additions & 0 deletions
4
korge/src/wasmMain/kotlin/korlibs/korge/KorgeReload_getReloadedClass.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package korlibs.korge | ||
|
||
internal actual val KorgeReloadInternal: KorgeReloadInternalImpl = object : KorgeReloadInternalImpl() { | ||
} |
34 changes: 34 additions & 0 deletions
34
korge/src/wasmMain/kotlin/korlibs/korge/service/storage/NativeStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package korlibs.korge.service.storage | ||
|
||
import korlibs.korge.view.* | ||
import kotlinx.browser.* | ||
|
||
@JsName("Object") | ||
private external object JsObject { | ||
fun keys(): JsArray<JsString> | ||
} | ||
|
||
actual class NativeStorage actual constructor(val views: Views) : IStorageWithKeys { | ||
override fun toString(): String = "NativeStorage(${toMap()})" | ||
|
||
actual override fun keys(): List<String> { | ||
val keys = JsObject.keys() | ||
return (0 until keys.length).map { keys[it].toString() } | ||
} | ||
|
||
actual override fun set(key: String, value: String) { | ||
localStorage.setItem(key, value) | ||
} | ||
|
||
actual override fun getOrNull(key: String): String? { | ||
return localStorage.getItem(key) | ||
} | ||
|
||
actual override fun remove(key: String) { | ||
localStorage.removeItem(key) | ||
} | ||
|
||
actual override fun removeAll() { | ||
localStorage.clear() | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
korge/src/wasmMain/kotlin/korlibs/korge/service/vibration/NativeVibration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package korlibs.korge.service.vibration | ||
|
||
import korlibs.io.wasm.* | ||
import korlibs.time.TimeSpan | ||
import korlibs.korge.view.Views | ||
import kotlinx.browser.window | ||
|
||
actual class NativeVibration actual constructor(val views: Views) { | ||
|
||
/** | ||
* @param timings list of alternating ON-OFF durations in milliseconds. Staring with ON. | ||
* @param amplitudes has no effect on JS backend | ||
*/ | ||
@ExperimentalUnsignedTypes | ||
actual fun vibratePattern(timings: Array<TimeSpan>, amplitudes: Array<Double>) { | ||
window.navigator.vibrate(jsArrayOf(*timings.map { it.milliseconds.toJsNumber() }.toTypedArray())) | ||
} | ||
|
||
/** | ||
* @param time vibration duration in milliseconds | ||
* @param amplitude has no effect on JS backend | ||
*/ | ||
@ExperimentalUnsignedTypes | ||
actual fun vibrate(time: TimeSpan, amplitude: Double) { | ||
window.navigator.vibrate(time.milliseconds.toJsNumber()) | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
korge/src/wasmMain/kotlin/korlibs/korge/tests/enrichTestGameWindow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package korlibs.korge.tests | ||
|
||
actual fun enrichTestGameWindow(window: ViewsForTesting.TestGameWindow) { | ||
} |
11 changes: 11 additions & 0 deletions
11
korgw/src/wasmMain/kotlin/korlibs/graphics/gl/AGOpenglFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package korlibs.graphics.gl | ||
|
||
import korlibs.graphics.* | ||
|
||
@JsFun("() => { return ('ontouchstart' in window || navigator.maxTouchPoints); }") | ||
private external fun _isTouchDevice(): Boolean | ||
|
||
actual object AGOpenglFactory { | ||
actual fun create(nativeComponent: Any?): AGFactory = AGFactoryWebgl | ||
actual val isTouchDevice: Boolean get() = _isTouchDevice() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package korlibs.graphics.gl | ||
|
||
import korlibs.kgl.* | ||
import korlibs.graphics.* | ||
import korlibs.io.wasm.* | ||
import org.w3c.dom.* | ||
import kotlinx.browser.* | ||
|
||
object AGFactoryWebgl : AGFactory { | ||
override val supportsNativeFrame: Boolean = true | ||
override fun create(nativeControl: Any?, config: AGConfig): AG = AGWebgl(config) | ||
override fun createFastWindow(title: String, width: Int, height: Int): AGWindow { | ||
TODO() | ||
} | ||
} | ||
|
||
fun jsEmptyObject() = jsEmptyObj() | ||
|
||
fun jsObject(vararg pairs: Pair<String, Any?>): JsAny { | ||
val out = jsEmptyObject() | ||
for ((k, v) in pairs) if (v != null) out.setAny(k.toJsString(), v.toJsReference()) | ||
//for ((k, v) in pairs) out[k] = v | ||
return out | ||
} | ||
|
||
val korgwCanvasQuery: String? by lazy { window.getAny("korgwCanvasQuery")?.unsafeCast<JsString>()?.toString() } | ||
val isCanvasCreatedAndHandled get() = korgwCanvasQuery == null | ||
|
||
fun AGDefaultCanvas(): HTMLCanvasElement { | ||
return (korgwCanvasQuery?.let { document.querySelector(it) as HTMLCanvasElement }) | ||
?: (document.createElement("canvas") as HTMLCanvasElement) | ||
} | ||
|
||
fun AGWebgl(config: AGConfig, canvas: HTMLCanvasElement = AGDefaultCanvas()): AGOpengl = AGOpengl( | ||
KmlGlJsCanvas( | ||
canvas, jsObject( | ||
"premultipliedAlpha" to false, // To be like the other targets | ||
"alpha" to false, | ||
"stencil" to true, | ||
"antialias" to config.antialiasHint | ||
) | ||
) | ||
).also { ag -> | ||
window.setAny("ag".toJsString(), ag.toJsReference()) | ||
|
||
// https://www.khronos.org/webgl/wiki/HandlingContextLost | ||
// https://gist.github.com/mattdesl/9995467 | ||
|
||
canvas.addEventListener("webglcontextlost", { e -> | ||
//contextVersion++ | ||
e.preventDefault() | ||
null | ||
}, false.toJsBoolean()) | ||
|
||
canvas.addEventListener("webglcontextrestored", { e -> | ||
ag.contextLost() | ||
//e.preventDefault() | ||
null | ||
}, false.toJsBoolean()) | ||
} |
3 changes: 3 additions & 0 deletions
3
korgw/src/wasmMain/kotlin/korlibs/io/file/registry/WindowsRegistry.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package korlibs.io.file.registry | ||
|
||
actual object WindowsRegistry : WindowsRegistryBase() |
Oops, something went wrong.