Sepehr Taghdisian avatar Sepehr Taghdisian committed c8a8d88

branch init, working on basic animation

Comments (0)

Files changed (23)

include/core/util.h

 
 #include "types.h"
 #include "core-api.h"
+#include "allocator.h"
 
 /* terminal ANSI colors for linux console only */
 #if defined(_POSIXLIB_)
 /**
  * loads a text file from disk and returns string buffer \n
  * @param filepath path of the text
- * @return null-terminated string containing file text, must be freed after use
+ * @return null-terminated string containing file text, must be freed after use (use A_FREE)
  * @ingroup util
  */
-CORE_API char* util_readtextfile(const char* txt_filepath);
+CORE_API char* util_readtextfile(const char* txt_filepath, struct allocator* alloc);
 
 #ifdef __cplusplus
 }

include/engine/anim.h

 
 /* animation set: clip bank */ 
 struct anim_set* anim_create_animset(struct allocator* alloc, struct allocator* tmp_alloc, 
-    const char* animset_filepath, reshandle_t clip_hdl);
+    const char* animset_json, reshandle_t clip_hdl);
 void anim_destroy_animset(struct anim_set* animset);
 
+void anim_update_clip_hierarchal(struct anim_clip* clip, fl32 t, const uint32* bindmap, 
+    const cmphandle_t* xforms, bool_t looped);
 
 #endif /* __ANIM_H__ */

include/engine/components/cmp-anim.h

+/***********************************************************************************
+ * Copyright (c) 2013, Sepehr Taghdisian
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without modification, 
+ * are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice, 
+ *   this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation 
+ *   and/or other materials provided with the distribution.
+ *
+ ***********************************************************************************/
+
+#ifndef __CMPANIM_H__
+#define __CMPANIM_H__
+
+#include "core/types.h"
+#include "../cmp-types.h"
+
+/* fwd */
+struct anim_set;
+
+/* */
+struct cmp_anim
+{
+    /* interface */
+    char filepath[128];
+    fl32 play_rate;
+    
+    /* internal */
+    fl32 t; /* elapsed time */
+    struct anim_set* set;
+    const cmphandle_t* xform_hdls; /* handle to xform components, used for hierarchal animation */
+    uint32 xform_cnt;   /* counf of mats or xform_hdls */
+    uint32* bindmap;  /* maps animation poses to joints in the target resource (count: clip->pose_cnt) */
+    uint32 filepathhash;
+    struct allocator* alloc;    /* we have dynamic allocation in this component */
+};
+
+ENGINE_API result_t cmp_anim_modify(struct cmp_obj* obj, struct allocator* alloc,
+    struct allocator* tmp_alloc, void* data, cmphandle_t cur_hdl);
+
+/* descriptors */
+static const struct cmp_value cmp_anim_values[] = {
+    {"anim_file", CMP_VALUE_STRING, offsetof(struct cmp_anim, filepath), 128, 1,
+    cmp_anim_modify, "customdlg; filepicker; filter=*.h3da;"},
+    {"play_rate", CMP_VALUE_FLOAT, offsetof(struct cmp_anim, play_rate), sizeof(fl32),
+    1,  NULL, ""}
+};
+static const uint32 cmp_anim_valuecnt = 2;
+static const uint16 cmp_anim_type = 0x068b;
+
+/* */
+result_t cmp_anim_register(struct allocator* alloc);
+void cmp_anim_reload(const char* filepath, reshandle_t hdl, uptr_t param1, uptr_t param2);
+
+#endif /* __CMPANIM_H__ */

include/engine/h3d-types.h

 #endif
 };
 
-struct h3d_phx_shape
+struct _GCCPACKED_ h3d_phx_shape
 {
     uint32 type; /* h3d_phx_shapetype */
     fl32 local_pos[3];
 #endif
 };
 
-struct h3d_phx_rigid
+struct _GCCPACKED_ h3d_phx_rigid
 {
     char name[32];
     uint32 type; /* h3d_phx_rigidtype */
 #endif
 };
 
-struct h3d_phx
+struct _GCCPACKED_ h3d_phx
 {
     uint32 mtl_cnt;
     uint32 geo_cnt;
 #include "err.h"
 #include "mem-mgr.h"
 
-char* util_readtextfile(const char* txt_filepath)
+char* util_readtextfile(const char* txt_filepath, struct allocator* alloc)
 {
     FILE* f = fopen(txt_filepath, "rb");
     if (f == NULL)  {
     }
     fseek(f, 0, SEEK_SET);
 
-    char* buffer = ALLOC(s + 1, 0);    
+    char* buffer = A_ALLOC(alloc, s + 1, 0);    
     if (buffer == NULL) {
         fclose(f);
         err_printn(__FILE__, __LINE__, RET_OUTOFMEMORY);

src/core/vec-math.c

         return quat_setq(r, &qa);
     }
     
-    fl32 ht = acos(cos_ht);
+    fl32 ht = acosf(cos_ht);
     fl32 sin_ht = sqrtf(1.0f - cos_ht*cos_ht);
     if (fabsf(sin_ht) < 0.001f)  {
         return quat_setf(r,

src/engine/anim.c

 #include "core/core.h"
 #include "core/file-mgr.h"
 #include "core/json.h"
+#include "core/vec-math.h"
 
 #include "anim.h"
 #include "h3d-types.h"
 #include "mem-ids.h"
 #include "res-mgr.h"
 
+#include "cmp-mgr.h"
+#include "components/cmp-xform.h"
+
 /*************************************************************************************************
  * fwd declarations
  */
 }
 
 struct anim_set* anim_create_animset(struct allocator* alloc, struct allocator* tmp_alloc,
-    const char* animset_filepath, reshandle_t clip_hdl)
+    const char* animset_json, reshandle_t clip_hdl)
 {
     ASSERT(clip_hdl != INVALID_HANDLE);
 
     animset->alloc = alloc;
     animset->clip_hdl = clip_hdl;
 
-    result_t r = anim_loadanimset(alloc, tmp_alloc, animset, rs_get_anim(clip_hdl), animset_filepath);
+    result_t r = anim_loadanimset(alloc, tmp_alloc, animset, rs_get_anim(clip_hdl), animset_json);
     if (IS_FAIL(r)) {
-        err_printf(__FILE__, __LINE__, "creating animset '%s' failed: could not load data from file",
-            animset_filepath);
+        err_printf(__FILE__, __LINE__, "creating animset failed: could not load data from file");
         anim_destroy_animset(animset);
         return NULL;
     }
 }
 
 result_t anim_loadanimset(struct allocator* alloc, struct allocator* tmp_alloc, 
-    struct anim_set* animset, struct anim_clip* clip, const char* animset_filepath)
+    struct anim_set* animset, struct anim_clip* clip, const char* animset_json)
 {
-    file_t f = io_file_openmem(tmp_alloc, animset_filepath, FALSE, MID_ANIM);
-    if (f == FILE_NULL) {
-        err_printf(__FILE__, __LINE__, "loading animset '%s' failed: could not open file",
-            animset_filepath); 
-        return RET_FAIL;
-    }
-
     /* parse animset json file */
     json_t jclips;
-    json_t jroot = json_parsefile(f, tmp_alloc);
-    io_file_close(f);
+    json_t jroot = json_parsestring(animset_json);
     if (jroot == NULL || (jclips = json_getitem(jroot, "clips")) == NULL)  {
         if (jroot)
             json_destroy(jroot);
-        err_printf(__FILE__, __LINE__, "loading animset '%s' failed: invalid JSON animset file",
-            animset_filepath); 
+        err_printf(__FILE__, __LINE__, "loading animset failed: invalid JSON animset"); 
         return RET_FAIL;
     }
     
     int32 subclip_cnt = json_getarr_count(jclips);
     if (subclip_cnt == 0)   {
         json_destroy(jroot);
-        err_printf(__FILE__, __LINE__, "loading animset '%s' failed: no subclips in file",
-            animset_filepath); 
+        err_printf(__FILE__, __LINE__, "loading animset failed: no subclips in data!"); 
         return RET_FAIL;
     }
 
     animset->subclips = A_ALLOC(alloc, sizeof(struct anim_subclip)*subclip_cnt, MID_ANIM);
     if (animset->subclips == NULL)  {
         json_destroy(jroot);
-        err_printf(__FILE__, __LINE__, "loading animset '%s' failed: not enough memory",
-            animset_filepath); 
+        err_printf(__FILE__, __LINE__, "loading animset failed: not enough memory"); 
         return RET_FAIL;
     }
 
         struct anim_subclip* subclip = &animset->subclips[i];
 
         str_safecpy(subclip->name, sizeof(subclip->name), 
-            json_gets_child(jsubclip, "name", "[no-name]"));
+            json_gets_child(jsubclip, "name", "[noname]"));
         subclip->frame_start = minun(clip->frame_cnt-1, json_geti_child(jsubclip, "start", 0));
         int32 frame_end = json_geti_child(jsubclip, "end", -1);
         if (frame_end == -1)
         A_FREE(alloc, animset->subclips);
 }
 
+void anim_update_clip_hierarchal(struct anim_clip* clip, fl32 t, const uint32* bindmap, 
+    const cmphandle_t* xforms, bool_t looped)
+{
+    uint32 frame_idx = clampn((uint32)(t/clip->ft), 0, clip->frame_cnt-1);
+    uint32 nextframe_idx = looped ? ((frame_idx + 1) % clip->frame_cnt) : (frame_idx + 1);
+
+    /* interpolate between two frames (samples) by time
+     * normalize between 0~1 */
+    fl32 ivalue = (t - (frame_idx * clip->ft)) / clip->ft;
+
+    const struct anim_channel* sampl = &clip->channels[frame_idx];
+    const struct anim_channel* next_sampl = &clip->channels[nextframe_idx];
+
+    struct mat3f xfm;
+    mat3_setidentity(&xfm);
+
+    for (uint32 i = 0, pose_cnt = clip->pose_cnt; i < pose_cnt; i++)    {
+        struct vec3f pos_lerp;
+        struct quat4f rot_slerp;
+
+        vec3_lerp(&pos_lerp, &sampl->poses[i].pos_scale, &next_sampl->poses[i].pos_scale, ivalue);
+        quat_slerp(&rot_slerp, &sampl->poses[i].rot, &next_sampl->poses[i].rot, ivalue);
+
+        mat3_set_trans(&xfm, &pos_lerp);
+        mat3_set_rotquat(&xfm, &rot_slerp);
+
+        cmphandle_t xfh = xforms[bindmap[i]];
+        struct cmp_xform* xf = cmp_getinstancedata(xfh);
+        mat3_setm(&xf->mat, &xfm);
+
+        cmp_updateinstance(xfh);
+    }
+}
     ASSERT(params);
     memset(params, 0x00, sizeof(struct init_params));
 
