Revision: 48705
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=48705
Author:   moguri
Date:     2012-07-07 04:12:55 +0000 (Sat, 07 Jul 2012)
Log Message:
-----------
LibLoad() now returns a KX_LibLoadStatus object that can be used to monitor the 
state of an async libload as well as register callbacks.

Modified Paths:
--------------
    branches/soc-2012-swiss_cheese/source/gameengine/Converter/CMakeLists.txt
    
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_BlenderSceneConverter.cpp
    
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_BlenderSceneConverter.h
    branches/soc-2012-swiss_cheese/source/gameengine/Ketsji/KX_PythonInit.cpp
    
branches/soc-2012-swiss_cheese/source/gameengine/Ketsji/KX_PythonInitTypes.cpp

Added Paths:
-----------
    
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_LibLoadStatus.cpp
    
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_LibLoadStatus.h

Modified: 
branches/soc-2012-swiss_cheese/source/gameengine/Converter/CMakeLists.txt
===================================================================
--- branches/soc-2012-swiss_cheese/source/gameengine/Converter/CMakeLists.txt   
2012-07-07 04:11:00 UTC (rev 48704)
+++ branches/soc-2012-swiss_cheese/source/gameengine/Converter/CMakeLists.txt   
2012-07-07 04:12:55 UTC (rev 48705)
@@ -84,6 +84,7 @@
        KX_ConvertProperties.cpp
        KX_ConvertSensors.cpp
        KX_IpoConvert.cpp
+       KX_LibLoadStatus.cpp
        KX_SoftBodyDeformer.cpp
 
        BL_ActionActuator.h
@@ -106,6 +107,7 @@
        KX_ConvertProperties.h
        KX_ConvertSensors.h
        KX_IpoConvert.h
+       KX_LibLoadStatus.h
        KX_SoftBodyDeformer.h
 )
 

Modified: 
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_BlenderSceneConverter.cpp
===================================================================
--- 
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_BlenderSceneConverter.cpp
     2012-07-07 04:11:00 UTC (rev 48704)
+++ 
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_BlenderSceneConverter.cpp
     2012-07-07 04:12:55 UTC (rev 48705)
@@ -61,6 +61,7 @@
 #endif
 
 #include "KX_BlenderSceneConverter.h"
+#include "KX_LibLoadStatus.h"
 #include "KX_BlenderScalarInterpolator.h"
 #include "BL_BlenderDataConversion.h"
 #include "BlenderWorldInfo.h"
