Skip to content

Instantly share code, notes, and snippets.

@svanellewee
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save svanellewee/049bbceeba0751c664e3 to your computer and use it in GitHub Desktop.

Select an option

Save svanellewee/049bbceeba0751c664e3 to your computer and use it in GitHub Desktop.
setting up user input
(defproject roggy "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]
[org.lwjgl.lwjgl/lwjgl "2.9.1"]
[org.lwjgl.lwjgl/lwjgl-platform "2.9.1"
:classifier "natives-osx"
;; LWJGL stores natives in the root of the jar; this
;; :native-prefix will extract them.
:native-prefix ""]]
:main ^:skip-aot roggy.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
(ns roggy.core
(:import (org.lwjgl LWJGLException)
(org.lwjgl.input Keyboard Mouse)
(org.lwjgl.opengl Display DisplayMode GL11))
(:gen-class))
(defn init []
(try
(Display/setDisplayMode (DisplayMode. 800 600))
(Display/create)
(catch LWJGLException e
(str "caught exception: " (.getMessage e)))))
(defn setup-opengl []
(GL11/glMatrixMode GL11/GL_PROJECTION)
(GL11/glLoadIdentity)
(GL11/glOrtho 0 800 0 600 1 -1)
(GL11/glMatrixMode GL11/GL_MODELVIEW)
)
(defn poll-keyboard-input
([] (poll-keyboard-input []))
([ states ]
(if-not (Keyboard/next)
states
(let [ key (Keyboard/getEventKey)
pressed-released (if (Keyboard/getEventKeyState) "pressed" "released")
key-state (str pressed-released (condp = key
Keyboard/KEY_A "A"
Keyboard/KEY_S "S"
(str "Default" key )))]
(recur (conj states key-state) )))))
(defn main-loop []
(when-not (Display/isCloseRequested)
(let [value (poll-keyboard-input)]
(if (< 0 (count value) )
(println "---->" value)))
(Display/update)
(recur)) )
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, World!")
(init)
(setup-opengl)
(main-loop))
(ns breakout-clj.core
(:import (org.lwjgl LWJGLException)
(org.lwjgl.input Keyboard Mouse)
(org.lwjgl.opengl Display DisplayMode GL11))
(:gen-class))
;; this creates a state-update pipeline
;; input updates the pipeline with :control info
;; update players then takes the :control info and updates the state with new player value
;; no destruction takes place (except for the GC of course)
;;
(defn with-display
[ fn-setup-display fn-main-loop ]
(try
(Display/setDisplayMode (DisplayMode. 800 600))
(Display/create)
(fn-setup-display)
(fn-main-loop)
(catch LWJGLException e#
(str "Caught Exception" (.getMessage e#)) )))
(defn setup-display
[]
(GL11/glMatrixMode GL11/GL_PROJECTION)
(GL11/glLoadIdentity)
(GL11/glOrtho 0 800 0 600 1 -1)
(GL11/glMatrixMode GL11/GL_MODELVIEW))
(defn poll-keyboard-input
([ state ]
(if-not (Keyboard/next)
state
(let [ key (Keyboard/getEventKey)
pressed-released (if (Keyboard/getEventKeyState)
:pressed
:released)
new-state (if (and
(or
(= key Keyboard/KEY_A)
(= key Keyboard/KEY_S))
(= pressed-released :pressed))
(assoc state :control { :pressed-released pressed-released :movement (condp = key
Keyboard/KEY_A :up
Keyboard/KEY_S :down
:ignore)})
state)
]
(recur new-state )))))
(defn update-players
[ state ]
(if-let [control (:control state) ]
(when-let [ movement (:movement control) ]
(let [ paddle (:paddle state)
old-x (-> state :paddle :x)
old-y (-> state :paddle :y)
new-paddle (condp = movement
:up (assoc paddle :x old-x :y (+ old-y 1))
:down (assoc paddle :x old-x :y (- old-y 1))
paddle) ]
{ :paddle new-paddle }))
state))
(defn draw-paddle
[ x y ]
(GL11/glClear (bit-or GL11/GL_COLOR_BUFFER_BIT GL11/GL_DEPTH_BUFFER_BIT))
(GL11/glColor3f 0.5 0.5 1.0)
(GL11/glBegin GL11/GL_QUADS)
(GL11/glVertex2f x y)
(GL11/glVertex2f (+ x 15) y)
(GL11/glVertex2f (+ x 15) (+ y 80) )
(GL11/glVertex2f x (+ y 80))
(GL11/glEnd)
)
(defn apply-callbacks
[ state callbacks ]
(if (empty? callbacks) state
(let [ callback (first callbacks)
the-other-callbacks (rest callbacks)
new-state (callback state) ]
(recur new-state the-other-callbacks))))
(defn make-main-loop
[ & callbacks ]
(letfn [ (main-loop
([] (main-loop {:paddle {:x 10 :y 10} }))
( [ state ]
(when-not (Display/isCloseRequested)
(let [ new-state (apply-callbacks state callbacks) ]
(when-not (empty? new-state)
(println ">-->" new-state))
(Display/update)
(recur new-state)))))
]
main-loop))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, World!")
(let [ main-loop (make-main-loop poll-keyboard-input update-players)]
(println main-loop)
(with-display
setup-display main-loop))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment