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