Skip to content

Commit

Permalink
clojure example
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Nov 16, 2020
1 parent 2650685 commit b05e8f8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.cpcache
docs/apidocs
target
build
Expand Down
1 change: 1 addition & 0 deletions docs/Getting Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ var width = 640;
var height = 480;

// Create window
glfwInit();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
long windowHandle = glfwCreateWindow(width, height, "Skija LWJGL Demo", NULL, NULL);
Expand Down
8 changes: 8 additions & 0 deletions examples/clojure/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{:deps {org.jetbrains.skija/skija-macos {:mvn/version "0.6.6"}
org.lwjgl/lwjgl {:mvn/version "3.2.3"}
org.lwjgl/lwjgl$natives-macos {:mvn/version "3.2.3"}
org.lwjgl/lwjgl-glfw {:mvn/version "3.2.3"}
org.lwjgl/lwjgl-glfw$natives-macos {:mvn/version "3.2.3"}
org.lwjgl/lwjgl-opengl {:mvn/version "3.2.3"}
org.lwjgl/lwjgl-opengl$natives-macos {:mvn/version "3.2.3"}}
:mvn/repos {"space-maven" {:url "https://packages.jetbrains.team/maven/p/skija/maven"}}}
5 changes: 5 additions & 0 deletions examples/clojure/script/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -o errexit -o nounset -o pipefail
cd "`dirname $0`/.."

clj -J-XstartOnFirstThread -X lwjgl.main/-main
47 changes: 47 additions & 0 deletions examples/clojure/src/lwjgl/main.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(ns lwjgl.main
(:import
[org.jetbrains.skija BackendRenderTarget Canvas ColorSpace DirectContext FramebufferFormat Paint Rect Surface SurfaceColorFormat SurfaceOrigin]
[org.lwjgl.glfw GLFW]
[org.lwjgl.opengl GL GL11]
[org.lwjgl.system MemoryUtil]))

(set! *warn-on-reflection* true)

(defn color [^long l]
(.intValue (Long/valueOf l)))

(defn draw [^Canvas canvas]
(.clear canvas (color 0xFFFFFFFF))
(let [layer (.save canvas)
paint (doto (Paint.) (.setColor (color 0xFFCC3333)))]
(.translate canvas 320 240)
(.rotate canvas (mod (/ (System/currentTimeMillis) 10) 360))
(.drawRect canvas (Rect/makeXYWH -50 -50 100 100) paint)
(.restoreToCount canvas layer)))

(defn -main [& args]
(GLFW/glfwInit)
(GLFW/glfwWindowHint GLFW/GLFW_VISIBLE GLFW/GLFW_FALSE)
(GLFW/glfwWindowHint GLFW/GLFW_RESIZABLE GLFW/GLFW_TRUE)
(let [width 640
height 480
window (GLFW/glfwCreateWindow width height "Skija LWJGL Demo" MemoryUtil/NULL MemoryUtil/NULL)]
(GLFW/glfwMakeContextCurrent window)
(GLFW/glfwSwapInterval 1)
(GLFW/glfwShowWindow window)
(GL/createCapabilities)

(let [context (DirectContext/makeGL)
fb-id (GL11/glGetInteger 0x8CA6)
target (BackendRenderTarget/makeGL width height 0 8 fb-id FramebufferFormat/GR_GL_RGBA8)
surface (Surface/makeFromBackendRenderTarget context target SurfaceOrigin/BOTTOM_LEFT SurfaceColorFormat/RGBA_8888 (ColorSpace/getSRGB))
canvas (.getCanvas surface)]
(loop []
(when (not (GLFW/glfwWindowShouldClose window))
(draw canvas)

(.flush context)
(GLFW/glfwSwapBuffers window)
(GLFW/glfwPollEvents)
(recur)))
)))

0 comments on commit b05e8f8

Please sign in to comment.