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