-    char* buffer = util_readtextfile(cfg_jsonfile);
+    char* buffer = util_readtextfile(cfg_jsonfile, mem_heap());
     if (buffer == NULL) {
         err_printf(__FILE__, __LINE__, "loading confing file '%s' failed", cfg_jsonfile);
         FREE(params);

src/engine/cmp-register-main.c

 #include "components/cmp-trigger.h"
 #include "components/cmp-attachdock.h"
 #include "components/cmp-attachment.h"
+#include "components/cmp-anim.h"
 
 result_t cmp_register_main_components()
 {
     if (IS_FAIL(r))
         return RET_FAIL;
 
+    r = cmp_anim_register(alloc);
+    if (IS_FAIL(r))
+        return RET_FAIL;
+
     r = cmp_light_register(alloc);
     if (IS_FAIL(r))
         return RET_FAIL;

src/engine/components/cmp-anim.c

+/***********************************************************************************
+ * Copyright (c) 2013, Sepehr Taghdisian
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without modification, 
+ * are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice, 
+ *   this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation 
+ *   and/or other materials provided with the distribution.
+ *
+ ***********************************************************************************/
+
+#include "core/core.h"
+
+#include "anim.h"
+#include "cmp-mgr.h"
+#include "res-mgr.h"
+#include "gfx-model.h"
+#include "anim.h"
+#include "mem-ids.h"
+#include "engine.h"
+
+#include "components/cmp-anim.h"
+#include "components/cmp-model.h"
+
+
+/*************************************************************************************************
+ * fwd declarations
+ */
+result_t cmp_anim_create(struct cmp_obj* obj, void* data, cmphandle_t hdl);
+void cmp_anim_destroy(struct cmp_obj* obj, void* data, cmphandle_t hdl);
+void cmp_anim_update(cmp_t c, fl32 dt, void* param);
+void cmp_anim_destroydata(struct cmp_obj* obj, struct cmp_anim* data, cmphandle_t hdl,
+    bool_t release_mesh);
+result_t cmp_anim_bind(struct cmp_obj* obj, void* data, struct allocator* alloc, 
+    struct allocator* tmp_alloc, reshandle_t hdl);
+result_t cmp_anim_createhierarchy(struct cmp_anim* a, struct cmp_model* m, struct gfx_model* gmodel);
+void cmp_anim_destroyhierarchy(struct cmp_anim* a);
+
+void cmp_anim_clearinstances(reshandle_t clip_hdl, const struct cmp_instance_desc** insts, 
+    uint32 cnt);
+void cmp_anim_rebuildhinstances(struct allocator* alloc, struct allocator* tmp_alloc, 
+    reshandle_t clip_hdl, const struct cmp_instance_desc** insts, uint32 cnt);
+void cmp_anim_destroyinstances(reshandle_t clip_hdl, const struct cmp_instance_desc** insts, 
+    uint32 cnt);
+
+void cmp_anim_play(cmphandle_t hdl);
+void cmp_anim_stop(cmphandle_t hdl);
+
+/*************************************************************************************************/
+result_t cmp_anim_register(struct allocator* alloc)
+{
+    struct cmp_createparams params;
+    memset(&params, 0x00, sizeof(params));
+
+    params.name = "anim";
+    params.stride = sizeof(struct cmp_anim);
+    params.create_func = cmp_anim_create;
+    params.destroy_func = cmp_anim_destroy;
+    //params.debug_func = cmp_anim_debug;
+    params.grow_cnt = 300;
+    params.initial_cnt = 300;
+    params.values = cmp_anim_values;
+    params.value_cnt = cmp_anim_valuecnt;
+    params.type = cmp_anim_type;
+    params.update_funcs[CMP_UPDATE_STAGE1] = cmp_anim_update;
+    params.flags = CMP_FLAG_ALWAYSUPDATE;
+    return cmp_register_component(alloc, &params);
+}
+
+result_t cmp_anim_create(struct cmp_obj* obj, void* data, cmphandle_t hdl)
+{
+    struct cmp_anim* anim = data;
+    anim->play_rate = 1.0f;
+
+    obj->anim_cmp = hdl;
+    return RET_OK;
+}
+
+void cmp_anim_destroy(struct cmp_obj* obj, void* data, cmphandle_t hdl)
+{
+    cmp_anim_destroydata(obj, data, hdl, TRUE);
+
+    obj->anim_cmp = INVALID_HANDLE;
+}
+
+void cmp_anim_update(cmp_t c, fl32 dt, void* param)
+{
+    uint32 cnt;
+    const struct cmp_instance_desc** updates = cmp_get_updateinstances(c, &cnt);
+    for (uint32 i = 0; i < cnt; i++)	{
+        const struct cmp_instance_desc* inst = updates[i];
+        struct cmp_anim* a = (struct cmp_anim*)inst->data;
+#if !defined(_RETAIL_)
+        if (a->set && a->set->clip_hdl != INVALID_HANDLE)   {
+#endif
+            struct anim_clip* clip = rs_get_anim(a->set->clip_hdl);
+            fl32 t = a->t;
+            
+            t = fmodf(t, clip->duration);;
+
+            anim_update_clip_hierarchal(clip, t, a->bindmap, a->xform_hdls, TRUE);
+#if !defined(_RETAIL_)
+        }
+#endif
+        a->t += dt * a->play_rate;
+
+    }
+}
+
+result_t cmp_anim_modify(struct cmp_obj* obj, struct allocator* alloc, struct allocator* tmp_alloc, 
+    void* data, cmphandle_t cur_hdl)
+{
+    result_t r;
+    struct cmp_anim* a = data;
+
+    uint32 filehash = hashtable_str(a->filepath);
+    bool_t reload = (a->filepathhash == filehash);
+    cmp_anim_destroydata(obj, a, cur_hdl, !reload);
+
+    a->alloc = alloc;
+    if (str_isempty(a->filepath))
+        return RET_OK;
+
+    reshandle_t clip_hdl = a->set != NULL ? a->set->clip_hdl : INVALID_HANDLE;
+    if (!reload)
+        clip_hdl = rs_load_anim(a->filepath, 0);
+    
+    if (clip_hdl == INVALID_HANDLE) {
+        a->filepath[0] = 0;
+        return RET_FAIL;
+    }
+
+    /* anim-set */
+    a->set = anim_create_animset(alloc, tmp_alloc, "{\"clips\":[{\"name\":\"test\"}]}", clip_hdl);
+    if (a->set == NULL) {
+        err_sendtolog(TRUE);
+        return RET_FAIL;
+    }
+
+    /* bind */
+    r = cmp_anim_bind(obj, a, alloc, tmp_alloc, cur_hdl);
+    if (IS_FAIL(r)) {
+        err_sendtolog(TRUE);
+        log_print(LOG_WARNING, "binding anim-set failed");
+        return RET_FAIL;
+    }
+
+    a->filepathhash = filehash;
+    return RET_OK;
+}
+
+result_t cmp_anim_bind(struct cmp_obj* obj, void* data, struct allocator* alloc, 
+    struct allocator* tmp_alloc, reshandle_t hdl)
+{
+    struct cmp_anim* a = data;
+
+    /* bind animation to object, by evaluating object's model component
+     * - obj->model_cmp is invalid: wait to model to update (will be called from cmp_model)
+     * - obj->model_cmp is skinned: bind skeleton, find first geometry that is skinned, bind to that
+     * - obj->model_cmp is not skinned: bind hierarchal animation
+     */
+     if (obj->model_cmp == INVALID_HANDLE)
+        return RET_OK;
+
+    struct cmp_model* m = cmp_getinstancedata(obj->model_cmp);
+    if (m->model_hdl == INVALID_HANDLE)
+        return RET_OK;
+
+    /* choose between skinned geometry and hierarchal */
+    struct gfx_model* gmodel = rs_get_model(m->model_hdl);
+    uint32 geo_idx = INVALID_INDEX;
+    for (uint32 i = 0; i < gmodel->geo_cnt; i++)    {
+        if (gmodel->geos[i].skeleton != NULL)   {
+            geo_idx = i;
+            break;
+        }
+    }
+
+    result_t r = RET_FAIL;
+    if (geo_idx != INVALID_INDEX)   {
+        ASSERT(0);
+    }    else   {
+        r = cmp_anim_createhierarchy(a, m, gmodel);
+    }
+
+    if (IS_OK(r))   {
+        cmp_anim_play(hdl);
+    }
+
+    return r;
+}
+
+result_t cmp_anim_createhierarchy(struct cmp_anim* a, struct cmp_model* m, struct gfx_model* gmodel)
+{
+    a->xform_hdls = m->xforms;
+    a->xform_cnt = m->xform_cnt;
+
+    /* bind maps */
+    struct anim_clip* clip = rs_get_anim(a->set->clip_hdl);
+    uint32 pose_cnt = clip->pose_cnt;
+    a->bindmap = A_ALLOC(a->alloc, sizeof(uint32)*clip->pose_cnt, MID_ANIM);
+    if (a->bindmap == NULL)    {
+        cmp_anim_destroyhierarchy(a);
+        return RET_OUTOFMEMORY;
+    }
+
+    for (uint32 i = 0; i < pose_cnt; i++)   {
+        a->bindmap[i] = gfx_model_findnode(gmodel, clip->binds + i*32);
+        if (a->bindmap[i] == INVALID_INDEX) {
+            cmp_anim_destroyhierarchy(a);
+            return RET_FAIL;
+        }
+    } /* foreach: pose */
+
+    return RET_OK;
+}
+
+void cmp_anim_destroyhierarchy(struct cmp_anim* a)
+{
+    if (a->alloc != NULL && a->bindmap != NULL)    {
+        A_FREE(a->alloc, a->bindmap);
+    }
+
+    a->xform_cnt = 0;
+    a->xform_hdls = NULL;
+    a->bindmap = NULL;
+}
+
+void cmp_anim_play(cmphandle_t hdl)
+{
+    cmp_updateinstance(hdl);
+}
+
+void cmp_anim_stop(cmphandle_t hdl)
+{
+    cmp_updateinstance_reset(hdl);
+}
+
+void cmp_anim_destroydata(struct cmp_obj* obj, struct cmp_anim* data, cmphandle_t hdl,
+    bool_t release_clip)
+{
+    cmp_anim_stop(hdl);
+
+    cmp_anim_destroyhierarchy(data);
+
+    if (release_clip && data->set && data->set->clip_hdl != INVALID_HANDLE)   {
+        rs_unload(data->set->clip_hdl);
+        data->filepathhash = 0;
+        data->filepath[0] = 0;
+        data->set->clip_hdl = INVALID_HANDLE;
+    }
+
+    if (data->set != NULL)
+        anim_destroy_animset(data->set);
+
+    data->alloc = NULL;
+}
+
+void cmp_anim_reload(const char* filepath, reshandle_t hdl, uptr_t param1, uptr_t param2)
+{
+    uint32 cnt;
+    struct allocator* tmp_alloc = eng_get_framealloc();
+    cmp_t c = cmp_findtype(cmp_anim_type);
+    const struct cmp_instance_desc** insts = cmp_get_allinstances(c, &cnt, tmp_alloc);
+
+    /* reload model and refresh all model component data */
+    cmp_anim_destroyinstances(hdl, insts, cnt);
+    reshandle_t nhdl = rs_load_anim(filepath, RS_LOAD_REFRESH);
+    if (nhdl != INVALID_HANDLE)
+        cmp_anim_rebuildhinstances(eng_get_dataalloc(), tmp_alloc, hdl, insts, cnt);
+    else
+        cmp_anim_clearinstances(hdl, insts, cnt);  /* this happens when model-reload fails,
+                                                       we have to invalidate handles */
+
+    A_FREE(tmp_alloc, insts);
+}
+
+void cmp_anim_destroyinstances(reshandle_t clip_hdl, const struct cmp_instance_desc** insts, 
+    uint32 cnt)
+{
+    for (uint32 i = 0; i < cnt; i++) {
+        const struct cmp_instance_desc* inst = insts[i];
+        const struct cmp_anim* a = (const struct cmp_anim*)inst->data;
+        if (a->set && clip_hdl == a->set->clip_hdl)
+            cmp_anim_destroydata(inst->host, (struct cmp_anim*)inst->data, inst->hdl, FALSE);
+    }
+}
+
+void cmp_anim_rebuildhinstances(struct allocator* alloc, struct allocator* tmp_alloc, 
+    reshandle_t clip_hdl, const struct cmp_instance_desc** insts, uint32 cnt)
+{
+    for (uint32 i = 0; i < cnt; i++) {
+        const struct cmp_instance_desc* inst = insts[i];
+        const struct cmp_anim* a = (const struct cmp_anim*)inst->data;
+        if (a->set && clip_hdl == a->set->clip_hdl)  {
+            cmp_anim_modify(inst->host, alloc, tmp_alloc, inst->data, inst->hdl);
+        }
+    }
+}
+
+void cmp_anim_clearinstances(reshandle_t clip_hdl, const struct cmp_instance_desc** insts, 
+    uint32 cnt)
+{
+    for (uint32 i = 0; i < cnt; i++) {
+        const struct cmp_instance_desc* inst = insts[i];
+        const struct cmp_anim* a = (const struct cmp_anim*)inst->data;
+        if (a->set && clip_hdl == a->set->clip_hdl)  {
+            a->set->clip_hdl = INVALID_HANDLE;
+        }
+    }
+}

src/engine/d3d/app-d3d.cpp

 
     switch (msg)    {
     case WM_CREATE:
-        g_app->init = TRUE;
         if (g_app->create_fn != NULL)
             g_app->create_fn(app_get_wndname(hwnd));
         break;
 
     /* set as default render-target */
     app_set_rendertarget(app->name);
+    app->init = TRUE;
 
     return RET_OK;
 }
 {
     ASSERT(g_app);
     struct app_win* app = g_app;
+
+    if (!app->init)
+        return RET_FAIL;
+    
     /* search for the specified swapchain and resize it */
     struct app_swapchain* rsc = (name != NULL) ? app_find_swapchain(name) : app->render_target;
     if (rsc == NULL)

src/engine/gfx-model.c

 						h3dm_filepath);
 				goto err_cleanup;
 			}
+
+            /* NOTE: set root matrix to identity */
+            if (i == 0)
+                mat3_setidentity(&node->local_mat);
 			model->node_cnt ++;
 		}
 	}
 
     /* calculate sum of aabb(s) from renderable nodes */
 	aabb_setzero(&model->bb);
+    struct mat3f node_mat;  /* transform matrix, relative to model */
 	for (uint32 i = 0; i < renderable_idx; i++)   {
-		aabb_merge(&model->bb, &model->bb, &model->nodes[model->renderable_idxs[i]].bb);
+        struct gfx_model_node* node = &model->nodes[model->renderable_idxs[i]];
+        mat3_setm(&node_mat, &node->local_mat);
+        while (node->parent_id != INVALID_INDEX)	{
+            node = &model->nodes[node->parent_id];
+            mat3_mul(&node_mat, &node_mat, &node->local_mat);
+        }
+        
+        /* transform local box to model-relative bounding box and merge with final */
+        struct aabb bb;
+        aabb_xform(&bb, &model->nodes[model->renderable_idxs[i]].bb, &node_mat);
+		aabb_merge(&model->bb, &model->bb, &bb);
     }
     /* for empty models, we set a minimal bounding-box */
     if (aabb_iszero(&model->bb))    {
         A_ALIGNED_FREE(alloc, occ->poss);
 }
 
+uint32 gfx_model_findnode(const struct gfx_model* model, const char* name)
+{
+    uint32 namehash = hashtable_str(name);
+    for (uint32 i = 0, cnt = model->node_cnt; i < cnt; i++) {
+        if (namehash == model->nodes[i].name_hash)
+            return i;
+    }
+
+    return INVALID_INDEX;
+}
+
+

