VideoTexture module.
[blender.git] / source / gameengine / BlenderRoutines / BL_KetsjiEmbedStart.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Blender's Ketsji startpoint
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <signal.h>
36 #include <stdlib.h>
37
38 #ifdef WIN32
39 // don't show stl-warnings
40 #pragma warning (disable:4786)
41 #endif
42
43 #include "GL/glew.h"
44
45 #include "KX_BlenderGL.h"
46 #include "KX_BlenderCanvas.h"
47 #include "KX_BlenderKeyboardDevice.h"
48 #include "KX_BlenderMouseDevice.h"
49 #include "KX_BlenderRenderTools.h"
50 #include "KX_BlenderSystem.h"
51 #include "BL_Material.h"
52
53 #include "KX_KetsjiEngine.h"
54 #include "KX_BlenderSceneConverter.h"
55 #include "KX_PythonInit.h"
56 #include "KX_PyConstraintBinding.h"
57
58 #include "RAS_GLExtensionManager.h"
59 #include "RAS_OpenGLRasterizer.h"
60 #include "RAS_VAOpenGLRasterizer.h"
61 #include "RAS_ListRasterizer.h"
62
63 #include "NG_LoopBackNetworkDeviceInterface.h"
64 #include "SND_DeviceManager.h"
65
66 #include "SYS_System.h"
67
68         /***/
69
70 #include "DNA_view3d_types.h"
71 #include "DNA_screen_types.h"
72 #include "BKE_global.h"
73 #include "BKE_utildefines.h"
74 #include "BIF_screen.h"
75 #include "BIF_scrarea.h"
76
77 #include "BKE_main.h"   
78 #include "BLI_blenlib.h"
79 #include "BLO_readfile.h"
80 #include "DNA_scene_types.h"
81         /***/
82
83 #include "GPU_extensions.h"
84
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 #include "BSE_headerbuttons.h"
89 void update_for_newframe();
90 #ifdef __cplusplus
91 }
92 #endif
93
94 static BlendFileData *load_game_data(char *filename) {
95         BlendReadError error;
96         BlendFileData *bfd= BLO_read_from_file(filename, &error);
97         if (!bfd) {
98                 printf("Loading %s failed: %s\n", filename, BLO_bre_as_string(error));
99         }
100         return bfd;
101 }
102
103 extern "C" void StartKetsjiShell(struct ScrArea *area,
104                                                                  char* scenename,
105                                                                  struct Main* maggie1,
106                                                                  struct SpaceIpo *sipo,
107                                                                  int always_use_expand_framing)
108 {
109         int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
110         
111         Main* blenderdata = maggie1;
112
113         char* startscenename = scenename;
114         char pathname[FILE_MAXDIR+FILE_MAXFILE], oldsce[FILE_MAXDIR+FILE_MAXFILE];
115         STR_String exitstring = "";
116         BlendFileData *bfd= NULL;
117
118         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
119         BLI_strncpy(oldsce, G.sce, sizeof(oldsce));
120         setGamePythonPath(G.sce);
121
122         // Acquire Python's GIL (global interpreter lock)
123         // so we can safely run Python code and API calls
124         PyGILState_STATE gilstate = PyGILState_Ensure();
125         
126         PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
127         
128         bgl::InitExtensions(true);
129
130         do
131         {
132                 View3D *v3d= (View3D*) area->spacedata.first;
133
134                 // get some preferences
135                 SYS_SystemHandle syshandle = SYS_GetSystem();
136                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
137                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
138                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
139                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
140                 bool game2ipo = (SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
141                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
142
143                 // create the canvas, rasterizer and rendertools
144                 RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
145                 canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
146                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
147                 RAS_IRasterizer* rasterizer = NULL;
148                 
149                 if(displaylists) {
150                         if (GLEW_VERSION_1_1)
151                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
152                         else
153                                 rasterizer = new RAS_ListRasterizer(canvas);
154                 }
155                 else if (GLEW_VERSION_1_1)
156                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
157                 else
158                         rasterizer = new RAS_OpenGLRasterizer(canvas);
159                 
160                 // create the inputdevices
161                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
162                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
163                 
164                 // create a networkdevice
165                 NG_NetworkDeviceInterface* networkdevice = new
166                         NG_LoopBackNetworkDeviceInterface();
167                 
168                 //
169                 SYS_SystemHandle hSystem = SYS_GetSystem();
170                 bool noaudio = SYS_GetCommandLineInt(hSystem,"noaudio",0);
171
172                 if (noaudio)/*(noaudio) intrr: disable game engine audio (openal) */
173                         SND_DeviceManager::SetDeviceType(snd_e_dummydevice);
174
175                 // get an audiodevice
176                 SND_DeviceManager::Subscribe();
177                 SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
178                 audiodevice->UseCD();
179                 
180                 // create a ketsji/blendersystem (only needed for timing and stuff)
181                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
182                 
183                 // create the ketsjiengine
184                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
185                 
186                 // set the devices
187                 ketsjiengine->SetKeyboardDevice(keyboarddevice);
188                 ketsjiengine->SetMouseDevice(mousedevice);
189                 ketsjiengine->SetNetworkDevice(networkdevice);
190                 ketsjiengine->SetCanvas(canvas);
191                 ketsjiengine->SetRenderTools(rendertools);
192                 ketsjiengine->SetRasterizer(rasterizer);
193                 ketsjiengine->SetNetworkDevice(networkdevice);
194                 ketsjiengine->SetAudioDevice(audiodevice);
195                 ketsjiengine->SetUseFixedTime(usefixed);
196                 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
197
198
199                 //lock frame and camera enabled - storing global values
200                 int tmp_lay= G.scene->lay;
201                 Object *tmp_camera = G.scene->camera;
202
203                 if (G.vd->scenelock==0){
204                         G.scene->lay= v3d->lay;
205                         G.scene->camera= v3d->camera;
206                 }
207
208         
209                 // some blender stuff
210                 MT_CmMatrix4x4 projmat;
211                 MT_CmMatrix4x4 viewmat;
212                 float camzoom;
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                 if(v3d->persp==V3D_CAMOB) {
227                         camzoom = (1.41421 + (v3d->camzoom / 50.0));
228                         camzoom *= camzoom;
229                 }
230                 else
231                         camzoom = 2.0;
232
233                 camzoom = 4.0 / camzoom;
234                 
235                 ketsjiengine->SetDrawType(v3d->drawtype);
236                 ketsjiengine->SetCameraZoom(camzoom);
237                 
238                 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
239                 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
240                 {
241                         exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
242                         if (bfd) BLO_blendfiledata_free(bfd);
243                         
244                         char basedpath[240];
245                         // base the actuator filename with respect
246                         // to the original file working directory
247
248                         if (exitstring != "")
249                                 strcpy(basedpath, exitstring.Ptr());
250
251                         // load relative to the last loaded file, this used to be relative
252                         // to the first file but that makes no sense, relative paths in
253                         // blend files should be relative to that file, not some other file
254                         // that happened to be loaded first
255                         BLI_convertstringcode(basedpath, pathname);
256                         bfd = load_game_data(basedpath);
257                         
258                         // if it wasn't loaded, try it forced relative
259                         if (!bfd)
260                         {
261                                 // just add "//" in front of it
262                                 char temppath[242];
263                                 strcpy(temppath, "//");
264                                 strcat(temppath, basedpath);
265                                 
266                                 BLI_convertstringcode(temppath, pathname);
267                                 bfd = load_game_data(temppath);
268                         }
269                         
270                         // if we got a loaded blendfile, proceed
271                         if (bfd)
272                         {
273                                 blenderdata = bfd->main;
274                                 startscenename = bfd->curscene->id.name + 2;
275
276                                 if(blenderdata) {
277                                         BLI_strncpy(G.sce, blenderdata->name, sizeof(G.sce));
278                                         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
279                                 }
280                         }
281                         // else forget it, we can't find it
282                         else
283                         {
284                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
285                         }
286                 }
287                 
288                 Scene *blscene = NULL;
289                 if (!bfd)
290                 {
291                         blscene = (Scene*) blenderdata->scene.first;
292                         for (Scene *sce= (Scene*) blenderdata->scene.first; sce; sce= (Scene*) sce->id.next)
293                         {
294                                 if (startscenename == (sce->id.name+2))
295                                 {
296                                         blscene = sce;
297                                         break;
298                                 }
299                         }
300                 } else {
301                         blscene = bfd->curscene;
302                 }
303
304                 if (blscene)
305                 {
306                         int startFrame = blscene->r.cfra;
307                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
308                 }
309
310
311                 // Quad buffered needs a special window.
312                 if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
313                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
314                 
315                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
316                 {
317                         if (v3d->persp != V3D_CAMOB)
318                         {
319                                 ketsjiengine->EnableCameraOverride(startscenename);
320                                 ketsjiengine->SetCameraOverrideUseOrtho((v3d->persp == V3D_ORTHO));
321                                 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
322                                 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
323                                 ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
324                         }
325                         
326                         // create a scene converter, create and convert the startingscene
327                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata,sipo, ketsjiengine);
328                         ketsjiengine->SetSceneConverter(sceneconverter);
329                         sceneconverter->addInitFromFrame=false;
330                         if (always_use_expand_framing)
331                                 sceneconverter->SetAlwaysUseExpandFraming(true);
332
333                         bool usemat = false, useglslmat = false;
334
335                         if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
336                                 usemat = true;
337
338                         if(GPU_extensions_minimum_support())
339                                 useglslmat = true;
340                         else if(G.fileflags & G_FILE_GAME_MAT_GLSL)
341                                 usemat = false;
342
343             if(usemat && (G.fileflags & G_FILE_GAME_MAT))
344                                 sceneconverter->SetMaterials(true);
345                         if(useglslmat && (G.fileflags & G_FILE_GAME_MAT_GLSL))
346                                 sceneconverter->SetGLSLMaterials(true);
347                                         
348                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
349                                 mousedevice,
350                                 networkdevice,
351                                 audiodevice,
352                                 startscenename,
353                                 blscene);
354                         
355                         // some python things
356                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
357                         ketsjiengine->SetPythonDictionary(dictionaryobject);
358                         initRasterizer(rasterizer, canvas);
359                         PyObject *gameLogic = initGameLogic(ketsjiengine, startscene);
360                         PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
361                         PyObject *gameLogic_keys = PyDict_Keys(PyModule_GetDict(gameLogic));
362                         PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module.
363                         
364                         initGameKeys();
365                         initPythonConstraintBinding();
366                         initMathutils();
367                         initVideoTexture();
368
369                         if (sceneconverter)
370                         {
371                                 // convert and add scene
372                                 sceneconverter->ConvertScene(
373                                         startscenename,
374                                         startscene,
375                                         dictionaryobject,
376                                         keyboarddevice,
377                                         rendertools,
378                                         canvas);
379                                 ketsjiengine->AddScene(startscene);
380                                 
381                                 // init the rasterizer
382                                 rasterizer->Init();
383                                 
384                                 // start the engine
385                                 ketsjiengine->StartEngine(true);
386                                 
387
388                                 // Set the animation playback rate for ipo's and actions
389                                 // the framerate below should patch with FPS macro defined in blendef.h
390                                 // Could be in StartEngine set the framerate, we need the scene to do this
391                                 ketsjiengine->SetAnimFrameRate( (((double) blscene->r.frs_sec) / blscene->r.frs_sec_base) );
392                                 
393                                 // the mainloop
394                                 printf("\nBlender Game Engine Started\n\n");
395                                 while (!exitrequested)
396                                 {
397                                         // first check if we want to exit
398                                         exitrequested = ketsjiengine->GetExitCode();
399                                         
400                                         // kick the engine
401                                         bool render = ketsjiengine->NextFrame();
402                                         
403                                         if (render)
404                                         {
405                                                 // render the frame
406                                                 ketsjiengine->Render();
407                                         }
408                                         
409                                         // test for the ESC key
410                                         while (qtest())
411                                         {
412                                                 short val; 
413                                                 unsigned short event = extern_qread(&val);
414                                                 
415                                                 if (keyboarddevice->ConvertBlenderEvent(event,val))
416                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
417                                                 
418                                                         /* Coordinate conversion... where
419                                                         * should this really be?
420                                                 */
421                                                 if (event==MOUSEX) {
422                                                         val = val - scrarea_get_win_x(area);
423                                                 } else if (event==MOUSEY) {
424                                                         val = scrarea_get_win_height(area) - (val - scrarea_get_win_y(area)) - 1;
425                                                 }
426                                                 
427                                                 mousedevice->ConvertBlenderEvent(event,val);
428                                         }
429                                 }
430                                 printf("\nBlender Game Engine Finished\n\n");
431                                 exitstring = ketsjiengine->GetExitString();
432
433
434                                 // when exiting the mainloop
435                                 
436                                 // Clears the dictionary by hand:
437                                 // This prevents, extra references to global variables
438                                 // inside the GameLogic dictionary when the python interpreter is finalized.
439                                 // which allows the scene to safely delete them :)
440                                 // see: (space.c)->start_game
441                                 
442                                 //PyDict_Clear(PyModule_GetDict(gameLogic));
443                                 
444                                 // Keep original items, means python plugins will autocomplete members
445                                 int listIndex;
446                                 PyObject *gameLogic_keys_new = PyDict_Keys(PyModule_GetDict(gameLogic));
447                                 for (listIndex=0; listIndex < PyList_Size(gameLogic_keys_new); listIndex++)  {
448                                         PyObject* item = PyList_GET_ITEM(gameLogic_keys_new, listIndex);
449                                         if (!PySequence_Contains(gameLogic_keys, item)) {
450                                                 PyDict_DelItem( PyModule_GetDict(gameLogic), item);
451                                         }
452                                 }
453                                 Py_DECREF(gameLogic_keys_new);
454                                 gameLogic_keys_new = NULL;
455                                 
456                                 ketsjiengine->StopEngine();
457                                 exitGamePythonScripting();
458                                 networkdevice->Disconnect();
459                         }
460                         if (sceneconverter)
461                         {
462                                 delete sceneconverter;
463                                 sceneconverter = NULL;
464                         }
465                         
466                         Py_DECREF(gameLogic_keys);
467                         gameLogic_keys = NULL;
468                 }
469                 //lock frame and camera enabled - restoring global values
470                 if (G.vd->scenelock==0){
471                         G.scene->lay= tmp_lay;
472                         G.scene->camera= tmp_camera;
473                 }
474
475                 // set the cursor back to normal
476                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
477                 
478                 // clean up some stuff
479                 audiodevice->StopCD();
480                 
481                 if (ketsjiengine)
482                 {
483                         delete ketsjiengine;
484                         ketsjiengine = NULL;
485                 }
486                 if (kxsystem)
487                 {
488                         delete kxsystem;
489                         kxsystem = NULL;
490                 }
491                 if (networkdevice)
492                 {
493                         delete networkdevice;
494                         networkdevice = NULL;
495                 }
496                 if (keyboarddevice)
497                 {
498                         delete keyboarddevice;
499                         keyboarddevice = NULL;
500                 }
501                 if (mousedevice)
502                 {
503                         delete mousedevice;
504                         mousedevice = NULL;
505                 }
506                 if (rasterizer)
507                 {
508                         delete rasterizer;
509                         rasterizer = NULL;
510                 }
511                 if (rendertools)
512                 {
513                         delete rendertools;
514                         rendertools = NULL;
515                 }
516                 if (canvas)
517                 {
518                         delete canvas;
519                         canvas = NULL;
520                 }
521                 SND_DeviceManager::Unsubscribe();
522         
523         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
524
525         if (bfd) BLO_blendfiledata_free(bfd);
526
527         BLI_strncpy(G.sce, oldsce, sizeof(G.sce));
528
529         // Release Python's GIL
530         PyGILState_Release(gilstate);
531 }
532
533 extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
534                                                                  char* scenename,
535                                                                  struct Main* maggie,
536                                                                  struct SpaceIpo *sipo,
537                                                                  int always_use_expand_framing)
538 {
539     int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
540
541         Main* blenderdata = maggie;
542
543         char* startscenename = scenename;
544         char pathname[FILE_MAXDIR+FILE_MAXFILE];
545         STR_String exitstring = "";
546
547         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
548
549         // Acquire Python's GIL (global interpreter lock)
550         // so we can safely run Python code and API calls
551         PyGILState_STATE gilstate = PyGILState_Ensure();
552
553         bgl::InitExtensions(true);
554
555         do
556         {
557
558                 // get some preferences
559                 SYS_SystemHandle syshandle = SYS_GetSystem();
560                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
561                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
562                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
563                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
564                 bool game2ipo = true;//(SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
565                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
566                 bool usemat = false;
567
568                 // create the canvas, rasterizer and rendertools
569                 RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
570                 //canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
571                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
572                 RAS_IRasterizer* rasterizer = NULL;
573
574                 if(displaylists) {
575                         if (GLEW_VERSION_1_1)
576                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
577                         else
578                                 rasterizer = new RAS_ListRasterizer(canvas);
579                 }
580                 else if (GLEW_VERSION_1_1)
581                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
582                 else
583                         rasterizer = new RAS_OpenGLRasterizer(canvas);
584
585                 // create the inputdevices
586                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
587                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
588
589                 // create a networkdevice
590                 NG_NetworkDeviceInterface* networkdevice = new
591                         NG_LoopBackNetworkDeviceInterface();
592
593                 // get an audiodevice
594                 SND_DeviceManager::Subscribe();
595                 SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
596                 audiodevice->UseCD();
597
598                 // create a ketsji/blendersystem (only needed for timing and stuff)
599                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
600
601                 // create the ketsjiengine
602                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
603
604                 Scene *blscene = NULL;
605
606                 blscene = (Scene*) maggie->scene.first;
607                 for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
608                 {
609                         if (startscenename == (sce->id.name+2))
610                         {
611                                 blscene = sce;
612                                 break;
613                         }
614                 }
615
616         int cframe = 1, startFrame;
617                 if (blscene)
618                 {
619                         cframe=blscene->r.cfra;
620                         startFrame = blscene->r.sfra;
621                         blscene->r.cfra=startFrame;
622                         update_for_newframe();
623                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
624                 }
625
626                 // Quad buffered needs a special window.
627                 if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
628                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
629
630                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
631                 {
632                         // create a scene converter, create and convert the startingscene
633                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(maggie,sipo, ketsjiengine);
634                         ketsjiengine->SetSceneConverter(sceneconverter);
635                         sceneconverter->addInitFromFrame=true;
636                         
637                         if (always_use_expand_framing)
638                                 sceneconverter->SetAlwaysUseExpandFraming(true);
639
640                         if(usemat)
641                                 sceneconverter->SetMaterials(true);
642
643                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
644                                 mousedevice,
645                                 networkdevice,
646                                 audiodevice,
647                                 startscenename,
648                                 blscene);
649
650                         // some python things
651                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
652                         ketsjiengine->SetPythonDictionary(dictionaryobject);
653                         initRasterizer(rasterizer, canvas);
654                         PyObject *gameLogic = initGameLogic(ketsjiengine, startscene);
655                         PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module
656                         initGameKeys();
657                         initPythonConstraintBinding();
658                         initMathutils();
659
660                         if (sceneconverter)
661                         {
662                                 // convert and add scene
663                                 sceneconverter->ConvertScene(
664                                         startscenename,
665                                         startscene,
666                                         dictionaryobject,
667                                         keyboarddevice,
668                                         rendertools,
669                                         canvas);
670                                 ketsjiengine->AddScene(startscene);
671
672                                 // start the engine
673                                 ketsjiengine->StartEngine(false);
674                                 
675                                 ketsjiengine->SetUseFixedTime(true);
676                                 
677                                 ketsjiengine->SetTicRate(
678                                         (double) blscene->r.frs_sec /
679                                         (double) blscene->r.frs_sec_base);
680
681                                 // the mainloop
682                                 while ((blscene->r.cfra<=blscene->r.efra)&&(!exitrequested))
683                                 {
684                     printf("frame %i\n",blscene->r.cfra);
685                     // first check if we want to exit
686                                         exitrequested = ketsjiengine->GetExitCode();
687         
688                                         // kick the engine
689                                         ketsjiengine->NextFrame();
690                                     blscene->r.cfra=blscene->r.cfra+1;
691                                     update_for_newframe();
692                                         
693                                 }
694                                 exitstring = ketsjiengine->GetExitString();
695                         }
696                         if (sceneconverter)
697                         {
698                                 delete sceneconverter;
699                                 sceneconverter = NULL;
700                         }
701                 }
702                 blscene->r.cfra=cframe;
703                 // set the cursor back to normal
704                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
705
706                 // clean up some stuff
707                 audiodevice->StopCD();
708                 if (ketsjiengine)
709                 {
710                         delete ketsjiengine;
711                         ketsjiengine = NULL;
712                 }
713                 if (kxsystem)
714                 {
715                         delete kxsystem;
716                         kxsystem = NULL;
717                 }
718                 if (networkdevice)
719                 {
720                         delete networkdevice;
721                         networkdevice = NULL;
722                 }
723                 if (keyboarddevice)
724                 {
725                         delete keyboarddevice;
726                         keyboarddevice = NULL;
727                 }
728                 if (mousedevice)
729                 {
730                         delete mousedevice;
731                         mousedevice = NULL;
732                 }
733                 SND_DeviceManager::Unsubscribe();
734
735         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
736
737         // Release Python's GIL
738         PyGILState_Release(gilstate);
739 }