This repository was archived by the owner on Mar 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added GlfwWindow, Vec type (and aliases), and package
- Loading branch information
1 parent
0201213
commit 9510bd8
Showing
6 changed files
with
121 additions
and
6 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 was deleted.
Oops, something went wrong.
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,7 @@ | ||
package me.zeroeightsix.kape | ||
|
||
class Kape { | ||
|
||
|
||
|
||
} |
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,7 @@ | ||
package me.zeroeightsix.kape.math | ||
|
||
class Vec2<T : Number>(val x: T, val y: T) | ||
|
||
typealias Vec2f = Vec2<Float> | ||
typealias Vec2d = Vec2<Double> | ||
typealias Vec2i = Vec2<Int> |
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,68 @@ | ||
package me.zeroeightsix.kape.window | ||
|
||
import me.zeroeightsix.kape.math.Vec2 | ||
import me.zeroeightsix.kape.math.Vec2i | ||
import org.lwjgl.glfw.Callbacks.glfwFreeCallbacks | ||
import org.lwjgl.glfw.GLFW | ||
import org.lwjgl.glfw.GLFW.* | ||
import org.lwjgl.glfw.GLFWKeyCallback | ||
import org.lwjgl.system.MemoryUtil.NULL | ||
|
||
private fun Boolean.asGlfwBool() = if (this) GLFW_TRUE else GLFW_FALSE | ||
|
||
class GlfwWindow private constructor(private val handle: Long) { | ||
|
||
fun freeCallbacks() = glfwFreeCallbacks(this.handle) | ||
fun destroy() = glfwDestroyWindow(this.handle) | ||
|
||
fun freeAndDestroy() { | ||
freeCallbacks() | ||
destroy() | ||
} | ||
|
||
companion object { | ||
private fun installCallbacks(handle: Long) { | ||
glfwSetKeyCallback(handle, glfwKeyCallback { window, key, scancode, action, mods -> | ||
println("$window: $key#$scancode ($action) mods $mods") | ||
}) | ||
} | ||
|
||
fun fromHandle(handle: Long, installCallbacks: Boolean = true) = | ||
GlfwWindow(handle).also { if (installCallbacks) installCallbacks(it.handle) } | ||
|
||
fun createWindow( | ||
title: String = "me.zeroeightsix.kape.Kape window", | ||
makeVisible: Boolean = true, | ||
resizable: Boolean = true, | ||
size: Vec2i = Vec2(300, 300) | ||
): Result<GlfwWindow> { | ||
if (!glfwInit()) return Result.failure(IllegalStateException("Unable to initialise GLFW")) | ||
|
||
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE) | ||
glfwWindowHint(GLFW_RESIZABLE, resizable.asGlfwBool()) | ||
val handle = glfwCreateWindow(size.x, size.y, title, NULL, NULL) | ||
|
||
if (handle == NULL) return Result.failure(IllegalStateException("Failed to create GLFW window!")) | ||
|
||
installCallbacks(handle) | ||
|
||
if (makeVisible) { | ||
glfwShowWindow(handle) | ||
} | ||
|
||
return Result.success(GlfwWindow(handle)) | ||
} | ||
|
||
fun terminate() { | ||
glfwTerminate() | ||
glfwSetErrorCallback(null)?.free() | ||
} | ||
|
||
private fun glfwKeyCallback(onInvoke: (Long, Int, Int, Int, Int) -> Unit) = object : GLFWKeyCallback() { | ||
override fun invoke(window: Long, key: Int, scancode: Int, action: Int, mods: Int) { | ||
onInvoke(window, key, scancode, action, mods) | ||
} | ||
} | ||
} | ||
|
||
} |
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,12 @@ | ||
import me.zeroeightsix.kape.window.GlfwWindow | ||
|
||
fun main() { | ||
|
||
val window = GlfwWindow.createWindow().getOrThrow() | ||
|
||
Thread.sleep(1000) | ||
println("Exiting") | ||
window.destroy() | ||
GlfwWindow.terminate() | ||
|
||
} |