From 2dfe078cf4082caf641c96449907f36a9029b486 Mon Sep 17 00:00:00 2001
From: Javier Sancho <jsf@jsancho.org>
Date: Mon, 9 Mar 2020 17:55:04 +0100
Subject: [PATCH] refactor

---
 Makefile.am                    |   6 +-
 irrlicht.scm                   |   1 +
 irrlicht/device.scm            |   1 -
 irrlicht/irr.scm               |  24 +++++++
 irrlicht/scene.scm             |   2 +-
 src/EDriverTypes.cpp           |  12 ++--
 src/EMaterialFlags.cpp         | 114 +++++++++++++++++++++++++++++++++
 src/EMaterialFlags.h           |  35 ++++++++++
 src/GuileIrrlicht.cpp          |   2 +
 src/IAnimatedMesh.cpp          |   4 +-
 src/IAnimatedMesh.h            |   4 +-
 src/IAnimatedMeshSceneNode.cpp |   4 +-
 src/IAnimatedMeshSceneNode.h   |   3 +-
 src/IGUIElement.cpp            |   4 +-
 src/IGUIElement.h              |   4 +-
 src/IGUIEnvironment.cpp        |   5 +-
 src/IGUIEnvironment.h          |   4 +-
 src/IGUIStaticText.cpp         |   4 +-
 src/IGUIStaticText.h           |   4 +-
 src/IReferenceCounted.cpp      |  51 +++++++++++++++
 src/IReferenceCounted.h        |  38 +++++++++++
 src/ISceneManager.cpp          |   4 +-
 src/ISceneManager.h            |   4 +-
 src/ISceneNode.cpp             |   4 +-
 src/ISceneNode.h               |   4 +-
 src/IVideoDriver.cpp           |   4 +-
 src/IVideoDriver.h             |   4 +-
 src/IrrlichtDevice.cpp         |  14 ++--
 src/IrrlichtDevice.h           |   7 +-
 src/{util.cpp => wchar.cpp}    |   2 +-
 src/wchar.h                    |  26 ++++++++
 src/{util.h => wrapped.h}      |  19 ++++--
 32 files changed, 354 insertions(+), 64 deletions(-)
 create mode 100644 irrlicht/irr.scm
 create mode 100644 src/EMaterialFlags.cpp
 create mode 100644 src/EMaterialFlags.h
 create mode 100644 src/IReferenceCounted.cpp
 create mode 100644 src/IReferenceCounted.h
 rename src/{util.cpp => wchar.cpp} (98%)
 create mode 100644 src/wchar.h
 rename src/{util.h => wrapped.h} (80%)

diff --git a/Makefile.am b/Makefile.am
index 36003c1..8f92257 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,6 +3,7 @@ lib_LTLIBRARIES = libguile-irrlicht.la
 libguile_irrlicht_la_SOURCES = \
   src/dimension2d.cpp \
   src/EDriverTypes.cpp \
+  src/EMaterialFlags.cpp \
   src/GuileIrrlicht.cpp \
   src/IAnimatedMesh.cpp \
   src/IAnimatedMeshSceneNode.cpp \
@@ -12,10 +13,11 @@ libguile_irrlicht_la_SOURCES = \
   src/IrrlichtDevice.cpp \
   src/ISceneManager.cpp \
   src/ISceneNode.cpp \
+  src/IReferenceCounted.cpp \
   src/IVideoDriver.cpp \
   src/rect.cpp \
-  src/util.cpp \
-  src/vector3d.cpp
+  src/vector3d.cpp \
+  src/wchar.cpp
 libguile_irrlicht_la_CPPFLAGS = @GUILE_CFLAGS@
 libguile_irrlicht_la_LDFLAGS = \
   -version-info 0:1 \
diff --git a/irrlicht.scm b/irrlicht.scm
index 327c45e..1b635f2 100644
--- a/irrlicht.scm
+++ b/irrlicht.scm
@@ -25,6 +25,7 @@
   (let ((public-modules
          '((irrlicht device)
            (irrlicht gui)
+           (irrlicht irr)
            (irrlicht scene)))
         (current-interface
          (module-public-interface (current-module))))
diff --git a/irrlicht/device.scm b/irrlicht/device.scm
index 2a61890..e07a812 100644
--- a/irrlicht/device.scm
+++ b/irrlicht/device.scm
@@ -20,7 +20,6 @@
 
 (define-module (irrlicht device)
   #:export (create-device
-            device-drop!
             get-gui-environment
             get-scene-manager
             get-video-driver
diff --git a/irrlicht/irr.scm b/irrlicht/irr.scm
new file mode 100644
index 0000000..9b89142
--- /dev/null
+++ b/irrlicht/irr.scm
@@ -0,0 +1,24 @@
+;;; guile-irrlicht --- FFI bindings for Irrlicht Engine
+;;; Copyright (C) 2019 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)
+  #:export (drop!))
+
+(load-extension "libguile-irrlicht" "init_guile_irrlicht")
diff --git a/irrlicht/scene.scm b/irrlicht/scene.scm
index b815700..17437fe 100644
--- a/irrlicht/scene.scm
+++ b/irrlicht/scene.scm
@@ -18,7 +18,7 @@
 ;;; <http://www.gnu.org/licenses/>.
 
 
-(define-module (irrlicht gui)
+(define-module (irrlicht scene)
   #:export (add-animated-mesh-scene-node
             get-mesh))
 
