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