Skip to content

Instantly share code, notes, and snippets.

@Efimero
Created May 8, 2019 12:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Efimero/2c0af4ae3aeaf2c85136aa06ff077919 to your computer and use it in GitHub Desktop.
Save Efimero/2c0af4ae3aeaf2c85136aa06ff077919 to your computer and use it in GitHub Desktop.
LWJGL3 example implemented in Clojure (linux)
(defproject test-3d "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.0"]
[org.lwjgl/lwjgl "3.2.2"]
[org.lwjgl/lwjgl "3.2.2" :classifier "natives-linux"]
[org.lwjgl/lwjgl-opengl "3.2.2"]
[org.lwjgl/lwjgl-opengl "3.2.2" :classifier "natives-linux"]
[org.lwjgl/lwjgl-glfw "3.2.2"]
[org.lwjgl/lwjgl-glfw "3.2.2" :classifier "natives-linux"]]
:main ^:skip-aot test-3d.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
(ns test-3d.core
(:import (org.lwjgl Version)
(org.lwjgl.glfw Callbacks GLFW GLFWKeyCallbackI GLFWErrorCallback)
(org.lwjgl.opengl GL GL11)
(org.lwjgl.system MemoryStack MemoryUtil))
(:gen-class))
(defn -init []
(.set (GLFWErrorCallback/createPrint System/err))
(when-not (GLFW/glfwInit)
(throw (IllegalStateException. "Unable to initialize GLFW")))
(GLFW/glfwDefaultWindowHints)
(GLFW/glfwWindowHint GLFW/GLFW_VISIBLE GLFW/GLFW_FALSE)
(GLFW/glfwWindowHint GLFW/GLFW_RESIZABLE GLFW/GLFW_TRUE)
(def window (atom (GLFW/glfwCreateWindow (int 300) (int 300) (String. "I'm gay!") (long 0) (long 0))))
(when (nil? window)
(throw (RuntimeException. "Failed to create the GLFW window")))
(GLFW/glfwSetKeyCallback
@window
(reify GLFWKeyCallbackI
(invoke [$this $window $key $scancode $action $mods]
(when (and (= $key GLFW/GLFW_KEY_ESCAPE)
(= $action GLFW/GLFW_RELEASE))
(GLFW/glfwSetWindowShouldClose $window true)))))
(try
(let [stack (MemoryStack/stackPush)
pWidth (.mallocInt stack (int 1))
pHeight (.mallocInt stack (int 1))]
(GLFW/glfwGetWindowSize @window pWidth pHeight)
(let [vidmode (GLFW/glfwGetVideoMode (GLFW/glfwGetPrimaryMonitor))]
(GLFW/glfwSetWindowPos
@window
(/ (- (.width vidmode) (.get pWidth 0)) 2)
(/ (- (.height vidmode) (.get pHeight 0)) 2) ))))
(GLFW/glfwMakeContextCurrent @window)
(GLFW/glfwSwapInterval (int 1))
(GLFW/glfwShowWindow @window))
(defn -loop []
(GL/createCapabilities)
(GL11/glClearColor (float 1) (float 0) (float 0) (float 0))
(while (not (GLFW/glfwWindowShouldClose @window))
(GL11/glClear (bit-or GL11/GL_COLOR_BUFFER_BIT GL11/GL_DEPTH_BUFFER_BIT))
(GLFW/glfwSwapBuffers @window)
(GLFW/glfwPollEvents)))
(defn -run []
(println "Hello, World! I'm gay!\nVersion:" (Version/getVersion))
(-init)
(-loop)
(Callbacks/glfwFreeCallbacks @window)
(GLFW/glfwDestroyWindow @window)
(GLFW/glfwTerminate)
(.free (GLFW/glfwSetErrorCallback nil)))
(defn -main
"Entry point"
[& args]
(-run))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment