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