Ensure that foreign libraries are defined and loaded at image startup
[cl-gtk2.git] / gtk-glext / gtkglext-drawing-area.lisp
1 (in-package :gtkglext)
2
3 (defclass gl-drawing-area (drawing-area)
4   ((on-expose :initarg :on-expose :initform nil :accessor gl-drawing-area-on-expose)
5    (on-init :initarg :on-init :initform nil :accessor gl-drawing-area-on-init)
6    (on-resize :initarg :on-resize :initform nil :accessor gl-drawing-area-on-resize))
7   (:metaclass gobject-class)
8   (:g-type-name . "GtkGLDrawingArea"))
9
10 (defun resize (widget width height)
11   (with-gl-context (widget)
12     (if (gl-drawing-area-on-resize widget)
13         (funcall (gl-drawing-area-on-resize widget) widget width height)
14         (progn
15           (gl:viewport 0 0 width height)
16
17           ;; set projection to account for aspect
18           (gl:matrix-mode :projection)
19           (gl:load-identity)
20           (glu:perspective 90 (/ width height) 0.5 20) ; 90 degrees field of view y, clip 0.5-20 z
21
22           ;; set modelview to identity
23           (gl:matrix-mode :modelview)
24           (gl:load-identity)))))
25
26 (defun gl-drawing-area-configure (widget event)
27   (declare (ignore event))
28   (multiple-value-bind (width height)
29       (gdk:drawable-get-size (widget-window widget))
30     #+nil(format t "configure ~Dx~D~%" width height)
31     (when (widget-realized-p widget)
32       (resize widget width height))))
33
34 (defun gl-drawing-area-realize (widget)
35   #+nil(format t "realize~%")
36   (bwhen (init-fn (gl-drawing-area-on-init widget))
37     (with-gl-context (widget)
38       (funcall init-fn widget)))
39   (multiple-value-bind (width height)
40       (gdk:drawable-get-size (widget-window widget))
41     (resize widget width height))
42   nil)
43
44 (defun gl-drawing-area-exposed (widget event)
45   (bwhen (draw-fn (gl-drawing-area-on-expose widget))
46     (with-gl-context (widget)
47       (funcall draw-fn widget event)))
48   nil)
49
50 (defun gl-drawing-area-parent-set (widget event)
51   (declare (ignore event))
52   (unless (gtk-widget-set-gl-capability widget
53                                         *gl-config*
54                                         nil
55                                         nil
56                                         :rgba-type)
57     (warn "set gl capability for ~A (with ~A) failed~%" widget *gl-config*)))
58
59 (register-object-type-implementation "GtkGLDrawingArea" gl-drawing-area "GtkDrawingArea" nil nil)
60
61 (defmethod initialize-instance :after ((widget gl-drawing-area) &key &allow-other-keys)
62   (connect-signal widget "realize" #'gl-drawing-area-realize)
63   (connect-signal widget "expose-event" #'gl-drawing-area-exposed)
64   (connect-signal widget "configure-event" #'gl-drawing-area-configure)
65   (connect-signal widget "parent-set" #'gl-drawing-area-parent-set))