]> git.jsancho.org Git - guile-irrlicht.git/commitdiff
GOOPS and POC with create-device
authorJavier Sancho <jsf@jsancho.org>
Fri, 1 May 2020 11:41:56 +0000 (13:41 +0200)
committerJavier Sancho <jsf@jsancho.org>
Fri, 1 May 2020 11:41:56 +0000 (13:41 +0200)
Makefile.am
irrlicht.scm
irrlicht/base.scm [new file with mode: 0644]
irrlicht/foreign.scm [new file with mode: 0644]
irrlicht/irr.scm [new file with mode: 0644]
src/device.cpp
src/device.h
src/wrapped.h

index 4b8303bb53f2f73811ba0f650586172bf7376d53..fa12476c1eac43ea6f6e9ddc58c77c029e59bd95 100644 (file)
@@ -101,4 +101,8 @@ SUFFIXES = .scm .go
 moddir = @GUILE_SITE@
 godir = @GUILE_SITE_CCACHE@
 
-SOURCES = irrlicht.scm
+SOURCES = \
+  irrlicht.scm \
+  irrlicht/base.scm \
+  irrlicht/foreign.scm \
+  irrlicht/irr.scm
index 14e4457c909ac6cfc1ec3fc4791e6b1355331deb..307cbdf301640ede816dcb27942e7d6842baee49 100644 (file)
@@ -1,5 +1,5 @@
 ;;; guile-irrlicht --- FFI bindings for Irrlicht Engine
-;;; Copyright (C) 2019 Javier Sancho <jsf@jsancho.org>
+;;; Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
 ;;;
 ;;; This file is part of guile-irrlicht.
 ;;;
@@ -18,6 +18,6 @@
 ;;; <http://www.gnu.org/licenses/>.
 
 
