Reordered some of the includes to fix STL issues.
[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 WIN32
33         #pragma warning (disable:4786) // suppress stl-MSVC debug info warning
34 #endif
35
36 #include "KX_Scene.h"
37 #include "KX_GameObject.h"
38 #include "KX_BlenderSceneConverter.h"
39 #include "KX_IpoConvert.h"
40 #include "RAS_MeshObject.h"
41 #include "KX_PhysicsEngineEnums.h"
42
43 #include "DummyPhysicsEnvironment.h"
44
45 //to decide to use sumo/ode or dummy physics - defines USE_ODE
46 #include "KX_ConvertPhysicsObject.h"
47
48 #ifdef USE_ODE
49 #include "OdePhysicsEnvironment.h"
50 #endif //USE_ODE
51
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 #ifdef USE_SUMO_SOLID
118         KX_ClearSumoSharedShapes();
119 #endif
120 }
121
122
123
124 void KX_BlenderSceneConverter::SetNewFileName(const STR_String& filename)
125 {
126         m_newfilename = filename;
127 }
128
129
130
131 bool KX_BlenderSceneConverter::TryAndLoadNewFile()
132 {
133         bool result = false;
134
135         // find the file
136 /*      if ()
137         {
138                 result = true;
139         }
140         // if not, clear the newfilename
141         else
142         {
143                 m_newfilename = "";     
144         }
145 */
146         return result;
147 }
148
149
150
151         /**
152          * Find the specified scene by name, or the first
153          * scene if nothing matches (shouldn't happen).
154          */
155 #if 0
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 #endif
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); /*unused*/
176
177         e_PhysicsEngine physics_engine = UseSumo;
178
179         /* FIXME: Force physics engine = sumo.
180         This isn't really a problem - no other physics engines are available. 
181         if (blenderscene)
182         {
183                 int i=0;
184         
185                 if (blenderscene->world)
186                 {
187                         switch (blenderscene->world->physicsEngine)
188                         {
189                                 
190                                 case WOPHY_ODE:
191                                 {
192                                         physics_engine = UseODE;
193                                         break;
194                                 }
195                                 case WOPHY_DYNAMO:
196                                 {
197                                         physics_engine = UseDynamo;
198                                         break;
199                                 }
200                                 case WOPHY_SUMO:
201                                 {
202                                         physics_engine = UseSumo;
203                                         break;
204                                 }
205                                 case WOPHY_NONE:
206                                 {
207                                         physics_engine = UseNone;
208                                 }
209                         }
210                   
211                 }
212         }
213         */
214         switch (physics_engine)
215         {
216         
217 #ifdef USE_SUMO_SOLID
218                 case UseSumo:
219                         destinationscene ->SetPhysicsEnvironment(new SumoPhysicsEnvironment());
220                         break;
221 #endif
222 #ifdef USE_ODE
223                 case UseODE:
224                         destinationscene ->SetPhysicsEnvironment(new ODEPhysicsEnvironment());
225                         break;
226 #endif //USE_ODE
227         
228                 case UseDynamo:
229                 {
230                 }
231                 
232                 default:
233                 case UseNone:
234                         physics_engine = UseNone;
235                         destinationscene ->SetPhysicsEnvironment(new DummyPhysicsEnvironment());
236                         break;
237         }
238
239         BL_ConvertBlenderObjects(m_maggie,
240                 scenename,
241                 destinationscene,
242                 m_ketsjiEngine,
243                 physics_engine,
244                 dictobj,
245                 keyinputdev,
246                 rendertools,
247                 canvas,
248                 this,
249                 m_alwaysUseExpandFraming
250                 );
251
252         m_map_blender_to_gameactuator.clear();
253         m_map_blender_to_gamecontroller.clear();
254         
255         m_map_blender_to_gameobject.clear();
256         m_map_mesh_to_gamemesh.clear();
257         m_map_gameobject_to_blender.clear();
258 }
259
260
261
262 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
263         bool to_what)
264 {
265         m_alwaysUseExpandFraming= to_what;
266 }
267
268         
269
270 void KX_BlenderSceneConverter::RegisterGameObject(
271                                                                         KX_GameObject *gameobject, 
272                                                                         struct Object *for_blenderobject) 
273 {
274         m_map_gameobject_to_blender.insert(CHashedPtr(gameobject),for_blenderobject);
275         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
276 }
277
278
279
280 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
281                                                                         struct Object *for_blenderobject) 
282 {
283         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
284         
285         return obp?*obp:NULL;
286 }
287
288
289
290 struct Object *KX_BlenderSceneConverter::FindBlenderObject(
291                                                                         KX_GameObject *for_gameobject) 
292 {
293         struct Object **obp= m_map_gameobject_to_blender[CHashedPtr(for_gameobject)];
294         
295         return obp?*obp:NULL;
296 }
297
298         
299
300 void KX_BlenderSceneConverter::RegisterGameMesh(
301                                                                         RAS_MeshObject *gamemesh,
302                                                                         struct Mesh *for_blendermesh)
303 {
304         m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
305         m_meshobjects.push_back(gamemesh);
306 }
307
308
309
310 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
311                                                                         struct Mesh *for_blendermesh,
312                                                                         unsigned int onlayer)
313 {
314         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
315         
316         if (meshp && onlayer==(*meshp)->GetLightLayer()) {
317                 return *meshp;
318         } else {
319                 return NULL;
320         }
321 }
322
323         
324
325
326         
327
328 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
329 {
330         m_polymaterials.push_back(polymat);
331 }
332
333
334
335 void KX_BlenderSceneConverter::RegisterInterpolatorList(
336                                                                         BL_InterpolatorList *ipoList,
337                                                                         struct Ipo *for_ipo)
338 {
339         m_map_blender_to_gameipolist.insert(CHashedPtr(for_ipo), ipoList);
340 }
341
342
343
344 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
345                                                                         struct Ipo *for_ipo)
346 {
347         BL_InterpolatorList **listp = m_map_blender_to_gameipolist[CHashedPtr(for_ipo)];
348                 
349         return listp?*listp:NULL;
350 }
351
352
353
354 void KX_BlenderSceneConverter::RegisterGameActuator(
355                                                                         SCA_IActuator *act,
356                                                                         struct bActuator *for_actuator)
357 {
358         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
359 }
360
361
362
363 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
364                                                                         struct bActuator *for_actuator)
365 {
366         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
367         
368         return actp?*actp:NULL;
369 }
370
371
372
373 void KX_BlenderSceneConverter::RegisterGameController(
374                                                                         SCA_IController *cont,
375                                                                         struct bController *for_controller)
376 {
377         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
378 }
379
380
381
382 SCA_IController *KX_BlenderSceneConverter::FindGameController(
383                                                                         struct bController *for_controller)
384 {
385         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
386         
387         return contp?*contp:NULL;
388 }
389
390
391
392 void KX_BlenderSceneConverter::RegisterWorldInfo(
393                                                                         KX_WorldInfo *worldinfo)
394 {
395         m_worldinfos.push_back(worldinfo);
396 }