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