-(define-module (irrlicht))
-
-(load-extension "libguile-irrlicht" "init_guile_irrlicht")
+(define-module (irrlicht)
+  #:use-module (irrlicht irr)
+  #:re-export (create-device))
diff --git a/irrlicht/base.scm b/irrlicht/base.scm
new file mode 100644 (file)
index 0000000..bb04442
--- /dev/null
@@ -0,0 +1,35 @@
+;;; guile-irrlicht --- FFI bindings for Irrlicht Engine
+;;; Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
+;;;
+;;; This file is part of guile-irrlicht.
+;;;
+;;; Guile-irrlicht is free software; you can redistribute it and/or modify
+;;; it under the terms of the GNU Lesser General Public License as
+;;; published by the Free Software Foundation; either version 3 of the
+;;; License, or (at your option) any later version.
+;;;
+;;; Guile-irrlicht is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;;; General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU Lesser General Public
+;;; License along with guile-irrlicht.  If not, see
+;;; <http://www.gnu.org/licenses/>.
+
+
+(define-module (irrlicht base)
+  #:use-module (oop goops)
+  #:use-module (system foreign)
+  #:export (<irrlicht-base>
+            wrapped-obj))
+
+(define-class <irrlicht-base> ()
+  (irr-ptr #:init-value %null-pointer #:accessor irrlicht-pointer #:init-keyword #:ptr)
+  (irr-class #:init-value "irrlicht-base" #:getter irrlicht-class)
+  (wrapped-obj #:accessor wrapped-obj #:allocation #:virtual
+               #:slot-ref (lambda (obj)
+                            (cons (slot-ref obj 'irr-class)
+                                  (slot-ref obj 'irr-ptr)))
+               #:slot-set! (lambda (obj wrapped)
+                             (slot-set! obj 'irr-ptr (cdr wrapped)))))
diff --git a/irrlicht/foreign.scm b/irrlicht/foreign.scm
new file mode 100644 (file)
index 0000000..ff46223
--- /dev/null
@@ -0,0 +1,23 @@
+;;; guile-irrlicht --- FFI bindings for Irrlicht Engine
+;;; Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
+;;;
+;;; This file is part of guile-irrlicht.
+;;;
+;;; Guile-irrlicht is free software; you can redistribute it and/or modify
+;;; it under the terms of the GNU Lesser General Public License as
+;;; published by the Free Software Foundation; either version 3 of the
+;;; License, or (at your option) any later version.
+;;;
+;;; Guile-irrlicht is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;;; General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU Lesser General Public
+;;; License along with guile-irrlicht.  If not, see
+;;; <http://www.gnu.org/licenses/>.
+
+
+(define-module (irrlicht foreign))
+
+(load-extension "libguile-irrlicht" "init_guile_irrlicht")
diff --git a/irrlicht/irr.scm b/irrlicht/irr.scm
new file mode 100644 (file)
index 0000000..8704c15
--- /dev/null
@@ -0,0 +1,65 @@
+;;; guile-irrlicht --- FFI bindings for Irrlicht Engine
+;;; Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
+;;;
+;;; This file is part of guile-irrlicht.
+;;;
+;;; Guile-irrlicht is free software; you can redistribute it and/or modify
+;;; it under the terms of the GNU Lesser General Public License as
+;;; published by the Free Software Foundation; either version 3 of the
+;;; License, or (at your option) any later version.
+;;;
+;;; Guile-irrlicht is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;;; General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU Lesser General Public
+;;; License along with guile-irrlicht.  If not, see
+;;; <http://www.gnu.org/licenses/>.
+
+
+(define-module (irrlicht irr)
+  #:use-module (oop goops)
+  #:use-module (irrlicht base)
+  #:use-module (irrlicht foreign)
+  #:export (<reference-counted>
+            <irrlicht-device>
+            create-device))
+
+
+;; IReferenceCounted
+(define-class <reference-counted> (<irrlicht-base>)
+  (irr-class #:init-value "irr::IReferenceCounted" #:getter irrlicht-class))
+
+
+;; IEventReceiver
+(define-class <event-receiver> (<irrlicht-base>)
+  (irr-class #:init-value "irr::IEventReceiver" #:getter irrlicht-class))
+
+
+;; IrrlichtDevice
+(define-class <irrlicht-device> (<reference-counted>)
+  (irr-class #:init-value "irr::IrrlichtDevice" #:getter irrlicht-class))
+
+(define* (create-device #:key
+                        (device-type 'software)
+                        (window-size '(640 480))
+                        (bits 16)
+                        (fullscreen #f)
+                        (stencilbuffer #f)
+                        (vsync #f)
+                        (receiver (make <event-receiver>)))
+  (if (not (is-a? receiver <event-receiver>))
+      (error
+       "In procedure create-device: Wrong type argument (expecting <event-receiver>):"
+       receiver))
+
+  (make <irrlicht-device>
+    #:ptr
+    (irr_createDevice device-type
+                      window-size
+                      bits
+                      fullscreen
+                      stencilbuffer
+                      vsync
+                      (wrapped-obj receiver))))
index 3b4dc10207f76d98b2c650ab6955deb2a8812240..0ecb69435ee3a9ce77b76248f9807ff0c7f47e2f 100644 (file)
@@ -37,7 +37,7 @@ extern "C" {
   init_device (void)
   {
     init_device_type ();
-    DEFINE_GSUBR ("create-device", 0, 0, 1, irr_createDevice);
+    DEFINE_GSUBR ("irr_createDevice", 7, 0, 0, irr_createDevice);
     DEFINE_GSUBR ("get-timer", 1, 0, 0, irr_getTimer);
     DEFINE_GSUBR ("is-window-active?", 1, 0, 0, irr_isWindowActive);
     DEFINE_GSUBR ("run", 1, 0, 0, irr_run);
@@ -52,26 +52,15 @@ extern "C" {
                        wrap_device, unwrap_device);
 
   SCM
-  irr_createDevice (SCM rest)
+  irr_createDevice (SCM device_type,
+                    SCM window_size,
+                    SCM bits,
+                    SCM fullscreen,
+                    SCM stencilbuffer,
+                    SCM vsync,
+                    SCM receiver)
   {
-    SCM device_type = scm_from_utf8_symbol ("software");
-    SCM window_size = scm_list_2 (scm_from_uint32 (640),
-                                  scm_from_uint32 (480));
-    SCM bits = scm_from_uint32 (16);
-    SCM fullscreen = SCM_BOOL_F;
-    SCM stencilbuffer = SCM_BOOL_F;
-    SCM vsync = SCM_BOOL_F;
-    SCM receiver = SCM_UNDEFINED;
-
-    scm_c_bind_keyword_arguments ("create-device", rest, (scm_t_keyword_arguments_flags)0,
-                                  scm_from_utf8_keyword ("device-type"), &device_type,
-                                  scm_from_utf8_keyword ("window-size"), &window_size,
-                                  scm_from_utf8_keyword ("bits"), &bits,
-                                  scm_from_utf8_keyword ("fullscreen"), &fullscreen,
-                                  scm_from_utf8_keyword ("stencilbuffer"), &stencilbuffer,
-                                  scm_from_utf8_keyword ("vsync"), &vsync,
-                                  scm_from_utf8_keyword ("receiver"), &receiver,
-                                  SCM_UNDEFINED);
+    UNWRAP (receiver);
 
     irr::IrrlichtDevice* device =
       irr::createDevice (scm_to_driver_type (device_type),
@@ -80,8 +69,8 @@ extern "C" {
                          scm_to_bool (fullscreen),
                          scm_to_bool (stencilbuffer),
                          scm_to_bool (vsync),
-                         receiver == SCM_UNDEFINED ? 0 : unwrap_event_receiver (receiver));
-    return wrap_device (device);
+                         UNWRAPPED (receiver));
+    return scm_from_pointer (device, NULL);
   }
 
   SCM
index ccf6482a5196655c89f43705e2a48da3b3a3eb38..158e51da1e325d8d72ab867d647ab24535b02670 100644 (file)
@@ -35,7 +35,13 @@ extern "C" {
                         device_p, wrap_device, unwrap_device);
 
   SCM
-  irr_createDevice (SCM rest);
+  irr_createDevice (SCM device_type,
+                    SCM window_size,
+                    SCM bits,
+                    SCM fullscreen,
+                    SCM stencilbuffer,
+                    SCM vsync,
+                    SCM receiver);
 
   SCM
   irr_getTimer (SCM wrapped_device);
index daaaa25404d07c95e32fd71f7e6d7a927e100353..4514fa64afbb769c2686bc5f46697b584007b4ac 100644 (file)
     return SCM_IS_A_P (wrapped_obj, wrapped_##INIT);                    \
   }
 
+
+#define UNWRAP(OBJ)                                                     \
+  char* OBJ##_class = scm_to_utf8_stringn (scm_car (OBJ), NULL);        \
+  void* OBJ##_pointer = scm_to_pointer (scm_cdr (OBJ));
+
+
+#define UNWRAPPED(OBJ)                                                  \
+  (!strcmp (OBJ##_class, "irr::IEventReceiver") ? (irr::IEventReceiver*)OBJ##_pointer : 0)
+
 #endif