Skip to content

Commit cb0d461

Browse files
committed
Fixes #15
1 parent 514ea01 commit cb0d461

File tree

15 files changed

+83
-69
lines changed

15 files changed

+83
-69
lines changed

Diff for: glfw/build.gradle.kts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ kotlin {
1414

1515
sourceSets {
1616
commonMain.dependencies {
17-
api(projects.math)
17+
api(projects.windowing)
1818

19-
// TODO: remove, KT-57 something something
19+
// TODO: remove
2020
api(projects.util)
2121
}
2222

@@ -25,7 +25,7 @@ kotlin {
2525
implementation(libs.lwjgl.glfw)
2626
runtimeOnly(projects.lwjgl)
2727
}
28-
28+
2929
nativeMain.dependencies {
3030
implementation(libs.wgpu4k.glfw)
3131
}

Diff for: glfw/src/commonMain/kotlin/GlfwWindow.kt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package quest.laxla.spock.glfw
2+
3+
import kotlinx.coroutines.Deferred
4+
import quest.laxla.spock.ExperimentalSpockApi
5+
import quest.laxla.spock.math.Vector2ui
6+
import quest.laxla.spock.windowing.Window
7+
8+
/**
9+
* @since 0.0.1-alpha.4
10+
*/
11+
public expect class GlfwWindow(width: UInt, height: UInt, title: String) : Window {
12+
override val shouldClose: Boolean
13+
14+
@ExperimentalSpockApi
15+
override val size: Deferred<Vector2ui>
16+
17+
override suspend fun close()
18+
}

Diff for: glfw/src/jvmMain/kotlin/Window.jvm.kt

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
package quest.laxla.spock.glfw
22

33
import kotlinx.coroutines.Deferred
4-
import kotlinx.coroutines.Job
54
import org.lwjgl.glfw.GLFW
65
import org.lwjgl.glfw.GLFW.glfwGetWindowSize
76
import org.lwjgl.glfw.GLFW.glfwWindowShouldClose
87
import org.lwjgl.system.MemoryStack
98
import org.lwjgl.system.MemoryUtil.NULL
9+
import quest.laxla.spock.ExperimentalSpockApi
1010
import quest.laxla.spock.RawSpockApi
11-
import quest.laxla.spock.SuspendCloseable
1211
import quest.laxla.spock.math.UIntSpace
1312
import quest.laxla.spock.math.Vector2ui
1413
import quest.laxla.spock.math.vectorOf
14+
import quest.laxla.spock.windowing.Window
1515

1616
@OptIn(RawSpockApi::class)
17-
public actual class Window(@RawSpockApi public val raw: Long) : SuspendCloseable {
17+
public actual class GlfwWindow(@RawSpockApi public val raw: Long) : Window {
1818
public actual constructor(width: UInt, height: UInt, title: String) :
1919
this(GLFW.glfwCreateWindow(width.toInt(), height.toInt(), title, NULL, NULL))
2020

21-
public actual val shouldClose: Boolean get() = glfwWindowShouldClose(raw)
21+
public actual override val shouldClose: Boolean get() = glfwWindowShouldClose(raw)
2222

23-
public actual val size: Deferred<Vector2ui> = Glfw.async {
23+
@ExperimentalSpockApi
24+
public actual override val size: Deferred<Vector2ui> = Glfw.async {
2425
MemoryStack.create().run {
2526
val width = mallocInt(1)
2627
val height = mallocInt(1)
@@ -30,13 +31,6 @@ public actual class Window(@RawSpockApi public val raw: Long) : SuspendCloseable
3031
}
3132
}
3233

33-
public actual inline fun setFramebufferSizeCallback(crossinline callback: Window.(width: UInt, height: UInt) -> Unit): Job =
34-
Glfw.launch {
35-
GLFW.glfwSetFramebufferSizeCallback(raw) { _, width, height ->
36-
callback(width.toUInt(), height.toUInt())
37-
}
38-
}
39-
4034
actual override suspend fun close(): Unit = Glfw {
4135
GLFW.glfwDestroyWindow(raw) // why sigsegv here??
4236
}

Diff for: glfw/src/nativeMain/kotlin/Window.native.kt

+6-14
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ import glfw.glfwGetWindowSize
99
import glfw.glfwWindowShouldClose
1010
import kotlinx.cinterop.*
1111
import kotlinx.coroutines.Deferred
12-
import kotlinx.coroutines.Job
12+
import quest.laxla.spock.ExperimentalSpockApi
1313
import quest.laxla.spock.RawSpockApi
14-
import quest.laxla.spock.SuspendCloseable
1514
import quest.laxla.spock.math.UIntSpace
1615
import quest.laxla.spock.math.Vector2ui
1716
import quest.laxla.spock.math.vectorOf
17+
import quest.laxla.spock.windowing.Window
1818

1919
@OptIn(RawSpockApi::class)
20-
public actual class Window(@RawSpockApi public val raw: CPointer<GLFWwindow>) : SuspendCloseable {
20+
public actual class GlfwWindow(@RawSpockApi public val raw: CPointer<GLFWwindow>) : Window {
2121
public actual constructor(width: UInt, height: UInt, title: String) :
2222
this(glfwCreateWindow(width.toInt(), height.toInt(), title, null, null)!!)
2323

24-
public actual val shouldClose: Boolean get() = glfwWindowShouldClose(raw) == GlfwTrue
24+
public actual override val shouldClose: Boolean get() = glfwWindowShouldClose(raw) == GlfwTrue
2525

26-
public actual val size: Deferred<Vector2ui> = Glfw.async {
26+
@ExperimentalSpockApi
27+
public actual override val size: Deferred<Vector2ui> = Glfw.async {
2728
memScoped {
2829
val width = alloc<IntVar>()
2930
val height = alloc<IntVar>()
@@ -32,15 +33,6 @@ public actual class Window(@RawSpockApi public val raw: CPointer<GLFWwindow>) :
3233
UIntSpace.vectorOf(width.value.toUInt(), height.value.toUInt())
3334
}
3435
}
35-
public actual inline fun setFramebufferSizeCallback(crossinline callback: Window.(width: UInt, height: UInt) -> Unit): Job =
36-
Glfw.launch {
37-
38-
TODO()
39-
/*glfw.glfwSetFramebufferSizeCallback(
40-
raw,
41-
staticCFunction { raw, width: Int, height: Int -> Window(raw!!).callback(width.toUInt(), height.toUInt()) }
42-
)*/
43-
}
4436

4537
actual override suspend fun close(): Unit = Glfw {
4638
glfwDestroyWindow(raw)

Diff for: toolkit/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ kotlin {
3535
commonMain.dependencies {
3636
api(libs.wgpu4k)
3737
api(projects.core)
38+
api(projects.windowing)
3839

3940
// TODO: remove below dependencies, KT-74152
4041
api(projects.util)

Diff for: toolkit/src/commonMain/kotlin/Surface.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import io.ygdrasil.webgpu.SurfaceConfiguration
55
import io.ygdrasil.webgpu.SurfaceTexture
66
import io.ygdrasil.webgpu.TextureFormat
77
import kotlinx.collections.immutable.ImmutableSet
8-
import kotlinx.coroutines.Deferred
9-
import quest.laxla.spock.math.Vector2ui
8+
import quest.laxla.spock.windowing.Window
109

1110
/**
1211
* Represents a platform-specific surface (e.g., a window) onto which rendered images may be presented.
@@ -15,11 +14,11 @@ import quest.laxla.spock.math.Vector2ui
1514
*/
1615
public expect class Surface : AutoCloseable {
1716
/**
18-
* The size of this surface.
17+
* The window onto which this [Surface] is drawn.
1918
*
2019
* @since 0.0.1-alpha.4
2120
*/
22-
public val size: Deferred<Vector2ui>
21+
public val window: Window
2322

2423
/**
2524
* The optimal [TextureFormat] for the current system.

Diff for: toolkit/src/glfwMain/kotlin/Application.glfw.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal actual suspend fun Closer.webGpuApplication(
2424

2525
glfwWindowHint(GlfwResizable, GlfwFalse)
2626
glfwWindowHint(GlfwClientApi, GlfwNoApi)
27-
val window = +Window(preferredWidth, preferredHeight, title)
27+
val window = +GlfwWindow(preferredWidth, preferredHeight, title)
2828
val wgpu = +Wgpu()
2929
val surface = +wgpu.createSurface(window)
3030
val adapter = +wgpu.requestAdapter(surface)

Diff for: toolkit/src/glfwMain/kotlin/Surface.glfw.kt

+4-13
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ package quest.laxla.spock.toolkit
33
import io.ygdrasil.webgpu.*
44
import kotlinx.collections.immutable.ImmutableSet
55
import kotlinx.collections.immutable.toImmutableSet
6-
import kotlinx.coroutines.Deferred
76
import quest.laxla.spock.ExperimentalSpockApi
87
import quest.laxla.spock.RawSpockApi
9-
import quest.laxla.spock.glfw.Window
10-
import quest.laxla.spock.math.Vector2ui
118
import quest.laxla.spock.math.component1
129
import quest.laxla.spock.math.component2
10+
import quest.laxla.spock.windowing.Window
1311

1412
@OptIn(RawSpockApi::class)
1513
public actual class Surface internal constructor(
@@ -19,16 +17,8 @@ public actual class Surface internal constructor(
1917
* @since 0.0.1-alpha.4
2018
*/
2119
@RawSpockApi public val raw: NativeSurface,
22-
/**
23-
* The window onto which this [Surface] is drawn.
24-
*
25-
* @since 0.0.1-alpha.4
26-
*/
27-
public val window: Window
20+
public actual val window: Window
2821
) : AutoCloseable by raw {
29-
@OptIn(ExperimentalSpockApi::class)
30-
public actual val size: Deferred<Vector2ui> get() = window.size
31-
3222
public actual val preferredTextureFormat: TextureFormat? = null
3323

3424
public actual val supportedTextureFormats: ImmutableSet<TextureFormat>
@@ -40,8 +30,9 @@ public actual class Surface internal constructor(
4030
public actual val currentTexture: SurfaceTexture
4131
get() = raw.getCurrentTexture()
4232

33+
@OptIn(ExperimentalSpockApi::class)
4334
public actual suspend fun configure(configuration: SurfaceConfiguration) {
44-
val (width, height) = size.await()
35+
val (width, height) = window.size.await()
4536
raw.configure(configuration, width, height)
4637
}
4738

Diff for: toolkit/src/glfwMain/kotlin/Wgpu.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import io.ygdrasil.webgpu.Device
77
import io.ygdrasil.webgpu.DeviceDescriptor
88
import io.ygdrasil.webgpu.NativeSurface
99
import quest.laxla.spock.RawSpockApi
10-
import quest.laxla.spock.glfw.Window
10+
import quest.laxla.spock.glfw.GlfwWindow
1111

1212
/**
1313
* An instance of the wgpu WebGpu implementation, a [Surface] and [Adapter] factory.
@@ -27,14 +27,14 @@ internal expect fun createWebGpuOrNull(): Wgpu?
2727
*/
2828
public fun Wgpu(): Wgpu = createWebGpuOrNull() ?: error("Failed creating WebGpu instance")
2929

30-
internal expect fun Wgpu.getRawSurfaceOrNull(window: Window): NativeSurface?
30+
internal expect fun Wgpu.getRawSurfaceOrNull(window: GlfwWindow): NativeSurface?
3131

3232
/**
3333
* Creates a new [Surface] for this [window].
3434
*
3535
* @since 0.0.1-alpha.4
3636
*/
37-
public fun Wgpu.createSurface(window: Window): Surface = getRawSurfaceOrNull(window)?.let { raw ->
37+
public fun Wgpu.createSurface(window: GlfwWindow): Surface = getRawSurfaceOrNull(window)?.let { raw ->
3838
Surface(raw, window)
3939
} ?: error("Failed creating surface for window $window")
4040

Diff for: toolkit/src/jvmMain/kotlin/Wgpu.jvm.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import org.lwjgl.glfw.GLFWNativeWin32
1616
import org.lwjgl.glfw.GLFWNativeX11.glfwGetX11Display
1717
import org.lwjgl.glfw.GLFWNativeX11.glfwGetX11Window
1818
import quest.laxla.spock.*
19-
import quest.laxla.spock.glfw.Window
19+
import quest.laxla.spock.glfw.GlfwWindow
2020

2121
public actual typealias Wgpu = WGPU
2222

@@ -30,7 +30,7 @@ private const val X11 = "x11"
3030

3131
@OptIn(ExperimentalSpockApi::class)
3232
@RawSpockApi
33-
internal actual fun Wgpu.getRawSurfaceOrNull(window: Window): NativeSurface? = when (KTarget.current.operatingSystem) {
33+
internal actual fun Wgpu.getRawSurfaceOrNull(window: GlfwWindow): NativeSurface? = when (KTarget.current.operatingSystem) {
3434
OperatingSystem.Linux, OperatingSystem.FreeBsd -> when (val sessionType = System.getenv(SessionType)) {
3535
X11 -> getSurfaceFromX11Window(
3636
glfwGetX11Display().toNativeAddress(),

Diff for: toolkit/src/linuxMain/kotlin/Wgpu.linux.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ package quest.laxla.spock.toolkit
55
import ffi.NativeAddress
66
import io.ygdrasil.webgpu.NativeSurface
77
import kotlinx.cinterop.ExperimentalForeignApi
8-
import quest.laxla.spock.glfw.Window
8+
import quest.laxla.spock.glfw.GlfwWindow
99

10-
internal actual fun Wgpu.getRawSurfaceOrNull(window: Window): NativeSurface? = window.x11Display?.let { display ->
10+
internal actual fun Wgpu.getRawSurfaceOrNull(window: GlfwWindow): NativeSurface? = window.x11Display?.let { display ->
1111
window.x11Window?.let { window ->
1212
getSurfaceFromX11Window(NativeAddress(display), window)
1313
}

Diff for: toolkit/src/linuxMain/kotlin/X11.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import kotlinx.cinterop.CPointer
99
import kotlinx.cinterop.ExperimentalForeignApi
1010
import platform.posix.size_t
1111
import quest.laxla.spock.RawSpockApi
12-
import quest.laxla.spock.glfw.Window
12+
import quest.laxla.spock.glfw.GlfwWindow
1313

1414
@OptIn(RawSpockApi::class)
15-
public val Window.x11Display: CPointer<out CPointed>? get() = glfwGetX11Display(raw)
15+
public val GlfwWindow.x11Display: CPointer<out CPointed>? get() = glfwGetX11Display(raw)
1616

1717
@OptIn(RawSpockApi::class)
18-
public val Window.x11Window: size_t? get() = glfwGetX11Window(raw).takeIf { it != 0uL }
18+
public val GlfwWindow.x11Window: size_t? get() = glfwGetX11Window(raw).takeIf { it != 0uL }

Diff for: windowing/build.gradle.kts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@file:OptIn(ExperimentalWasmDsl::class)
2+
3+
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
4+
5+
plugins {
6+
alias(libs.plugins.publish)
7+
dokka
8+
multiplatform
9+
}
10+
11+
kotlin {
12+
jvm()
13+
linuxArm64()
14+
linuxX64()
15+
mingwX64()
16+
wasmJs().browser()
17+
18+
sourceSets {
19+
commonMain.dependencies {
20+
api(projects.math)
21+
22+
// TODO: remove
23+
api(projects.util)
24+
}
25+
}
26+
}

Diff for: windowing/gradle.properties

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
POM_ARTIFACT_ID=spock-windowing
2+
POM_DESCRIPTION=Windowing abstraction library
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
package quest.laxla.spock.glfw
1+
package quest.laxla.spock.windowing
22

33
import kotlinx.coroutines.Deferred
4-
import kotlinx.coroutines.Job
54
import quest.laxla.spock.ExperimentalSpockApi
65
import quest.laxla.spock.SuspendCloseable
76
import quest.laxla.spock.math.Vector2ui
87

98
/**
10-
* @since 0.0.1-alpha.1
9+
* @since 0.0.1-alpha.4
1110
*/
12-
public expect class Window(width: UInt, height: UInt, title: String) : SuspendCloseable {
11+
public interface Window : SuspendCloseable {
1312
/**
1413
* Was this window instructed by the operating system to close?
1514
*
@@ -24,12 +23,4 @@ public expect class Window(width: UInt, height: UInt, title: String) : SuspendCl
2423
*/
2524
@ExperimentalSpockApi
2625
public val size: Deferred<Vector2ui>
27-
28-
/**
29-
* @since 0.0.1-alpha.4
30-
*/
31-
@ExperimentalSpockApi
32-
public inline fun setFramebufferSizeCallback(crossinline callback: Window.(width: UInt, height: UInt) -> Unit): Job
33-
34-
override suspend fun close()
3526
}

0 commit comments

Comments
 (0)