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