]> git.jsancho.org Git - guile-irrlicht.git/blobdiff - src/video-driver.cpp
fixing bugs
[guile-irrlicht.git] / src / video-driver.cpp
index 57231a6d2623bd342aa934df16b4a9ccbefcd7d7..643ad89e809b488f34cc3581b1705b46896e282a 100644 (file)
 
 #include <irrlicht/irrlicht.h>
 #include <libguile.h>
-
 #include "color.h"
-#include "device.h"
+#include "gsubr.h"
+#include "matrix4.h"
+#include "primitive-types.h"
 #include "rect.h"
-#include "texture.h"
+#include "vertex3d.h"
 #include "video-driver.h"
-#include "wrapped.h"
 
-extern "C" {
 
-  void
-  init_video_driver (void)
-  {
-    init_video_driver_type ();
-    scm_c_define_gsubr ("begin-scene", 1, 0, 1, (scm_t_subr)irr_video_beginScene);
-    scm_c_define_gsubr ("get-texture", 2, 0, 0, (scm_t_subr)irr_video_getTexture);
-    scm_c_define_gsubr ("get-video-driver", 1, 0, 0, (scm_t_subr)irr_getVideoDriver);
-    scm_c_export ("begin-scene", "get-texture", "get-video-driver", NULL);
-  }
+using namespace irr;
+
+
+SCM
+video_IVideoDriver_beginScene (SCM video_driver,
+                               SCM back_buffer,
+                               SCM z_buffer,
+                               SCM color,
+                               SCM video_data,
+                               SCM source_rect)
+{
+  video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
+
+  // Source rect
+  core::rect<s32>* sourceRectAddress = 0;
+  if (!scm_is_false (source_rect))
+    {
+      core::rect<s32> sourceRect = scm_to_rect_s32 (source_rect);
+      sourceRectAddress = &sourceRect;
+    }
+
+  return scm_from_bool (driver->beginScene (scm_to_bool (back_buffer),
+                                            scm_to_bool (z_buffer),
+                                            scm_to_color (color),
+                                            video::SExposedVideoData (),
+                                            sourceRectAddress));
+}
 
-  DEFINE_WRAPPED_TYPE (irr::video::IVideoDriver*, "video-driver",
-                       init_video_driver_type, video_driver_p,
-                       wrap_video_driver, unwrap_video_driver);
 
-  SCM
-  irr_video_beginScene (SCM wrapped_video_driver,
-                        SCM rest)
-  {
-    SCM back_buffer = scm_from_bool(1);
-    SCM z_buffer = scm_from_bool(1);
-    SCM color = scm_list_4 (scm_from_uint32 (255),
-                            scm_from_uint32 (0),
-                            scm_from_uint32 (0),
-                            scm_from_uint32 (0));
-    SCM video_data = scm_from_bool(0);
-    SCM source_rect = scm_from_bool(0);
-
-    scm_c_bind_keyword_arguments ("begin-scene", rest, (scm_t_keyword_arguments_flags)0,
-                                  scm_from_utf8_keyword ("back-buffer"), &back_buffer,
-                                  scm_from_utf8_keyword ("z-buffer"), &z_buffer,
-                                  scm_from_utf8_keyword ("color"), &color,
-                                  scm_from_utf8_keyword ("video-data"), &video_data,
-                                  scm_from_utf8_keyword ("source-rect"), &source_rect,
-                                  SCM_UNDEFINED);
-
-    irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
-    irr::core::rect<irr::s32>* sourceRectAddress = 0;
-    if (!scm_is_false (source_rect))
-      {
-        irr::core::rect<irr::s32> sourceRect = scm_to_rect_s32 (source_rect);
-        sourceRectAddress = &sourceRect;
-      }
-    return scm_from_bool (driver->beginScene (scm_to_bool (back_buffer),
-                                              scm_to_bool (z_buffer),
-                                              scm_to_color (color),
-                                              irr::video::SExposedVideoData (),
-                                              sourceRectAddress));
-  }
+SCM
+video_IVideoDriver_drawVertexPrimitiveList (SCM video_driver,
+                                            SCM vertices,
+                                            SCM indices,
+                                            SCM v_type,
+                                            SCM p_type)
+{
+  // Build vertex array
+  u32 vertex_count = scm_to_uint32 (scm_length (vertices));
+  video::S3DVertex s3d_vertices [vertex_count];
+  for (int i = 0; i < vertex_count; i++)
+    {
+      video::S3DVertex* vertex =
+        (video::S3DVertex*) scm_to_pointer (scm_list_ref (vertices, scm_from_int (i)));
+      s3d_vertices[i] = video::S3DVertex (vertex->Pos,
+                                          vertex->Normal,
+                                          vertex->Color,
+                                          vertex->TCoords);
+    }
+
+  // Build index array
+  u32 index_count = scm_to_uint32 (scm_length (indices));
+  SCM flat_indices = scm_apply_0 (scm_eval_string (scm_from_utf8_string ("append")),
+                                  indices);
+  int flat_length = scm_to_int (scm_length (flat_indices));
+  u32 c_indices [flat_length];
+  for (int i = 0; i < flat_length; i++)
+    {
+      c_indices[i] = scm_to_uint32 (scm_list_ref (flat_indices, scm_from_int (i)));
+    }
+
+  // Draw vertices
+  video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
+  driver->drawVertexPrimitiveList (&s3d_vertices[0],
+                                   vertex_count,
+                                   &c_indices[0],
+                                   index_count,
+                                   scm_to_vertex_type (v_type),
+                                   scm_to_primitive_type (p_type),
+                                   video::EIT_32BIT);
+  return SCM_UNSPECIFIED;
+}
 
-  SCM
-  irr_video_getTexture (SCM wrapped_video_driver,
-                        SCM filename)
-  {
-    irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
-    irr::video::ITexture* texture = driver->getTexture (scm_to_utf8_stringn (filename, NULL));
-    return wrap_texture (texture);
-  }
 
-  SCM
-  irr_getVideoDriver (SCM wrapped_obj)
+SCM
+video_IVideoDriver_endScene (SCM video_driver)
+{
+  video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
+  return scm_from_bool (driver->endScene ());
+}
+
+
+SCM
+video_IVideoDriver_getFPS (SCM video_driver)
+{
+  video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
+  return scm_from_int32 (driver->getFPS ());
+}
+
+
+SCM
+video_IVideoDriver_getTexture (SCM video_driver,
+                               SCM filename)
+{
+  video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
+  video::ITexture* texture = driver->getTexture (scm_to_utf8_stringn (filename, NULL));
+  return scm_from_pointer ((void*) texture, NULL);
+}
+
+
+SCM
+video_IVideoDriver_setMaterial (SCM video_driver,
+                                SCM material)
+{
+  video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
+  driver->setMaterial (*((video::SMaterial*) scm_to_pointer (material)));
+  return SCM_UNSPECIFIED;
+}
+
+
+SCM
+video_IVideoDriver_setTransform (SCM video_driver,
+                                 SCM state,
+                                 SCM mat)
+{
+  video::IVideoDriver* driver = (video::IVideoDriver*) scm_to_pointer (video_driver);
+  driver->setTransform (scm_to_transformation_state (state),
+                        scm_to_matrix4 (mat));
+  return SCM_UNSPECIFIED;
+}
+
+
+extern "C" {
+
+  void
+  init_video_driver (void)
   {
-    irr::video::IVideoDriver* driver;
-    if (device_p (wrapped_obj))
-      {
-        driver = unwrap_device (wrapped_obj)->getVideoDriver ();
-      }
-    else
-      {
-        scm_error (scm_arg_type_key, NULL, "Cannot get video driver from object: ~S",
-                   scm_list_1 (wrapped_obj), scm_list_1 (wrapped_obj));
-      }
-    return wrap_video_driver (driver);
+    DEFINE_GSUBR ("video_IVideoDriver_beginScene", 6, 0, 0, video_IVideoDriver_beginScene);
+    DEFINE_GSUBR ("video_IVideoDriver_drawVertexPrimitiveList", 5, 0, 1,
+                  video_IVideoDriver_drawVertexPrimitiveList);
+    DEFINE_GSUBR ("video_IVideoDriver_endScene", 1, 0, 0, video_IVideoDriver_endScene);
+    DEFINE_GSUBR ("video_IVideoDriver_getFPS", 1, 0, 0, video_IVideoDriver_getFPS);
+    DEFINE_GSUBR ("video_IVideoDriver_getTexture", 2, 0, 0, video_IVideoDriver_getTexture);
+    DEFINE_GSUBR ("video_IVideoDriver_setMaterial", 2, 0, 0, video_IVideoDriver_setMaterial);
+    DEFINE_GSUBR ("video_IVideoDriver_setTransform", 3, 0, 0, video_IVideoDriver_setTransform);
   }
 
 }
+
+video::E_TRANSFORMATION_STATE
+scm_to_transformation_state (SCM transformation_state)
+{
+  char* state = scm_to_utf8_stringn (scm_symbol_to_string (transformation_state), NULL);
+  if (!strcmp (state, "view"))
+    {
+      return video::ETS_VIEW;
+    }
+  else if (!strcmp (state, "world"))
+    {
+      return video::ETS_WORLD;
+    }
+  else if (!strcmp (state, "projection"))
+    {
+      return video::ETS_PROJECTION;
+    }
+  else if (!strcmp (state, "texture0"))
+    {
+      return video::ETS_TEXTURE_0;
+    }
+  else if (!strcmp (state, "texture1"))
+    {
+      return video::ETS_TEXTURE_1;
+    }
+  else if (!strcmp (state, "texture2"))
+    {
+      return video::ETS_TEXTURE_2;
+    }
+  else if (!strcmp (state, "texture3"))
+    {
+      return video::ETS_TEXTURE_3;
+    }
+  else
+    {
+      scm_error (scm_arg_type_key, NULL, "Wrong transformation state: ~S",
+                 scm_list_1 (transformation_state), scm_list_1 (transformation_state));
+    }
+}