diff --git a/src/EDriverTypes.cpp b/src/EDriverTypes.cpp
index ec41fe9..8b3f678 100644
--- a/src/EDriverTypes.cpp
+++ b/src/EDriverTypes.cpp
@@ -29,27 +29,27 @@ extern "C" {
   scm_to_driver_type (SCM driver_type)
   {
     char* driverType = scm_to_utf8_stringn (scm_symbol_to_string (driver_type), NULL);
-    if (!strcmp(driverType, "null"))
+    if (!strcmp (driverType, "null"))
       {
         return irr::video::EDT_NULL;
       }
-    else if (!strcmp(driverType, "software"))
+    else if (!strcmp (driverType, "software"))
       {
         return irr::video::EDT_SOFTWARE;
       }
-    else if (!strcmp(driverType, "burnings"))
+    else if (!strcmp (driverType, "burnings"))
       {
         return irr::video::EDT_BURNINGSVIDEO;
       }
-    else if (!strcmp(driverType, "direct3d8"))
+    else if (!strcmp (driverType, "direct3d8"))
       {
         return irr::video::EDT_DIRECT3D8;
       }
-    else if (!strcmp(driverType, "direct3d9"))
+    else if (!strcmp (driverType, "direct3d9"))
       {
         return irr::video::EDT_DIRECT3D9;
       }
-    else if (!strcmp(driverType, "opengl"))
+    else if (!strcmp (driverType, "opengl"))
       {
         return irr::video::EDT_OPENGL;
       }
diff --git a/src/EMaterialFlags.cpp b/src/EMaterialFlags.cpp
new file mode 100644
index 0000000..7c38fe8
--- /dev/null
+++ b/src/EMaterialFlags.cpp
@@ -0,0 +1,114 @@
+/* guile-irrlicht --- GNU Guile 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/>.
+*/
+
+#include <irrlicht/irrlicht.h>
+#include <libguile.h>
+#include "EMaterialFlags.h"
+
+extern "C" {
+
+  irr::video::E_MATERIAL_FLAG
+  scm_to_material_flag (SCM material_flag)
+  {
+    char* flag = scm_to_utf8_stringn (scm_symbol_to_string (material_flag), NULL);
+    if (!strcmp (flag, "wireframe"))
+      {
+        return irr::video::EMF_WIREFRAME;
+      }
+    else if (!strcmp (flag, "pointcloud"))
+      {
+        return irr::video::EMF_POINTCLOUD;
+      }
+    else if (!strcmp (flag, "gouraud-shading"))
+      {
+        return irr::video::EMF_GOURAUD_SHADING;
+      }
+    else if (!strcmp (flag, "lighting"))
+      {
+        return irr::video::EMF_LIGHTING;
+      }
+    else if (!strcmp (flag, "zbuffer"))
+      {
+        return irr::video::EMF_ZBUFFER;
+      }
+    else if (!strcmp (flag, "zwrite-enable"))
+      {
+        return irr::video::EMF_ZWRITE_ENABLE;
+      }
+    else if (!strcmp (flag, "back-face-culling"))
+      {
+        return irr::video::EMF_BACK_FACE_CULLING;
+      }
+    else if (!strcmp (flag, "front-face-culling"))
+      {
+        return irr::video::EMF_FRONT_FACE_CULLING;
+      }
+    else if (!strcmp (flag, "bilinear-filter"))
+      {
+        return irr::video::EMF_BILINEAR_FILTER;
+      }
+    else if (!strcmp (flag, "trilinear-filter"))
+      {
+        return irr::video::EMF_TRILINEAR_FILTER;
+      }
+    else if (!strcmp (flag, "anisotropic-filter"))
+      {
+        return irr::video::EMF_ANISOTROPIC_FILTER;
+      }
+    else if (!strcmp (flag, "fog-enable"))
+      {
+        return irr::video::EMF_FOG_ENABLE;
+      }
+    else if (!strcmp (flag, "normalize-normals"))
+      {
+        return irr::video::EMF_NORMALIZE_NORMALS;
+      }
+    else if (!strcmp (flag, "texture-wrap"))
+      {
+        return irr::video::EMF_TEXTURE_WRAP;
+      }
+    else if (!strcmp (flag, "anti-aliasing"))
+      {
+        return irr::video::EMF_ANTI_ALIASING;
+      }
+    else if (!strcmp (flag, "color-mask"))
+      {
+        return irr::video::EMF_COLOR_MASK;
+      }
+    else if (!strcmp (flag, "color-material"))
+      {
+        return irr::video::EMF_COLOR_MATERIAL;
+      }
+    else if (!strcmp (flag, "use-mip-maps"))
+      {
+        return irr::video::EMF_USE_MIP_MAPS;
+      }
+    else if (!strcmp (flag, "blend-operation"))
+      {
+        return irr::video::EMF_BLEND_OPERATION;
+      }
+    else if (!strcmp (flag, "polygon-offset"))
+      {
+        return irr::video::EMF_POLYGON_OFFSET;
+      }
+  }
+
+}
diff --git a/src/EMaterialFlags.h b/src/EMaterialFlags.h
new file mode 100644
index 0000000..be94343
--- /dev/null
+++ b/src/EMaterialFlags.h
@@ -0,0 +1,35 @@
+/* guile-irrlicht --- GNU Guile 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/>.
+*/
+
+#ifndef __GUILE_IRRLICHT_E_MATERIAL_FLAGS_INCLUDED__
+#define __GUILE_IRRLICHT_E_MATERIAL_FLAGS_INCLUDED__
+
+#include <irrlicht/irrlicht.h>
+#include <libguile.h>
+
+extern "C" {
+
+  irr::video::E_MATERIAL_FLAG
+  scm_to_material_flag (SCM material_flag);
+
+}
+
+#endif
diff --git a/src/GuileIrrlicht.cpp b/src/GuileIrrlicht.cpp
index 2c20385..8b6275f 100644
--- a/src/GuileIrrlicht.cpp
+++ b/src/GuileIrrlicht.cpp
@@ -28,6 +28,7 @@
 #include "IrrlichtDevice.h"
 #include "ISceneManager.h"
 #include "ISceneNode.h"
+#include "IReferenceCounted.h"
 #include "IVideoDriver.h"
 
 extern "C" {
@@ -41,6 +42,7 @@ extern "C" {
     init_gui_element ();
     init_gui_environment ();
     init_gui_static_text ();
+    init_reference_counted ();
     init_scene_manager ();
     init_scene_node ();
     init_video_driver ();
diff --git a/src/IAnimatedMesh.cpp b/src/IAnimatedMesh.cpp
index 52394ef..883527e 100644
--- a/src/IAnimatedMesh.cpp
+++ b/src/IAnimatedMesh.cpp
@@ -22,7 +22,7 @@
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
 #include "IAnimatedMesh.h"
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -33,7 +33,7 @@ extern "C" {
   }
 
   DEFINE_WRAPPED_TYPE (irr::scene::IAnimatedMesh*, "animated-mesh",
-                       init_animated_mesh_type,
+                       init_animated_mesh_type, animated_mesh_p,
                        wrap_animated_mesh, unwrap_animated_mesh);
 
 }
diff --git a/src/IAnimatedMesh.h b/src/IAnimatedMesh.h
index 3926002..b9f77c3 100644
--- a/src/IAnimatedMesh.h
+++ b/src/IAnimatedMesh.h
@@ -24,7 +24,7 @@
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -32,7 +32,7 @@ extern "C" {
   init_animated_mesh (void);
 
   DECLARE_WRAPPED_TYPE (irr::scene::IAnimatedMesh*, init_animated_mesh_type,
-                        wrap_animated_mesh, unwrap_animated_mesh);
+                        animated_mesh_p, wrap_animated_mesh, unwrap_animated_mesh);
 }
 
 #endif
diff --git a/src/IAnimatedMeshSceneNode.cpp b/src/IAnimatedMeshSceneNode.cpp
index d10b9b4..ae6e29c 100644
--- a/src/IAnimatedMeshSceneNode.cpp
+++ b/src/IAnimatedMeshSceneNode.cpp
@@ -22,7 +22,7 @@
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
 #include "IAnimatedMeshSceneNode.h"
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -33,7 +33,7 @@ extern "C" {
   }
 
   DEFINE_WRAPPED_TYPE (irr::scene::IAnimatedMeshSceneNode*, "animated-mesh-scene-node",
-                       init_animated_mesh_scene_node_type,
+                       init_animated_mesh_scene_node_type, animated_mesh_scene_node_p,
                        wrap_animated_mesh_scene_node, unwrap_animated_mesh_scene_node);
 
 }
diff --git a/src/IAnimatedMeshSceneNode.h b/src/IAnimatedMeshSceneNode.h
index 14752e9..b4dfd9b 100644
--- a/src/IAnimatedMeshSceneNode.h
+++ b/src/IAnimatedMeshSceneNode.h
@@ -24,7 +24,7 @@
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -32,6 +32,7 @@ extern "C" {
   init_animated_mesh_scene_node (void);
 
   DECLARE_WRAPPED_TYPE (irr::scene::IAnimatedMeshSceneNode*, init_animated_mesh_scene_node_type,
+                        animated_mesh_scene_node_p,
                         wrap_animated_mesh_scene_node, unwrap_animated_mesh_scene_node);
 }
 
diff --git a/src/IGUIElement.cpp b/src/IGUIElement.cpp
index 80d7676..810fdb6 100644
--- a/src/IGUIElement.cpp
+++ b/src/IGUIElement.cpp
@@ -22,7 +22,7 @@
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
 #include "IGUIElement.h"
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -33,7 +33,7 @@ extern "C" {
   }
 
   DEFINE_WRAPPED_TYPE (irr::gui::IGUIElement*, "gui-element",
-                       init_gui_element_type,
+                       init_gui_element_type, gui_element_p,
                        wrap_gui_element, unwrap_gui_element);
 
 }
