Improved use of nicer types in callbacks.
[cl-glfw/jecs.git] / lib / types.lisp
blobf9765e25325868fd20e80f98fa9d26019de7b91e
1 (defpackage #:cl-glfw-types
2 (:use #:cl #:cffi)
3 (:shadow #:boolean #:byte #:float #:char #:string #:pointer)
4 (:export #:enum #:boolean #:bitfield #:byte #:short #:int #:sizei #:ubyte #:ushort #:uint
5 #:float #:clampf #:double #:clampd #:void #:uint64 #:int64
6 #:intptr #:sizeiptr
7 #:handle #:pointer
8 #:char
9 #:half))
11 (in-package #:cl-glfw-types)
13 (defmacro defgltype (type parser actual-type)
14 `(define-foreign-type ,type ()
16 (:actual-type ,actual-type)
17 (:simple-parser ,parser)))
19 (defgltype gl-enum enum :uint32)
20 (defgltype gl-boolean boolean :uint8)
21 (defgltype gl-bitfield bitfield :uint32)
22 (defgltype gl-byte byte :int8)
23 (defgltype gl-short short :int16)
24 (defgltype gl-int int :int32)
25 (defgltype gl-sizei sizei :int32)
26 (defgltype gl-ubyte ubyte :uint8)
27 (defgltype gl-ushort ushort :uint16)
28 (defgltype gl-uint uint :uint32)
29 (defgltype gl-float float :float)
30 (defgltype gl-clampf clampf :float)
31 (defgltype gl-double double :double)
32 (defgltype gl-clampd clampd :double)
33 (defgltype gl-void void :void)
36 #-cffi-features:no-long-long
37 (defgltype gl-uint64 uint64 :uint64)
38 #-cffi-features:no-long-long
39 (defgltype gl-int64 int64 :int64)
41 ;; Find a CFFI integer type the same foreign-size as a pointer
42 (defgltype gl-intprt intptr #.(find-symbol (format nil "INT~d" (* 8 (cffi:foreign-type-size :pointer))) (find-package '#:keyword)))
43 (defgltype gl-sizeiptr sizeiptr #.(find-symbol (format nil "INT~d" (* 8 (cffi:foreign-type-size :pointer))) (find-package '#:keyword)))
45 (defgltype gl-handle handle :unsigned-int)
46 (defgltype gl-char char :char)
47 ;;(defctype string :string)
48 (defgltype gl-half half :unsigned-short) ; this is how glext.h defines it anyway
49 (defctype pointer :pointer)
51 (defmethod cffi:expand-to-foreign (value (type gl-boolean))
52 `(if ,value 1 0))
54 (defmethod cffi:expand-from-foreign (value (type gl-boolean))
55 `(not (= ,value 0)))
57 (defmethod cffi:expand-to-foreign (value (type gl-clampf))
58 `(coerce ,value 'single-float))
60 (defmethod cffi:expand-to-foreign (value (type gl-clampd))
61 `(coerce ,value 'double-float))
63 (defmethod cffi:expand-to-foreign (value (type gl-float))
64 `(coerce ,value 'single-float))
66 (defmethod cffi:expand-to-foreign (value (type gl-double))
67 `(coerce ,value 'double-float))
69 ;; TODO: Maybe we can find/write a converter to a half? Does anyone want it?
70 ;; TODO: Might we want converters to integer types? What would it be? round, or floor (or even ceil)?