Skip to content

Instantly share code, notes, and snippets.

@ztellman
Created October 17, 2009 15:36
Show Gist options
  • Save ztellman/212372 to your computer and use it in GitHub Desktop.
Save ztellman/212372 to your computer and use it in GitHub Desktop.
;; Draw 1000 moving rects on the screen.
;; With penumbra, I get about 50 fps on a dual-core machine with an nVidia 8800.
;; On a single-core machine with an ATI Radeon X300, I get about 2 fps.
(ns examples.squares
(:use [clojure.contrib.def :only (defvar-)]
[clojure.contrib.seq-utils :only (flatten)]
[penumbra opengl window]))
(defstruct rect :offset :size :depth :veloc :color)
(defn move [rect]
(assoc rect :offset (map + (:offset rect) (:veloc rect))))
(defn- init [state]
(enable :depth-test)
(ortho-view 0 0 640 480 -1 1)
(start-update-loop 60 #(map move %))
state)
(defn draw [rect]
(let [[x y] (:offset rect)
z (:depth rect)
[w h] (:size rect)
[r g b] (:color rect)]
(color r g b)
(vertex x y 0)
(vertex (+ x w) y 0)
(vertex (+ x w) (+ y h) 0)
(vertex x (+ y h) 0)))
(defn display [[delta time] state]
(draw-quads
(doseq [c state]
(draw c)))
(write-to-screen (format "%d fps" (int (/ 1 delta))) 0 1))
(defn gen-rectangle []
(struct-map rect
:offset [(rand-int 640) (rand-int 480)]
:size [(rand-int 64) (rand-int 64)]
:veloc [(- (rand) 0.5) (- (rand) 0.5)]
:depth (rand)
:color [(rand) (rand) (rand)]))
(start
{:init init, :display display}
(take 1000 (repeatedly gen-rectangle)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment