I recently tried to test OpenGL in Clojure with the lwjgl library. I started with this code:
(ns test.core (:import [org.lwjgl.opengl Display DisplayMode GL11])) (defn init-window [width height title] (Display/setDisplayMode (DisplayMode. width height)) (Display/setTitle title) (Display/create)) (defn update [] (GL11/glClearColor 0 0 0 0) (GL11/glClear GL11/GL_COLOR_BUFFER_BIT)) (defn run [] (init-window 800 600 "test") (while (not (Display/isCloseRequested)) (update) (Display/update)) (Display/destroy)) (defn -main [& args] (.start (Thread. run)))
This worked fine, as in emacs, with the nREPL plugin, I could run it and, while it worked, changed something (for example, calling glClearColor ).
I decided to split this into two separate files, so I could reuse the init-window function:
(ns copengl.core (:import [org.lwjgl.opengl Display DisplayMode GL11])) (defn init-window [width height title] (Display/setDisplayMode (DisplayMode. width height)) (Display/setTitle title) (Display/create)) (defn mainloop [{:keys [update-fn]}] (while (not (Display/isCloseRequested)) (update-fn) (Display/update)) (Display/destroy)) (defn run [data] (init-window (:width data) (:height data) (:title data)) (mainloop data)) (defn start [data] (.start (Thread. (partial run data))))
and then in a separate file
(ns test.core (:import [org.lwjgl.opengl Display DisplayMode GL11]) (:require [copengl.core :as starter])) (def -main [& args] (starter/start {:width 800 :height 600 :title "Test" :update-fn update})
This, however, does not allow me to change during work. I have to close the window and then run -main again to see the change.
So far, I have tried to put these last two code snippets into a single file, which also did not work. However, if I changed the call from update-fn to the name of my update function ( update ) when they were in the same file, I could change things while working.
I suppose this is because when I create a map using the update function, it passes a valid function, so if I redefine update using the nREPL plugin to evaluate it, there is no effect because the mainloop function uses the function - without looking to the update symbol and using this.
Is there a way to split the code between the two files, while still maintaining the ability to change the code while working?