diff --git a/src/IGUIElement.h b/src/IGUIElement.h
index 659dcbc..ab8db92 100644
--- a/src/IGUIElement.h
+++ b/src/IGUIElement.h
@@ -24,7 +24,7 @@
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -32,7 +32,7 @@ extern "C" {
   init_gui_element (void);
 
   DECLARE_WRAPPED_TYPE (irr::gui::IGUIElement*, init_gui_element_type,
-                        wrap_gui_element, unwrap_gui_element);
+                        gui_element_p, wrap_gui_element, unwrap_gui_element);
 }
 
 #endif
diff --git a/src/IGUIEnvironment.cpp b/src/IGUIEnvironment.cpp
index de4f9f6..bea7f1e 100644
--- a/src/IGUIEnvironment.cpp
+++ b/src/IGUIEnvironment.cpp
@@ -25,7 +25,8 @@
 #include "IGUIEnvironment.h"
 #include "IGUIStaticText.h"
 #include "rect.h"
-#include "util.h"
+#include "wchar.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -37,7 +38,7 @@ extern "C" {
   }
 
   DEFINE_WRAPPED_TYPE (irr::gui::IGUIEnvironment*, "gui-environment",
-                       init_gui_environment_type,
+                       init_gui_environment_type, gui_environment_p,
                        wrap_gui_environment, unwrap_gui_environment);
 
   SCM
