Skip to content

Commit f4d9ec7

Browse files
committed
refresh complete
1 parent 35ccb23 commit f4d9ec7

File tree

14 files changed

+855
-75
lines changed

14 files changed

+855
-75
lines changed

.idea/kotlinc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

awt/src/main/kotlin/uno/awt/LwjglCanvas.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ abstract class LwjglCanvas(val glDebug: Boolean = false) : Canvas() {
5858
// glfwWindowHint can be used here to configure the GL context
5959
val glfwWindow = GlfwWindow fromWin32Window hwnd
6060
glWindow = GlWindow(glfwWindow, forwardCompatible = false).apply {
61-
makeContextCurrent()
61+
makeCurrent()
6262
}
6363

6464
if (glDebug)
@@ -68,7 +68,7 @@ abstract class LwjglCanvas(val glDebug: Boolean = false) : Canvas() {
6868

6969
init()
7070

71-
glfwWindow.makeContextCurrent(false)
71+
glfwWindow.makeCurrent(false)
7272

7373
// println("/LwjglCanvas.initInternal ${Date().toInstant()}")
7474
}

core/src/main/kotlin/uno/glfw/GlWindow.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import gln.cap.Caps
44
import gln.misc.GlDebugSeverity
55
import gln.misc.GlDebugSource
66
import gln.misc.GlDebugType
7-
import org.lwjgl.glfw.GLFW
87
import org.lwjgl.opengl.GL
98
import org.lwjgl.opengl.GL43C
109
import org.lwjgl.opengl.GLUtil
@@ -21,17 +20,19 @@ open class GlWindow(val glfwWindow: GlfwWindow,
2120
* legacy functionality by mistake.
2221
* LWJGL will not try to load deprecated functions, so calling them will crash but the context will actually expose them"
2322
*/
23+
init {
24+
makeCurrent()
25+
/* This line is critical for LWJGL's interoperation with GLFW's OpenGL context,
26+
or any context that is managed externally.
27+
LWJGL detects the context that is current in the current thread, creates the GLCapabilities instance and
28+
makes the OpenGL bindings available for use. */
29+
// GL.createCapabilities() // useless, it's in Caps instantiation
30+
}
2431
val caps = Caps(profile, forwardCompatible)
2532

2633
var debugProc: Callback? = null
2734

2835
fun init(show: Boolean = true) {
29-
makeContextCurrent()
30-
/* This line is critical for LWJGL's interoperation with GLFW's OpenGL context,
31-
or any context that is managed externally.
32-
LWJGL detects the context that is current in the current thread, creates the GLCapabilities instance and
33-
makes the OpenGL bindings available for use. */
34-
GL.createCapabilities()
3536
glfwWindow.show(show)
3637
if (glfw.hints.context.debug) {
3738
debugProc = GLUtil.setupDebugMessageCallback()
@@ -46,17 +47,17 @@ open class GlWindow(val glfwWindow: GlfwWindow,
4647
}
4748

4849
inline fun inGlContext(block: () -> Unit) {
49-
makeContextCurrent()
50+
makeCurrent()
5051
GL.setCapabilities(caps.gl)
5152
block()
52-
makeContextCurrent(false)
53+
makeCurrent(false)
5354
}
5455

5556
/** for Java */
5657
override fun inContext(runnable: Runnable) {
57-
makeContextCurrent()
58+
makeCurrent()
5859
GL.setCapabilities(caps.gl)
5960
runnable.run()
60-
makeContextCurrent(false)
61+
makeCurrent(false)
6162
}
6263
}

core/src/main/kotlin/uno/glfw/GlfwWindow.kt

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,35 @@ import org.lwjgl.glfw.*
1212
import org.lwjgl.glfw.GLFW.*
1313
import org.lwjgl.system.MemoryStack
1414
import org.lwjgl.system.MemoryUtil
15-
import org.lwjgl.system.MemoryUtil.*
15+
import org.lwjgl.system.MemoryUtil.memGetAddress
16+
import org.lwjgl.system.MemoryUtil.memUTF8
1617
import org.lwjgl.system.Pointer
1718
import uno.kotlin.*
1819
import java.util.*
1920
import java.util.function.BooleanSupplier
2021
import java.util.function.Consumer
21-
22-
class Wut(dummy: Int) {
23-
24-
constructor() : this(0)
25-
init {
26-
println("init")
27-
}
28-
}
2922
open class GlfwWindow(var handle: Long) {
3023

3124
@Throws(RuntimeException::class)
3225
constructor(windowSize: Vec2i,
3326
title: String = "Glfw Window",
3427
monitor: GlfwMonitor = GlfwMonitor.NULL,
35-
share: GlfwWindow = NULL,
28+
share: GlfwWindow? = null,
3629
position: Vec2i? = null) : this(windowSize.x, windowSize.y, title, monitor, share, position)
3730

3831
@Throws(RuntimeException::class)
3932
constructor(x: Int,
4033
title: String = "Glfw Window",
4134
monitor: GlfwMonitor = GlfwMonitor.NULL,
42-
share: GlfwWindow = NULL,
35+
share: GlfwWindow? = null,
4336
position: Vec2i? = null) : this(x, x, title, monitor, share, position)
4437

4538
@Throws(RuntimeException::class)
4639
constructor(width: Int, height: Int,
4740
title: String = "Glfw Window",
4841
monitor: GlfwMonitor = GlfwMonitor.NULL,
49-
share: GlfwWindow = NULL,
50-
position: Vec2i? = null) : this(glfwCreateWindow(width, height, title, monitor.handle, share.handle)) {
42+
share: GlfwWindow? = null,
43+
position: Vec2i? = null) : this(glfwCreateWindow(width, height, title, monitor.handle, share?.handle ?: MemoryUtil.NULL)) {
5144

5245
this.title = title
5346

@@ -57,11 +50,10 @@ open class GlfwWindow(var handle: Long) {
5750
// --- [ glfwCreateWindow ] ---
5851

5952
init {
60-
println("init")
61-
// if (handle == MemoryUtil.NULL) {
62-
// glfw.terminate()
63-
// throw RuntimeException("Failed to create the GLFW window")
64-
// }
53+
if (handle == MemoryUtil.NULL) {
54+
glfw.terminate()
55+
throw RuntimeException("Failed to create the GLFW window")
56+
}
6557
}
6658

6759
// --- [ glfwDestroyWindow ] ---
@@ -361,7 +353,8 @@ open class GlfwWindow(var handle: Long) {
361353
enum class CursorMode(val i: Int) {
362354
Normal(GLFW_CURSOR_NORMAL),
363355
Hidden(GLFW_CURSOR_HIDDEN),
364-
Disabled(GLFW_CURSOR_DISABLED), ;
356+
Disabled(GLFW_CURSOR_DISABLED),
357+
Captured(GLFW_CURSOR_CAPTURED);
365358

366359
companion object {
367360
infix fun of(i: Int) = values().first { it.i == i }
@@ -420,7 +413,7 @@ open class GlfwWindow(var handle: Long) {
420413
var keyCB: KeyCB?
421414
@Deprecated(message = "Write only property", level = DeprecationLevel.HIDDEN) get() = error("")
422415
set(value) {
423-
val cb = value?.let { GLFWKeyCallbackI { wnd, key, scanCode, action, mods -> it(GlfwWindow(wnd), key, scanCode, action, mods) } }
416+
val cb = value?.let { GLFWKeyCallbackI { wnd, key, scanCode, action, mods -> it(GlfwWindow(wnd), Key of key, scanCode, InputAction of action, mods) } }
424417
glfwSetKeyCallback(handle, cb)?.free()
425418
}
426419

@@ -453,7 +446,7 @@ open class GlfwWindow(var handle: Long) {
453446
var cursorPosCB: CursorPosCB?
454447
@Deprecated(message = "Write only property", level = DeprecationLevel.HIDDEN) get() = error("")
455448
set(value) {
456-
val cb = value?.let { GLFWCursorPosCallbackI { wnd, xPos, yPos -> it(GlfwWindow(wnd), Vec2(xPos, yPos)) } }
449+
val cb = value?.let { GLFWCursorPosCallbackI { wnd, xPos, yPos -> it(GlfwWindow(wnd), Vec2d(xPos, yPos)) } }
457450
glfwSetCursorPosCallback(handle, cb)?.free()
458451
}
459452

@@ -487,17 +480,19 @@ open class GlfwWindow(var handle: Long) {
487480
glfwSetDropCallback(handle, cb)?.free()
488481
}
489482

490-
491483
// --- [ glfwGetClipboardString ] ---
492484
// --- [ glfwSetClipboardString ] ---
485+
// Parameters
486+
// [in] window Deprecated. Any valid window or NULL.
487+
// -> glfw object
493488
var clipboardString: String?
494489
get() = glfwGetClipboardString(handle)
495490
set(value) {
496491
value?.let { glfwSetClipboardString(handle, it) }
497492
}
498493

499494
// --- [ glfwMakeContextCurrent ] ---
500-
fun makeContextCurrent(current: Boolean = true) = glfwMakeContextCurrent(if (current) handle else MemoryUtil.NULL)
495+
fun makeCurrent(current: Boolean = true) = glfwMakeContextCurrent(if (current) handle else MemoryUtil.NULL)
501496

502497
// --- [ glfwSwapBuffers ] ---
503498

@@ -507,16 +502,16 @@ open class GlfwWindow(var handle: Long) {
507502
fun present() = swapBuffers()
508503

509504
inline fun inContext(block: () -> Unit) {
510-
makeContextCurrent()
505+
makeCurrent()
511506
block()
512-
makeContextCurrent(false)
507+
makeCurrent(false)
513508
}
514509

515510
/** for Java */
516511
open fun inContext(runnable: Runnable) {
517-
makeContextCurrent()
512+
makeCurrent()
518513
runnable.run()
519-
makeContextCurrent(false)
514+
makeCurrent(false)
520515
}
521516

522517
var autoSwap = true
@@ -573,7 +568,5 @@ open class GlfwWindow(var handle: Long) {
573568

574569
@JvmStatic
575570
infix fun from(handle: Long): GlfwWindow = GlfwWindow(handle)
576-
577-
val NULL = GlfwWindow(MemoryUtil.NULL)
578571
}
579572
}

core/src/main/kotlin/uno/glfw/glfw.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ object glfw {
277277
// -> GlfwWindow
278278

279279
// --- [ glfwRawMouseMotionSupported ] ---
280-
val isRawMouseMotionSupported: Boolean
280+
val rawMouseMotionSupported: Boolean
281281
get() = glfwRawMouseMotionSupported()
282282

283283
// --- [ glfwGetKeyName ] ---
@@ -294,7 +294,8 @@ object glfw {
294294
// -> GlfwWindow
295295

296296
// --- [ glfwCreateCursor ] ---
297-
fun createCursor(image: GLFWImage, xhot: Int, yhot: Int) = GlfwCursor(glfwCreateCursor(image, xhot, yhot))
297+
// --- [ glfwDestroyCursor ] ---
298+
// -> GlfwCursor
298299

299300
// --- [ glfwCreateStandardCursor ] ---
300301

@@ -349,7 +350,14 @@ object glfw {
349350

350351
// --- [ glfwSetClipboardString ] ---
351352
// --- [ glfwGetClipboardString ] ---
352-
// -> GlfwWindow
353+
// Parameters
354+
// [in] window Deprecated. Any valid window or NULL.
355+
// -> here
356+
var clipboardString: String?
357+
get() = glfwGetClipboardString(NULL)
358+
set(value) {
359+
value?.let { glfwSetClipboardString(NULL, it) }
360+
}
353361

354362
// --- [ glfwGetTime ] ---
355363
// --- [ glfwSetTime ] ---

core/src/main/kotlin/uno/glfw/helpers.kt

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
@file:OptIn(ExperimentalUnsignedTypes::class)
2+
13
package uno.glfw
24

35
import glm_.vec2.Vec2
46
import glm_.vec2.Vec2d
57
import glm_.vec2.Vec2i
68
import gln.L
79
import kool.*
10+
import org.lwjgl.glfw.GLFW
811
import org.lwjgl.glfw.GLFW.glfwDestroyCursor
912
import org.lwjgl.glfw.GLFWGammaRamp
13+
import org.lwjgl.glfw.GLFWImage
1014
import org.lwjgl.system.MemoryStack
11-
import org.lwjgl.system.MemoryUtil.memPutAddress
12-
import uno.kotlin.ptrInt
15+
import org.lwjgl.system.MemoryUtil
16+
import org.lwjgl.system.MemoryUtil.*
17+
import uno.kotlin.ptrUByte
1318
import uno.kotlin.ptrUShort
1419

1520

@@ -27,7 +32,7 @@ operator fun Ptr<GlfwVidMode>.get(index: Int) = GlfwVidMode((adr.L + index * Glf
2732

2833
class GlfwGammaRamp(val red: IntArray, val green: IntArray, val blue: IntArray) {
2934

30-
fun toStack(stack: MemoryStack): Ptr<GlfwGammaRamp> {
35+
infix fun toStack(stack: MemoryStack): Ptr<GlfwGammaRamp> {
3136
val r = stack.ptrUShort(red.size)
3237
val g = stack.ptrUShort(red.size)
3338
val b = stack.ptrUShort(red.size)
@@ -36,13 +41,11 @@ class GlfwGammaRamp(val red: IntArray, val green: IntArray, val blue: IntArray)
3641
g[i] = green[i].toUShort()
3742
b[i] = blue[i].toUShort()
3843
}
39-
val s = stack.ptrInt()
40-
s[0] = red.size
4144
val ptr = stack.nmalloc(GLFWGammaRamp.ALIGNOF, GLFWGammaRamp.SIZEOF)
4245
memPutAddress(ptr + GLFWGammaRamp.RED, r.adr.L)
4346
memPutAddress(ptr + GLFWGammaRamp.GREEN, g.adr.L)
4447
memPutAddress(ptr + GLFWGammaRamp.BLUE, b.adr.L)
45-
memPutAddress(ptr + GLFWGammaRamp.SIZE, s.adr.L)
48+
memPutInt(ptr + GLFWGammaRamp.SIZE, red.size)
4649
return ptr.toPtr()
4750
}
4851
}
@@ -57,9 +60,21 @@ fun GlfwGammaRamp(ptr: Ptr<GlfwGammaRamp>): GlfwGammaRamp {
5760

5861
@JvmInline
5962
value class GlfwCursor(val handle: Long) {
63+
// --- [ glfwCreateCursor ] ---
64+
constructor(image: GlfwImage, hot: Int) : this(image, hot, hot)
65+
constructor(image: GlfwImage, xHot: Int, yHot: Int) : this(stack { GLFW.nglfwCreateCursor(image.toStack(it).adr.L, xHot, yHot) })
6066

6167
// --- [ glfwDestroyCursor ] ---
6268
fun destroy() = glfwDestroyCursor(handle)
69+
70+
val isValid: Boolean
71+
get() = handle != MemoryUtil.NULL
72+
val isNotValid: Boolean
73+
get() = !isValid
74+
75+
companion object {
76+
val NULL = GlfwCursor(MemoryUtil.NULL)
77+
}
6378
}
6479

6580
typealias WindowPosCB = (window: GlfwWindow, pos: Vec2i) -> Unit
@@ -71,11 +86,11 @@ typealias WindowIconifyCB = (window: GlfwWindow, iconified: Boolean) -> Unit
7186
typealias WindowMaximizeCB = (window: GlfwWindow, maximized: Boolean) -> Unit
7287
typealias FramebufferSizeCB = (window: GlfwWindow, size: Vec2i) -> Unit
7388
typealias WindowContentScaleCB = (window: GlfwWindow, scale: Vec2) -> Unit
74-
typealias KeyCB = (window: GlfwWindow, key: Int, scanCode: Int, action: Int, mods: Int) -> Unit
89+
typealias KeyCB = (window: GlfwWindow, key: Key, scanCode: Int, action: InputAction, mods: Int) -> Unit
7590
typealias CharCB = (window: GlfwWindow, codePoint: Int) -> Unit
7691
typealias CharModsCB = (window: GlfwWindow, codePoint: Int, mods: Int) -> Unit
7792
typealias MouseButtonCB = (window: GlfwWindow, button: Int, action: Int, mods: Int) -> Unit
78-
typealias CursorPosCB = (window: GlfwWindow, pos: Vec2) -> Unit
93+
typealias CursorPosCB = (window: GlfwWindow, pos: Vec2d) -> Unit
7994
typealias CursorEnterCB = (window: GlfwWindow, entered: Boolean) -> Unit
8095
typealias ScrollCB = (window: GlfwWindow, scroll: Vec2d) -> Unit
8196
typealias DropCB = (window: GlfwWindow, names: Array<String>) -> Unit
@@ -88,4 +103,19 @@ enum class VSync {
88103
val i = ordinal - 2
89104
}
90105

91-
typealias GLFWglproc = Long
106+
typealias GLFWglproc = Long
107+
108+
class GlfwImage(val width: Int, val height: Int, val pixels: UByteArray) {
109+
constructor(size: Int, pixels: UByteArray) : this(size, size, pixels)
110+
111+
infix fun toStack(stack: MemoryStack): Ptr<GlfwImage> {
112+
val ptr = stack.nmalloc(GLFWImage.ALIGNOF, GLFWImage.SIZEOF)
113+
memPutInt(ptr + GLFWImage.WIDTH, width)
114+
memPutInt(ptr + GLFWImage.HEIGHT, height)
115+
val pPixels = stack.ptrUByte(pixels.size)
116+
for (i in pixels.indices)
117+
pPixels[i] = pixels[i]
118+
memPutAddress(ptr + GLFWImage.PIXELS, pPixels.adr.L)
119+
return ptr.toPtr()
120+
}
121+
}

core/src/main/kotlin/uno/glfw/inputEnums.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ enum class Key(val i: Int) {
142142
}
143143
}
144144

145+
enum class InputAction(val i: Int) {
146+
Press(GLFW_PRESS), Release(GLFW_RELEASE), Repeat(GLFW_REPEAT);
147+
companion object {
148+
infix fun of(i: Int) = values().first { it.i == i }
149+
}
150+
}
151+
145152
enum class MouseButton(@JvmField val i: Int) {
146153

147154
`1`(0),
@@ -152,9 +159,9 @@ enum class MouseButton(@JvmField val i: Int) {
152159
`6`(5),
153160
`7`(6),
154161
`8`(7),
155-
LAST (`8`.i),
156-
LEFT (`1`.i),
157-
RIGHT (`2`.i),
162+
LAST(`8`.i),
163+
LEFT(`1`.i),
164+
RIGHT(`2`.i),
158165
MIDDLE(`3`.i);
159166

160167
companion object {

0 commit comments

Comments
 (0)