@@ -953,9 +954,7 @@
 void KX_BlenderSceneConverter::MergeAsyncLoads()
 {
        vector<pair<KX_Scene*,KX_Scene*> >::iterator sit;
-       for (sit=m_mergequeue.begin(); sit!=m_mergequeue.end(); ++sit)
-       {
-               printf("Merging scene: %s\n", 
(*sit).first->GetName().ReadPtr());
+       for (sit=m_mergequeue.begin(); sit!=m_mergequeue.end(); ++sit) {
                (*sit).first->MergeScene((*sit).second);
                delete (*sit).second;
        }
@@ -968,23 +967,29 @@
        m_mergequeue.push_back(pair<KX_Scene*,KX_Scene*>(merge_scene, other));
 }
 
-typedef struct {KX_BlenderSceneConverter *converter; KX_KetsjiEngine *engine; 
Scene *scene; KX_Scene *merge_scene;} async_args;
-void *async_convert(void *ptr)
+static void *async_convert(void *ptr)
 {
-       int cleanedup=0;
-       KX_Scene *new_scene=NULL;
-       async_args *args = (async_args*)ptr;
+       KX_Scene *new_scene = NULL;
+       KX_LibLoadStatus *status = (KX_LibLoadStatus*)ptr;
+       vector<Scene*> *scenes = (vector<Scene*>*)status->GetData();
 
-       new_scene = args->engine->CreateScene(args->scene);
+       for (unsigned int i=0; i<scenes->size(); ++i) {
+               new_scene = status->GetEngine()->CreateScene((*scenes)[i]);
 
-       if (new_scene)
-               args->converter->AddScenesToMergeQueue(args->merge_scene, 
new_scene);
+               if (new_scene)
+                       
status->GetConverter()->AddScenesToMergeQueue(status->GetMergeScene(), 
new_scene);
 
-       delete args;
+               status->AddProgress(1.f/scenes->size());
+       }
+
+       status->Finish();
+
+       delete status->GetData();
+       status->SetData(NULL);
        return NULL;
 }
 
-bool KX_BlenderSceneConverter::LinkBlendFileMemory(void *data, int length, 
const char *path, char *group, KX_Scene *scene_merge, char **err_str, short 
options)
+KX_LibLoadStatus *KX_BlenderSceneConverter::LinkBlendFileMemory(void *data, 
int length, const char *path, char *group, KX_Scene *scene_merge, char 
**err_str, short options)
 {
        BlendHandle *bpy_openlib = BLO_blendhandle_from_memory(data, length);
 
@@ -992,7 +997,7 @@
        return LinkBlendFile(bpy_openlib, path, group, scene_merge, err_str, 
options);
 }
 
-bool KX_BlenderSceneConverter::LinkBlendFilePath(const char *path, char 
*group, KX_Scene *scene_merge, char **err_str, short options)
+KX_LibLoadStatus *KX_BlenderSceneConverter::LinkBlendFilePath(const char 
*path, char *group, KX_Scene *scene_merge, char **err_str, short options)
 {
        BlendHandle *bpy_openlib = BLO_blendhandle_from_file((char *)path, 
NULL);
 
@@ -1024,32 +1029,33 @@
        BLO_library_append_end(NULL, main_tmp, &bpy_openlib, idcode, flag);
 }
 
-bool KX_BlenderSceneConverter::LinkBlendFile(BlendHandle *bpy_openlib, const 
char *path, char *group, KX_Scene *scene_merge, char **err_str, short options)
+KX_LibLoadStatus *KX_BlenderSceneConverter::LinkBlendFile(BlendHandle 
*bpy_openlib, const char *path, char *group, KX_Scene *scene_merge, char 
**err_str, short options)
 {
        Main *main_newlib; /* stored as a dynamic 'main' until we free it */
        int idcode= BKE_idcode_from_name(group);
        ReportList reports;
        static char err_local[255];
+       KX_LibLoadStatus *status;
        
        /* only scene and mesh supported right now */
        if (idcode!=ID_SCE && idcode!=ID_ME &&idcode!=ID_AC) {
                snprintf(err_local, sizeof(err_local), "invalid ID type given 
\"%s\"\n", group);
                *err_str= err_local;
                BLO_blendhandle_close(bpy_openlib);
-               return false;
+               return NULL;
        }
        
        if (GetMainDynamicPath(path)) {
                snprintf(err_local, sizeof(err_local), "blend file already open 
\"%s\"\n", path);
                *err_str= err_local;
                BLO_blendhandle_close(bpy_openlib);
-               return false;
+               return NULL;
        }
 
        if (bpy_openlib==NULL) {
                snprintf(err_local, sizeof(err_local), "could not open 
blendfile \"%s\"\n", path);
                *err_str= err_local;
-               return false;
+               return NULL;
        }
        
        main_newlib= (Main *)MEM_callocN( sizeof(Main), "BgeMain");
@@ -1077,6 +1083,8 @@
        strncpy(main_newlib->name, path, sizeof(main_newlib->name));    
        
        
+       status = new KX_LibLoadStatus(this, m_ketsjiEngine, scene_merge, path);
+
        if (idcode==ID_ME) {
                /* Convert all new meshes into BGE meshes */
                ID* mesh;
@@ -1101,23 +1109,16 @@
        else if (idcode==ID_SCE) {              
                /* Merge all new linked in scene into the existing one */
                ID *scene;
+               // scenes gets deleted by the thread when it's done using it 
(look in async_convert())
+               vector<Scene*> *scenes =  (options & LIB_LOAD_ASYNC) ? new 
vector<Scene*>() : NULL;
+
                for (scene= (ID *)main_newlib->scene.first; scene; scene= (ID 
*)scene->next ) {
                        if (options & LIB_LOAD_VERBOSE)
                                printf("SceneName: %s\n", scene->name+2);
                        
-                       if (options & LIB_LOAD_ASYNC)
-                       {
-                               pthread_t id;
-                               async_args *args = new async_args(); // Gets 
deleted in the thread
-                               args->converter = this;
-                               args->engine = m_ketsjiEngine;
-                               args->scene = (Scene*)scene;
-                               args->merge_scene = scene_merge;
-                               pthread_create(&id, NULL, &async_convert, 
(void*)args);
-                               m_threadinfo->threads.push_back(id);
-                       }
-                       else 
-                       {
+                       if (options & LIB_LOAD_ASYNC) {
+                               scenes->push_back((Scene*)scene);
+                       } else {
                                /* merge into the base  scene */
                                KX_Scene* other= 
m_ketsjiEngine->CreateScene((Scene *)scene);
                                scene_merge->MergeScene(other);
@@ -1127,8 +1128,14 @@
                        }
                }
 
+               if (options & LIB_LOAD_ASYNC) {
+                       pthread_t id;
+                       status->SetData(scenes);
+                       pthread_create(&id, NULL, &async_convert, 
(void*)status);
+                       m_threadinfo->threads.push_back(id);
+               }
+
                /* Handle any text datablocks */
-
                addImportMain(main_newlib);
 
                /* Now handle all the actions */
@@ -1143,7 +1150,7 @@
                }
        }
        
-       return true;
+       return status;
 }
 
 /* Note m_map_*** are all ok and don't need to be freed

Modified: 
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_BlenderSceneConverter.h
===================================================================
--- 
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_BlenderSceneConverter.h
       2012-07-07 04:11:00 UTC (rev 48704)
+++ 
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_BlenderSceneConverter.h
       2012-07-07 04:12:55 UTC (rev 48705)
@@ -162,9 +162,9 @@
        struct Main*              GetMainDynamicPath(const char *path);
        vector<struct Main*> &GetMainDynamic();
        
-       bool LinkBlendFileMemory(void *data, int length, const char *path, char 
*group, KX_Scene *scene_merge, char **err_str, short options);
-       bool LinkBlendFilePath(const char *path, char *group, KX_Scene 
*scene_merge, char **err_str, short options);
-       bool LinkBlendFile(struct BlendHandle *bpy_openlib, const char *path, 
char *group, KX_Scene *scene_merge, char **err_str, short options);
+       class KX_LibLoadStatus *LinkBlendFileMemory(void *data, int length, 
const char *path, char *group, KX_Scene *scene_merge, char **err_str, short 
options);
+       class KX_LibLoadStatus *LinkBlendFilePath(const char *path, char 
*group, KX_Scene *scene_merge, char **err_str, short options);
+       class KX_LibLoadStatus *LinkBlendFile(struct BlendHandle *bpy_openlib, 
const char *path, char *group, KX_Scene *scene_merge, char **err_str, short 
options);
        bool MergeScene(KX_Scene *to, KX_Scene *from);
        RAS_MeshObject *ConvertMeshSpecial(KX_Scene* kx_scene, Main *maggie, 
const char *name);
        bool FreeBlendFile(struct Main *maggie);

Added: 
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_LibLoadStatus.cpp
===================================================================
--- 
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_LibLoadStatus.cpp 
                            (rev 0)
+++ 
branches/soc-2012-swiss_cheese/source/gameengine/Converter/KX_LibLoadStatus.cpp 
    2012-07-07 04:12:55 UTC (rev 48705)
@@ -0,0 +1,249 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Mitchell Stokes
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file KX_LibLoadStatus.cpp
+ *  \ingroup bgeconv
+ */
+
+#include "KX_LibLoadStatus.h"
+#include "PIL_time.h"
+
+KX_LibLoadStatus::KX_LibLoadStatus(class KX_BlenderSceneConverter* 
kx_converter,
+                               class KX_KetsjiEngine* kx_engine,
+                               class KX_Scene* merge_scene,
+                               const char *path) :
+                       m_converter(kx_converter),
+                       m_engine(kx_engine),
+                       m_mergescene(merge_scene),
+                       m_data(NULL),
+                       m_libname(path),
+                       m_refcount(1),
+                       m_progress(0.f),
+#ifdef WITH_PYTHON
+                       m_finish_cb(NULL),
+                       m_progress_cb(NULL)
+#endif
+{
+       m_endtime = m_starttime = PIL_check_seconds_timer();
+}
+
+void KX_LibLoadStatus::Finish()
+{
+       m_progress = 1.f;
+       m_endtime = PIL_check_seconds_timer();
+
+       RunFinishCallback();
+       RunProgressCallback();
+}
+
+void KX_LibLoadStatus::RunFinishCallback()
+{
+#ifdef WITH_PYTHON

@@ Diff output truncated at 10240 characters. @@
_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to