Last active
December 20, 2015 08:29
-
-
Save catharinejm/6100499 to your computer and use it in GitHub Desktop.
LWJGL Timing example in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Java Version: http://lwjgl.org/wiki/index.php?title=LWJGL_Basics_4_(Timing)#Example | |
(ns lwjgl-tutorial.timer-example | |
(:import [org.lwjgl LWJGLException Sys] | |
[org.lwjgl.input Keyboard] | |
[org.lwjgl.opengl Display DisplayMode GL11])) | |
(defn init-gl! [] | |
(GL11/glMatrixMode GL11/GL_PROJECTION) | |
(GL11/glLoadIdentity) | |
(GL11/glOrtho 0 800 0 600 1 -1) | |
(GL11/glMatrixMode GL11/GL_MODELVIEW)) | |
(defn get-time [] | |
(/ (* (Sys/getTime) 1000) | |
(Sys/getTimerResolution))) | |
(defn calc-delta [{:keys [last-frame] :as state}] | |
(let [time (get-time)] | |
(assoc state :last-frame time :delta (- time last-frame)))) | |
(def key-velocity-map | |
{:left [Keyboard/KEY_LEFT -] | |
:right [Keyboard/KEY_RIGHT] | |
:up [Keyboard/KEY_UP] | |
:down [Keyboard/KEY_DOWN -]}) | |
(defn delta-v [delta dir] | |
(let [[kb-key transform-fn] (key-velocity-map dir)] | |
(if (Keyboard/isKeyDown kb-key) | |
((or transform-fn identity) (* 0.35 delta)) | |
0))) | |
(defn new-coord [coord delta bounds dirs] | |
(let [new-coord (apply + coord (map (partial delta-v delta) dirs))] | |
(second (sort (conj bounds new-coord))))) | |
(defn update-fps [{:keys [fps last-fps] :as state}] | |
(if (> (- (get-time) last-fps) | |
1000) | |
(do | |
(Display/setTitle (str "FPS: " fps)) | |
(assoc state :fps 1 :last-fps (+ last-fps 1000))) | |
(update-in state [:fps] inc))) | |
(defn update [{:keys [x y rotation delta] :as state}] | |
(let [rotation (+ rotation (* 0.15 delta)) | |
x (new-coord x delta [0 800] [:left :right]) | |
y (new-coord y delta [0 600] [:up :down]) | |
new-fps (update-fps state)] | |
(merge state | |
new-fps | |
{:x x :y y :rotation rotation}))) | |
(defn render-gl! [{:keys [x y rotation] :as state}] | |
(GL11/glClear (bit-or GL11/GL_COLOR_BUFFER_BIT GL11/GL_DEPTH_BUFFER_BIT)) | |
(GL11/glColor3f 0.5 0.5 1.0) | |
(GL11/glPushMatrix) | |
(GL11/glTranslatef x y 0) | |
(GL11/glRotatef rotation 0 0 1) | |
(GL11/glTranslatef (- x) (- y) 0) | |
(GL11/glBegin GL11/GL_QUADS) | |
(GL11/glVertex2f (- x 50) (- y 50)) | |
(GL11/glVertex2f (+ x 50) (- y 50)) | |
(GL11/glVertex2f (+ x 50) (+ y 50)) | |
(GL11/glVertex2f (- x 50) (+ y 50)) | |
(GL11/glEnd) | |
(GL11/glPopMatrix)) | |
(defn start! [] | |
(Display/setDisplayMode (DisplayMode. 800 600)) | |
(Display/create) | |
(init-gl!) | |
(loop [quad-state {:x 300.0 | |
:y 400.0 | |
:rotation 0.0 | |
:last-frame (get-time) | |
:fps 0 | |
:last-fps (get-time)}] | |
(when-not (Display/isCloseRequested) | |
(let [updated-state (-> quad-state | |
calc-delta | |
update)] | |
(render-gl! updated-state) | |
(Display/update) | |
(Display/sync 60) | |
(recur updated-state)))) | |
(Display/destroy)) | |
(defn -main [& args] | |
(start!)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment