Initial commit

This commit is contained in:
peter.selby@bytedance.com 2023-12-27 15:51:15 -06:00
commit e8a17566dd
4 changed files with 136 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.cpcache/
target/
.nrepl-port

36
build.clj Normal file
View File

@ -0,0 +1,36 @@
(ns build
(:require [clojure.tools.build.api :as b]))
(def lib 'org.fudo/ois)
(def version (format "0.1.%s" (b/git-count-revs nil)))
(def class-dir "target/classes")
(def basis (b/create-basis {:project "deps.edn"}))
(def jar-file (format "target/%s-%s.jar" (name lib) version))
(def uber-file (format "target/%s-%s-uber.jar" (name lib) version))
(defn clean [_]
(b/delete {:path "target"}))
(defn jar [_]
(b/write-pom {:class-dir class-dir
:lib lib
:version version
:basis basis
:src-dirs ["src"]})
(b/copy-dir {:src-dirs ["src" "resources"]
:target-dir class-dir})
(b/jar {:class-dir class-dir
:jar-file jar-file
:main 'ois.core}))
(defn uber [_]
(clean nil)
(b/copy-dir {:src-dirs ["src" "resources"]
:target-dir class-dir})
(b/compile-clj {:basis basis
:ns-compile '[ois.core]
:class-dir class-dir})
(b/uber {:class-dir class-dir
:uber-file uber-file
:basis basis
:main 'ois.core}))

16
deps.edn Normal file
View File

@ -0,0 +1,16 @@
{
:paths ["src"]
:deps {
org.clojure/clojure { :mvn/version "1.11.1" }
metosin/malli { :mvn/version "0.13.0" }
org.lwjgl/lwjgl { :mvn/version "3.3.3" }
org.lwjgl/lwjgl-glfw { :mvn/version "3.3.3" }
org.lwjgl/lwjgl-opengl { :mvn/version "3.3.3" }
}
:aliases {
:build {
:deps {io.github.clojure/tools.build { :mvn/version "0.9.6" }}
:ns-default build
}
}
}

81
src/ois/core.clj Normal file
View File

@ -0,0 +1,81 @@
(ns ois.core
(:gen-class)
(:import (org.lwjgl Version)
(org.lwjgl.glfw GLFWErrorCallback GLFW GLFWKeyCallbackI Callbacks)
(org.lwjgl.opengl GL GL33)
(org.lwjgl.system MemoryStack)
(java.lang IllegalStateException)))
(require '[nrepl.server :refer [start-server stop-server]])
(defonce server (start-server :port 7888))
(declare window)
(declare init main-loop draw)
(defn -main [& args]
(println (str "Hello LWJGL " (Version/getVersion) "!"))
(init)
(main-loop)
(Callbacks/glfwFreeCallbacks window)
(GLFW/glfwDestroyWindow window)
(GLFW/glfwTerminate)
(-> (GLFW/glfwSetErrorCallback nil) (.free)))
(defn init []
(-> (GLFWErrorCallback/createPrint System/err) (.set))
(when (not (GLFW/glfwInit))
(throw (IllegalStateException. "Failed to initialized GLFW.")))
(GLFW/glfwDefaultWindowHints)
(GLFW/glfwWindowHint GLFW/GLFW_VISIBLE GLFW/GLFW_FALSE)
(GLFW/glfwWindowHint GLFW/GLFW_RESIZABLE GLFW/GLFW_TRUE)
(def window (GLFW/glfwCreateWindow 300 300 "Hello World!" 0 0))
(when (zero? window)
(throw (RuntimeException. "Failed to create 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)))))
(let [stack (MemoryStack/stackPush)
p-width (.mallocInit stack 1)
p-height (.mallocInit stack 1)]
(GLFW/glfwGetWindowSize ^long window p-width p-height)
(let [vidmode (-> (GLFW/glfwGetPrimaryMonitor)
(GLFW/glfwGetVideoMode))
xpos (/ (- (.width vidmode)
(.get p-width 0))
2)
ypos (/ (- (.width vidmode)
(.get p-height 0))
2)]
(GLFW/glfwSetWindowPos window xpos ypos))
(MemoryStack/stackPop))
(GLFW/glfwMakeContextCurrent window)
(GLFW/glfwSwapInterval 1)
(GLFW/glfwShowWindow window))
(defn main-loop []
(GL/createCapabilities)
(GL33/glClearColor 1.0 0.0 0.0 0.0)
(while (not (GLFW/glfwWindowShouldClose window))
(draw)))
(defn draw []
(GL33/glClearColor 1.0 0.0 0.0 0.0)
(GL33/glClear (bit-or GL33/GL_COLOR_BUFFER_BIT
GL33/GL33/GL_DEPTH_BUFFER_BIT))
(GLFW/glfwSwapBuffers windows)
(GLFW/glfwPollEvents))