forked from 3b/cl-opengl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.lisp
431 lines (375 loc) · 16.3 KB
/
interface.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
;;;
;;; interface.lisp --- CLOS interface to the GLUT API.
;;;
;;; Copyright (c) 2006-2011, Luis Oliveira <loliveira@common-lisp.net>
;;; All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;; o Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;; o Redistributions in binary form must reproduce the above copyright
;;; notice, this list of conditions and the following disclaimer in the
;;; documentation and/or other materials provided with the distribution.
;;; o Neither the name of the author nor the names of the contributors may
;;; be used to endorse or promote products derived from this software
;;; without specific prior written permission.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
;;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(in-package #:cl-glut)
;;; This an experimental interface to GLUT. The main goal of this interface
;;; is to provide an easy, flexible way to explore OpenGL. GLUT is not very
;;; helpful in achieving this goal (even though Freeglut is much better than
;;; the original GLUT in this aspect).
;;;
;;; At the moment, not all of GLUT's capabilities are accessible when
;;; using this high-level interface *exclusively*. Patches and
;;; suggestions are most welcome!
(defvar *id->window* (make-array 0 :adjustable t)
"Mapping of GLUT window IDs to instances of the BASE-WINDOW class.")
(defvar *windows-with-idle-event* '())
;;;; Timers
(defparameter *timer-functions* nil)
(defcallback timer-cb :void ((id :int))
(let ((function (cdr (assoc id *timer-functions*))))
(when function
(funcall function))))
(defvar *timer-id-counter* 0)
(defun schedule-timer (millis function)
(setf *timer-id-counter* (logand (1+ *timer-id-counter*) #xFFFFFFFF))
(push (cons *timer-id-counter* function) *timer-functions*)
(timer-func millis (callback timer-cb) *timer-id-counter*))
;;;; Events
;;; One callback is defined for each GLUT event. Enabling an event for
;;; a given window means registering the respective shared callback
;;; for its window id (using the *-func GLUT functions).
;;;
;;; There is one generic function for each event which is called by
;;; the shared callback to dispatch to the correct method based on the
;;; window (and possibly the other arguments).
;;;
;;; Ugh, some of these event are not implemented by Freeglut, which
;;; is what we care about... Better remove them?
;;;
;;; TODO: The JOYSTICK event has parameters, meaning a way to accept
;;; parameters eg: (:joystick n m) when enabling an event is
;;; necessary. Unlikely to be implemented anytime soon; I
;;; haven't seen a joystick in years.
(defparameter *events* '())
(defstruct event name gf cb func arg-count)
(defun find-event-or-lose (event-name)
(or (find event-name *events* :key #'event-name)
(error "No such event: ~A" event-name)))
(defgeneric enable-event (window event-name))
(defgeneric disable-event (window event-name))
(defmacro when-current-window-exists (&body body)
"Evals BODY when GLUT's current window exists in *ID->WINDOW*.
Lexically binds CURRENT-WINDOW to the respective object."
(let ((id (gensym)))
`(let ((,id (get-window)))
(when (> (length *id->window*) ,id)
(let ((current-window (aref *id->window* ,id)))
(unless (null current-window)
;;(format t "glut callback: ~A -> ~A~%" ,id current-window)
,@body))))))
(define-foreign-type ascii-to-char ()
()
(:actual-type :unsigned-char)
(:simple-parser ascii-to-char))
;;; Without this EVAL-WHEN, the type expansion wouldn't be visible to
;;; the DEFCALLBACKs generated by DEFINE-GLUT-EVENTS.
(eval-when (:compile-toplevel :load-toplevel :execute)
(defmethod expand-from-foreign (value (type ascii-to-char))
`(code-char ,value)))
;;; The first element in ARGS is a dummy name for the first argument
;;; in the event's generic function, the window.
(defmacro define-glut-event (name args &body callback-body)
(let ((arg-names (mapcar #'car (cdr args)))
(event-cb (gl::symbolicate "%" name))
(event-func (gl::symbolicate name '#:-func))
(event-name (intern (symbol-name name) '#:keyword)))
`(progn
(defgeneric ,name (,(car args) ,@arg-names))
(defcallback ,event-cb :void ,(cdr args)
,@callback-body)
;; When we put #'foo-func instead of 'foo-func in the
;; FUNC slot weird stuff happens. No idea why.
(push (make-event :name ,event-name :gf #',name :cb ',event-cb
:func ',event-func :arg-count ,(length arg-names))
*events*))))
(defmacro define-glut-events (&body event-specs)
`(progn
(setq *events* '())
,@(loop for (name args) in event-specs collect
`(define-glut-event ,name ,args
(when-current-window-exists
(,name current-window ,@(mapcar #'car (cdr args))))))))
(define-glut-events
;; (idle (window))
(keyboard (window (key ascii-to-char) (x :int) (y :int)))
(special (window (special-key special-keys) (x :int) (y :int)))
(reshape (window (width :int) (height :int)))
(visibility (window (state visibility-state)))
(display (window))
(mouse (window (button mouse-button) (state mouse-button-state)
(x :int) (y :int)))
(motion (window (x :int) (y :int)))
(passive-motion (window (x :int) (y :int)))
(entry (window (state entry-state)))
(menu-state (window (state menu-state)))
(spaceball-motion (window (x :int) (y :int) (z :int)))
(spaceball-rotate (window (x :int) (y :int) (z :int)))
(spaceball-button (window (button :int) (state :int)))
(button-box (window (button :int) (state :int)))
(dials (window (dial :int) (value :int)))
(tablet-motion (window (x :int) (y :int)))
(tablet-button (window (button :int) (state :int) (x :int) (y :int)))
(menu-status (window (status menu-state) (x :int) (y :int)))
(overlay-display (window))
(window-status (window (state window-status)))
(keyboard-up (window (key ascii-to-char) (x :int) (y :int)))
(special-up (window (special-key special-keys) (x :int) (y :int)))
;; (joystick (window (buttons joystick-buttons) (x :int) (y :int)
;; (z :int)))
(mouse-wheel (window (button mouse-button) (pressed mouse-button-state)
(x :int) (y :int)))
(close (window))
(menu-destroy (window)))
;;; These two functions should not be called directly and are called
;;; by ENABLE-EVENT and DISABLE-EVENT. See below.
(defun register-callback (event)
(funcall (event-func event) (get-callback (event-cb event))))
(defun unregister-callback (event)
(funcall (event-func event) (null-pointer)))
;;;; Windows
;;; The WINDOW (top-level windows) and SUB-WINDOW (those pseudo-windows
;;; that live inside top-level windows and have their own GL context)
;;; classes inherit BASE-WINDOW.
;;;
;;; See DISPLAY-WINDOW's documentation.
(defparameter +default-title+
(concatenate 'string (lisp-implementation-type) " "
(lisp-implementation-version)))
(defvar *run-main-loop-after-display* t
"This special variable controls whether the DISPLAY-WINDOW
method specialized on GLUT:WINDOW will call GLUT:MAIN-LOOP.")
(defclass base-window ()
((name :reader name :initarg :name :initform (gensym "GLUT-WINDOW"))
(id :reader id)
(destroyed :accessor destroyed :initform nil)
(pos-x :accessor pos-x :initarg :pos-x)
(pos-y :accessor pos-y :initarg :pos-y)
(height :accessor height :initarg :height)
(width :accessor width :initarg :width)
(title :accessor title :initarg :title)
(tick-interval :accessor tick-interval :initarg :tick-interval)
;; When this slot unbound, DISPLAY-WINDOW calls
;; FIND-APPLICABLE-EVENTS to populate it.
(events :accessor events :initarg :events))
(:default-initargs :pos-x -1 :pos-y -1 :height 300 :width 300
:title +default-title+ :tick-interval nil))
(defmethod initialize-instance :before
((win base-window) &key name &allow-other-keys)
(declare (ignore win name))
(glut:init))
(defgeneric display-window (window)
(:documentation
"Creates the underlying GLUT window structures and displays
WINDOW. The creation takes place in :AROUND methods so the
user can define :before methods on DISPLAY-WINDOW and do
OpenGL stuff with it, for example."))
(defun find-window (name)
(loop for window across *id->window*
when (and (not (null window)) (eq (name window) name))
do (return window)))
(defmacro with-window (window &body body)
(let ((current-id (gensym)))
`(let ((,current-id (get-window)))
(unwind-protect
(progn
(set-window (id ,window))
,@body)
(set-window ,current-id)))))
(defparameter *global-idle-hooks* nil
"A list of functions to be called without arguments from GLUT's idle event.")
;;; We do some extra stuff to provide an IDLE event per-window since
;;; GLUT's IDLE event is global.
(define-glut-event idle (window)
(mapc #'funcall *global-idle-hooks*)
(loop for win in *windows-with-idle-event*
do (with-window win
(idle win))))
(defun find-applicable-events (window)
(loop for event in *events*
when (compute-applicable-methods
(event-gf event)
(cons window (loop repeat (event-arg-count event) collect t)))
collect event))
(defun enable-tick (window millis)
(setf (tick-interval window) millis)
(timer-func millis (callback tick-timer-cb) (id window)))
(defun disable-tick (window)
(setf (tick-interval window) nil))
(defmethod display-window :around ((win base-window))
(unless (slot-boundp win 'events)
(setf (events win) (find-applicable-events win)))
(with-window win
(glut:position-window (pos-x win) (pos-y win))
(glut:reshape-window (width win) (height win))
(glut:set-window-title (title win))
(dolist (event (events win))
(register-callback event)))
(when (member :idle (events win) :key #'event-name)
(push win *windows-with-idle-event*))
;; save window in the *id->window* array
(when (<= (length *id->window*) (id win))
(setq *id->window*
(adjust-array *id->window* (1+ (id win)) :initial-element nil)))
(setf (aref *id->window* (id win)) win)
;; setup tick timer
(when (tick-interval win)
(enable-tick win (tick-interval win)))
(call-next-method))
(defmethod display-window ((win base-window))
(when *global-idle-hooks*
(register-callback (find-event-or-lose :idle)))
(values))
(defmethod enable-event ((window base-window) event-name)
(let ((event (find-event-or-lose event-name)))
(when (not (find event (events window)))
(with-window window
(register-callback event))
(push event (events window))
(when (eq event-name :idle)
(push window *windows-with-idle-event*)))))
(defmethod disable-event ((window base-window) event-name)
(if (eq event-name :display)
(warn "GLUT would be upset if we set the DISPLAY callback to NULL. ~
So we won't do that.")
(let ((event (find-event-or-lose event-name)))
(when (find event (events window))
;; We don't actually disable the CLOSE event since we need it
;; for bookkeeping. See the CLOSE methods below.
(unless (or (eq event-name :idle)
(eq event-name :close))
(with-window window
(unregister-callback event)))
(setf (events window) (delete event (events window)))
(when (eq event-name :idle)
(setq *windows-with-idle-event*
(delete window *windows-with-idle-event*))
;; We need to disable the idle callback here too in
;; addition to close.
(when (and (null *windows-with-idle-event*)
(null *global-idle-hooks*))
(unregister-callback event)))))))
(defun destroy-current-window ()
(when-current-window-exists
(cond
((game-mode current-window)
(leave-game-mode))
(t
(destroy-window (id current-window))
#+darwin
(progn
(setf (destroyed current-window) t)
(close current-window))))))
(defmethod close :around ((w base-window))
(when (member :close (events w) :key #'event-name)
(call-next-method))
(setf (aref *id->window* (id w)) nil)
(setq *windows-with-idle-event* (delete w *windows-with-idle-event*))
#+darwin
(when (not (destroyed w))
(setf (destroyed w) t)
(destroy-window (id w)))
(when (and (null *windows-with-idle-event*)
(null *global-idle-hooks*))
(unregister-callback (find-event-or-lose :idle)))
#+darwin
(progn
(when (= 0 (length (remove-if #'null *id->window*)))
;; We want to leave the glut event loop if all glut windows are
;; closed, even when :action-continue-execution is set.
(leave-main-loop))
(ecase *window-close-action*
;; :action-exit is probably unnecessary, as it should never be used.
(:action-exit
#+sbcl (sb-ext:quit)
#+ccl (ccl:quit)
#-(or sbcl ccl) (warn "Don't know how to quit."))
(:action-glutmainloop-returns
(leave-main-loop))
(:action-continue-execution
nil))))
(defmethod close ((w base-window))
(values))
(defgeneric tick (window))
(defcallback tick-timer-cb :void ((id :int))
(when (> (length *id->window*) id)
(let ((window (aref *id->window* id)))
(unless (null window)
(tick window)
(when (tick-interval window)
(timer-func (tick-interval window)
(callback tick-timer-cb) id))))))
;;;; Top-level Windows
(defclass window (base-window)
((sub-windows :accessor sub-windows :initform nil)
;; Can sub-windows have a different display mode?
(mode :initarg :mode :initform nil)
(game-mode :initarg :game-mode :accessor game-mode :initform nil)))
(defmethod (setf title) :before (string (win window))
(when (slot-boundp win 'id)
(with-window win
(set-window-title string))))
(defun setup-game-mode (win)
(let ((width (get :screen-width))
(height (get :screen-height)))
(setf (pos-x win) 0
(pos-y win) 0
(width win) width
(height win) height)
(game-mode-string (format nil "~Dx~D" width height))))
(defmethod display-window :around ((win window))
(when (game-mode win)
(setup-game-mode win))
(init-window-position (pos-x win) (pos-y win))
(init-window-size (width win) (height win))
(without-fp-traps
(apply #'init-display-mode (slot-value win 'mode))
(setf (slot-value win 'id)
(if (game-mode win)
(enter-game-mode)
(create-window (title win))))
(call-next-method)
(when *run-main-loop-after-display*
(glut:main-loop))))
;;;; Sub-windows
(defclass sub-window (base-window)
((parent :reader parent
:initform (error "Must specify a PARENT window."))))
(defmethod initialize-instance :after ((win sub-window) &key parent
&allow-other-keys)
(let ((parent-window (typecase parent
(window parent)
(symbol (find-window parent)))))
(check-type parent-window window)
(setf (slot-value win 'parent) parent-window)
(push win (sub-windows parent-window))))
(defmethod display-window :around ((win sub-window))
(setf (slot-value win 'id) (create-sub-window (id (parent win)) 0 0 0 0))
(call-next-method))