src/engine/gl/app-gl.c

 {
     struct app_gl* app = g_app;
 
+    if (!app->init)
+        return RET_FAIL;
+
     /* for main window (this is where main render buffers are created), resize internal buffers */
     if (str_isequal(wnd_name, app->name) && (app->width != width || app->height != height))   {
         gfx_resize(width, height);  

src/engine/luabind/luaengine.i

     void addRigidBody(const char* h3dpfile);
     void removeRigidBody();
 
-    void addAttachDock(const char* dock1, const char* dock2 = "", const char* dock3 = "", 
+    void loadAttachDock(const char* dock1, const char* dock2 = "", const char* dock3 = "", 
         const char* dock4 = "");
     void attach(const object& obj, const char* dock_name);
     void detach();
 
+    void loadAnimation(const char* h3da_filepath);
+
     void addForce(const vec3& force);
     void addForce(fl32 fx, fl32 fy, fl32 fz);
     void addTorque(const vec3& torque);

src/engine/luabind/luaengine_wrap.cxx

-/* ----------------------------------------------------------------------------
- * This file was automatically generated by SWIG (http://www.swig.org).
- * Version 2.0.9
- * 
- * This file is not intended to be easily readable and contains a number of 
- * coding conventions designed to improve portability and efficiency. Do not make
- * changes to this file unless you know what you are doing--modify the SWIG 
- * interface file instead. 
- * ----------------------------------------------------------------------------- */
-
-#define SWIGLUA
-#define SWIG_LUA_TARGET SWIG_LUA_FLAVOR_LUA
-#define SWIG_LUA_MODULE_GLOBAL
-
-
-#ifdef __cplusplus
-/* SwigValueWrapper is described in swig.swg */
-template<typename T> class SwigValueWrapper {
-  struct SwigMovePointer {
-    T *ptr;
-    SwigMovePointer(T *p) : ptr(p) { }
-    ~SwigMovePointer() { delete ptr; }
-    SwigMovePointer& operator=(SwigMovePointer& rhs) { T* oldptr = ptr; ptr = 0; delete oldptr; ptr = rhs.ptr; rhs.ptr = 0; return *this; }
-  } pointer;
-  SwigValueWrapper& operator=(const SwigValueWrapper<T>& rhs);
-  SwigValueWrapper(const SwigValueWrapper<T>& rhs);
-public:
-  SwigValueWrapper() : pointer(0) { }
-  SwigValueWrapper& operator=(const T& t) { SwigMovePointer tmp(new T(t)); pointer = tmp; return *this; }
-  operator T&() const { return *pointer.ptr; }
-  T *operator&() { return pointer.ptr; }
-};
-
-template <typename T> T SwigValueInit() {
-  return T();
-}
-#endif
-
-/* -----------------------------------------------------------------------------
- *  This section contains generic SWIG labels for method/variable
- *  declarations/attributes, and other compiler dependent labels.
- * ----------------------------------------------------------------------------- */
-
-/* template workaround for compilers that cannot correctly implement the C++ standard */
-#ifndef SWIGTEMPLATEDISAMBIGUATOR
-# if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560)
-#  define SWIGTEMPLATEDISAMBIGUATOR template
-# elif defined(__HP_aCC)
-/* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */
-/* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */
-#  define SWIGTEMPLATEDISAMBIGUATOR template
-# else
-#  define SWIGTEMPLATEDISAMBIGUATOR
-# endif
-#endif
-
-/* inline attribute */
-#ifndef SWIGINLINE
-# if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__))
-#   define SWIGINLINE inline
-# else
-#   define SWIGINLINE
-# endif
-#endif
-
-/* attribute recognised by some compilers to avoid 'unused' warnings */
-#ifndef SWIGUNUSED
-# if defined(__GNUC__)
-#   if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
-#     define SWIGUNUSED __attribute__ ((__unused__)) 
-#   else
-#     define SWIGUNUSED
-#   endif
-# elif defined(__ICC)
-#   define SWIGUNUSED __attribute__ ((__unused__)) 
-# else
-#   define SWIGUNUSED 
-# endif
-#endif
-
-#ifndef SWIG_MSC_UNSUPPRESS_4505
-# if defined(_MSC_VER)
-#   pragma warning(disable : 4505) /* unreferenced local function has been removed */
-# endif 
-#endif
-
-#ifndef SWIGUNUSEDPARM
-# ifdef __cplusplus
-#   define SWIGUNUSEDPARM(p)
-# else
-#   define SWIGUNUSEDPARM(p) p SWIGUNUSED 
-# endif
-#endif
-
-/* internal SWIG method */
-#ifndef SWIGINTERN
-# define SWIGINTERN static SWIGUNUSED
-#endif
-
-/* internal inline SWIG method */
-#ifndef SWIGINTERNINLINE
-# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE
-#endif
-
-/* exporting methods */
-#if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
-#  ifndef GCC_HASCLASSVISIBILITY
-#    define GCC_HASCLASSVISIBILITY
-#  endif
-#endif
-
-#ifndef SWIGEXPORT
-# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
-#   if defined(STATIC_LINKED)
-#     define SWIGEXPORT
-#   else
-#     define SWIGEXPORT __declspec(dllexport)
-#   endif
-# else
-#   if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)
-#     define SWIGEXPORT __attribute__ ((visibility("default")))
-#   else
-#     define SWIGEXPORT
-#   endif
-# endif
-#endif
-
-/* calling conventions for Windows */
-#ifndef SWIGSTDCALL
-# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
-#   define SWIGSTDCALL __stdcall
-# else
-#   define SWIGSTDCALL
-# endif 
-#endif
-
-/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
-#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
-# define _CRT_SECURE_NO_DEPRECATE
-#endif
-
-/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */
-#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE)
-# define _SCL_SECURE_NO_DEPRECATE
-#endif
-
-
-/* -----------------------------------------------------------------------------
- * swigrun.swg
- *
- * This file contains generic C API SWIG runtime support for pointer
- * type checking.
- * ----------------------------------------------------------------------------- */
-
-/* This should only be incremented when either the layout of swig_type_info changes,
-   or for whatever reason, the runtime changes incompatibly */
-#define SWIG_RUNTIME_VERSION "4"
-
-/* define SWIG_TYPE_TABLE_NAME as "SWIG_TYPE_TABLE" */
-#ifdef SWIG_TYPE_TABLE
-# define SWIG_QUOTE_STRING(x) #x
-# define SWIG_EXPAND_AND_QUOTE_STRING(x) SWIG_QUOTE_STRING(x)
-# define SWIG_TYPE_TABLE_NAME SWIG_EXPAND_AND_QUOTE_STRING(SWIG_TYPE_TABLE)
-#else
-# define SWIG_TYPE_TABLE_NAME
-#endif
-
-/*
-  You can use the SWIGRUNTIME and SWIGRUNTIMEINLINE macros for
-  creating a static or dynamic library from the SWIG runtime code.
-  In 99.9% of the cases, SWIG just needs to declare them as 'static'.
-  
-  But only do this if strictly necessary, ie, if you have problems
-  with your compiler or suchlike.
-*/
-
-#ifndef SWIGRUNTIME
-# define SWIGRUNTIME SWIGINTERN
-#endif
-
-#ifndef SWIGRUNTIMEINLINE
-# define SWIGRUNTIMEINLINE SWIGRUNTIME SWIGINLINE
-#endif
-
-/*  Generic buffer size */
-#ifndef SWIG_BUFFER_SIZE
-# define SWIG_BUFFER_SIZE 1024
-#endif
-
-/* Flags for pointer conversions */
-#define SWIG_POINTER_DISOWN        0x1
-#define SWIG_CAST_NEW_MEMORY       0x2
-
-/* Flags for new pointer objects */
-#define SWIG_POINTER_OWN           0x1
-
-
-/* 
-   Flags/methods for returning states.
-   
-   The SWIG conversion methods, as ConvertPtr, return an integer 
-   that tells if the conversion was successful or not. And if not,
-   an error code can be returned (see swigerrors.swg for the codes).
-   
-   Use the following macros/flags to set or process the returning
-   states.
-   
-   In old versions of SWIG, code such as the following was usually written:
-
-     if (SWIG_ConvertPtr(obj,vptr,ty.flags) != -1) {
-       // success code
-     } else {
-       //fail code
-     }
-
-   Now you can be more explicit:
-
-    int res = SWIG_ConvertPtr(obj,vptr,ty.flags);
-    if (SWIG_IsOK(res)) {
-      // success code
-    } else {
-      // fail code
-    }
-
-   which is the same really, but now you can also do
-
-    Type *ptr;
-    int res = SWIG_ConvertPtr(obj,(void **)(&ptr),ty.flags);
-    if (SWIG_IsOK(res)) {
-      // success code
-      if (SWIG_IsNewObj(res) {
-        ...
-	delete *ptr;
-      } else {
-        ...
-      }
-    } else {
-      // fail code
-    }
-    
-   I.e., now SWIG_ConvertPtr can return new objects and you can
-   identify the case and take care of the deallocation. Of course that
-   also requires SWIG_ConvertPtr to return new result values, such as
-
-      int SWIG_ConvertPtr(obj, ptr,...) {         
-        if (<obj is ok>) {			       
-          if (<need new object>) {		       
-            *ptr = <ptr to new allocated object>; 
-            return SWIG_NEWOBJ;		       
-          } else {				       
-            *ptr = <ptr to old object>;	       
-            return SWIG_OLDOBJ;		       
-          } 				       
-        } else {				       
-          return SWIG_BADOBJ;		       
-        }					       
-      }
-
-   Of course, returning the plain '0(success)/-1(fail)' still works, but you can be
-   more explicit by returning SWIG_BADOBJ, SWIG_ERROR or any of the
-   SWIG errors code.
-
-   Finally, if the SWIG_CASTRANK_MODE is enabled, the result code
-   allows to return the 'cast rank', for example, if you have this
-
-       int food(double)
-       int fooi(int);
-
-   and you call
- 
-      food(1)   // cast rank '1'  (1 -> 1.0)
-      fooi(1)   // cast rank '0'
-
-   just use the SWIG_AddCast()/SWIG_CheckState()
-*/
-
-#define SWIG_OK                    (0) 
-#define SWIG_ERROR                 (-1)
-#define SWIG_IsOK(r)               (r >= 0)
-#define SWIG_ArgError(r)           ((r != SWIG_ERROR) ? r : SWIG_TypeError)  
-
-/* The CastRankLimit says how many bits are used for the cast rank */
-#define SWIG_CASTRANKLIMIT         (1 << 8)
-/* The NewMask denotes the object was created (using new/malloc) */
-#define SWIG_NEWOBJMASK            (SWIG_CASTRANKLIMIT  << 1)
-/* The TmpMask is for in/out typemaps that use temporal objects */
-#define SWIG_TMPOBJMASK            (SWIG_NEWOBJMASK << 1)
-/* Simple returning values */
-#define SWIG_BADOBJ                (SWIG_ERROR)
-#define SWIG_OLDOBJ                (SWIG_OK)
-#define SWIG_NEWOBJ                (SWIG_OK | SWIG_NEWOBJMASK)
-#define SWIG_TMPOBJ                (SWIG_OK | SWIG_TMPOBJMASK)
-/* Check, add and del mask methods */
-#define SWIG_AddNewMask(r)         (SWIG_IsOK(r) ? (r | SWIG_NEWOBJMASK) : r)
-#define SWIG_DelNewMask(r)         (SWIG_IsOK(r) ? (r & ~SWIG_NEWOBJMASK) : r)
-#define SWIG_IsNewObj(r)           (SWIG_IsOK(r) && (r & SWIG_NEWOBJMASK))
-#define SWIG_AddTmpMask(r)         (SWIG_IsOK(r) ? (r | SWIG_TMPOBJMASK) : r)
-#define SWIG_DelTmpMask(r)         (SWIG_IsOK(r) ? (r & ~SWIG_TMPOBJMASK) : r)
-#define SWIG_IsTmpObj(r)           (SWIG_IsOK(r) && (r & SWIG_TMPOBJMASK))
-
-/* Cast-Rank Mode */
-#if defined(SWIG_CASTRANK_MODE)
-#  ifndef SWIG_TypeRank
-#    define SWIG_TypeRank             unsigned long
-#  endif
-#  ifndef SWIG_MAXCASTRANK            /* Default cast allowed */
-#    define SWIG_MAXCASTRANK          (2)
-#  endif
-#  define SWIG_CASTRANKMASK          ((SWIG_CASTRANKLIMIT) -1)
-#  define SWIG_CastRank(r)           (r & SWIG_CASTRANKMASK)
-SWIGINTERNINLINE int SWIG_AddCast(int r) { 
-  return SWIG_IsOK(r) ? ((SWIG_CastRank(r) < SWIG_MAXCASTRANK) ? (r + 1) : SWIG_ERROR) : r;
-}
-SWIGINTERNINLINE int SWIG_CheckState(int r) { 
-  return SWIG_IsOK(r) ? SWIG_CastRank(r) + 1 : 0; 
-}
-#else /* no cast-rank mode */
-#  define SWIG_AddCast
-#  define SWIG_CheckState(r) (SWIG_IsOK(r) ? 1 : 0)
-#endif
-
-
-#include <string.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef void *(*swig_converter_func)(void *, int *);
-typedef struct swig_type_info *(*swig_dycast_func)(void **);
-
-/* Structure to store information on one type */
-typedef struct swig_type_info {
-  const char             *name;			/* mangled name of this type */
-  const char             *str;			/* human readable name of this type */
-  swig_dycast_func        dcast;		/* dynamic cast function down a hierarchy */
-  struct swig_cast_info  *cast;			/* linked list of types that can cast into this type */
-  void                   *clientdata;		/* language specific type data */
-  int                    owndata;		/* flag if the structure owns the clientdata */
-} swig_type_info;
-
-/* Structure to store a type and conversion function used for casting */
-typedef struct swig_cast_info {
-  swig_type_info         *type;			/* pointer to type that is equivalent to this type */
-  swig_converter_func     converter;		/* function to cast the void pointers */
-  struct swig_cast_info  *next;			/* pointer to next cast in linked list */
-  struct swig_cast_info  *prev;			/* pointer to the previous cast */
-} swig_cast_info;
-
-/* Structure used to store module information
- * Each module generates one structure like this, and the runtime collects
- * all of these structures and stores them in a circularly linked list.*/
-typedef struct swig_module_info {
-  swig_type_info         **types;		/* Array of pointers to swig_type_info structures that are in this module */
-  size_t                 size;		        /* Number of types in this module */
-  struct swig_module_info *next;		/* Pointer to next element in circularly linked list */
-  swig_type_info         **type_initial;	/* Array of initially generated type structures */
-  swig_cast_info         **cast_initial;	/* Array of initially generated casting structures */
-  void                    *clientdata;		/* Language specific module data */
-} swig_module_info;
-
-/* 
-  Compare two type names skipping the space characters, therefore
-  "char*" == "char *" and "Class<int>" == "Class<int >", etc.
-
-  Return 0 when the two name types are equivalent, as in
-  strncmp, but skipping ' '.
-*/
-SWIGRUNTIME int
-SWIG_TypeNameComp(const char *f1, const char *l1,
-		  const char *f2, const char *l2) {
-  for (;(f1 != l1) && (f2 != l2); ++f1, ++f2) {
-    while ((*f1 == ' ') && (f1 != l1)) ++f1;
-    while ((*f2 == ' ') && (f2 != l2)) ++f2;
-    if (*f1 != *f2) return (*f1 > *f2) ? 1 : -1;
-  }
-  return (int)((l1 - f1) - (l2 - f2));
-}
-
-/*
-  Check type equivalence in a name list like <name1>|<name2>|...
-  Return 0 if not equal, 1 if equal
-*/
-SWIGRUNTIME int
-SWIG_TypeEquiv(const char *nb, const char *tb) {
-  int equiv = 0;
-  const char* te = tb + strlen(tb);
-  const char* ne = nb;
-  while (!equiv && *ne) {
-    for (nb = ne; *ne; ++ne) {
-      if (*ne == '|') break;
-    }
-    equiv = (SWIG_TypeNameComp(nb, ne, tb, te) == 0) ? 1 : 0;
-    if (*ne) ++ne;
-  }
-  return equiv;
-}
-
-/*
-  Check type equivalence in a name list like <name1>|<name2>|...
-  Return 0 if equal, -1 if nb < tb, 1 if nb > tb
-*/
-SWIGRUNTIME int
-SWIG_TypeCompare(const char *nb, const char *tb) {
-  int equiv = 0;
-  const char* te = tb + strlen(tb);
-  const char* ne = nb;
-  while (!equiv && *ne) {
-    for (nb = ne; *ne; ++ne) {
-      if (*ne == '|') break;
-    }
-    equiv = (SWIG_TypeNameComp(nb, ne, tb, te) == 0) ? 1 : 0;
-    if (*ne) ++ne;
-  }
-  return equiv;
-}
-
-
-/*
-  Check the typename
-*/
-SWIGRUNTIME swig_cast_info *
-SWIG_TypeCheck(const char *c, swig_type_info *ty) {
-  if (ty) {
-    swig_cast_info *iter = ty->cast;
-    while (iter) {
-      if (strcmp(iter->type->name, c) == 0) {
-        if (iter == ty->cast)
-          return iter;
-        /* Move iter to the top of the linked list */
-        iter->prev->next = iter->next;
-        if (iter->next)
-          iter->next->prev = iter->prev;
-        iter->next = ty->cast;
-        iter->prev = 0;
-        if (ty->cast) ty->cast->prev = iter;
-        ty->cast = iter;
-        return iter;
-      }
-      iter = iter->next;
-    }
-  }
-  return 0;
-}
-
-/* 
-  Identical to SWIG_TypeCheck, except strcmp is replaced with a pointer comparison
-*/
-SWIGRUNTIME swig_cast_info *
-SWIG_TypeCheckStruct(swig_type_info *from, swig_type_info *ty) {
-  if (ty) {
-    swig_cast_info *iter = ty->cast;
-    while (iter) {
-      if (iter->type == from) {
-        if (iter == ty->cast)
-          return iter;
-        /* Move iter to the top of the linked list */
-        iter->prev->next = iter->next;
-        if (iter->next)
-          iter->next->prev = iter->prev;
-        iter->next = ty->cast;
-        iter->prev = 0;
-        if (ty->cast) ty->cast->prev = iter;
-        ty->cast = iter;
-        return iter;
-      }
-      iter = iter->next;
-    }
-  }
-  return 0;
-}
-
-/*
-  Cast a pointer up an inheritance hierarchy
-*/
-SWIGRUNTIMEINLINE void *
-SWIG_TypeCast(swig_cast_info *ty, void *ptr, int *newmemory) {
-  return ((!ty) || (!ty->converter)) ? ptr : (*ty->converter)(ptr, newmemory);
-}
-
-/* 
-   Dynamic pointer casting. Down an inheritance hierarchy
-*/
-SWIGRUNTIME swig_type_info *
-SWIG_TypeDynamicCast(swig_type_info *ty, void **ptr) {
-  swig_type_info *lastty = ty;
-  if (!ty || !ty->dcast) return ty;
-  while (ty && (ty->dcast)) {
-    ty = (*ty->dcast)(ptr);
-    if (ty) lastty = ty;
-  }
-  return lastty;
-}
-
-/*
-  Return the name associated with this type
-*/
-SWIGRUNTIMEINLINE const char *
-SWIG_TypeName(const swig_type_info *ty) {
-  return ty->name;
-}
-
-/*
-  Return the pretty name associated with this type,
-  that is an unmangled type name in a form presentable to the user.
-*/
-SWIGRUNTIME const char *
-SWIG_TypePrettyName(const swig_type_info *type) {
-  /* The "str" field contains the equivalent pretty names of the
-     type, separated by vertical-bar characters.  We choose
-     to print the last name, as it is often (?) the most
-     specific. */
-  if (!type) return NULL;
-  if (type->str != NULL) {
-    const char *last_name = type->str;
-    const char *s;
-    for (s = type->str; *s; s++)
-      if (*s == '|') last_name = s+1;
-    return last_name;
-  }
-  else
-    return type->name;
-}
-
-/* 
-   Set the clientdata field for a type
-*/
-SWIGRUNTIME void
-SWIG_TypeClientData(swig_type_info *ti, void *clientdata) {
-  swig_cast_info *cast = ti->cast;
-  /* if (ti->clientdata == clientdata) return; */
-  ti->clientdata = clientdata;
-  
-  while (cast) {
-    if (!cast->converter) {
-      swig_type_info *tc = cast->type;
-      if (!tc->clientdata) {
-	SWIG_TypeClientData(tc, clientdata);
-      }
-    }    
-    cast = cast->next;
-  }
-}
-SWIGRUNTIME void
-SWIG_TypeNewClientData(swig_type_info *ti, void *clientdata) {
-  SWIG_TypeClientData(ti, clientdata);
-  ti->owndata = 1;
-}
-  
-/*
-  Search for a swig_type_info structure only by mangled name
-  Search is a O(log #types)
-  
-  We start searching at module start, and finish searching when start == end.  
-  Note: if start == end at the beginning of the function, we go all the way around
-  the circular list.
-*/
-SWIGRUNTIME swig_type_info *
-SWIG_MangledTypeQueryModule(swig_module_info *start, 
-                            swig_module_info *end, 
-		            const char *name) {
-  swig_module_info *iter = start;
-  do {
-    if (iter->size) {
-      register size_t l = 0;
-      register size_t r = iter->size - 1;
-      do {
-	/* since l+r >= 0, we can (>> 1) instead (/ 2) */
-	register size_t i = (l + r) >> 1; 
-	const char *iname = iter->types[i]->name;
-	if (iname) {
-	  register int compare = strcmp(name, iname);
-	  if (compare == 0) {	    
-	    return iter->types[i];
-	  } else if (compare < 0) {
-	    if (i) {
-	      r = i - 1;
-	    } else {
-	      break;
-	    }
-	  } else if (compare > 0) {
-	    l = i + 1;
-	  }
-	} else {
-	  break; /* should never happen */
-	}
-      } while (l <= r);
-    }
-    iter = iter->next;
-  } while (iter != end);
-  return 0;
-}
-
-/*
-  Search for a swig_type_info structure for either a mangled name or a human readable name.
-  It first searches the mangled names of the types, which is a O(log #types)
-  If a type is not found it then searches the human readable names, which is O(#types).
-  
-  We start searching at module start, and finish searching when start == end.  
-  Note: if start == end at the beginning of the function, we go all the way around
-  the circular list.
-*/
-SWIGRUNTIME swig_type_info *
-SWIG_TypeQueryModule(swig_module_info *start, 
-                     swig_module_info *end, 
-		     const char *name) {
-  /* STEP 1: Search the name field using binary search */
-  swig_type_info *ret = SWIG_MangledTypeQueryModule(start, end, name);
-  if (ret) {
-    return ret;
-  } else {
-    /* STEP 2: If the type hasn't been found, do a complete search
-       of the str field (the human readable name) */
-    swig_module_info *iter = start;
-    do {
-      register size_t i = 0;
-      for (; i < iter->size; ++i) {
-	if (iter->types[i]->str && (SWIG_TypeEquiv(iter->types[i]->str, name)))
-	  return iter->types[i];
-      }
-      iter = iter->next;
-    } while (iter != end);
-  }
-  
-  /* neither found a match */
-  return 0;
-}
-
-/* 
-   Pack binary data into a string
-*/
-SWIGRUNTIME char *
-SWIG_PackData(char *c, void *ptr, size_t sz) {
-  static const char hex[17] = "0123456789abcdef";
-  register const unsigned char *u = (unsigned char *) ptr;
-  register const unsigned char *eu =  u + sz;
-  for (; u != eu; ++u) {
-    register unsigned char uu = *u;
-    *(c++) = hex[(uu & 0xf0) >> 4];
-    *(c++) = hex[uu & 0xf];
-  }
-  return c;
-}
-
-/* 
-   Unpack binary data from a string
-*/
-SWIGRUNTIME const char *
-SWIG_UnpackData(const char *c, void *ptr, size_t sz) {
-  register unsigned char *u = (unsigned char *) ptr;
-  register const unsigned char *eu = u + sz;
-  for (; u != eu; ++u) {
-    register char d = *(c++);
-    register unsigned char uu;
-    if ((d >= '0') && (d <= '9'))
-      uu = ((d - '0') << 4);
-    else if ((d >= 'a') && (d <= 'f'))
-      uu = ((d - ('a'-10)) << 4);
-    else 
-      return (char *) 0;
-    d = *(c++);
-    if ((d >= '0') && (d <= '9'))
-      uu |= (d - '0');
-    else if ((d >= 'a') && (d <= 'f'))
-      uu |= (d - ('a'-10));
-    else 
-      return (char *) 0;
-    *u = uu;
-  }
-  return c;
-}
-
-/* 
-   Pack 'void *' into a string buffer.
-*/
-SWIGRUNTIME char *
-SWIG_PackVoidPtr(char *buff, void *ptr, const char *name, size_t bsz) {
-  char *r = buff;
-  if ((2*sizeof(void *) + 2) > bsz) return 0;
-  *(r++) = '_';
-  r = SWIG_PackData(r,&ptr,sizeof(void *));
-  if (strlen(name) + 1 > (bsz - (r - buff))) return 0;
-  strcpy(r,name);
-  return buff;
-}
-
-SWIGRUNTIME const char *
-SWIG_UnpackVoidPtr(const char *c, void **ptr, const char *name) {
-  if (*c != '_') {
-    if (strcmp(c,"NULL") == 0) {
-      *ptr = (void *) 0;
-      return name;
-    } else {
-      return 0;
-    }
-  }
-  return SWIG_UnpackData(++c,ptr,sizeof(void *));
-}
-
-SWIGRUNTIME char *
-SWIG_PackDataName(char *buff, void *ptr, size_t sz, const char *name, size_t bsz) {
-  char *r = buff;
-  size_t lname = (name ? strlen(name) : 0);
-  if ((2*sz + 2 + lname) > bsz) return 0;
-  *(r++) = '_';
-  r = SWIG_PackData(r,ptr,sz);
-  if (lname) {
-    strncpy(r,name,lname+1);
-  } else {
-    *r = 0;
-  }
-  return buff;
-}
-
-SWIGRUNTIME const char *
-SWIG_UnpackDataName(const char *c, void *ptr, size_t sz, const char *name) {
-  if (*c != '_') {
-    if (strcmp(c,"NULL") == 0) {
-      memset(ptr,0,sz);
-      return name;
-    } else {
-      return 0;
-    }
-  }
-  return SWIG_UnpackData(++c,ptr,sz);
-}
-
-#ifdef __cplusplus
-}
-#endif
-
-/* -----------------------------------------------------------------------------
- * luarun.swg
- *
- * This file contains the runtime support for Lua modules
- * and includes code for managing global variables and pointer
- * type checking.
- * ----------------------------------------------------------------------------- */
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include "lua.h"
-#include "lauxlib.h"
-#include <stdlib.h>  /* for malloc */
-#include <assert.h>  /* for a few sanity tests */
-
-/* -----------------------------------------------------------------------------
- * Lua flavors
- * ----------------------------------------------------------------------------- */
-
-#define SWIG_LUA_FLAVOR_LUA 1
-#define SWIG_LUA_FLAVOR_ELUA 2
-#define SWIG_LUA_FLAVOR_ELUAC 3
-
-#if !defined(SWIG_LUA_TARGET)
-# error SWIG_LUA_TARGET not defined
-#endif
-
-#if (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC)
-#  define SWIG_LUA_CONSTTAB_INT(B, C) LSTRKEY(B), LNUMVAL(C)
-#  define SWIG_LUA_CONSTTAB_FLOAT(B, C) LSTRKEY(B), LNUMVAL(C)
-#  define SWIG_LUA_CONSTTAB_STRING(B, C) LSTRKEY(B), LSTRVAL(C)
-#  define SWIG_LUA_CONSTTAB_CHAR(B, C) LSTRKEY(B), LNUMVAL(C)
-#else /* SWIG_LUA_FLAVOR_LUA */
-#  define SWIG_LUA_CONSTTAB_INT(B, C) SWIG_LUA_INT, (char *)B, (long)C, 0, 0, 0
-#  define SWIG_LUA_CONSTTAB_FLOAT(B, C) SWIG_LUA_FLOAT, (char *)B, 0, (double)C, 0, 0
-#  define SWIG_LUA_CONSTTAB_STRING(B, C) SWIG_LUA_STRING, (char *)B, 0, 0, (void *)C, 0
-#  define SWIG_LUA_CONSTTAB_CHAR(B, C) SWIG_LUA_CHAR, (char *)B, (long)C, 0, 0, 0
-#endif
-
-#if (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC)
-#  define LRO_STRVAL(v) {{.p = (char *) v}, LUA_TSTRING}
-#  define LSTRVAL LRO_STRVAL
-#endif
-
-/* -----------------------------------------------------------------------------
- * compatibility defines
- * ----------------------------------------------------------------------------- */
-
-/* History of Lua C API length functions:  In Lua 5.0 (and before?)
-   there was "lua_strlen".  In Lua 5.1, this was renamed "lua_objlen",
-   but a compatibility define of "lua_strlen" was added.  In Lua 5.2,
-   this function was again renamed, to "lua_rawlen" (to emphasize that
-   it doesn't call the "__len" metamethod), and the compatibility
-   define of lua_strlen was removed.  All SWIG uses have been updated
-   to "lua_rawlen", and we add our own defines of that here for older
-   versions of Lua.  */
-#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501
-# define lua_rawlen lua_strlen
-#elif LUA_VERSION_NUM == 501
-# define lua_rawlen lua_objlen
-#endif
-
-
-/* lua_pushglobaltable is the recommended "future-proof" way to get
-   the global table for Lua 5.2 and later.  Here we define
-   lua_pushglobaltable ourselves for Lua versions before 5.2.  */
-#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 502
-# define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX)
-#endif
-
-
-/* -----------------------------------------------------------------------------
- * global swig types
- * ----------------------------------------------------------------------------- */
-/* Constant table */
-#define SWIG_LUA_INT     1
-#define SWIG_LUA_FLOAT   2
-#define SWIG_LUA_STRING  3
-#define SWIG_LUA_POINTER 4
-#define SWIG_LUA_BINARY  5
-#define SWIG_LUA_CHAR    6
-
-/* Structure for variable linking table */
-typedef struct {
-  const char *name;
-  lua_CFunction get;
-  lua_CFunction set;
-} swig_lua_var_info;
-
-/* Constant information structure */
-typedef struct {
-    int type;
-    char *name;
-    long lvalue;
-    double dvalue;
-    void   *pvalue;
-    swig_type_info **ptype;
-} swig_lua_const_info;
-
-typedef struct {
-  const char     *name;
-  lua_CFunction   method;
-} swig_lua_method;
-
-typedef struct {
-  const char     *name;
-  lua_CFunction   getmethod;
-  lua_CFunction   setmethod;
-} swig_lua_attribute;
-
-typedef struct swig_lua_class {
-  const char    *name;
-  swig_type_info   **type;
-  lua_CFunction  constructor;
-  void    (*destructor)(void *);
-  swig_lua_method   *methods;
-  swig_lua_attribute     *attributes;
-  struct swig_lua_class **bases;
-  const char **base_names;
-} swig_lua_class;
-
-/* this is the struct for wrapping all pointers in SwigLua
-*/
-typedef struct {
-  swig_type_info   *type;
-  int     own;  /* 1 if owned & must be destroyed */
-  void        *ptr;
-} swig_lua_userdata;
-
-/* this is the struct for wrapping arbitrary packed binary data
-(currently it is only used for member function pointers)
-the data ordering is similar to swig_lua_userdata, but it is currently not possible
-to tell the two structures apart within SWIG, other than by looking at the type
-*/
-typedef struct {
-  swig_type_info   *type;
-  int     own;  /* 1 if owned & must be destroyed */
-  char data[1];       /* arbitary amount of data */    
-} swig_lua_rawdata;
-
-/* Common SWIG API */
-#define SWIG_NewPointerObj(L, ptr, type, owner)       SWIG_Lua_NewPointerObj(L, (void *)ptr, type, owner)
-#define SWIG_ConvertPtr(L,idx, ptr, type, flags)    SWIG_Lua_ConvertPtr(L,idx,ptr,type,flags)
-#define SWIG_MustGetPtr(L,idx, type,flags, argnum,fnname)  SWIG_Lua_MustGetPtr(L,idx, type,flags, argnum,fnname)
-/* for C++ member pointers, ie, member methods */
-#define SWIG_ConvertMember(L, idx, ptr, sz, ty)       SWIG_Lua_ConvertPacked(L, idx, ptr, sz, ty)
-#define SWIG_NewMemberObj(L, ptr, sz, type)      SWIG_Lua_NewPackedObj(L, ptr, sz, type)
-
-/* Runtime API */
-#define SWIG_GetModule(clientdata) SWIG_Lua_GetModule((lua_State*)(clientdata))
-#define SWIG_SetModule(clientdata, pointer) SWIG_Lua_SetModule((lua_State*) (clientdata), pointer)
-#define SWIG_MODULE_CLIENTDATA_TYPE lua_State*
-
-/* Contract support */
-#define SWIG_contract_assert(expr, msg)  \
-  if (!(expr)) { lua_pushstring(L, (char *) msg); goto fail; } else
-
-/* helper #defines */
-#define SWIG_fail {goto fail;}
-#define SWIG_fail_arg(func_name,argnum,type) \
-  {lua_pushfstring(L,"Error in %s (arg %d), expected '%s' got '%s'",\
-  func_name,argnum,type,SWIG_Lua_typename(L,argnum));\
-  goto fail;}
-#define SWIG_fail_ptr(func_name,argnum,type) \
-  SWIG_fail_arg(func_name,argnum,(type && type->str)?type->str:"void*")
-#define SWIG_check_num_args(func_name,a,b) \
-  if (lua_gettop(L)<a || lua_gettop(L)>b) \
-  {lua_pushfstring(L,"Error in %s expected %d..%d args, got %d",func_name,a,b,lua_gettop(L));\
-  goto fail;}
-
-
-#define SWIG_Lua_get_table(L,n) \
-  (lua_pushstring(L, n), lua_rawget(L,-2))
-
-#define SWIG_Lua_add_function(L,n,f) \
-  (lua_pushstring(L, n), \
-      lua_pushcfunction(L, f), \
-      lua_rawset(L,-3))
-
-/* special helper for allowing 'nil' for usertypes */
-#define SWIG_isptrtype(L,I) (lua_isuserdata(L,I) || lua_isnil(L,I))
-
-#ifdef __cplusplus
-/* Special helper for member function pointers 
-it gets the address, casts it, then dereferences it */
-//#define SWIG_mem_fn_as_voidptr(a)  (*((char**)&(a)))
-#endif
-
-/* storing/access of swig_module_info */
-SWIGRUNTIME swig_module_info *
-SWIG_Lua_GetModule(lua_State* L) {
-  swig_module_info *ret = 0;
-  lua_pushstring(L,"swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME);
-  lua_rawget(L,LUA_REGISTRYINDEX);
-  if (lua_islightuserdata(L,-1))
-    ret=(swig_module_info*)lua_touserdata(L,-1);
-  lua_pop(L,1);  /* tidy */
-  return ret;
-}
-
-SWIGRUNTIME void
-SWIG_Lua_SetModule(lua_State* L, swig_module_info *module) {
-  /* add this all into the Lua registry: */
-  lua_pushstring(L,"swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME);
-  lua_pushlightuserdata(L,(void*)module);
-  lua_rawset(L,LUA_REGISTRYINDEX);
-}
-
-/* -----------------------------------------------------------------------------
- * global variable support code: modules
- * ----------------------------------------------------------------------------- */
-
-/* this function is called when trying to set an immutable.
-default action is to print an error.
-This can removed with a compile flag SWIGLUA_IGNORE_SET_IMMUTABLE */
-SWIGINTERN int SWIG_Lua_set_immutable(lua_State* L)
-{
-/*  there should be 1 param passed in: the new value */
-#ifndef SWIGLUA_IGNORE_SET_IMMUTABLE
-  lua_pop(L,1);  /* remove it */
-  lua_pushstring(L,"This variable is immutable");
-  lua_error(L);
-#endif
-    return 0;   /* should not return anything */
-}
-
-/* the module.get method used for getting linked data */
-SWIGINTERN int SWIG_Lua_module_get(lua_State* L)
-{
-/*  there should be 2 params passed in
-  (1) table (not the meta table)
-  (2) string name of the attribute
-  printf("SWIG_Lua_module_get %p(%s) '%s'\n",
-   lua_topointer(L,1),lua_typename(L,lua_type(L,1)),
-   lua_tostring(L,2));
-*/
-  /* get the metatable */
-#if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
-  assert(lua_isrotable(L,1)); /* just in case */
-#else
-  assert(lua_istable(L,1)); /* default Lua action */
-#endif
-  lua_getmetatable(L,1);  /* get the metatable */
-#if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
-  assert(lua_isrotable(L,-1));  /* just in case */
-#else
-  assert(lua_istable(L,-1));
-#endif
-  SWIG_Lua_get_table(L,".get");  /* get the .get table */
-  lua_remove(L,3);  /* remove metatable */
-#if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
-  if (lua_isrotable(L,-1))
-#else
-  if (lua_istable(L,-1))
-#endif
-  {
-    /* look for the key in the .get table */
-    lua_pushvalue(L,2);  /* key */
-    lua_rawget(L,-2);
-    lua_remove(L,3);  /* remove .get */
-    if (lua_iscfunction(L,-1))
-    {  /* found it so call the fn & return its value */
-      lua_call(L,0,1);
-      return 1;
-    }
-    lua_pop(L,1);  /* remove the top */
-  }
-  lua_pop(L,1);  /* remove the .get */
-  lua_pushnil(L);  /* return a nil */
-  return 1;
-}
-
-/* the module.set method used for setting linked data */
-SWIGINTERN int SWIG_Lua_module_set(lua_State* L)
-{
-/*  there should be 3 params passed in
-  (1) table (not the meta table)
-  (2) string name of the attribute
-  (3) any for the new value
-*/
-  /* get the metatable */
-#if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
-  assert(lua_isrotable(L,1));  /* just in case */
-#else
-  assert(lua_istable(L,1)); /* default Lua action */
-#endif
-  lua_getmetatable(L,1);  /* get the metatable */
-#if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
-  assert(lua_isrotable(L,-1));  /* just in case */
-#else
-  assert(lua_istable(L,-1));
-#endif
-  SWIG_Lua_get_table(L,".set");  /* get the .set table */
-  lua_remove(L,4);  /* remove metatable */
-#if ((SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) || (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUAC))
-  if (lua_isrotable(L,-1))
-#else
-  if (lua_istable(L,-1))
-#endif
-  {
-    /* look for the key in the .set table */
-    lua_pushvalue(L,2);  /* key */
-    lua_rawget(L,-2);
-    lua_remove(L,4);  /* remove .set */
-    if (lua_iscfunction(L,-1))
-    {  /* found it so call the fn & return its value */
-      lua_pushvalue(L,3);  /* value */
-      lua_call(L,1,0);
-      return 0;
-    }
-#if (SWIG_LUA_TARGET == SWIG_LUA_FLAVOR_ELUA) 
-    else {
-      return 0; // Exits stoically if an invalid key is initialized.
-    }
-#endif
-  }
-  lua_settop(L,3);  /* reset back to start */
-  /* we now have the table, key & new value, so just set directly */
-  lua_rawset(L,1);  /* add direct */
-  return 0;
-}
-
-#if ((SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUA) && (SWIG_LUA_TARGET != SWIG_LUA_FLAVOR_ELUAC))
-/* registering a module in lua. Pushes the module table on the stack. */
-SWIGINTERN void  SWIG_Lua_module_begin(lua_State* L,const char* name)
-{
-  assert(lua_istable(L,-1));  /* just in case */
-  lua_pushstring(L,name);
-  lua_newtable(L);   /* the table */
-  /* add meta table */
-  lua_newtable(L);    /* the meta table */
-  SWIG_Lua_add_function(L,"__index",SWIG_Lua_module_get);
-  SWIG_Lua_add_function(L,"__newindex",SWIG_Lua_module_set);
-  lua_pushstring(L,".get");
-  lua_newtable(L);    /* the .get table */
-  lua_rawset(L,-3);  /* add .get into metatable */
-  lua_pushstring(L,".set");
-  lua_newtable(L);    /* the .set table */
-  lua_rawset(L,-3);  /* add .set into metatable */
-  lua_setmetatable(L,-2);  /* sets meta table in module */
-#ifdef SWIG_LUA_MODULE_GLOBAL
-  /* If requested, install the module directly into the global namespace. */
-  lua_rawset(L,-3);        /* add module into parent */
-  SWIG_Lua_get_table(L,name);   /* get the table back out */
-#else
-  /* Do not install the module table as global name. The stack top has
-     the module table with the name below. We pop the top and replace
-     the name with it. */
-  lua_replace(L,-2);
-#endif
-}
-
-/* ending the register */
-SWIGINTERN void  SWIG_Lua_module_end(lua_State* L)
-{
-  lua_pop(L,1);       /* tidy stack (remove module) */
-}
-
-/* adding a linked variable to the module */
-SWIGINTERN void SWIG_Lua_module_add_variable(lua_State* L,const char* name,lua_CFunction getFn,lua_CFunction setFn)
-{
-  assert(lua_istable(L,-1));  /* just in case */
-  lua_getmetatable(L,-1);  /* get the metatable */
-  assert(lua_istable(L,-1));  /* just in case */
-  SWIG_Lua_get_table(L,".get"); /* find the .get table */
-  assert(lua_istable(L,-1));  /* should be a table: */
-  SWIG_Lua_add_function(L,name,getFn);
-  lua_pop(L,1);       /* tidy stack (remove table) */
-  if (setFn)  /* if there is a set fn */
-  {
-    SWIG_Lua_get_table(L,".set"); /* find the .set table */
-    assert(lua_istable(L,-1));  /* should be a table: */
-    SWIG_Lua_add_function(L,name,setFn);
-    lua_pop(L,1);       /* tidy stack (remove table) */
-  }
-  lua_pop(L,1);       /* tidy stack (remove meta) */
-}
-#endif
-
-/* adding a function module */
-SWIGINTERN void  SWIG_Lua_module_add_function(lua_State* L,const char* name,lua_CFunction fn)
-{
-  SWIG_Lua_add_function(L,name,fn);
-}
-
-/* -----------------------------------------------------------------------------
- * global variable support code: classes
- * ----------------------------------------------------------------------------- */
-
-/* the class.get method, performs the lookup of class attributes */
-SWIGINTERN int  SWIG_Lua_class_get(lua_State* L)
-{
-/*  there should be 2 params passed in
-  (1) userdata (not the meta table)
-  (2) string name of the attribute
-*/
-  assert(lua_isuserdata(L,-2));  /* just in case */
-  lua_getmetatable(L,-2);    /* get the meta table */
-  assert(lua_istable(L,-1));  /* just in case */
-  SWIG_Lua_get_table(L,".get"); /* find the .get table */
-  assert(lua_istable(L,-1));  /* just in case */
-  /* look for the key in the .get table */
-  lua_pushvalue(L,2);  /* key */
-  lua_rawget(L,-2);
-  lua_remove(L,-2); /* stack tidy, remove .get table */
-  if (lua_iscfunction(L,-1))
-  {  /* found it so call the fn & return its value */
-    lua_pushvalue(L,1);  /* the userdata */
-    lua_call(L,1,1);  /* 1 value in (userdata),1 out (result) */
-    lua_remove(L,-2); /* stack tidy, remove metatable */
-    return 1;
-  }
-  lua_pop(L,1);  /* remove whatever was there */
-  /* ok, so try the .fn table */
-  SWIG_Lua_get_table(L,".fn"); /* find the .get table */
-  assert(lua_istable(L,-1));  /* just in case */
-  lua_pushvalue(L,2);  /* key */
-  lua_rawget(L,-2);  /* look for the fn */
-  lua_remove(L,-2); /* stack tidy, remove .fn table */
-  if (lua_isfunction(L,-1)) /* note: if its a C function or lua function */
-  {  /* found it so return the fn & let lua call it */
-    lua_remove(L,-2); /* stack tidy, remove metatable */
-    return 1;
-  }
-  lua_pop(L,1);  /* remove whatever was there */
-  /* NEW: looks for the __getitem() fn
-  this is a user provided get fn */
-  SWIG_Lua_get_table(L,"__getitem"); /* find the __getitem fn */
-  if (lua_iscfunction(L,-1))  /* if its there */
-  {  /* found it so call the fn & return its value */
-    lua_pushvalue(L,1);  /* the userdata */
-    lua_pushvalue(L,2);  /* the parameter */
-    lua_call(L,2,1);  /* 2 value in (userdata),1 out (result) */
-    lua_remove(L,-2); /* stack tidy, remove metatable */
-    return 1;
-  }
-  return 0;  /* sorry not known */
-}
-
-/* the class.set method, performs the lookup of class attributes */
-SWIGINTERN int  SWIG_Lua_class_set(lua_State* L)
-{
-/*  there should be 3 params passed in
-  (1) table (not the meta table)
-  (2) string name of the attribute
-  (3) any for the new value
-printf("SWIG_Lua_class_set %p(%s) '%s' %p(%s)\n",
-      lua_topointer(L,1),lua_typename(L,lua_type(L,1)),
-      lua_tostring(L,2),
-      lua_topointer(L,3),lua_typename(L,lua_type(L,3)));*/
-
-  assert(lua_isuserdata(L,1));  /* just in case */
-  lua_getmetatable(L,1);    /* get the meta table */
-  assert(lua_istable(L,-1));  /* just in case */
-
-  SWIG_Lua_get_table(L,".set"); /* find the .set table */
-  if (lua_istable(L,-1))
-  {
-    /* look for the key in the .set table */
-    lua_pushvalue(L,2);  /* key */
-    lua_rawget(L,-2);
-    if (lua_iscfunction(L,-1))
-    {  /* found it so call the fn & return its value */
-      lua_pushvalue(L,1);  /* userdata */
-      lua_pushvalue(L,3);  /* value */
-      lua_call(L,2,0);
-      return 0;
-    }
-    lua_pop(L,1);  /* remove the value */
-  }
-  lua_pop(L,1);  /* remove the value .set table */
-  /* NEW: looks for the __setitem() fn
-  this is a user provided set fn */
-  SWIG_Lua_get_table(L,"__setitem"); /* find the fn */
-  if (lua_iscfunction(L,-1))  /* if its there */
-  {  /* found it so call the fn & return its value */
-    lua_pushvalue(L,1);  /* the userdata */
-    lua_pushvalue(L,2);  /* the parameter */
-    lua_pushvalue(L,3);  /* the value */
-    lua_call(L,3,0);  /* 3 values in ,0 out */
-    lua_remove(L,-2); /* stack tidy, remove metatable */
-    return 1;
-  }
-  return 0;
-}
-
-/* the class.destruct method called by the interpreter */
-SWIGINTERN int  SWIG_Lua_class_destruct(lua_State* L)
-{
-/*  there should be 1 params passed in
-  (1) userdata (not the meta table) */
-  swig_lua_userdata* usr;
-  swig_lua_class* clss;
-  assert(lua_isuserdata(L,-1));  /* just in case */
-  usr=(swig_lua_userdata*)lua_touserdata(L,-1);  /* get it */
-  /* if must be destroyed & has a destructor */
-  if (usr->own) /* if must be destroyed */
-  {
-    clss=(swig_lua_class*)usr->type->clientdata;  /* get the class */
-    if (clss && clss->destructor)  /* there is a destroy fn */
-    {
-      clss->destructor(usr->ptr);  /* bye bye */
-    }
-  }
-  return 0;
-}
-
-/* the class.__tostring method called by the interpreter and print */
-SWIGINTERN int  SWIG_Lua_class_tostring(lua_State* L)
-{
-/*  there should be 1 param passed in
-  (1) userdata (not the metatable) */
-  assert(lua_isuserdata(L,1));  /* just in case */
-  unsigned long userData = (unsigned long)lua_touserdata(L,1); /* get the userdata address for later */
-  lua_getmetatable(L,1);    /* get the meta table */
-  assert(lua_istable(L,-1));  /* just in case */
-  
-  lua_getfield(L, -1, ".type");
-  const char* className = lua_tostring(L, -1);
-  
-  char output[256];
-  sprintf(output, "<%s userdata: %lX>", className, userData);
-  
-  lua_pushstring(L, (const char*)output);
-  return 1;
-}
-
-/* to manually disown some userdata */
-SWIGINTERN int  SWIG_Lua_class_disown(lua_State* L)
-{
-/*  there should be 1 params passed in
-  (1) userdata (not the meta table) */
-  swig_lua_userdata* usr;
-  assert(lua_isuserdata(L,-1));  /* just in case */
-  usr=(swig_lua_userdata*)lua_touserdata(L,-1);  /* get it */
-  
-  usr->own = 0; /* clear our ownership */
-  return 0;
-}
-
-/* gets the swig class registry (or creates it) */
-SWIGINTERN void  SWIG_Lua_get_class_registry(lua_State* L)
-{
-  /* add this all into the swig registry: */
-  lua_pushstring(L,"SWIG");
-  lua_rawget(L,LUA_REGISTRYINDEX);  /* get the registry */
-  if (!lua_istable(L,-1))  /* not there */
-  {  /* must be first time, so add it */
-    lua_pop(L,1);  /* remove the result */
-    lua_pushstring(L,"SWIG");
-    lua_newtable(L);
-    lua_rawset(L,LUA_REGISTRYINDEX);
-    /* then get it */
-    lua_pushstring(L,"SWIG");
-    lua_rawget(L,LUA_REGISTRYINDEX);
-  }
-}
-
-/* helper fn to get the classes metatable from the register */
-SWIGINTERN void  SWIG_Lua_get_class_metatable(lua_State* L,const char* cname)
-{
-  SWIG_Lua_get_class_registry(L);  /* get the registry */
-  lua_pushstring(L,cname);  /* get the name */
-  lua_rawget(L,-2);    /* get it */
-  lua_remove(L,-2);    /* tidy up (remove registry) */
-}
-
-/* helper add a variable to a registered class */
-SWIGINTERN void  SWIG_Lua_add_class_variable(lua_State* L,const char* name,lua_CFunction getFn,lua_CFunction setFn)
-{
-  assert(lua_istable(L,-1));  /* just in case */
-  SWIG_Lua_get_table(L,".get"); /* find the .get table */
-  assert(lua_istable(L,-1));  /* just in case */
-  SWIG_Lua_add_function(L,name,getFn);
-  lua_pop(L,1);       /* tidy stack (remove table) */
-  if (setFn)
-  {
-    SWIG_Lua_get_table(L,".set"); /* find the .set table */
-    assert(lua_istable(L,-1));  /* just in case */
-    SWIG_Lua_add_function(L,name,setFn);
-    lua_pop(L,1);       /* tidy stack (remove table) */
-  }
-}
-
-/* helper to recursively add class details (attributes & operations) */
-SWIGINTERN void  SWIG_Lua_add_class_details(lua_State* L,swig_lua_class* clss)
-{
-  int i;
-  /* call all the base classes first: we can then override these later: */
-  for(i=0;clss->bases[i];i++)
-  {
-    SWIG_Lua_add_class_details(L,clss->bases[i]);
-  }
-  /* add fns */
-  for(i=0;clss->attributes[i].name;i++){
-    SWIG_Lua_add_class_variable(L,clss->attributes[i].name,clss->attributes[i].getmethod,clss->attributes[i].setmethod);
-  }
-  /* add methods to the metatable */
-  SWIG_Lua_get_table(L,".fn"); /* find the .fn table */
-  assert(lua_istable(L,-1));  /* just in case */
-  for(i=0;clss->methods[i].name;i++){
-    SWIG_Lua_add_function(L,clss->methods[i].name,clss->methods[i].method);
-  }
-  lua_pop(L,1);       /* tidy stack (remove table) */
-  /*   add operator overloads
-    these look ANY method which start with "__" and assume they
-    are operator overloads & add them to the metatable
-    (this might mess up is someone defines a method __gc (the destructor)*/
-  for(i=0;clss->methods[i].name;i++){
-    if (clss->methods[i].name[0]=='_' && clss->methods[i].name[1]=='_'){
-      SWIG_Lua_add_function(L,clss->methods[i].name,clss->methods[i].method);
-    }
-  }
-}
-
-/* set up the base classes pointers.
-Each class structure has a list of pointers to the base class structures.
-This function fills them.
-It cannot be done at compile time, as this will not work with hireachies
-spread over more than one swig file. 
-Therefore it must be done at runtime, querying the SWIG type system.
-*/
-SWIGINTERN void SWIG_Lua_init_base_class(lua_State* L,swig_lua_class* clss)
-{
-  int i=0;
-  swig_module_info* module=SWIG_GetModule(L);
-  for(i=0;clss->base_names[i];i++)
-  {
-    if (clss->bases[i]==0) /* not found yet */
-    {