Skip to content
This repository was archived by the owner on Mar 26, 2022. It is now read-only.

Commit

Permalink
Added GlfwWindow, Vec type (and aliases), and package
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroeightysix committed Sep 24, 2020
1 parent 0201213 commit 9510bd8
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 6 deletions.
28 changes: 27 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.gradle.internal.os.OperatingSystem.*

val lwjglVersion = "3.2.3"

val lwjglNatives = when (current()) {
LINUX -> "natives-linux"
MAC_OS -> "natives-macos"
WINDOWS -> "natives-windows"
else -> throw Error("Unrecognized or unsupported Operating system. Please set \"lwjglNatives\" manually")
}

plugins {
kotlin("jvm") version "1.4.10"
Expand All @@ -10,6 +20,22 @@ repositories {
mavenCentral()
}

dependencies {
implementation(platform("org.lwjgl:lwjgl-bom:$lwjglVersion"))

implementation("org.lwjgl", "lwjgl")
implementation("org.lwjgl", "lwjgl-glfw")
implementation("org.lwjgl", "lwjgl-opengl")
implementation("org.lwjgl", "lwjgl-stb")
runtimeOnly("org.lwjgl", "lwjgl", classifier = lwjglNatives)
runtimeOnly("org.lwjgl", "lwjgl-glfw", classifier = lwjglNatives)
runtimeOnly("org.lwjgl", "lwjgl-opengl", classifier = lwjglNatives)
runtimeOnly("org.lwjgl", "lwjgl-stb", classifier = lwjglNatives)
}

tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = listOf("-Xallow-result-return-type")
}
}
5 changes: 0 additions & 5 deletions src/main/kotlin/Kape.kt

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/kotlin/me/zeroeightsix/kape/Kape.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.zeroeightsix.kape

class Kape {



}
7 changes: 7 additions & 0 deletions src/main/kotlin/me/zeroeightsix/kape/math/Vec2.kt
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>
68 changes: 68 additions & 0 deletions src/main/kotlin/me/zeroeightsix/kape/window/GlfwWindow.kt
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)
}
}
}

}
12 changes: 12 additions & 0 deletions src/test/kotlin/Main.kt
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()

}

0 comments on commit 9510bd8

Please sign in to comment.