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