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