]> git.jsancho.org Git - guile-irrlicht.git/blob - irrlicht/foreign.scm
Some doc
[guile-irrlicht.git] / irrlicht / foreign.scm
1 ;;; guile-irrlicht --- FFI bindings for Irrlicht Engine
2 ;;; Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
3 ;;;
4 ;;; This file is part of guile-irrlicht.
5 ;;;
6 ;;; Guile-irrlicht is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU Lesser General Public License as
8 ;;; published by the Free Software Foundation; either version 3 of the
9 ;;; License, or (at your option) any later version.
10 ;;;
11 ;;; Guile-irrlicht is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;;; General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with guile-irrlicht.  If not, see
18 ;;; <http://www.gnu.org/licenses/>.
19
20
21 (define-module (irrlicht foreign)
22   #:use-module (system foreign)
23   #:use-module (irrlicht base)
24   #:export (get-irrlicht-proc
25             null-object?
26             remember-wrapped
27             mem-wrapped))
28
29 ;; We use a hash table to store foreign irrlicht methods related with their
30 ;; classes
31 ;; This is needed because we need to "simulate" C++ inheritance
32 (define remote-proc-table (make-hash-table))
33
34 (define (get-irrlicht-proc proc-name . objects)
35   (let* ((name (if (null? objects)
36                    proc-name
37                    (let ((classes (map irr-class objects)))
38                      (string-join (cons (car classes) (cons proc-name (cdr classes))) "_"))))
39          (proc (hash-ref remote-proc-table name)))
40     (cond ((not proc)
41            (load-extension "libguile-irrlicht" "init_guile_irrlicht")
42            (let ((new-proc (eval-string name)))
43              (hash-set! remote-proc-table name new-proc)
44              new-proc))
45           (else
46            proc))))
47
48 (define (null-object? object)
49   (null-pointer? (irr-pointer object)))
50
51 ;; Table for storing foreign irrlicht wrapped objects by its pointer address
52 ;; We can recover them later, when we have an address without knowing its type, like in
53 ;; events case
54 (define wrapped-obj-table (make-hash-table))
55
56 (define (remember-wrapped object)
57   (or (hash-ref wrapped-obj-table
58                 (pointer-address (irr-pointer object)))
59       object))
60
61 (define (mem-wrapped object)
62   (hash-set! wrapped-obj-table
63              (pointer-address (irr-pointer object))
64              object)
65   object)