From 3f20e632b9b9864b049051993d1b46683da895e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thea=20Sch=C3=B6bl?= Date: Tue, 4 Dec 2018 13:06:31 +0100 Subject: [PATCH] Started making use of LWJGL --- .idea/encodings.xml | 4 + .idea/libraries/lwjgl.xml | 9 ++ .idea/libraries/lwjgl_glfw.xml | 9 ++ .idea/libraries/lwjgl_glfw_natives_linux.xml | 9 ++ .idea/libraries/lwjgl_jawt.xml | 9 ++ .idea/libraries/lwjgl_jemalloc.xml | 9 ++ .../lwjgl_jemalloc_natives_linux.xml | 9 ++ .idea/libraries/lwjgl_natives_linux.xml | 9 ++ .idea/libraries/lwjgl_opengl.xml | 9 ++ .../libraries/lwjgl_opengl_natives_linux.xml | 9 ++ .idea/libraries/lwjgl_rpmalloc.xml | 9 ++ .../lwjgl_rpmalloc_natives_linux.xml | 9 ++ Bsl.iml | 11 ++ src/LWJGL_Main.kt | 123 ++++++++++++++++++ 14 files changed, 237 insertions(+) create mode 100644 .idea/encodings.xml create mode 100644 .idea/libraries/lwjgl.xml create mode 100644 .idea/libraries/lwjgl_glfw.xml create mode 100644 .idea/libraries/lwjgl_glfw_natives_linux.xml create mode 100644 .idea/libraries/lwjgl_jawt.xml create mode 100644 .idea/libraries/lwjgl_jemalloc.xml create mode 100644 .idea/libraries/lwjgl_jemalloc_natives_linux.xml create mode 100644 .idea/libraries/lwjgl_natives_linux.xml create mode 100644 .idea/libraries/lwjgl_opengl.xml create mode 100644 .idea/libraries/lwjgl_opengl_natives_linux.xml create mode 100644 .idea/libraries/lwjgl_rpmalloc.xml create mode 100644 .idea/libraries/lwjgl_rpmalloc_natives_linux.xml create mode 100644 src/LWJGL_Main.kt diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..15a15b2 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/libraries/lwjgl.xml b/.idea/libraries/lwjgl.xml new file mode 100644 index 0000000..96364d3 --- /dev/null +++ b/.idea/libraries/lwjgl.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lwjgl_glfw.xml b/.idea/libraries/lwjgl_glfw.xml new file mode 100644 index 0000000..2d29cef --- /dev/null +++ b/.idea/libraries/lwjgl_glfw.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lwjgl_glfw_natives_linux.xml b/.idea/libraries/lwjgl_glfw_natives_linux.xml new file mode 100644 index 0000000..20a2995 --- /dev/null +++ b/.idea/libraries/lwjgl_glfw_natives_linux.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lwjgl_jawt.xml b/.idea/libraries/lwjgl_jawt.xml new file mode 100644 index 0000000..d5dc3a4 --- /dev/null +++ b/.idea/libraries/lwjgl_jawt.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lwjgl_jemalloc.xml b/.idea/libraries/lwjgl_jemalloc.xml new file mode 100644 index 0000000..21f820c --- /dev/null +++ b/.idea/libraries/lwjgl_jemalloc.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lwjgl_jemalloc_natives_linux.xml b/.idea/libraries/lwjgl_jemalloc_natives_linux.xml new file mode 100644 index 0000000..b03a6c5 --- /dev/null +++ b/.idea/libraries/lwjgl_jemalloc_natives_linux.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lwjgl_natives_linux.xml b/.idea/libraries/lwjgl_natives_linux.xml new file mode 100644 index 0000000..c4b3d5c --- /dev/null +++ b/.idea/libraries/lwjgl_natives_linux.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lwjgl_opengl.xml b/.idea/libraries/lwjgl_opengl.xml new file mode 100644 index 0000000..8f53cf6 --- /dev/null +++ b/.idea/libraries/lwjgl_opengl.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lwjgl_opengl_natives_linux.xml b/.idea/libraries/lwjgl_opengl_natives_linux.xml new file mode 100644 index 0000000..02c5a1f --- /dev/null +++ b/.idea/libraries/lwjgl_opengl_natives_linux.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lwjgl_rpmalloc.xml b/.idea/libraries/lwjgl_rpmalloc.xml new file mode 100644 index 0000000..55d1e80 --- /dev/null +++ b/.idea/libraries/lwjgl_rpmalloc.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/lwjgl_rpmalloc_natives_linux.xml b/.idea/libraries/lwjgl_rpmalloc_natives_linux.xml new file mode 100644 index 0000000..4173e32 --- /dev/null +++ b/.idea/libraries/lwjgl_rpmalloc_natives_linux.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Bsl.iml b/Bsl.iml index 245d342..a821316 100644 --- a/Bsl.iml +++ b/Bsl.iml @@ -8,5 +8,16 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/LWJGL_Main.kt b/src/LWJGL_Main.kt new file mode 100644 index 0000000..5e94bf5 --- /dev/null +++ b/src/LWJGL_Main.kt @@ -0,0 +1,123 @@ +import org.lwjgl.* +import org.lwjgl.glfw.* +import org.lwjgl.opengl.* +import org.lwjgl.system.* + +import java.nio.* + +import com.sun.org.apache.xml.internal .utils.StringBufferPool.free +import java.time.Clock.system +import javax.swing.Spring.height +import javax.swing.Spring.width +import org.lwjgl.glfw.Callbacks.* +import org.lwjgl.glfw.GLFW.* +import org.lwjgl.opengl.GL11.* +import org.lwjgl.system.MemoryStack.* +import org.lwjgl.system.MemoryUtil.* + +class LWJGL_Main { + + // The window handle + private var window: Long = 0 + + fun run() { + System.out.println("Hello LWJGL " + Version.getVersion() + "!") + + init() + loop() + + // Free the window callbacks and destroy the window + glfwFreeCallbacks(window) + glfwDestroyWindow(window) + + // Terminate GLFW and free the error callback + glfwTerminate() + glfwSetErrorCallback(null)!!.free() + } + + private fun init() { + // Setup an error callback. The default implementation + // will print the error message in System.err. + GLFWErrorCallback.createPrint(System.err).set() + + // Initialize GLFW. Most GLFW functions will not work before doing this. + if (!glfwInit()) + throw IllegalStateException("Unable to initialize GLFW") + + // Configure GLFW + glfwDefaultWindowHints() // optional, the current window hints are already the default + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE) // the window will stay hidden after creation + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE) // the window will be resizable + + // Create the window + window = glfwCreateWindow(300, 300, "Hello World!", NULL, NULL) + if (window == NULL) + throw RuntimeException("Failed to create the GLFW window") + + // Setup a key callback. It will be called every time a key is pressed, repeated or released. + glfwSetKeyCallback(window) { window, key, scancode, action, mods -> + if (key === GLFW_KEY_ESCAPE && action === GLFW_RELEASE) + glfwSetWindowShouldClose(window, true) // We will detect this in the rendering loop + } + + // Get the thread stack and push a new frame + stackPush().use({ stack -> + val pWidth = stack.mallocInt(1) // int* + val pHeight = stack.mallocInt(1) // int* + + // Get the window size passed to glfwCreateWindow + glfwGetWindowSize(window, pWidth, pHeight) + + // Get the resolution of the primary monitor + val vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()) + + // Center the window + glfwSetWindowPos( + window, + (vidmode!!.width() - pWidth.get(0)) / 2, + (vidmode.height() - pHeight.get(0)) / 2 + ) + }) // the stack frame is popped automatically + + // Make the OpenGL context current + glfwMakeContextCurrent(window) + // Enable v-sync + glfwSwapInterval(1) + + // Make the window visible + glfwShowWindow(window) + } + + private fun loop() { + // This line is critical for LWJGL's interoperation with GLFW's + // OpenGL context, or any context that is managed externally. + // LWJGL detects the context that is current in the current thread, + // creates the GLCapabilities instance and makes the OpenGL + // bindings available for use. + GL.createCapabilities() + + // Set the clear color + glClearColor(1.0f, 0.0f, 0.0f, 0.0f) + + // Run the rendering loop until the user has attempted to close + // the window or has pressed the ESCAPE key. + while (!glfwWindowShouldClose(window)) { + glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) // clear the framebuffer + + glfwSwapBuffers(window) // swap the color buffers + + // Poll for window events. The key callback above will only be + // invoked during this call. + glfwPollEvents() + } + } + + companion object { + + @JvmStatic + fun main(args: Array) { + LWJGL_Main().run() + } + } + +} \ No newline at end of file