diff --git a/src/IGUIEnvironment.h b/src/IGUIEnvironment.h
index 6e28bfe..e4d6731 100644
--- a/src/IGUIEnvironment.h
+++ b/src/IGUIEnvironment.h
@@ -24,7 +24,7 @@
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -32,7 +32,7 @@ extern "C" {
   init_gui_environment (void);
 
   DECLARE_WRAPPED_TYPE (irr::gui::IGUIEnvironment*, init_gui_environment_type,
-                        wrap_gui_environment, unwrap_gui_environment);
+                        gui_environment_p, wrap_gui_environment, unwrap_gui_environment);
 
   SCM
   irr_gui_addStaticText (SCM guienv,
diff --git a/src/IGUIStaticText.cpp b/src/IGUIStaticText.cpp
index 4a66db5..2ae1b97 100644
--- a/src/IGUIStaticText.cpp
+++ b/src/IGUIStaticText.cpp
@@ -22,7 +22,7 @@
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
 #include "IGUIStaticText.h"
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -33,7 +33,7 @@ extern "C" {
   }
 
   DEFINE_WRAPPED_TYPE (irr::gui::IGUIStaticText*, "gui-static-text",
-                       init_gui_static_text_type,
+                       init_gui_static_text_type, gui_static_text_p,
                        wrap_gui_static_text, unwrap_gui_static_text);
 
 }
diff --git a/src/IGUIStaticText.h b/src/IGUIStaticText.h
index 39e2fbd..8fe6492 100644
--- a/src/IGUIStaticText.h
+++ b/src/IGUIStaticText.h
@@ -24,7 +24,7 @@
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -32,7 +32,7 @@ extern "C" {
   init_gui_static_text (void);
 
   DECLARE_WRAPPED_TYPE (irr::gui::IGUIStaticText*, init_gui_static_text_type,
-                        wrap_gui_static_text, unwrap_gui_static_text);
+                        gui_static_text_p, wrap_gui_static_text, unwrap_gui_static_text);
 }
 
 #endif
diff --git a/src/IReferenceCounted.cpp b/src/IReferenceCounted.cpp
new file mode 100644
index 0000000..b787821
--- /dev/null
+++ b/src/IReferenceCounted.cpp
@@ -0,0 +1,51 @@
+/* guile-irrlicht --- GNU Guile 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/>.
+*/
+
+#include <irrlicht/irrlicht.h>
+#include <libguile.h>
+
+#include "IReferenceCounted.h"
+#include "IrrlichtDevice.h"
+
+extern "C" {
+
+  void
+  init_reference_counted (void)
+  {
+    scm_c_define_gsubr ("drop!", 1, 0, 0, (scm_t_subr)irr_drop);
+  }
+
+  SCM
+  irr_drop (SCM wrapped_obj)
+  {
+    if (device_p (wrapped_obj))
+      {
+        irr::IrrlichtDevice* obj = unwrap_device (wrapped_obj);
+        return scm_from_bool (obj->drop ());
+      }
+    else
+      {
+        irr::IReferenceCounted* obj = (irr::IReferenceCounted*)scm_foreign_object_ref (wrapped_obj, 0);
+        return scm_from_bool (obj->drop ());
+      }
+  }
+
+}
diff --git a/src/IReferenceCounted.h b/src/IReferenceCounted.h
new file mode 100644
index 0000000..6d965d0
--- /dev/null
+++ b/src/IReferenceCounted.h
@@ -0,0 +1,38 @@
+/* guile-irrlicht --- GNU Guile 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/>.
+*/
+
+#ifndef __GUILE_I_REFERENCE_COUNTED_INCLUDED__
+#define __GUILE_I_REFERENCE_COUNTED_INCLUDED__
+
+#include <irrlicht/irrlicht.h>
+#include <libguile.h>
+
+extern "C" {
+
+  void
+  init_reference_counted (void);
+
+  SCM
+  irr_drop (SCM wrapped_obj);
+
+}
+
+#endif
diff --git a/src/ISceneManager.cpp b/src/ISceneManager.cpp
index e6cebf4..5e8d2d5 100644
--- a/src/ISceneManager.cpp
+++ b/src/ISceneManager.cpp
@@ -25,8 +25,8 @@
 #include "IAnimatedMeshSceneNode.h"
 #include "ISceneManager.h"
 #include "ISceneNode.h"
-#include "util.h"
 #include "vector3d.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -40,7 +40,7 @@ extern "C" {
   }
 
   DEFINE_WRAPPED_TYPE (irr::scene::ISceneManager*, "scene-manager",
-                       init_scene_manager_type,
+                       init_scene_manager_type, scene_manager_p,
                        wrap_scene_manager, unwrap_scene_manager);
 
   SCM
