3 (in-package :lambdamundo
)
6 (defmacro with-gensyms
((&rest names
) &body body
)
7 `(let ,(loop for n in names collect
`(,n
(gensym)))
10 (defmacro once-only
((&rest names
) &body body
)
11 (let ((gensyms (loop for n in names collect
(gensym))))
12 `(let (,@(loop for g in gensyms collect
`(,g
(gensym))))
13 `(let (,,@(loop for g in gensyms for n in names collect
``(,,g
,,n
)))
14 ,(let (,@(loop for n in names for g in gensyms collect
`(,n
,g
)))
18 (cffi:defcallback lambdamundo-resize-callback
19 :void
((w :int
) (h :int
))
20 (setf (win-width-of glrepl
:*glwindow
*) w
)
21 (setf (win-height-of glrepl
:*glwindow
*) h
)
22 (let* ((h (/ height width
))
27 (gl:viewport
0 0 width height
)
28 (gl:with-setup-projection
29 (gl:frustum
(- xmax
) xmax
(* (- xmax
) h
) (* xmax h
) znear zfar
))
32 (gl:translate-f
0 0 -
20)))
34 (cffi:defcallback lambdamundo-key-callback
:void
((key :int
) (action :int
))
35 (when-funcall (gethash key
*key-fns
*)))
37 ;; gl init and de-init
40 (gl:enable gl
:+texture-2d
+)
41 (gl:enable gl
:+blend
+)
42 (gl:enable gl
:+depth-test
+)
43 (gl:disable gl
:+cull-face
+)
44 (gl:disable gl
:+lighting
+))
48 ;; drawing --------------------
49 ;; each cell will have to know how to cull itself
51 (eval-when ( :load-toplevel
:compile-toplevel
:execute
)
52 (defparameter *draw-fns
* (make-hash-table :test
'equalp
)
53 "An table of functions called in order to render the scene")
55 (defparameter *draw-array
* (make-array 0 :adjustable t
:fill-pointer
0)
56 "An array that indexes *draw-fns* to establish draw order" )
58 (defun extend-draw-array (name priority
)
59 "If the name is in the array, adjust priority, else add it to the array"
60 (assert (not (null (gethash name
*draw-fns
*))) (name) "~S is not a drawable")
63 (position-if #'(lambda (x) (equal (car x
) name
)) *draw-array
*)))
65 (setf (aref *draw-array
* draw-priority-pos
) (cons name priority
))
66 (vector-push-extend (cons name priority
) *draw-array
*)))))
68 (defmacro make-draw-function
(name priority
&body forms
)
70 (setf (gethash ,name
*draw-fns
*)
71 (compile nil
'(lambda () ,@forms
)))
72 (extend-draw-array ,name
,priority
)
73 (sort *draw-array
* #'(lambda (a b
)
74 (< (car a
) (car b
))))))
75 (defun render-world ()
78 (for entry in-vector
*draw-array
*)
79 (funcall (gethash (car entry
) *draw-fns
*)))))
101 (gl:with-begin gl
:+quads
+
102 (gl:color-3f
0.0 1.0 0.0)
107 (apply #'gl
:vertex-3f
(nth (1- vertex
) vertices
)))))))
110 ;; animation --------------------
112 (defparameter *frames
* 0)
114 (defun update-world (dt)
117 (defparameter *esc-pressed
* nil
)
119 ;; main routine -------------------
121 (let ((t0 (glfw:get-time
))
123 (test-image (make-instance 'glrepl
::rgba-image
:width
64 :height
64)))
125 (for i from
0 below
(* 64 64))
126 (setf (glrepl::pixel test-image i
) #X000000FF
))
127 (glrepl::update-image test-image
)
129 (gl:clear-color
0.0 0.0 0.0 1.0)
131 (while (= (glfw::get-window-param glfw
:+opened
+) glfw
:+true
+))
132 (gl:clear gl
:+color-buffer-bit
+)
133 (setf dt
(- (glfw:get-time
) t0
))
134 (setf t0
(glfw:get-time
))
138 (glrepl:render-console
))
139 ;;(glrepl::render test-image)
141 ;; check for time available if time is avaliable render
142 ;; surrender any cpu time..
145 (glfw:close-window
)))))
147 (defun set-callbacks ()
148 (glfw:set-key-callback
(cffi:callback lambdamundo-key-callback
))
149 (glfw:set-window-size-callback
(cffi:callback lambdamundo-resize-callback
)))
151 (defun lambdamundo ()
154 (setf glrepl
:*glwindow
* (make-instance 'glrepl-window
))
155 (add-line glrepl
:*glwindow
*)
156 (add-line glrepl
:*glwindow
*)
157 (add-string glrepl
:*glwindow
* "(format nil \"Hello World\")")
158 (if (glfw:open-window
(win-width-of *glwindow
*) (win-height-of *glwindow
*) 16 16 16 16 16)
160 (glfw:set-window-title
"Lambdmundo")
163 (format t
"Making font..")
164 (setf (font-of glrepl
:*glwindow
*) (make-font (merge-pathnames #P
"VeraMono.ttf")));; prbly shld be mber of window
166 (glfw:swap-interval
1)
167 (glfw:enable glfw
:+key-repeat
+)
169 ;; (glrepl::dump (aref *font-images* 65))
177 (error "Failed to open window"))))
178 (error "Failed to init glfw"))))
181 ;; (lambdamundo-window ("Lambdamundo"
182 ;; :dimensions (640 480)
183 ;; :colourbits (0 0 0 0)
186 ;; :mode glfw:+window+)
188 ;; (:void ((key :int) (action :int)) )
190 ;; (:void ((width :int) (height :int))
193 ;; ((glfw:enable glfw:+key-repeat+)
194 ;; (glfw:swap-interval 0)
196 ;; (glfw:set-window-size-callback (cffi:callback window-size-callback))
197 ;; (glfw:set-key-callback (cffi:callback key-callback)))
199 ;; ;; to do -- we need to drop in and out of body forms