Reserved some Bullet 2.x constraint data.
[blender.git] / source / gameengine / BlenderRoutines / BL_KetsjiEmbedStart.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Blender's Ketsji startpoint
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <signal.h>
39
40 #ifdef WIN32
41 // don't show stl-warnings
42 #pragma warning (disable:4786)
43 #endif
44
45 #ifdef __APPLE__
46 #define GL_GLEXT_LEGACY 1
47 #endif 
48
49 #include "KX_BlenderGL.h"
50 #include "KX_BlenderCanvas.h"
51 #include "KX_BlenderKeyboardDevice.h"
52 #include "KX_BlenderMouseDevice.h"
53 #include "KX_BlenderRenderTools.h"
54 #include "KX_BlenderSystem.h"
55 #include "BL_Material.h"
56
57 #include "KX_KetsjiEngine.h"
58 #include "KX_BlenderSceneConverter.h"
59 #include "KX_PythonInit.h"
60 #include "KX_PyConstraintBinding.h"
61
62 #include "RAS_OpenGLRasterizer.h"
63 #include "RAS_VAOpenGLRasterizer.h"
64 #include "RAS_ListRasterizer.h"
65 #include "RAS_GLExtensionManager.h"
66
67 #include "NG_LoopBackNetworkDeviceInterface.h"
68 #include "SND_DeviceManager.h"
69
70 #include "SYS_System.h"
71
72         /***/
73
74 #include "DNA_view3d_types.h"
75 #include "DNA_screen_types.h"
76 #include "BKE_global.h"
77 #include "BIF_screen.h"
78 #include "BIF_scrarea.h"
79
80 #include "BKE_main.h"   
81 #include "BLI_blenlib.h"
82 #include "BLO_readfile.h"
83 #include "DNA_scene_types.h"
84         /***/
85
86 static BlendFileData *load_game_data(char *filename) {
87         BlendReadError error;
88         //this doesn't work anymore for relative paths, so use BLO_read_from_memory instead
89         //BlendFileData *bfd= BLO_read_from_file(filename, &error);
90         FILE* file = fopen(filename,"rb");
91         BlendFileData *bfd  = 0;
92         if (file)
93         {
94                 fseek(file, 0L, SEEK_END);
95                 int len= ftell(file);
96                 fseek(file, 0L, SEEK_SET);      
97                 char* filebuffer= new char[len];//MEM_mallocN(len, "text_buffer");
98                 int sizeread = fread(filebuffer,len,1,file);
99                 if (sizeread==1)
100                 {
101                         bfd = BLO_read_from_memory(filebuffer, len, &error);
102                 }
103                 fclose(file);
104         }
105
106         if (!bfd) {
107                 printf("Loading %s failed: %s\n", filename, BLO_bre_as_string(error));
108         }
109         
110         return bfd;
111 }
112
113 extern "C" void StartKetsjiShell(struct ScrArea *area,
114                                                                  char* scenename,
115                                                                  struct Main* maggie1,
116                                                                  struct SpaceIpo *sipo,
117                                                                  int always_use_expand_framing)
118 {
119         int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
120         
121         Main* blenderdata = maggie1;
122
123         char* startscenename = scenename;
124         char pathname[160];
125         strcpy (pathname, blenderdata->name);
126         STR_String exitstring = "";
127         BlendFileData *bfd= NULL;
128
129         bgl::InitExtensions(1);
130         
131         do
132         {
133                 View3D *v3d= (View3D*) area->spacedata.first;
134                 
135                 // get some preferences
136                 SYS_SystemHandle syshandle = SYS_GetSystem();
137                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
138                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
139                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
140                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
141                 bool game2ipo = (SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
142                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
143                 bool usemat = false;
144                 
145                 #ifdef GL_ARB_multitexture
146                 if(bgl::RAS_EXT_support._ARB_multitexture && bgl::QueryVersion(1, 1)) {
147                         usemat = (SYS_GetCommandLineInt(syshandle, "blender_material", 0) != 0);
148                         int unitmax=0;
149                         glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, (GLint*)&unitmax);
150                         bgl::max_texture_units = MAXTEX>unitmax?unitmax:MAXTEX;
151                         //std::cout << "using(" << bgl::max_texture_units << ") of(" << unitmax << ") texture units." << std::endl;
152                 } else {
153                         bgl::max_texture_units = 0;
154                 }
155                 #endif
156
157
158                 // create the canvas, rasterizer and rendertools
159                 RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
160                 canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
161                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
162                 RAS_IRasterizer* rasterizer = NULL;
163                 
164                 // let's see if we want to use vertexarrays or not
165                 int usevta = SYS_GetCommandLineInt(syshandle,"vertexarrays",1);
166                 bool useVertexArrays = (usevta > 0);
167                 
168                 bool lock_arrays = (displaylists && useVertexArrays);
169
170                 if(displaylists && !useVertexArrays)
171                         rasterizer = new RAS_ListRasterizer(canvas);
172                 else if (useVertexArrays && bgl::QueryVersion(1, 1))
173                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, lock_arrays);
174                 else
175                         rasterizer = new RAS_OpenGLRasterizer(canvas);
176                 
177                 // create the inputdevices
178                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
179                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
180                 
181                 // create a networkdevice
182                 NG_NetworkDeviceInterface* networkdevice = new
183                         NG_LoopBackNetworkDeviceInterface();
184                 
185                 // get an audiodevice
186                 SND_DeviceManager::Subscribe();
187                 SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
188                 audiodevice->UseCD();
189                 
190                 // create a ketsji/blendersystem (only needed for timing and stuff)
191                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
192                 
193                 // create the ketsjiengine
194                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
195                 
196                 // set the devices
197                 ketsjiengine->SetKeyboardDevice(keyboarddevice);
198                 ketsjiengine->SetMouseDevice(mousedevice);
199                 ketsjiengine->SetNetworkDevice(networkdevice);
200                 ketsjiengine->SetCanvas(canvas);
201                 ketsjiengine->SetRenderTools(rendertools);
202                 ketsjiengine->SetRasterizer(rasterizer);
203                 ketsjiengine->SetNetworkDevice(networkdevice);
204                 ketsjiengine->SetAudioDevice(audiodevice);
205                 ketsjiengine->SetUseFixedTime(usefixed);
206                 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
207
208                 
209         
210                 // some blender stuff
211                 MT_CmMatrix4x4 projmat;
212                 MT_CmMatrix4x4 viewmat;
213                 int i;
214                 
215                 for (i = 0; i < 16; i++)
216                 {
217                         float *viewmat_linear= (float*) v3d->viewmat;
218                         viewmat.setElem(i, viewmat_linear[i]);
219                 }
220                 for (i = 0; i < 16; i++)
221                 {
222                         float *projmat_linear = (float*) area->winmat;
223                         projmat.setElem(i, projmat_linear[i]);
224                 }
225                 
226                 float camzoom = (1.41421 + (v3d->camzoom / 50.0));
227                 camzoom *= camzoom;
228                 camzoom = 4.0 / camzoom;
229                 
230                 ketsjiengine->SetDrawType(v3d->drawtype);
231                 ketsjiengine->SetCameraZoom(camzoom);
232                 
233                 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
234                 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
235                 {
236                         exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
237                         if (bfd) BLO_blendfiledata_free(bfd);
238                         
239                         char basedpath[160];
240                         // base the actuator filename with respect
241                         // to the original file working directory
242                         if (exitstring != "")
243                                 strcpy(basedpath, exitstring.Ptr());
244
245                         BLI_convertstringcode(basedpath, pathname, 0);
246                         bfd = load_game_data(basedpath);
247                         
248                         // if it wasn't loaded, try it forced relative
249                         if (!bfd)
250                         {
251                                 // just add "//" in front of it
252                                 char temppath[162];
253                                 strcpy(temppath, "//");
254                                 strcat(temppath, basedpath);
255                                 
256                                 BLI_convertstringcode(temppath, pathname, 0);
257                                 bfd = load_game_data(temppath);
258                         }
259                         
260                         // if we got a loaded blendfile, proceed
261                         if (bfd)
262                         {
263                                 blenderdata = bfd->main;
264                                 startscenename = bfd->curscene->id.name + 2;
265                         }
266                         // else forget it, we can't find it
267                         else
268                         {
269                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
270                         }
271                 }
272                 
273                 Scene *blscene = NULL;
274                 if (!bfd)
275                 {
276                         blscene = (Scene*) blenderdata->scene.first;
277                         for (Scene *sce= (Scene*) blenderdata->scene.first; sce; sce= (Scene*) sce->id.next)
278                         {
279                                 if (startscenename == (sce->id.name+2))
280                                 {
281                                         blscene = sce;
282                                         break;
283                                 }
284                         }
285                 } else {
286                         blscene = bfd->curscene;
287                 }
288
289                 if (blscene)
290                 {
291                         int startFrame = blscene->r.cfra;
292                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
293                 }
294
295
296                 // Quad buffered needs a special window.
297                 if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
298                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
299                 
300                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
301                 {
302                         if (v3d->persp != 2)
303                         {
304                                 ketsjiengine->EnableCameraOverride(startscenename);
305                                 ketsjiengine->SetCameraOverrideUseOrtho((v3d->persp == 0));
306                                 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
307                                 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
308                         }
309                         
310                         // create a scene converter, create and convert the startingscene
311                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata,sipo, ketsjiengine);
312                         ketsjiengine->SetSceneConverter(sceneconverter);
313                         
314                         if (always_use_expand_framing)
315                                 sceneconverter->SetAlwaysUseExpandFraming(true);
316                         
317                         if(usemat)
318                                 sceneconverter->SetMaterials(true);
319                                         
320                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
321                                 mousedevice,
322                                 networkdevice,
323                                 audiodevice,
324                                 startscenename);
325                         
326                         // some python things
327                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
328                         ketsjiengine->SetPythonDictionary(dictionaryobject);
329                         initRasterizer(rasterizer, canvas);
330                         PyObject *gameLogic = initGameLogic(startscene);
331                         initGameKeys();
332                         initPythonConstraintBinding();
333
334                         
335                         if (sceneconverter)
336                         {
337                                 // convert and add scene
338                                 sceneconverter->ConvertScene(
339                                         startscenename,
340                                         startscene,
341                                         dictionaryobject,
342                                         keyboarddevice,
343                                         rendertools,
344                                         canvas);
345                                 ketsjiengine->AddScene(startscene);
346                                 
347                                 // init the rasterizer
348                                 rasterizer->Init();
349                                 
350                                 // start the engine
351                                 ketsjiengine->StartEngine(true);
352                                 
353                                 // the mainloop
354                                 while (!exitrequested)
355                                 {
356                                         // first check if we want to exit
357                                         exitrequested = ketsjiengine->GetExitCode();
358                                         
359                                         // kick the engine
360                                         ketsjiengine->NextFrame();
361                                         
362                                         // render the frame
363                                         ketsjiengine->Render();
364                                         
365                                         // test for the ESC key
366                                         while (qtest())
367                                         {
368                                                 short val; 
369                                                 unsigned short event = extern_qread(&val);
370                                                 
371                                                 if (keyboarddevice->ConvertBlenderEvent(event,val))
372                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
373                                                 
374                                                         /* Coordinate conversion... where
375                                                         * should this really be?
376                                                 */
377                                                 if (event==MOUSEX) {
378                                                         val = val - scrarea_get_win_x(area);
379                                                 } else if (event==MOUSEY) {
380                                                         val = scrarea_get_win_height(area) - (val - scrarea_get_win_y(area)) - 1;
381                                                 }
382                                                 
383                                                 mousedevice->ConvertBlenderEvent(event,val);
384                                         }
385                                 }
386                                 exitstring = ketsjiengine->GetExitString();
387                                 
388                                 // when exiting the mainloop
389                                 dictionaryClearByHand(gameLogic);
390                                 ketsjiengine->StopEngine();
391                                 exitGamePythonScripting();
392                                 networkdevice->Disconnect();
393                         }
394                         if (sceneconverter)
395                         {
396                                 delete sceneconverter;
397                                 sceneconverter = NULL;
398                         }
399                 }
400                 // set the cursor back to normal
401                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
402                 
403                 // clean up some stuff
404                 audiodevice->StopCD();
405                 
406                 if (ketsjiengine)
407                 {
408                         delete ketsjiengine;
409                         ketsjiengine = NULL;
410                 }
411                 if (kxsystem)
412                 {
413                         delete kxsystem;
414                         kxsystem = NULL;
415                 }
416                 if (networkdevice)
417                 {
418                         delete networkdevice;
419                         networkdevice = NULL;
420                 }
421                 if (keyboarddevice)
422                 {
423                         delete keyboarddevice;
424                         keyboarddevice = NULL;
425                 }
426                 if (mousedevice)
427                 {
428                         delete mousedevice;
429                         mousedevice = NULL;
430                 }
431                 if (rasterizer)
432                 {
433                         delete rasterizer;
434                         rasterizer = NULL;
435                 }
436                 if (rendertools)
437                 {
438                         delete rendertools;
439                         rendertools = NULL;
440                 }
441                 if (canvas)
442                 {
443                         delete canvas;
444                         canvas = NULL;
445                 }
446                 SND_DeviceManager::Unsubscribe();
447         
448         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
449
450         if (bfd) BLO_blendfiledata_free(bfd);
451 }
452 #ifdef __cplusplus
453 extern "C" {
454 #endif
455 #include "BSE_headerbuttons.h"
456 void update_for_newframe();
457 #ifdef __cplusplus
458 }
459 #endif
460
461 extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
462                                                                  char* scenename,
463                                                                  struct Main* maggie,
464                                                                  struct SpaceIpo *sipo,
465                                                                  int always_use_expand_framing)
466 {
467     int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
468
469         Main* blenderdata = maggie;
470
471         char* startscenename = scenename;
472         char pathname[160];
473         strcpy (pathname, maggie->name);
474         STR_String exitstring = "";
475         BlendFileData *bfd= NULL;
476
477         bgl::InitExtensions(1);
478
479         do
480         {
481 //              View3D *v3d= (View3D*) area->spacedata.first;
482
483                 // get some preferences
484                 SYS_SystemHandle syshandle = SYS_GetSystem();
485                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
486                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
487                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
488                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
489                 bool game2ipo = true;//(SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
490                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
491                 bool usemat = false;
492
493 /*              #ifdef GL_ARB_multitexture
494                 if(bgl::RAS_EXT_support._ARB_multitexture && bgl::QueryVersion(1, 1)) {
495                         usemat = (SYS_GetCommandLineInt(syshandle, "blender_material", 0) != 0);
496                         int unitmax=0;
497                         glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, (GLint*)&unitmax);
498                         bgl::max_texture_units = MAXTEX>unitmax?unitmax:MAXTEX;
499                         //std::cout << "using(" << bgl::max_texture_units << ") of(" << unitmax << ") texture units." << std::endl;
500                 } else {
501                         bgl::max_texture_units = 0;
502                 }
503                 #endif
504
505 */
506                 // create the canvas, rasterizer and rendertools
507                 RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
508                 //canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
509                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
510                 RAS_IRasterizer* rasterizer = NULL;
511
512                 // let's see if we want to use vertexarrays or not
513                 int usevta = SYS_GetCommandLineInt(syshandle,"vertexarrays",1);
514                 bool useVertexArrays = (usevta > 0);
515
516                 bool lock_arrays = (displaylists && useVertexArrays);
517
518                 if(displaylists && !useVertexArrays)
519                         rasterizer = new RAS_ListRasterizer(canvas);
520                 else if (useVertexArrays && bgl::QueryVersion(1, 1))
521                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, lock_arrays);
522                 else
523                         rasterizer = new RAS_OpenGLRasterizer(canvas);
524
525                 // create the inputdevices
526                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
527                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
528
529                 // create a networkdevice
530                 NG_NetworkDeviceInterface* networkdevice = new
531                         NG_LoopBackNetworkDeviceInterface();
532
533                 // get an audiodevice
534                 SND_DeviceManager::Subscribe();
535                 SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
536                 audiodevice->UseCD();
537
538                 // create a ketsji/blendersystem (only needed for timing and stuff)
539                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
540
541                 // create the ketsjiengine
542                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
543
544 /*              // set the devices
545                 ketsjiengine->SetKeyboardDevice(keyboarddevice);
546                 ketsjiengine->SetMouseDevice(mousedevice);
547                 ketsjiengine->SetNetworkDevice(networkdevice);
548                 ketsjiengine->SetCanvas(canvas);
549                 ketsjiengine->SetRenderTools(rendertools);
550                 ketsjiengine->SetRasterizer(rasterizer);
551                 ketsjiengine->SetNetworkDevice(networkdevice);
552                 ketsjiengine->SetAudioDevice(audiodevice);
553                 ketsjiengine->SetUseFixedTime(usefixed);
554                 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
555
556 */
557
558                 // some blender stuff
559                 MT_CmMatrix4x4 projmat;
560                 MT_CmMatrix4x4 viewmat;
561                 int i;
562
563 /*              for (i = 0; i < 16; i++)
564                 {
565                         float *viewmat_linear= (float*) v3d->viewmat;
566                         viewmat.setElem(i, viewmat_linear[i]);
567                 }
568                 for (i = 0; i < 16; i++)
569                 {
570                         float *projmat_linear = (float*) area->winmat;
571                         projmat.setElem(i, projmat_linear[i]);
572                 }
573 */
574 /*              float camzoom = (1.41421 + (v3d->camzoom / 50.0));
575                 camzoom *= camzoom;
576                 camzoom = 4.0 / camzoom;
577 */
578 //              ketsjiengine->SetDrawType(v3d->drawtype);
579 //              ketsjiengine->SetCameraZoom(camzoom);
580 /*
581                 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
582                 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
583                 {
584                         exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
585                         if (bfd) BLO_blendfiledata_free(bfd);
586
587                         char basedpath[160];
588                         // base the actuator filename with respect
589                         // to the original file working directory
590                         if (exitstring != "")
591                                 strcpy(basedpath, exitstring.Ptr());
592
593                         BLI_convertstringcode(basedpath, pathname, 0);
594                         bfd = load_game_data(basedpath);
595
596                         // if it wasn't loaded, try it forced relative
597                         if (!bfd)
598                         {
599                                 // just add "//" in front of it
600                                 char temppath[162];
601                                 strcpy(temppath, "//");
602                                 strcat(temppath, basedpath);
603
604                                 BLI_convertstringcode(temppath, pathname, 0);
605                                 bfd = load_game_data(temppath);
606                         }
607
608                         // if we got a loaded blendfile, proceed
609                         if (bfd)
610                         {
611                                 blenderdata = bfd->main;
612                                 startscenename = bfd->curscene->id.name + 2;
613                         }
614                         // else forget it, we can't find it
615                         else
616                         {
617                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
618                         }
619                 }
620 */
621                 Scene *blscene = NULL;
622                 if (!bfd)
623                 {
624                         blscene = (Scene*) maggie->scene.first;
625                         for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
626                         {
627                                 if (startscenename == (sce->id.name+2))
628                                 {
629                                         blscene = sce;
630                                         break;
631                                 }
632                         }
633                 } else {
634                         blscene = bfd->curscene;
635                 }
636         int cframe,startFrame;
637                 if (blscene)
638                 {
639                         cframe=blscene->r.cfra;
640                         startFrame = blscene->r.sfra;
641                         blscene->r.cfra=startFrame;
642                         update_for_newframe();
643                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
644                 }
645
646                 // Quad buffered needs a special window.
647                 if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
648                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
649
650                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
651                 {
652                         /*if (v3d->persp != 2)
653                         {
654                                 ketsjiengine->EnableCameraOverride(startscenename);
655                                 ketsjiengine->SetCameraOverrideUseOrtho((v3d->persp == 0));
656                                 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
657                                 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
658                         }*/
659
660                         // create a scene converter, create and convert the startingscene
661                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(maggie,sipo, ketsjiengine);
662                         ketsjiengine->SetSceneConverter(sceneconverter);
663
664                         if (always_use_expand_framing)
665                                 sceneconverter->SetAlwaysUseExpandFraming(true);
666
667                         if(usemat)
668                                 sceneconverter->SetMaterials(true);
669
670                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
671                                 mousedevice,
672                                 networkdevice,
673                                 audiodevice,
674                                 startscenename);
675
676                         // some python things
677                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
678                         ketsjiengine->SetPythonDictionary(dictionaryobject);
679                         initRasterizer(rasterizer, canvas);
680                         PyObject *gameLogic = initGameLogic(startscene);
681                         initGameKeys();
682                         initPythonConstraintBinding();
683
684                         if (sceneconverter)
685                         {
686                                 // convert and add scene
687                                 sceneconverter->ConvertScene(
688                                         startscenename,
689                                         startscene,
690                                         dictionaryobject,
691                                         keyboarddevice,
692                                         rendertools,
693                                         canvas);
694                                 ketsjiengine->AddScene(startscene);
695
696                                 // init the rasterizer
697                                 //rasterizer->Init();
698
699                                 // start the engine
700                                 ketsjiengine->StartEngine(false);
701
702                                 // the mainloop
703                                 while ((blscene->r.cfra<=blscene->r.efra)&&(!exitrequested))
704                                 {
705                     printf("frame %i\n",blscene->r.cfra);
706                     // first check if we want to exit
707                                         exitrequested = ketsjiengine->GetExitCode();
708
709                                         // kick the engine
710                                         ketsjiengine->NextFrame();
711                                     blscene->r.cfra=blscene->r.cfra+1;
712                                     update_for_newframe();
713                                         // render the frame
714                                         //ketsjiengine->Render();
715
716                                         // test for the ESC key
717                                         /*while (qtest())
718                                         {
719                                                 short val;
720                                                 unsigned short event = extern_qread(&val);
721
722                                                 if (keyboarddevice->ConvertBlenderEvent(event,val))
723                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
724
725                                                 if (event==MOUSEX) {
726                                                         val = val - scrarea_get_win_x(area);
727                                                 } else if (event==MOUSEY) {
728                                                         val = scrarea_get_win_height(area) - (val - scrarea_get_win_y(area)) - 1;
729                                                 }
730
731                                                 mousedevice->ConvertBlenderEvent(event,val);
732                                         }*/
733                                 }
734                                 exitstring = ketsjiengine->GetExitString();
735                                 // when exiting the mainloop
736                                 //dictionaryClearByHand(gameLogic);
737                                 //ketsjiengine->StopEngine();
738                                 //exitGamePythonScripting();
739                                 //networkdevice->Disconnect();
740                         }
741                         if (sceneconverter)
742                         {
743                                 delete sceneconverter;
744                                 sceneconverter = NULL;
745                         }
746                 }
747                 blscene->r.cfra=cframe;
748                 // set the cursor back to normal
749                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
750
751                 // clean up some stuff
752                 audiodevice->StopCD();
753                 if (ketsjiengine)
754                 {
755                         delete ketsjiengine;
756                         ketsjiengine = NULL;
757                 }
758                 if (kxsystem)
759                 {
760                         delete kxsystem;
761                         kxsystem = NULL;
762                 }
763                 if (networkdevice)
764                 {
765                         delete networkdevice;
766                         networkdevice = NULL;
767                 }
768                 if (keyboarddevice)
769                 {
770                         delete keyboarddevice;
771                         keyboarddevice = NULL;
772                 }
773                 if (mousedevice)
774                 {
775                         delete mousedevice;
776                         mousedevice = NULL;
777                 }
778                 /*if (rasterizer)
779                 {
780                         delete rasterizer;
781                         rasterizer = NULL;
782                 }
783                 if (rendertools)
784                 {
785                         delete rendertools;
786                         rendertools = NULL;
787                 }
788                 if (canvas)
789                 {
790                         delete canvas;
791                         canvas = NULL;
792                 }*/
793                 SND_DeviceManager::Unsubscribe();
794
795         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
796         if (bfd) BLO_blendfiledata_free(bfd);
797 }