diff --git a/src/ISceneManager.h b/src/ISceneManager.h
index 02b27de..d5b4b2f 100644
--- a/src/ISceneManager.h
+++ b/src/ISceneManager.h
@@ -24,7 +24,7 @@
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -32,7 +32,7 @@ extern "C" {
   init_scene_manager (void);
 
   DECLARE_WRAPPED_TYPE (irr::scene::ISceneManager*, init_scene_manager_type,
-                        wrap_scene_manager, unwrap_scene_manager);
+                        scene_manager_p, wrap_scene_manager, unwrap_scene_manager);
 
   SCM
   irr_scene_addAnimatedMeshSceneNode (SCM wrapped_scene_manager,
diff --git a/src/ISceneNode.cpp b/src/ISceneNode.cpp
index fd9b60c..29b094c 100644
--- a/src/ISceneNode.cpp
+++ b/src/ISceneNode.cpp
@@ -22,7 +22,7 @@
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
 #include "ISceneNode.h"
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -33,7 +33,7 @@ extern "C" {
   }
 
   DEFINE_WRAPPED_TYPE (irr::scene::ISceneNode*, "scene-node",
-                       init_scene_node_type,
+                       init_scene_node_type, scene_node_p,
                        wrap_scene_node, unwrap_scene_node);
 
 }
diff --git a/src/ISceneNode.h b/src/ISceneNode.h
index 79aa0a0..b663091 100644
--- a/src/ISceneNode.h
+++ b/src/ISceneNode.h
@@ -24,7 +24,7 @@
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -32,7 +32,7 @@ extern "C" {
   init_scene_node (void);
 
   DECLARE_WRAPPED_TYPE (irr::scene::ISceneNode*, init_scene_node_type,
-                        wrap_scene_node, unwrap_scene_node);
+                        scene_node_p, wrap_scene_node, unwrap_scene_node);
 }
 
 #endif
diff --git a/src/IVideoDriver.cpp b/src/IVideoDriver.cpp
index a784437..839db75 100644
--- a/src/IVideoDriver.cpp
+++ b/src/IVideoDriver.cpp
@@ -22,7 +22,7 @@
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
 #include "IVideoDriver.h"
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -33,7 +33,7 @@ extern "C" {
   }
 
   DEFINE_WRAPPED_TYPE (irr::video::IVideoDriver*, "video-driver",
-                       init_video_driver_type,
+                       init_video_driver_type, video_driver_p,
                        wrap_video_driver, unwrap_video_driver);
 
 }
diff --git a/src/IVideoDriver.h b/src/IVideoDriver.h
index 4e9f032..df5c3de 100644
--- a/src/IVideoDriver.h
+++ b/src/IVideoDriver.h
@@ -24,7 +24,7 @@
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -32,7 +32,7 @@ extern "C" {
   init_video_driver (void);
 
   DECLARE_WRAPPED_TYPE (irr::video::IVideoDriver*, init_video_driver_type,
-                        wrap_video_driver, unwrap_video_driver);
+                        video_driver_p, wrap_video_driver, unwrap_video_driver);
 
 }
 
diff --git a/src/IrrlichtDevice.cpp b/src/IrrlichtDevice.cpp
index a220cac..9cccd58 100644
--- a/src/IrrlichtDevice.cpp
+++ b/src/IrrlichtDevice.cpp
@@ -28,7 +28,8 @@
 #include "IrrlichtDevice.h"
 #include "ISceneManager.h"
 #include "IVideoDriver.h"
