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