Initial revision
[blender.git] / source / gameengine / Converter / KX_BlenderSceneConverter.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #ifdef WIN32
33
34         #pragma warning (disable:4786) // suppress stl-MSVC debug info warning
35 #endif
36
37
38 #include "KX_Scene.h"
39 #include "KX_GameObject.h"
40 #include "KX_IpoConvert.h"
41 #include "RAS_MeshObject.h"
42 #include "KX_PhysicsEngineEnums.h"
43
44 #include "DummyPhysicsEnvironment.h"
45
46 #ifdef USE_ODE
47 #include "OdePhysicsEnvironment.h"
48 #endif //USE_ODE
49
50 //to decide to use sumo/ode or dummy physics
51 #include "KX_ConvertPhysicsObject.h"
52 #ifdef USE_SUMO_SOLID
53 #include "SumoPhysicsEnvironment.h"
54 #endif
55
56 #include "KX_BlenderSceneConverter.h"
57 #include "KX_BlenderScalarInterpolator.h"
58 #include "BL_BlenderDataConversion.h"
59 #include "BlenderWorldInfo.h"
60 #include "KX_Scene.h"
61
62 /* This little block needed for linking to Blender... */
63 #ifdef WIN32
64 #include "BLI_winstuff.h"
65 #endif
66
67 /* This list includes only data type definitions */
68 #include "DNA_scene_types.h"
69 #include "DNA_world_types.h"
70 #include "BKE_main.h"
71
72
73 KX_BlenderSceneConverter::KX_BlenderSceneConverter(
74                                                         struct Main* maggie,
75                                                         class KX_KetsjiEngine* engine
76                                                         )
77                                                         : m_maggie(maggie),
78                                                         m_ketsjiEngine(engine),
79                                                         m_alwaysUseExpandFraming(false)
80 {
81         m_newfilename = "";
82 }
83
84
85 KX_BlenderSceneConverter::~KX_BlenderSceneConverter()
86 {
87         // clears meshes, and hashmaps from blender to gameengine data
88         int i;
89         // delete sumoshapes
90         
91
92         int numipolists = m_map_blender_to_gameipolist.size();
93         for (i=0; i<numipolists; i++) {
94                 BL_InterpolatorList *ipoList= *m_map_blender_to_gameipolist.at(i);
95
96                 delete (ipoList);
97         }
98
99         vector<KX_WorldInfo*>::iterator itw = m_worldinfos.begin();
100         while (itw != m_worldinfos.end()) {
101                 delete (*itw);
102                 itw++;
103         }
104
105         vector<RAS_IPolyMaterial*>::iterator itp = m_polymaterials.begin();
106         while (itp != m_polymaterials.end()) {
107                 delete (*itp);
108                 itp++;
109         }
110         
111         vector<RAS_MeshObject*>::iterator itm = m_meshobjects.begin();
112         while (itm != m_meshobjects.end()) {
113                 delete (*itm);
114                 itm++;
115         }
116 }
117
118
119
120 void KX_BlenderSceneConverter::SetNewFileName(const STR_String& filename)
121 {
122         m_newfilename = filename;
123 }
124
125
126
127 bool KX_BlenderSceneConverter::TryAndLoadNewFile()
128 {
129         bool result = false;
130
131         // find the file
132 /*      if ()
133         {
134                 result = true;
135         }
136         // if not, clear the newfilename
137         else
138         {
139                 m_newfilename = "";     
140         }
141 */
142         return result;
143 }
144
145
146
147         /**
148          * Find the specified scene by name, or the first
149          * scene if nothing matches (shouldn't happen).
150          */
151 static struct Scene *GetSceneForName2(struct Main *maggie, const STR_String& scenename) {
152         Scene *sce;
153
154         for (sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
155                 if (scenename == (sce->id.name+2))
156                         return sce;
157
158         return (Scene*) maggie->scene.first;
159 }
160
161
162 void KX_BlenderSceneConverter::ConvertScene(const STR_String& scenename,
163                                                                                         class KX_Scene* destinationscene,
164                                                                                         PyObject* dictobj,
165                                                                                         class SCA_IInputDevice* keyinputdev,
166                                                                                         class RAS_IRenderTools* rendertools,
167                                                                                         class RAS_ICanvas* canvas)
168 {
169         //find out which physics engine
170         Scene *blenderscene = GetSceneForName2(m_maggie, scenename);
171
172         e_PhysicsEngine physics_engine = UseSumo;
173
174         if (blenderscene)
175         {
176                 int i=0;
177                 
178                 if (blenderscene->world)
179                 {
180                         
181                         switch (blenderscene->world->pad1)
182                         {
183                                 
184                         case 4:
185                                 {
186                                         physics_engine = UseODE;
187                                         break;
188                                 }
189                         case 5:
190                                 {
191                                         physics_engine = UseDynamo;
192                                         break;
193                                 }
194                         case 7:
195                                 {
196                                         physics_engine = UseNone;
197                                         break;
198                                 };
199                         default:
200                                 {
201                                         physics_engine = UseSumo;
202                                 }
203                         }
204                 }
205         }
206
207         switch (physics_engine)
208         {
209
210         case UseSumo:
211                 {
212 #ifdef USE_SUMO_SOLID
213
214                         PHY_IPhysicsEnvironment* physEnv = 
215                                 new SumoPhysicsEnvironment();
216 #else
217                         physics_engine = UseNone;
218                         
219                         PHY_IPhysicsEnvironment* physEnv = 
220                                 new DummyPhysicsEnvironment();
221
222 #endif
223                         destinationscene ->SetPhysicsEnvironment(physEnv);
224                         break;
225                 }
226         case UseODE:
227                 {
228 #ifdef USE_ODE
229
230                         PHY_IPhysicsEnvironment* physEnv = 
231                                 new ODEPhysicsEnvironment();
232 #else
233                         PHY_IPhysicsEnvironment* physEnv = 
234                                 new DummyPhysicsEnvironment();
235
236 #endif //USE_ODE
237
238                         destinationscene ->SetPhysicsEnvironment(physEnv);
239                         break;
240                 }
241         case UseDynamo:
242                 {
243                 }
244
245         case UseNone:
246                 {
247                 };
248         default:
249                 {
250                         physics_engine = UseNone;
251                         
252                         PHY_IPhysicsEnvironment* physEnv = 
253                                 new DummyPhysicsEnvironment();
254                         destinationscene ->SetPhysicsEnvironment(physEnv);
255
256                 }
257         }
258
259         BL_ConvertBlenderObjects(m_maggie,
260                                                          scenename,
261                                                          destinationscene,
262                                                          m_ketsjiEngine,
263                                                          physics_engine,
264                                                          dictobj,
265                                                          keyinputdev,
266                                                          rendertools,
267                                                          canvas,
268                                                          this,
269                                                          m_alwaysUseExpandFraming
270                                                          );
271
272         m_map_blender_to_gameactuator.clear();
273         m_map_blender_to_gamecontroller.clear();
274
275         m_map_blender_to_gameobject.clear();
276         m_map_mesh_to_gamemesh.clear();
277         m_map_gameobject_to_blender.clear();
278 }
279
280
281
282 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
283         bool to_what)
284 {
285         m_alwaysUseExpandFraming= to_what;
286 }
287
288         
289
290 void KX_BlenderSceneConverter::RegisterGameObject(
291                                                                         KX_GameObject *gameobject, 
292                                                                         struct Object *for_blenderobject) 
293 {
294         m_map_gameobject_to_blender.insert(CHashedPtr(gameobject),for_blenderobject);
295         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
296 }
297
298
299
300 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
301                                                                         struct Object *for_blenderobject) 
302 {
303         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
304         
305         return obp?*obp:NULL;
306 }
307
308
309
310 struct Object *KX_BlenderSceneConverter::FindBlenderObject(
311                                                                         KX_GameObject *for_gameobject) 
312 {
313         struct Object **obp= m_map_gameobject_to_blender[CHashedPtr(for_gameobject)];
314         
315         return obp?*obp:NULL;
316 }
317
318         
319
320 void KX_BlenderSceneConverter::RegisterGameMesh(
321                                                                         RAS_MeshObject *gamemesh,
322                                                                         struct Mesh *for_blendermesh)
323 {
324         m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
325         m_meshobjects.push_back(gamemesh);
326 }
327
328
329
330 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
331                                                                         struct Mesh *for_blendermesh,
332                                                                         unsigned int onlayer)
333 {
334         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
335         
336         if (meshp && onlayer==(*meshp)->GetLightLayer()) {
337                 return *meshp;
338         } else {
339                 return NULL;
340         }
341 }
342
343         
344
345
346         
347
348 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
349 {
350         m_polymaterials.push_back(polymat);
351 }
352
353
354
355 void KX_BlenderSceneConverter::RegisterInterpolatorList(
356                                                                         BL_InterpolatorList *ipoList,
357                                                                         struct Ipo *for_ipo)
358 {
359         m_map_blender_to_gameipolist.insert(CHashedPtr(for_ipo), ipoList);
360 }
361
362
363
364 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
365                                                                         struct Ipo *for_ipo)
366 {
367         BL_InterpolatorList **listp = m_map_blender_to_gameipolist[CHashedPtr(for_ipo)];
368                 
369         return listp?*listp:NULL;
370 }
371
372
373
374 void KX_BlenderSceneConverter::RegisterGameActuator(
375                                                                         SCA_IActuator *act,
376                                                                         struct bActuator *for_actuator)
377 {
378         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
379 }
380
381
382
383 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
384                                                                         struct bActuator *for_actuator)
385 {
386         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
387         
388         return actp?*actp:NULL;
389 }
390
391
392
393 void KX_BlenderSceneConverter::RegisterGameController(
394                                                                         SCA_IController *cont,
395                                                                         struct bController *for_controller)
396 {
397         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
398 }
399
400
401
402 SCA_IController *KX_BlenderSceneConverter::FindGameController(
403                                                                         struct bController *for_controller)
404 {
405         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
406         
407         return contp?*contp:NULL;
408 }
409
410
411
412 void KX_BlenderSceneConverter::RegisterWorldInfo(
413                                                                         KX_WorldInfo *worldinfo)
414 {
415         m_worldinfos.push_back(worldinfo);
416 }