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