ec751376d4afe80a447e5462b30864d8e5d0490a
[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                                                                  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         resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path
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                 bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
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 && !novertexarrays)
153                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
154                         else
155                                 rasterizer = new RAS_ListRasterizer(canvas);
156                 }
157                 else if (GLEW_VERSION_1_1 && !novertexarrays)
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                                         setGamePythonPath(G.sce);
284                                 }
285                         }
286                         // else forget it, we can't find it
287                         else
288                         {
289                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
290                         }
291                 }
292                 
293                 Scene *blscene = NULL;
294                 if (!bfd)
295                 {
296                         blscene = (Scene*) blenderdata->scene.first;
297                         for (Scene *sce= (Scene*) blenderdata->scene.first; sce; sce= (Scene*) sce->id.next)
298                         {
299                                 if (startscenename == (sce->id.name+2))
300                                 {
301                                         blscene = sce;
302                                         break;
303                                 }
304                         }
305                 } else {
306                         blscene = bfd->curscene;
307                 }
308
309                 if (blscene)
310                 {
311                         int startFrame = blscene->r.cfra;
312                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
313                         
314                         // Quad buffered needs a special window.
315                         if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
316                                 rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
317                 }
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                                 ketsjiengine->SetCameraOverrideLens(v3d->lens);
329                         }
330                         
331                         // create a scene converter, create and convert the startingscene
332                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata, ketsjiengine);
333                         ketsjiengine->SetSceneConverter(sceneconverter);
334                         sceneconverter->addInitFromFrame=false;
335                         if (always_use_expand_framing)
336                                 sceneconverter->SetAlwaysUseExpandFraming(true);
337
338                         bool usemat = false, useglslmat = false;
339
340                         if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
341                                 usemat = true;
342
343                         if(GPU_extensions_minimum_support())
344                                 useglslmat = true;
345                         else if(G.fileflags & G_FILE_GAME_MAT_GLSL)
346                                 usemat = false;
347
348             if(usemat && (G.fileflags & G_FILE_GAME_MAT))
349                                 sceneconverter->SetMaterials(true);
350                         if(useglslmat && (G.fileflags & G_FILE_GAME_MAT_GLSL))
351                                 sceneconverter->SetGLSLMaterials(true);
352                                         
353                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
354                                 mousedevice,
355                                 networkdevice,
356                                 audiodevice,
357                                 startscenename,
358                                 blscene);
359                         
360                         // some python things
361                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata);
362                         ketsjiengine->SetPythonDictionary(dictionaryobject);
363                         initRasterizer(rasterizer, canvas);
364                         PyObject *gameLogic = initGameLogic(ketsjiengine, startscene);
365                         PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
366                         PyObject *gameLogic_keys = PyDict_Keys(PyModule_GetDict(gameLogic));
367                         PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module.
368                         
369                         initGameKeys();
370                         initPythonConstraintBinding();
371                         initMathutils();
372                         initGeometry();
373                         initBGL();
374 #ifdef WITH_FFMPEG
375                         initVideoTexture();
376 #endif
377
378                         //initialize Dome Settings
379                         if(blscene->r.stereomode == RAS_IRasterizer::RAS_STEREO_DOME)
380                                 ketsjiengine->InitDome(blscene->r.domeres, blscene->r.domemode, blscene->r.domeangle, blscene->r.domeresbuf, blscene->r.dometilt, blscene->r.dometext);
381
382                         if (sceneconverter)
383                         {
384                                 // convert and add scene
385                                 sceneconverter->ConvertScene(
386                                         startscenename,
387                                         startscene,
388                                         dictionaryobject,
389                                         keyboarddevice,
390                                         rendertools,
391                                         canvas);
392                                 ketsjiengine->AddScene(startscene);
393                                 
394                                 // init the rasterizer
395                                 rasterizer->Init();
396                                 
397                                 // start the engine
398                                 ketsjiengine->StartEngine(true);
399                                 
400
401                                 // Set the animation playback rate for ipo's and actions
402                                 // the framerate below should patch with FPS macro defined in blendef.h
403                                 // Could be in StartEngine set the framerate, we need the scene to do this
404                                 ketsjiengine->SetAnimFrameRate( (((double) blscene->r.frs_sec) / blscene->r.frs_sec_base) );
405                                 
406                                 // the mainloop
407                                 printf("\nBlender Game Engine Started\n\n");
408                                 while (!exitrequested)
409                                 {
410                                         // first check if we want to exit
411                                         exitrequested = ketsjiengine->GetExitCode();
412                                         
413                                         // kick the engine
414                                         bool render = ketsjiengine->NextFrame();
415                                         
416                                         if (render)
417                                         {
418                                                 // render the frame
419                                                 ketsjiengine->Render();
420                                         }
421                                         
422                                         // test for the ESC key
423                                         while (qtest())
424                                         {
425                                                 short val; 
426                                                 unsigned short event = extern_qread(&val);
427                                                 
428                                                 if (keyboarddevice->ConvertBlenderEvent(event,val))
429                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
430                                                 
431                                                         /* Coordinate conversion... where
432                                                         * should this really be?
433                                                 */
434                                                 if (event==MOUSEX) {
435                                                         val = val - scrarea_get_win_x(area);
436                                                 } else if (event==MOUSEY) {
437                                                         val = scrarea_get_win_height(area) - (val - scrarea_get_win_y(area)) - 1;
438                                                 }
439                                                 
440                                                 mousedevice->ConvertBlenderEvent(event,val);
441                                         }
442                                 }
443                                 printf("\nBlender Game Engine Finished\n\n");
444                                 exitstring = ketsjiengine->GetExitString();
445
446
447                                 // when exiting the mainloop
448                                 
449                                 // Clears the dictionary by hand:
450                                 // This prevents, extra references to global variables
451                                 // inside the GameLogic dictionary when the python interpreter is finalized.
452                                 // which allows the scene to safely delete them :)
453                                 // see: (space.c)->start_game
454                                 
455                                 //PyDict_Clear(PyModule_GetDict(gameLogic));
456                                 
457                                 // Keep original items, means python plugins will autocomplete members
458                                 int listIndex;
459                                 PyObject *gameLogic_keys_new = PyDict_Keys(PyModule_GetDict(gameLogic));
460                                 for (listIndex=0; listIndex < PyList_Size(gameLogic_keys_new); listIndex++)  {
461                                         PyObject* item = PyList_GET_ITEM(gameLogic_keys_new, listIndex);
462                                         if (!PySequence_Contains(gameLogic_keys, item)) {
463                                                 PyDict_DelItem( PyModule_GetDict(gameLogic), item);
464                                         }
465                                 }
466                                 Py_DECREF(gameLogic_keys_new);
467                                 gameLogic_keys_new = NULL;
468                                 
469                                 ketsjiengine->StopEngine();
470                                 exitGamePythonScripting();
471                                 networkdevice->Disconnect();
472                         }
473                         if (sceneconverter)
474                         {
475                                 delete sceneconverter;
476                                 sceneconverter = NULL;
477                         }
478                         
479                         Py_DECREF(gameLogic_keys);
480                         gameLogic_keys = NULL;
481                 }
482                 //lock frame and camera enabled - restoring global values
483                 if (G.vd->scenelock==0){
484                         G.scene->lay= tmp_lay;
485                         G.scene->camera= tmp_camera;
486                 }
487
488                 // set the cursor back to normal
489                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
490                 
491                 // clean up some stuff
492                 audiodevice->StopCD();
493                 
494                 if (ketsjiengine)
495                 {
496                         delete ketsjiengine;
497                         ketsjiengine = NULL;
498                 }
499                 if (kxsystem)
500                 {
501                         delete kxsystem;
502                         kxsystem = NULL;
503                 }
504                 if (networkdevice)
505                 {
506                         delete networkdevice;
507                         networkdevice = NULL;
508                 }
509                 if (keyboarddevice)
510                 {
511                         delete keyboarddevice;
512                         keyboarddevice = NULL;
513                 }
514                 if (mousedevice)
515                 {
516                         delete mousedevice;
517                         mousedevice = NULL;
518                 }
519                 if (rasterizer)
520                 {
521                         delete rasterizer;
522                         rasterizer = NULL;
523                 }
524                 if (rendertools)
525                 {
526                         delete rendertools;
527                         rendertools = NULL;
528                 }
529                 if (canvas)
530                 {
531                         delete canvas;
532                         canvas = NULL;
533                 }
534                 SND_DeviceManager::Unsubscribe();
535         
536         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
537         
538         Py_DECREF(pyGlobalDict);
539         
540         if (bfd) BLO_blendfiledata_free(bfd);
541
542         BLI_strncpy(G.sce, oldsce, sizeof(G.sce));
543
544         // Release Python's GIL
545         PyGILState_Release(gilstate);
546 }
547
548 extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
549                                                                  char* scenename,
550                                                                  struct Main* maggie,
551                                                                  int always_use_expand_framing)
552 {
553     int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
554
555         Main* blenderdata = maggie;
556
557         char* startscenename = scenename;
558         char pathname[FILE_MAXDIR+FILE_MAXFILE];
559         STR_String exitstring = "";
560
561         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
562
563         // Acquire Python's GIL (global interpreter lock)
564         // so we can safely run Python code and API calls
565         PyGILState_STATE gilstate = PyGILState_Ensure();
566
567         bgl::InitExtensions(true);
568
569         do
570         {
571
572                 // get some preferences
573                 SYS_SystemHandle syshandle = SYS_GetSystem();
574                 /*
575                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
576                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
577                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
578                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
579                 */
580                 bool game2ipo = true;//(SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
581                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
582                 bool usemat = false;
583
584                 // create the canvas, rasterizer and rendertools
585                 RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
586                 //canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
587                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
588                 RAS_IRasterizer* rasterizer = NULL;
589
590                 if(displaylists) {
591                         if (GLEW_VERSION_1_1)
592                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
593                         else
594                                 rasterizer = new RAS_ListRasterizer(canvas);
595                 }
596                 else if (GLEW_VERSION_1_1)
597                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
598                 else
599                         rasterizer = new RAS_OpenGLRasterizer(canvas);
600
601                 // create the inputdevices
602                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
603                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
604
605                 // create a networkdevice
606                 NG_NetworkDeviceInterface* networkdevice = new
607                         NG_LoopBackNetworkDeviceInterface();
608
609                 // get an audiodevice
610                 SND_DeviceManager::Subscribe();
611                 SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
612                 audiodevice->UseCD();
613
614                 // create a ketsji/blendersystem (only needed for timing and stuff)
615                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
616
617                 // create the ketsjiengine
618                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
619
620                 Scene *blscene = NULL;
621
622                 blscene = (Scene*) maggie->scene.first;
623                 for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
624                 {
625                         if (startscenename == (sce->id.name+2))
626                         {
627                                 blscene = sce;
628                                 break;
629                         }
630                 }
631
632         int cframe = 1, startFrame;
633                 if (blscene)
634                 {
635                         cframe=blscene->r.cfra;
636                         startFrame = blscene->r.sfra;
637                         blscene->r.cfra=startFrame;
638                         update_for_newframe();
639                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
640                 }
641
642                 // Quad buffered needs a special window.
643                 if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
644                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
645
646                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
647                 {
648                         // create a scene converter, create and convert the startingscene
649                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(maggie, ketsjiengine);
650                         ketsjiengine->SetSceneConverter(sceneconverter);
651                         sceneconverter->addInitFromFrame=true;
652                         
653                         if (always_use_expand_framing)
654                                 sceneconverter->SetAlwaysUseExpandFraming(true);
655
656                         if(usemat)
657                                 sceneconverter->SetMaterials(true);
658
659                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
660                                 mousedevice,
661                                 networkdevice,
662                                 audiodevice,
663                                 startscenename,
664                                 blscene);
665
666                         // some python things
667                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata);
668                         ketsjiengine->SetPythonDictionary(dictionaryobject);
669                         initRasterizer(rasterizer, canvas);
670                         PyObject *gameLogic = initGameLogic(ketsjiengine, startscene);
671                         PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module
672                         initGameKeys();
673                         initPythonConstraintBinding();
674                         initMathutils();
675                         initGeometry();
676                         initBGL();
677 #ifdef WITH_FFMPEG
678                         initVideoTexture();
679 #endif
680
681                         if (sceneconverter)
682                         {
683                                 // convert and add scene
684                                 sceneconverter->ConvertScene(
685                                         startscenename,
686                                         startscene,
687                                         dictionaryobject,
688                                         keyboarddevice,
689                                         rendertools,
690                                         canvas);
691                                 ketsjiengine->AddScene(startscene);
692
693                                 // start the engine
694                                 ketsjiengine->StartEngine(false);
695                                 
696                                 ketsjiengine->SetUseFixedTime(true);
697                                 
698                                 ketsjiengine->SetTicRate(
699                                         (double) blscene->r.frs_sec /
700                                         (double) blscene->r.frs_sec_base);
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                                         
714                                 }
715                                 exitstring = ketsjiengine->GetExitString();
716                         }
717                         if (sceneconverter)
718                         {
719                                 delete sceneconverter;
720                                 sceneconverter = NULL;
721                         }
722                 }
723                 blscene->r.cfra=cframe;
724                 // set the cursor back to normal
725                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
726
727                 // clean up some stuff
728                 audiodevice->StopCD();
729                 if (ketsjiengine)
730                 {
731                         delete ketsjiengine;
732                         ketsjiengine = NULL;
733                 }
734                 if (kxsystem)
735                 {
736                         delete kxsystem;
737                         kxsystem = NULL;
738                 }
739                 if (networkdevice)
740                 {
741                         delete networkdevice;
742                         networkdevice = NULL;
743                 }
744                 if (keyboarddevice)
745                 {
746                         delete keyboarddevice;
747                         keyboarddevice = NULL;
748                 }
749                 if (mousedevice)
750                 {
751                         delete mousedevice;
752                         mousedevice = NULL;
753                 }
754                 SND_DeviceManager::Unsubscribe();
755
756         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
757
758         // Release Python's GIL
759         PyGILState_Release(gilstate);
760 }