-#include "util.h"
+#include "wchar.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -37,7 +38,6 @@ extern "C" {
   {
     init_device_type ();
     scm_c_define_gsubr ("create-device", 7, 0, 0, (scm_t_subr)irr_createDevice);
-    scm_c_define_gsubr ("device-drop!", 1, 0, 0, (scm_t_subr)irr_deviceDrop);
     scm_c_define_gsubr ("get-gui-environment", 1, 0, 0, (scm_t_subr)irr_getGUIEnvironment);
     scm_c_define_gsubr ("get-scene-manager", 1, 0, 0, (scm_t_subr)irr_getSceneManager);
     scm_c_define_gsubr ("get-video-driver", 1, 0, 0, (scm_t_subr)irr_getVideoDriver);
@@ -45,7 +45,8 @@ extern "C" {
   }
 
   DEFINE_WRAPPED_TYPE (irr::IrrlichtDevice*, "device",
-                       init_device_type, wrap_device, unwrap_device);
+                       init_device_type, device_p,
+                       wrap_device, unwrap_device);
 
   SCM
   irr_createDevice (SCM deviceType,
@@ -66,13 +67,6 @@ extern "C" {
     return wrap_device (device);
   }
 
-  SCM
-  irr_deviceDrop (SCM wrapped_device)
-  {
-    irr::IrrlichtDevice* device = unwrap_device (wrapped_device);
-    return scm_from_bool (device->drop ());
-  }
-
   SCM
   irr_getGUIEnvironment (SCM wrapped_device)
   {
diff --git a/src/IrrlichtDevice.h b/src/IrrlichtDevice.h
index 4b5b604..898341b 100644
--- a/src/IrrlichtDevice.h
+++ b/src/IrrlichtDevice.h
@@ -24,7 +24,7 @@
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
-#include "util.h"
+#include "wrapped.h"
 
 extern "C" {
 
@@ -32,7 +32,7 @@ extern "C" {
   init_device (void);
 
   DECLARE_WRAPPED_TYPE (irr::IrrlichtDevice*, init_device_type,
-                        wrap_device, unwrap_device);
+                        device_p, wrap_device, unwrap_device);
 
   SCM
   irr_createDevice (SCM deviceType,
@@ -43,9 +43,6 @@ extern "C" {
                     SCM vsync,
                     SCM receiver);
 
-  SCM
-  irr_deviceDrop (SCM wrapped_device);
-
   SCM
   irr_getGUIEnvironment (SCM wrapped_device);
 
diff --git a/src/util.cpp b/src/wchar.cpp
similarity index 98%
rename from src/util.cpp
rename to src/wchar.cpp
index eb5b8a8..872c87a 100644
--- a/src/util.cpp
+++ b/src/wchar.cpp
@@ -21,7 +21,7 @@
 
 #include <libguile.h>
 #include <wchar.h>
-#include "util.h"
+#include "wchar.h"
 
 wchar_t*
 scm_to_wide_char_string (SCM text)
diff --git a/src/wchar.h b/src/wchar.h
new file mode 100644
index 0000000..d9697d2
--- /dev/null
+++ b/src/wchar.h
@@ -0,0 +1,26 @@
+/* guile-irrlicht --- GNU Guile 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/>.
+*/
+
+#include <libguile.h>
+#include <wchar.h>
+
+wchar_t*
+scm_to_wide_char_string (SCM text);
diff --git a/src/util.h b/src/wrapped.h
similarity index 80%
rename from src/util.h
rename to src/wrapped.h
index 3ce84a4..2412d7e 100644
--- a/src/util.h
+++ b/src/wrapped.h
@@ -20,9 +20,8 @@
 */
 
 #include <libguile.h>
-#include <wchar.h>
 
-#define DECLARE_WRAPPED_TYPE(TYPE, INIT, WRAP, UNWRAP)                  \
+#define DECLARE_WRAPPED_TYPE(TYPE, INIT, PRED, WRAP, UNWRAP)            \
   void                                                                  \
   INIT (void);                                                          \
                                                                         \
@@ -30,10 +29,13 @@
   WRAP (TYPE foreign_obj);                                              \
                                                                         \
   TYPE                                                                  \
-  UNWRAP (SCM wrapped_obj);
+  UNWRAP (SCM wrapped_obj);                                             \
+                                                                        \
+  bool                                                                  \
+  PRED (SCM wrapped_obj);
 
 
-#define DEFINE_WRAPPED_TYPE(TYPE, PRINT_NAME, INIT, WRAP, UNWRAP)       \
+#define DEFINE_WRAPPED_TYPE(TYPE, PRINT_NAME, INIT, PRED, WRAP, UNWRAP) \
   static SCM wrapped_type;                                              \
                                                                         \
   void                                                                  \
@@ -61,7 +63,10 @@
   {                                                                     \
     scm_assert_foreign_object_type (wrapped_type, wrapped_obj);         \
     return (TYPE)scm_foreign_object_ref (wrapped_obj, 0);               \
+  }                                                                     \
+                                                                        \
+  bool                                                                  \
+  PRED (SCM wrapped_obj)                                                \
+  {                                                                     \
+    return SCM_IS_A_P (wrapped_obj, wrapped_type);                      \
   }
-
-wchar_t*
-scm_to_wide_char_string (SCM text);
-- 
2.39.5